pry 0.12.2 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +110 -1
  3. data/LICENSE +1 -1
  4. data/README.md +331 -269
  5. data/bin/pry +5 -0
  6. data/lib/pry.rb +133 -119
  7. data/lib/pry/basic_object.rb +8 -4
  8. data/lib/pry/block_command.rb +22 -0
  9. data/lib/pry/class_command.rb +194 -0
  10. data/lib/pry/cli.rb +40 -31
  11. data/lib/pry/code.rb +39 -27
  12. data/lib/pry/code/code_file.rb +28 -24
  13. data/lib/pry/code/code_range.rb +4 -2
  14. data/lib/pry/code/loc.rb +15 -8
  15. data/lib/pry/code_object.rb +40 -38
  16. data/lib/pry/color_printer.rb +47 -46
  17. data/lib/pry/command.rb +166 -369
  18. data/lib/pry/command_set.rb +76 -73
  19. data/lib/pry/command_state.rb +31 -0
  20. data/lib/pry/commands/amend_line.rb +86 -81
  21. data/lib/pry/commands/bang.rb +18 -14
  22. data/lib/pry/commands/bang_pry.rb +15 -11
  23. data/lib/pry/commands/cat.rb +61 -54
  24. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  25. data/lib/pry/commands/cat/exception_formatter.rb +71 -60
  26. data/lib/pry/commands/cat/file_formatter.rb +55 -49
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  28. data/lib/pry/commands/cd.rb +40 -35
  29. data/lib/pry/commands/change_inspector.rb +29 -22
  30. data/lib/pry/commands/change_prompt.rb +44 -39
  31. data/lib/pry/commands/clear_screen.rb +16 -10
  32. data/lib/pry/commands/code_collector.rb +148 -133
  33. data/lib/pry/commands/disable_pry.rb +23 -19
  34. data/lib/pry/commands/easter_eggs.rb +19 -30
  35. data/lib/pry/commands/edit.rb +184 -161
  36. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  37. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  38. data/lib/pry/commands/exit.rb +39 -35
  39. data/lib/pry/commands/exit_all.rb +24 -20
  40. data/lib/pry/commands/exit_program.rb +20 -16
  41. data/lib/pry/commands/find_method.rb +168 -160
  42. data/lib/pry/commands/fix_indent.rb +16 -12
  43. data/lib/pry/commands/help.rb +140 -133
  44. data/lib/pry/commands/hist.rb +151 -150
  45. data/lib/pry/commands/import_set.rb +20 -16
  46. data/lib/pry/commands/jump_to.rb +25 -21
  47. data/lib/pry/commands/list_inspectors.rb +35 -28
  48. data/lib/pry/commands/ls.rb +124 -102
  49. data/lib/pry/commands/ls/constants.rb +59 -42
  50. data/lib/pry/commands/ls/formatter.rb +50 -46
  51. data/lib/pry/commands/ls/globals.rb +38 -34
  52. data/lib/pry/commands/ls/grep.rb +17 -13
  53. data/lib/pry/commands/ls/instance_vars.rb +29 -27
  54. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  55. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  56. data/lib/pry/commands/ls/local_names.rb +26 -22
  57. data/lib/pry/commands/ls/local_vars.rb +38 -28
  58. data/lib/pry/commands/ls/ls_entity.rb +47 -51
  59. data/lib/pry/commands/ls/methods.rb +44 -43
  60. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  61. data/lib/pry/commands/ls/self_methods.rb +23 -22
  62. data/lib/pry/commands/nesting.rb +21 -17
  63. data/lib/pry/commands/play.rb +93 -82
  64. data/lib/pry/commands/pry_backtrace.rb +24 -17
  65. data/lib/pry/commands/pry_version.rb +15 -11
  66. data/lib/pry/commands/raise_up.rb +27 -22
  67. data/lib/pry/commands/reload_code.rb +60 -48
  68. data/lib/pry/commands/reset.rb +16 -12
  69. data/lib/pry/commands/ri.rb +55 -45
  70. data/lib/pry/commands/save_file.rb +45 -43
  71. data/lib/pry/commands/shell_command.rb +51 -51
  72. data/lib/pry/commands/shell_mode.rb +21 -17
  73. data/lib/pry/commands/show_doc.rb +81 -68
  74. data/lib/pry/commands/show_info.rb +189 -171
  75. data/lib/pry/commands/show_input.rb +16 -11
  76. data/lib/pry/commands/show_source.rb +109 -45
  77. data/lib/pry/commands/stat.rb +35 -31
  78. data/lib/pry/commands/switch_to.rb +21 -15
  79. data/lib/pry/commands/toggle_color.rb +20 -16
  80. data/lib/pry/commands/watch_expression.rb +89 -86
  81. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  82. data/lib/pry/commands/whereami.rb +156 -148
  83. data/lib/pry/commands/wtf.rb +75 -50
  84. data/lib/pry/config.rb +311 -25
  85. data/lib/pry/config/attributable.rb +22 -0
  86. data/lib/pry/config/lazy_value.rb +29 -0
  87. data/lib/pry/config/memoized_value.rb +34 -0
  88. data/lib/pry/config/value.rb +24 -0
  89. data/lib/pry/control_d_handler.rb +28 -0
  90. data/lib/pry/core_extensions.rb +9 -7
  91. data/lib/pry/editor.rb +48 -21
  92. data/lib/pry/env.rb +18 -0
  93. data/lib/pry/exception_handler.rb +43 -0
  94. data/lib/pry/exceptions.rb +13 -16
  95. data/lib/pry/forwardable.rb +5 -1
  96. data/lib/pry/helpers.rb +2 -0
  97. data/lib/pry/helpers/base_helpers.rb +68 -197
  98. data/lib/pry/helpers/command_helpers.rb +50 -61
  99. data/lib/pry/helpers/documentation_helpers.rb +20 -13
  100. data/lib/pry/helpers/options_helpers.rb +14 -7
  101. data/lib/pry/helpers/platform.rb +7 -5
  102. data/lib/pry/helpers/table.rb +33 -26
  103. data/lib/pry/helpers/text.rb +17 -14
  104. data/lib/pry/history.rb +48 -56
  105. data/lib/pry/hooks.rb +21 -12
  106. data/lib/pry/indent.rb +54 -50
  107. data/lib/pry/input_completer.rb +248 -230
  108. data/lib/pry/input_lock.rb +8 -9
  109. data/lib/pry/inspector.rb +36 -24
  110. data/lib/pry/last_exception.rb +45 -45
  111. data/lib/pry/method.rb +141 -94
  112. data/lib/pry/method/disowned.rb +16 -4
  113. data/lib/pry/method/patcher.rb +12 -3
  114. data/lib/pry/method/weird_method_locator.rb +68 -44
  115. data/lib/pry/object_path.rb +33 -25
  116. data/lib/pry/output.rb +121 -35
  117. data/lib/pry/pager.rb +41 -42
  118. data/lib/pry/plugins.rb +25 -8
  119. data/lib/pry/prompt.rb +123 -54
  120. data/lib/pry/pry_class.rb +61 -98
  121. data/lib/pry/pry_instance.rb +217 -215
  122. data/lib/pry/repl.rb +18 -22
  123. data/lib/pry/repl_file_loader.rb +27 -21
  124. data/lib/pry/ring.rb +11 -6
  125. data/lib/pry/slop.rb +574 -563
  126. data/lib/pry/slop/commands.rb +164 -169
  127. data/lib/pry/slop/option.rb +172 -168
  128. data/lib/pry/syntax_highlighter.rb +26 -0
  129. data/lib/pry/system_command_handler.rb +17 -0
  130. data/lib/pry/testable.rb +59 -61
  131. data/lib/pry/testable/evalable.rb +21 -12
  132. data/lib/pry/testable/mockable.rb +18 -10
  133. data/lib/pry/testable/pry_tester.rb +71 -56
  134. data/lib/pry/testable/utility.rb +29 -21
  135. data/lib/pry/testable/variables.rb +49 -43
  136. data/lib/pry/version.rb +3 -1
  137. data/lib/pry/warning.rb +27 -0
  138. data/lib/pry/wrapped_module.rb +51 -42
  139. data/lib/pry/wrapped_module/candidate.rb +21 -14
  140. metadata +31 -30
  141. data/lib/pry/commands.rb +0 -6
  142. data/lib/pry/commands/disabled_commands.rb +0 -2
  143. data/lib/pry/commands/gem_cd.rb +0 -26
  144. data/lib/pry/commands/gem_install.rb +0 -32
  145. data/lib/pry/commands/gem_list.rb +0 -33
  146. data/lib/pry/commands/gem_open.rb +0 -29
  147. data/lib/pry/commands/gem_readme.rb +0 -25
  148. data/lib/pry/commands/gem_search.rb +0 -40
  149. data/lib/pry/commands/gem_stats.rb +0 -83
  150. data/lib/pry/commands/gist.rb +0 -102
  151. data/lib/pry/commands/install_command.rb +0 -54
  152. data/lib/pry/config/behavior.rb +0 -255
  153. data/lib/pry/config/convenience.rb +0 -28
  154. data/lib/pry/config/default.rb +0 -159
  155. data/lib/pry/config/memoization.rb +0 -48
  156. data/lib/pry/platform.rb +0 -91
  157. data/lib/pry/rubygem.rb +0 -84
  158. data/lib/pry/terminal.rb +0 -91
