summaryrefslogtreecommitdiff
path: root/lib/pry/commands/show_source.rb
blob: ea53e12a8572a8413f5eee97b40abc269365ceab (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
require 'pry/commands/show_info'

class Pry
  class Command::ShowSource < Command::ShowInfo
    match 'show-source'
    group 'Introspection'
    description 'Show the source for a method or class.'

    banner <<-'BANNER'
      Usage:   show-source [OPTIONS] [METH|CLASS]
      Aliases: $, show-method

      Show the source for a method or class. Tries instance methods first and then
      methods by default.

      show-source hi_method
      show-source hi_method
      show-source Pry#rep     # source for Pry#rep method
      show-source Pry         # for Pry class
      show-source Pry -a      # for all Pry class definitions (all monkey patches)
      show-source Pry.foo -e  # for class of the return value of expression `Pry.foo`
      show-source Pry --super # for superclass of Pry (Object class)

      https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
    BANNER

    def options(opt)
      opt.on :e, :eval, "evaluate the command's argument as a ruby expression and show the class its return value"
      super(opt)
    end

    def process
      if opts.present?(:e)
        obj = target.eval(args.first)
        self.args = Array.new(1) { Module === obj ? obj.name : obj.class.name }
      end
      super
    end

    # The source for code_object prepared for display.
    def content_for(code_object)
      code = Code.new(
        code_object.source || [],
        start_line_for(code_object)
      )
      code.with_line_numbers(use_line_numbers?).highlighted
    end
  end

  Pry::Commands.add_command(Pry::Command::ShowSource)
  Pry::Commands.alias_command 'show-method', 'show-source'
  Pry::Commands.alias_command '$', 'show-source'
end