summaryrefslogtreecommitdiff
path: root/lib/irb/command/show_source.rb
blob: 32bdf74d313e92ba5977f2837cc3b481d6e4f7de (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
76
77
78
79
80
81
# frozen_string_literal: true

require_relative "../source_finder"
require_relative "../pager"
require_relative "../color"

module IRB
  module Command
    class ShowSource < Base
      category "Context"
      description "Show the source code of a given method, class/module, or constant."

      help_message <<~HELP_MESSAGE
        Usage: show_source [target] [-s]

          -s  Show the super method. You can stack it like `-ss` to show the super of the super, etc.

        Examples:

          show_source Foo
          show_source Foo#bar
          show_source Foo#bar -s
          show_source Foo.baz
          show_source Foo::BAR
      HELP_MESSAGE

      class << self
        def transform_args(args)
          # Return a string literal as is for backward compatibility
          if args.empty? || string_literal?(args)
            args
          else # Otherwise, consider the input as a String for convenience
            args.strip.dump
          end
        end
      end

      def execute(str = nil)
        unless str.is_a?(String)
          puts "Error: Expected a string but got #{str.inspect}"
          return
        end

        str, esses = str.split(" -")
        super_level = esses ? esses.count("s") : 0
        source = SourceFinder.new(@irb_context).find_source(str, super_level)

        if source
          show_source(source)
        elsif super_level > 0
          puts "Error: Couldn't locate a super definition for #{str}"
        else
          puts "Error: Couldn't locate a definition for #{str}"
        end
        nil
      end

      private

      def show_source(source)
        if source.binary_file?
          content = "\n#{bold('Defined in binary file')}: #{source.file}\n\n"
        else
          code = source.colorized_content || 'Source not available'
          content = <<~CONTENT

            #{bold("From")}: #{source.file}:#{source.line}

            #{code.chomp}

          CONTENT
        end
        Pager.page_content(content)
      end

      def bold(str)
        Color.colorize(str, [:BOLD])
      end
    end
  end
end