@@ -1,40 +1,44 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::Stat < Pry::ClassCommand
3
- match 'stat'
4
- group 'Introspection'
5
- description 'View method information and set _file_ and _dir_ locals.'
6
- command_options shellwords: false
4
+ class Command
5
+ class Stat < Pry::ClassCommand
6
+ match 'stat'
7
+ group 'Introspection'
8
+ description 'View method information and set _file_ and _dir_ locals.'
9
+ command_options shellwords: false
7
10
 
8
- banner <<-'BANNER'
9
- Usage: stat [OPTIONS] [METH]
11
+ banner <<-'BANNER'
12
+ Usage: stat [OPTIONS] [METH]
10
13
 
11
- Show method information for method METH and set _file_ and _dir_ locals.
14
+ Show method information for method METH and set _file_ and _dir_ locals.
12
15
 
13
- stat hello_method
14
- BANNER
16
+ stat hello_method
17
+ BANNER
15
18
 
16
- def options(opt)
17
- method_options(opt)
18
- end
19
+ def options(opt)
20
+ method_options(opt)
21
+ end
22
+
23
+ def process
24
+ meth = method_object
25
+ aliases = meth.aliases
19
26
 
20
- def process
21
- meth = method_object
22
- aliases = meth.aliases
23
-
24
- output.puts unindent <<-EOS
25
- Method Information:
26
- --
27
- Name: #{meth.name}
28
- Alias#{ "es" if aliases.length > 1 }: #{ aliases.any? ? aliases.join(", ") : "None." }
29
- Owner: #{meth.owner ? meth.owner : "Unknown"}
30
- Visibility: #{meth.visibility}
31
- Type: #{meth.is_a?(::Method) ? "Bound" : "Unbound"}
32
- Arity: #{meth.arity}
33
- Method Signature: #{meth.signature}
34
- Source Location: #{meth.source_location ? meth.source_location.join(":") : "Not found."}
35
- EOS
27
+ output.puts(unindent(<<-OUTPUT))
28
+ Method Information:
29
+ --
30
+ Name: #{meth.name}
31
+ Alias#{'es' if aliases.length > 1}: #{aliases.any? ? aliases.join(', ') : 'None.'}
32
+ Owner: #{meth.owner || 'Unknown'}
33
+ Visibility: #{meth.visibility}
34
+ Type: #{meth.is_a?(::Method) ? 'Bound' : 'Unbound'}
35
+ Arity: #{meth.arity}
36
+ Method Signature: #{meth.signature}
37
+ Source Location: #{meth.source_location ? meth.source_location.join(':') : 'Not found.'}
38
+ OUTPUT
39
+ end
36
40
  end
