summaryrefslogtreecommitdiff
path: root/lib/pry/commands/ls/constants.rb
blob: c0f504581af353a5b57862d91e5cb8e662e5623b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

class Pry
  class Command
    class Ls < Pry::ClassCommand
      class Constants < Pry::Command::Ls::Formatter
        DEPRECATED_CONSTANTS = [
          :Data, :Fixnum, :Bignum, :TimeoutError, :NIL, :FALSE, :TRUE
        ].tap do |constants|
          constants << :JavaPackageModuleTemplate if Helpers::Platform.jruby?
        end
        include Pry::Command::Ls::Interrogatable

        def initialize(interrogatee, no_user_opts, opts, pry_instance)
          super(pry_instance)
          @interrogatee = interrogatee
          @no_user_opts = no_user_opts
          @default_switch = opts[:constants]
          @verbose_switch = opts[:verbose]
          @dconstants = opts.dconstants?
        end

        def correct_opts?
          super || (@no_user_opts && interrogating_a_module?)
        end

        def output_self
          mod = interrogatee_mod
          constants = WrappedModule.new(mod).constants(@verbose_switch)
          output_section('constants', grep.regexp[format(mod, constants)])
        end

        private

        def show_deprecated_constants?
          @dconstants == true
        end

        def format(mod, constants)
          constants.sort_by(&:downcase).map do |name|
            if Object.respond_to?(:deprecate_constant) &&
               DEPRECATED_CONSTANTS.include?(name) &&
               !show_deprecated_constants?
              next
            end

            if (const = (begin
                           !mod.autoload?(name) && (mod.const_get(name) || true)
                         rescue StandardError
                           nil
                         end))
              if begin
                   const < Exception
                 rescue StandardError
                   false
                 end
                color(:exception_constant, name)
              elsif begin
                      mod.const_get(name).is_a?(Module)
                    rescue StandardError
                      false
                    end
                color(:class_constant, name)
              else
                color(:constant, name)
              end
            else
              color(:unloaded_constant, name)
            end
          end
        end
      end
    end
  end
end