37
- end
38
41
 
39
- Pry::Commands.add_command(Pry::Command::Stat)
42
+ Pry::Commands.add_command(Pry::Command::Stat)
43
+ end
40
44
  end
@@ -1,23 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::SwitchTo < Pry::ClassCommand
3
- match 'switch-to'
4
- group 'Navigating Pry'
5
- description 'Start a new subsession on a binding in the current stack.'
4
+ class Command
5
+ class SwitchTo < Pry::ClassCommand
6
+ match 'switch-to'
7
+ group 'Navigating Pry'
8
+ description 'Start a new subsession on a binding in the current stack.'
6
9
 
7
- banner <<-'BANNER'
8
- Start a new subsession on a binding in the current stack (numbered by nesting).
9
- BANNER
10
+ banner <<-'BANNER'
11
+ Start a new subsession on a binding in the current stack (numbered by nesting).
12
+ BANNER
10
13
 
11
- def process(selection)
12
- selection = selection.to_i
14
+ def process(selection)
15
+ selection = selection.to_i
13
16
 
14
- if selection < 0 || selection > _pry_.binding_stack.size - 1
15
- raise CommandError, "Invalid binding index #{selection} - use `nesting` command to view valid indices."
16
- else
17
- Pry.start(_pry_.binding_stack[selection])
17
+ if selection < 0 || selection > pry_instance.binding_stack.size - 1
18
+ raise CommandError,
19
+ "Invalid binding index #{selection} - use `nesting` command " \
20
+ "to view valid indices."
21
+ else
22
+ Pry.start(pry_instance.binding_stack[selection])
23
+ end
18
24
  end
19
25
  end
20
- end
21
26
 
22
- Pry::Commands.add_command(Pry::Command::SwitchTo)
27
+ Pry::Commands.add_command(Pry::Command::SwitchTo)
28
+ end
23
29
  end
@@ -1,24 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::ToggleColor < Pry::ClassCommand
3
- match 'toggle-color'
4
- group 'Misc'
5
- description 'Toggle syntax highlighting.'
4
+ class Command
5
+ class ToggleColor < Pry::ClassCommand
6
+ match 'toggle-color'
7
+ group 'Misc'
8
+ description 'Toggle syntax highlighting.'
6
9
 
7
- banner <<-'BANNER'
8
- Usage: toggle-color
10
+ banner <<-'BANNER'
11
+ Usage: toggle-color
9
12
 
10
- Toggle syntax highlighting.
11
- BANNER
13
+ Toggle syntax highlighting.
14
+ BANNER
12
15
 
13
- def process
14
- _pry_.color = color_toggle
15
- output.puts "Syntax highlighting #{_pry_.color ? "on" : "off"}"
16
- end
16
+ def process
17
+ pry_instance.color = color_toggle
18
+ output.puts "Syntax highlighting #{pry_instance.color ? 'on' : 'off'}"
19
+ end
17
20
 
18
- def color_toggle
19
- !_pry_.color
20
- end
21
+ def color_toggle
22
+ !pry_instance.color
23
+ end
21
24
 
22
- Pry::Commands.add_command(self)
25
+ Pry::Commands.add_command(self)
26
+ end
23
27
  end
24
28
  end
@@ -1,107 +1,110 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::WatchExpression < Pry::ClassCommand
3
- require 'pry/commands/watch_expression/expression.rb'
4
-
5
- match 'watch'
6
- group 'Context'
7
- description 'Watch the value of an expression and print a notification whenever it changes.'
8
- command_options use_prefix: false
9
-
10
- banner <<-'BANNER'
11
- Usage: watch [EXPRESSION]
12
- watch
13
- watch --delete [INDEX]
14
-
15
- watch [EXPRESSION] adds an expression to the list of those being watched.
16
- It will be re-evaluated every time you hit enter in pry. If its value has
17
- changed, the new value will be printed to the console.
18
-
19
- This is useful if you are step-through debugging and want to see how
20
- something changes over time. It's also useful if you're trying to write
21
- a method inside pry and want to check that it gives the right answers
22
- every time you redefine it.
23
-
24
- watch on its own displays all the currently watched expressions and their
25
- values, and watch --delete [INDEX] allows you to delete expressions from
26
- the list being watched.
27
- BANNER
28
-
29
- def options(opt)
30
- opt.on :d, :delete,
31
- "Delete the watch expression with the given index. If no index is given; clear all watch expressions.",
32
- optional_argument: true, as: Integer
33
- opt.on :l, :list,
34
- "Show all current watch expressions and their values. Calling watch with no expressions or options will also show the watch expressions."
35
- end
4
+ class Command
5
+ class WatchExpression < Pry::ClassCommand
6
+ match 'watch'
7
+ group 'Context'
8
+ description 'Watch the value of an expression and print a notification ' \
9
+ 'whenever it changes.'
10
+ command_options use_prefix: false
11
+
12
+ banner <<-'BANNER'
13
+ Usage: watch [EXPRESSION]
14
+ watch
15
+ watch --delete [INDEX]
16
+
17
+ watch [EXPRESSION] adds an expression to the list of those being watched.
18
+ It will be re-evaluated every time you hit enter in pry. If its value has
19
+ changed, the new value will be printed to the console.
20
+
21
+ This is useful if you are step-through debugging and want to see how
22
+ something changes over time. It's also useful if you're trying to write
23
+ a method inside pry and want to check that it gives the right answers
24
+ every time you redefine it.
25
+
26
+ watch on its own displays all the currently watched expressions and their
27
+ values, and watch --delete [INDEX] allows you to delete expressions from
28
+ the list being watched.
29
+ BANNER
36
30
 
37
- def process
38
- case
39
- when opts.present?(:delete)
40
- delete opts[:delete]
41
- when opts.present?(:list) || args.empty?
42
- list
43
- else
44
- add_hook
45
- add_expression(args)
31
+ def options(opt)
32
+ opt.on :d, :delete,
33
+ "Delete the watch expression with the given index. If no index " \
34
+ "is given; clear all watch expressions.",
35
+ optional_argument: true, as: Integer
36
+ opt.on :l, :list,
37
+ "Show all current watch expressions and their values. Calling " \
38
+ "watch with no expressions or options will also show the watch " \
39
+ "expressions."
46
40
  end
47
- end
48
41
 
49
- private
42
+ def process
43
+ if opts.present?(:delete)
44
+ delete opts[:delete]
45
+ elsif opts.present?(:list) || args.empty?
46
+ list
47
+ else
48
+ add_hook
49
+ add_expression(args)
50
+ end
51
+ end
50
52
 
51
- def expressions
52
- _pry_.config.watch_expressions ||= []
53
- end
53
+ private
54
54
 
55
- def delete(index)
56
- if index
57
- output.puts "Deleting watch expression ##{index}: #{expressions[index - 1]}"
58
- expressions.delete_at(index - 1)
59
- else
60
- output.puts "Deleting all watched expressions"
61
- expressions.clear
55
+ def expressions
56
+ state.watch_expressions ||= []
62
57
  end
63
- end
64
58
 
65
- def list
66
- if expressions.empty?
67
- output.puts "No watched expressions"
68
- else
69
- _pry_.pager.open do |pager|
70
- pager.puts "Listing all watched expressions:"
71
- pager.puts ""
72
- expressions.each_with_index do |expr, index|
73
- pager.print with_line_numbers(expr.to_s, index + 1)
59
+ def delete(index)
60
+ if index
61
+ output.puts "Deleting watch expression ##{index}: #{expressions[index - 1]}"
62
+ expressions.delete_at(index - 1)
63
+ else
64
+ output.puts "Deleting all watched expressions"
65
+ expressions.clear
66
+ end
67
+ end
68
+
69
+ def list
70
+ if expressions.empty?
71
+ output.puts "No watched expressions"
72
+ else
73
+ pry_instance.pager.open do |pager|
74
+ pager.puts "Listing all watched expressions:"
75
+ pager.puts ""
76
+ expressions.each_with_index do |expr, index|
77
+ pager.print with_line_numbers(expr.to_s, index + 1)
78
+ end
79
+ pager.puts ""
74
80
  end
75
- pager.puts ""
76
81
  end
77
82
  end
78
- end
79
83
 
80
- def eval_and_print_changed(output)
81
- expressions.each do |expr|
82
- expr.eval!
83
- if expr.changed?
84
- output.puts "#{blue "watch"}: #{expr}"
84
+ def eval_and_print_changed(output)
85
+ expressions.each do |expr|
86
+ expr.eval!
87
+ output.puts "#{blue 'watch'}: #{expr}" if expr.changed?
85
88
  end
86
89
  end
87
- end
88
90
 
89
- # TODO: fix arguments.
90
- # https://github.com/pry/pry/commit/b031df2f2f5850ee6e9018f33d35f3485a9b0423
91
- def add_expression(_arguments)
92
- expressions << Expression.new(_pry_, target, arg_string)
93
- output.puts "Watching #{Code.new(arg_string).highlighted}"
94
- end
91
+ # TODO: fix arguments.
92
+ # https://github.com/pry/pry/commit/b031df2f2f5850ee6e9018f33d35f3485a9b0423
93
+ def add_expression(_arguments)
94
+ expressions << Expression.new(pry_instance, target, arg_string)
95
+ output.puts "Watching #{Code.new(arg_string).highlighted}"
96
+ end
97
+
98
+ def add_hook
99
+ hook = [:after_eval, :watch_expression]
100
+ return if pry_instance.hooks.hook_exists?(*hook)
95
101
 
96
- def add_hook
97
- hook = [:after_eval, :watch_expression]
98
- unless _pry_.hooks.hook_exists?(*hook)
99
- _pry_.hooks.add_hook(*hook) do |_, _pry_|
100
- eval_and_print_changed _pry_.output
102
+ pry_instance.hooks.add_hook(*hook) do |_, pry_instance|
103
+ eval_and_print_changed pry_instance.output
101
104
  end
102
105
  end
103
106
  end
104
- end
105
107
 
106
- Pry::Commands.add_command(Pry::Command::WatchExpression)
108
+ Pry::Commands.add_command(Pry::Command::WatchExpression)
109
+ end
107
110
  end
@@ -1,37 +1,42 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::WatchExpression
3
- class Expression
4
- attr_reader :target, :source, :value, :previous_value, :_pry_
4
+ class Command
5
+ class WatchExpression
6
+ class Expression
7
+ attr_reader :target, :source, :value, :previous_value, :pry_instance
5
8
 
6
- def initialize(_pry_, target, source)
7
- @_pry_ = _pry_
8
- @target = target
9
- @source = Code.new(source).strip
10
- end
9
+ def initialize(pry_instance, target, source)
10
+ @pry_instance = pry_instance
11
+ @target = target
12
+ @source = Code.new(source).strip
13
+ end
11
14
 
12
- def eval!
13
- @previous_value = value
14
- @value = Pry::ColorPrinter.pp(target_eval(target, source), "")
15
- end
15
+ def eval!
16
+ @previous_value = value
17
+ @value = Pry::ColorPrinter.pp(target_eval(target, source), ''.dup)
18
+ end
16
19
 
17
- def to_s
18
- "#{Code.new(source).highlighted.strip} => #{value}"
19
- end
20
+ def to_s
21
+ "#{Code.new(source).highlighted.strip} => #{value}"
22
+ end
20
23
 
21
- # Has the value of the expression changed?
22
- #
23
- # We use the pretty-printed string represenation to detect differences
24
- # as this avoids problems with dup (causes too many differences) and == (causes too few)
25
- def changed?
26
- (value != previous_value)
27
- end
24
+ # Has the value of the expression changed?
25
+ #
26
+ # We use the pretty-printed string represenation to detect differences
27
+ # as this avoids problems with dup (causes too many differences) and ==
28
+ # (causes too few)
29
+ def changed?
30
+ (value != previous_value)
31
+ end
28
32
 
29
- private
33
+ private
30
34
 
31
- def target_eval(target, source)
32
- target.eval(source)
33
- rescue => e
34
- e
35
+ def target_eval(target, source)
36
+ target.eval(source)
37
+ rescue StandardError => e
38
+ e
39
+ end
35
40
  end
36
41
  end
37
42
  end
@@ -1,197 +1,205 @@
1
- class Pry
2
- class Command::Whereami < Pry::ClassCommand
3
- def initialize(*)
4
- super
1
+ # frozen_string_literal: true
5
2
 
6
- @method_code = nil
7
- end
3
+ require 'method_source'
8
4
 
9
- class << self
10
- attr_accessor :method_size_cutoff
11
- end
5
+ class Pry
6
+ class Command
7
+ class Whereami < Pry::ClassCommand
8
+ def initialize(*)
9
+ super
12
10
 
13
- @method_size_cutoff = 30
11
+ @method_code = nil
12
+ end
14
13
 
15
- match 'whereami'
16
- description 'Show code surrounding the current context.'
17
- group 'Context'
14
+ class << self
15
+ attr_accessor :method_size_cutoff
16
+ end
18
17
 
19
- banner <<-'BANNER'
20
- Usage: whereami [-qn] [LINES]
18
+ @method_size_cutoff = 30
21
19
 
22
- Describe the current location. If you use `binding.pry` inside a method then
23
- whereami will print out the source for that method.
20
+ match 'whereami'
21
+ description 'Show code surrounding the current context.'
22
+ group 'Context'
24
23
 
25
- If a number is passed, then LINES lines before and after the current line will be
26
- shown instead of the method itself.
24
+ banner <<-'BANNER'
25
+ Usage: whereami [-qn] [LINES]
27
26
 
28
- The `-q` flag can be used to suppress error messages in the case that there's
29
- no code to show. This is used by pry in the default before_session hook to show
30
- you when you arrive at a `binding.pry`.
27
+ Describe the current location. If you use `binding.pry` inside a method then
28
+ whereami will print out the source for that method.
31
29
 
32
- The `-n` flag can be used to hide line numbers so that code can be copy/pasted
33
- effectively.
30
+ If a number is passed, then LINES lines before and after the current line will be
31
+ shown instead of the method itself.
34
32
 
35
- When pry was started on an Object and there is no associated method, whereami
36
- will instead output a brief description of the current object.
37
- BANNER
33
+ The `-q` flag can be used to suppress error messages in the case that there's
34
+ no code to show. This is used by pry in the default before_session hook to show
35
+ you when you arrive at a `binding.pry`.
38
36
 
39
- def setup
40
- @file = expand_path(target.eval('__FILE__'))
41
- @line = target.eval('__LINE__')
42
- @method = Pry::Method.from_binding(target)
43
- end
37
+ The `-n` flag can be used to hide line numbers so that code can be copy/pasted
38
+ effectively.
44
39
 
45
- def options(opt)
46
- opt.on :q, :quiet, "Don't display anything in case of an error"
47
- opt.on :n, :"no-line-numbers", "Do not display line numbers"
48
- opt.on :m, :"method", "Show the complete source for the current method."
49
- opt.on :c, :"class", "Show the complete source for the current class or module."
50
- opt.on :f, :"file", "Show the complete source for the current file."
51
- end
40
+ When pry was started on an Object and there is no associated method, whereami
41
+ will instead output a brief description of the current object.
42
+ BANNER
52
43
 
53
- def code
54
- @code ||= if opts.present?(:m)
55
- method_code or raise CommandError, "Cannot find method code."
56
- elsif opts.present?(:c)
57
- class_code or raise CommandError, "Cannot find class code."
58
- elsif opts.present?(:f)
59
- Pry::Code.from_file(@file)
60
- elsif args.any?
61
- code_window
62
- else
63
- default_code
64
- end
65
- end
44
+ def setup
45
+ if target.respond_to?(:source_location)
46
+ file, @line = target.source_location
47
+ @file = expand_path(file)
48
+ else
49
+ @file = expand_path(target.eval('__FILE__'))
50
+ @line = target.eval('__LINE__')
51
+ end
52
+ @method = Pry::Method.from_binding(target)
53
+ end
66
54
 
67
- def code?
68
- !!code
69
- rescue MethodSource::SourceNotFoundError
70
- false
71
- end
55
+ def options(opt)
56
+ opt.on :q, :quiet, "Don't display anything in case of an error"
57
+ opt.on :n, :"no-line-numbers", "Do not display line numbers"
58
+ opt.on :m, :method, "Show the complete source for the current method."
59
+ opt.on :c, :class, "Show the complete source for the current class or module."
60
+ opt.on :f, :file, "Show the complete source for the current file."
61
+ end
72
62
 
73
- def bad_option_combination?
74
- [opts.present?(:m), opts.present?(:f),
75
- opts.present?(:c), args.any?].count(true) > 1
76
- end
63
+ def code
64
+ @code ||= if opts.present?(:m)
65
+ method_code || raise(CommandError, "Cannot find method code.")
66
+ elsif opts.present?(:c)
67
+ class_code || raise(CommandError, "Cannot find class code.")
68
+ elsif opts.present?(:f)
69
+ Pry::Code.from_file(@file)
70
+ elsif args.any?
71
+ code_window
72
+ else
73
+ default_code
74
+ end
75
+ end
77
76
 
78
- def location
79
- "#{@file} @ line #{@line} #{@method && @method.name_with_owner}"
80
- end
77
+ def code?
78
+ !!code
79
+ rescue MethodSource::SourceNotFoundError
80
+ false
81
+ end
81
82
 
82
- def process
83
- if bad_option_combination?
84
- raise CommandError, "Only one of -m, -c, -f, and LINES may be specified."
83
+ def bad_option_combination?
84
+ [opts.present?(:m), opts.present?(:f),
85
+ opts.present?(:c), args.any?].count(true) > 1
85
86
  end
86
87
 
87
- if nothing_to_do?
88
- return
89
- elsif internal_binding?(target)
90
- handle_internal_binding
91
- return
88
+ def location
89
+ "#{@file}:#{@line} #{@method && @method.name_with_owner}"
92
90
  end
93
91
 
94
- set_file_and_dir_locals(@file)
92
+ def process
93
+ if bad_option_combination?
94
+ raise CommandError, "Only one of -m, -c, -f, and LINES may be specified."
95
+ end
95
96
 
96
- out = "\n#{bold('From:')} #{location}:\n\n" <<
97
- code.with_line_numbers(use_line_numbers?).with_marker(marker).highlighted << "\n"
97
+ return if nothing_to_do?
98
98
 
99
- _pry_.pager.page out
100
- end
99
+ if internal_binding?(target)
100
+ handle_internal_binding
101
+ return
102
+ end
101
103
 
102
- private
104
+ set_file_and_dir_locals(@file)
103
105
 
104
- def nothing_to_do?
105
- opts.quiet? && (internal_binding?(target) || !code?)
106
- end
106
+ pretty_code = code.with_line_numbers(use_line_numbers?)
107
+ .with_marker(marker)
108
+ .highlighted
109
+ pry_instance.pager.page(
110
+ "\n#{bold('From:')} #{location}:\n\n" + pretty_code + "\n"
111
+ )
112
+ end
107
113
 
108
- def use_line_numbers?
109
- !opts.present?(:n)
110
- end
114
+ private
111
115
 
112
- def marker
113
- !opts.present?(:n) && @line
114
- end
116
+ def nothing_to_do?
117
+ opts.quiet? && (internal_binding?(target) || !code?)
118
+ end
115
119
 
116
- def top_level?
117
- target_self == Pry.main
118
- end
120
+ def use_line_numbers?
121
+ !opts.present?(:n)
122
+ end
119
123
 
120
- def handle_internal_binding
121
- if top_level?
122
- output.puts "At the top level."
123
- else
124
- output.puts "Inside #{Pry.view_clip(target_self)}."
124
+ def marker
125
+ !opts.present?(:n) && @line
125
126
  end
126
- end
127
127
 
128
- def small_method?
129
- @method.source_range.count < self.class.method_size_cutoff
130
- end
128
+ def top_level?
129
+ target_self == Pry.main
130
+ end
131
131
 
132
- def default_code
133
- if method_code && small_method?
134
- method_code
135
- else
136
- code_window
132
+ def handle_internal_binding
133
+ if top_level?
134
+ output.puts "At the top level."
135
+ else
136
+ output.puts "Inside #{Pry.view_clip(target_self)}."
137
+ end
137
138
  end
138
- end
139
139
 
140
- def code_window
141
- Pry::Code.from_file(@file).around(@line, window_size)
142
- end
140
+ def small_method?
141
+ @method.source_range.count < self.class.method_size_cutoff
142
+ end
143
143
 
144
- def method_code
145
- return @method_code if @method_code
144
+ def default_code
145
+ if method_code && small_method?
146
+ method_code
147
+ else
148
+ code_window
149
+ end
150
+ end
146
151
 
147
- if valid_method?
148
- @method_code = Pry::Code.from_method(@method)
152
+ def code_window
153
+ Pry::Code.from_file(@file).around(@line, window_size)
149
154
  end
150
- end
151
155
 
152
- # This either returns the `target_self`
153
- # or it returns the class of `target_self` if `target_self` is not a class.
154
- # @return [Pry::WrappedModule]
155
- def target_class
156
- return Pry::WrappedModule(target_self) if target_self.is_a?(Module)
156
+ def method_code
157
+ return @method_code if @method_code
157
158
 
158
- Pry::WrappedModule(target_self.class)
159
- end
159
+ @method_code = Pry::Code.from_method(@method) if valid_method?
160
+ end
160
161
 
161
- def class_code
162
- @class_code ||=
163
- begin
164
- mod = @method ? Pry::WrappedModule(@method.owner) : target_class
165
- idx = mod.candidates.find_index { |v| expand_path(v.source_file) == @file }
166
- idx && Pry::Code.from_module(mod, idx)
167
- end
168
- end
162
+ # This either returns the `target_self`
163
+ # or it returns the class of `target_self` if `target_self` is not a class.
164
+ # @return [Pry::WrappedModule]
165
+ def target_class
166
+ return Pry::WrappedModule(target_self) if target_self.is_a?(Module)
169
167
 
170
- def valid_method?
171
- @method && @method.source? && expand_path(@method.source_file) == @file &&
172
- @method.source_range.include?(@line)
173
- end
168
+ Pry::WrappedModule(target_self.class)
169
+ end
174
170
 
175
- def expand_path(f)
176
- return if !f
171
+ def class_code
172
+ @class_code ||=
173
+ begin
174
+ mod = @method ? Pry::WrappedModule(@method.owner) : target_class
175
+ idx = mod.candidates.find_index { |v| expand_path(v.source_file) == @file }
176
+ idx && Pry::Code.from_module(mod, idx)
177
+ end
178
+ end
177
179
 
178
- if Pry.eval_path == f
179
- f
180
- else
181
- File.expand_path(f)
180
+ def valid_method?
181
+ @method && @method.source? && expand_path(@method.source_file) == @file &&
182
+ @method.source_range.include?(@line)
182
183
  end
183
- end
184
184
 
185
- def window_size
186
- if args.empty?
187
- _pry_.config.default_window_size
188
- else
189
- args.first.to_i
185
+ def expand_path(filename)
186
+ return unless filename
187
+ return filename if Pry.eval_path == filename
188
+
189
+ File.expand_path(filename)
190
+ end
191
+
192
+ def window_size
193
+ if args.empty?
194
+ pry_instance.config.default_window_size
195
+ else
196
+ args.first.to_i
197
+ end
190
198
  end
191
199
  end
192
- end
193
200
 
194
- Pry::Commands.add_command(Pry::Command::Whereami)
195
- Pry::Commands.alias_command '@', 'whereami'
196
- Pry::Commands.alias_command(/whereami[!?]+/, 'whereami')
201
+ Pry::Commands.add_command(Pry::Command::Whereami)
202
+ Pry::Commands.alias_command '@', 'whereami'
203
+ Pry::Commands.alias_command(/whereami[!?]+/, 'whereami')
204
+ end
197
205
  end