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,65 +1,66 @@
1
- # PP subclass for streaming inspect output in color.
1
+ # frozen_string_literal: true
2
+
3
+ require 'pp'
4
+ require 'English'
5
+
2
6
  class Pry
7
+ # PP subclass for streaming inspect output in color.
3
8
  class ColorPrinter < ::PP
4
- OBJ_COLOR = begin
5
- code = CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
6
- if code.start_with? "\e"
7
- code
8
- else
9
- "\e[0m\e[0;#{code}m"
9
+ Pry::SyntaxHighlighter.overwrite_coderay_comment_token!
10
+
11
+ def self.default(_output, value, pry_instance)
12
+ pry_instance.pager.open do |pager|
13
+ pager.print pry_instance.config.output_prefix
14
+ pp(value, pager, pry_instance.output.width - 1)
10
15
  end
11
16
  end
12
17
 
13
- CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"
14
-
15
- def self.pp(obj, out = $>, width = 79, newline = "\n")
16
- q = ColorPrinter.new(out, width, newline)
17
- q.guard_inspect_key { q.pp obj }
18
- q.flush
19
- out << "\n"
18
+ def self.pp(obj, output = $DEFAULT_OUTPUT, max_width = 79)
19
+ queue = ColorPrinter.new(output, max_width, "\n")
20
+ queue.guard_inspect_key { queue.pp(obj) }
21
+ queue.flush
22
+ output << "\n"
20
23
  end
21
24
 
22
- def text(str, width = str.length)
23
- # Don't recolorize output with color [Issue #751]
24
- if str.include?("\e[")
25
- super "#{str}\e[0m", width
26
- elsif str.start_with?('#<') || str == '=' || str == '>'
27
- super highlight_object_literal(str), width
28
- else
29
- super CodeRay.scan(str, :ruby).term, width
30
- end
25
+ def pp(object)
26
+ return super unless object.is_a?(String)
27
+
28
+ # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
29
+ # Strings prettier
30
+ text(object.inspect)
31
+ rescue StandardError => exception
32
+ raise if exception.is_a?(Pry::Pager::StopPaging)
33
+
34
+ text(highlight_object_literal(inspect_object(object)))
31
35
  end
32
36
 
33
- def pp(obj)
34
- if String === obj
35
- # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
36
- # Strings prettier
37
- text(obj.inspect)
37
+ def text(str, max_width = str.length)
38
+ if str.include?("\e[")
39
+ super("#{str}\e[0m", max_width)
40
+ elsif str.start_with?('#<') || %w[= >].include?(str)
41
+ super(highlight_object_literal(str), max_width)
38
42
  else
39
- super
40
- end
41
- rescue => e
42
- raise if e.is_a? Pry::Pager::StopPaging
43
-
44
- begin
45
- str = obj.inspect
46
- rescue Exception
47
- # Read the class name off of the singleton class to provide a default
48
- # inspect.
49
- singleton = class << obj; self; end
50
- ancestors = Pry::Method.safe_send(singleton, :ancestors)
51
- klass = ancestors.reject { |k| k == singleton }.first
52
- obj_id = obj.__id__.to_s(16) rescue 0
53
- str = "#<#{klass}:0x#{obj_id}>"
43
+ super(SyntaxHighlighter.highlight(str), max_width)
54
44
  end
55
-
56
- text highlight_object_literal(str)
57
45
  end
58
46
 
59
47
  private
60
48
 
61
49
  def highlight_object_literal(object_literal)
62
- "#{OBJ_COLOR}#{object_literal}\e[0m"
50
+ code = Pry::SyntaxHighlighter.keyword_token_color
51
+ obj_color = code.start_with?("\e") ? code : "\e[0m\e[0;#{code}m"
52
+ "#{obj_color}#{object_literal}\e[0m"
53
+ end
54
+
55
+ def inspect_object(object)
56
+ object.inspect
57
+ rescue StandardError
58
+ # Read the class name off of the singleton class to provide a default
59
+ # inspect.
60
+ singleton = class << object; self; end
61
+ ancestors = Pry::Method.safe_send(singleton, :ancestors)
62
+ klass = ancestors.find { |k| k != singleton }
63
+ "#<#{klass}:0x#{object.__id__.to_s(16)}>"
63
64
  end
64
65
  end
65
66
  end
@@ -1,19 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'delegate'
2
- require 'pry/helpers/documentation_helpers'
4
+ require 'shellwords'
3
5
 
4
6
  class Pry
5
7
  # The super-class of all commands, new commands should be created by calling
6
- # {Pry::CommandSet#command} which creates a BlockCommand or {Pry::CommandSet#create_command}
7
- # which creates a ClassCommand. Please don't use this class directly.
8
+ # {Pry::CommandSet#command} which creates a BlockCommand or
9
+ # {Pry::CommandSet#create_command} which creates a ClassCommand. Please don't
10
+ # use this class directly.
8
11
  class Command
9
12
  extend Helpers::DocumentationHelpers
10
13
  extend CodeObject::Helpers
11
14
 
15
+ include Pry::Helpers::BaseHelpers
16
+ include Pry::Helpers::CommandHelpers
17
+ include Pry::Helpers::Text
18
+
12
19
  # represents a void return value for a command
13
20
  VOID_VALUE = Object.new
14
21
 
15
22
  # give it a nice inspect
16
- def VOID_VALUE.inspect() "void" end
23
+ def VOID_VALUE.inspect
24
+ "void"
25
+ end
17
26
 
18
27
  # Properties of the command itself (as passed as arguments to
19
28
  # {CommandSet#command} or {CommandSet#create_command}).
@@ -45,8 +54,8 @@ class Pry
45
54
  @command_options
46
55
  end
47
56
  # backward compatibility
48
- alias_method :options, :command_options
49
- alias_method :options=, :command_options=
57
+ alias options command_options
58
+ alias options= command_options=
50
59
 
51
60
  # Define or get the command's banner
52
61
  def banner(arg = nil)
@@ -70,43 +79,25 @@ class Pry
70
79
  def source_file
71
80
  Array(block.source_location).first
72
81
  end
73
- alias_method :file, :source_file
82
+ alias file source_file
74
83
 
75
84
  def source_line
76
85
  Array(block.source_location).last
77
86
  end
78
- alias_method :line, :source_line
87
+ alias line source_line
79
88
 
80
89
  def default_options(match)
81
90
  {
82
- requires_gem: [],
83
91
  keep_retval: false,
84
92
  argument_required: false,
85
93
  interpolate: true,
86
94
  shellwords: true,
87
- listing: (String === match ? match : match.inspect),
95
+ listing: (match.is_a?(String) ? match : match.inspect),
88
96
  use_prefix: true,
89
97
  takes_block: false
90
98
  }
91
99
  end
92
- end
93
-
94
- # Make those properties accessible to instances
95
- def name; self.class.name; end
96
-
97
- def match; self.class.match; end
98
-
99
- def description; self.class.description; end
100
-
101
- def block; self.class.block; end
102
-
103
- def command_options; self.class.options; end
104
100
 
105
- def command_name; self.class.command_name; end
106
-
107
- def source; self.class.source; end
108
-
109
- class << self
110
101
  def name
111
102
  super.to_s == "" ? "#<class(Pry::Command #{match.inspect})>" : super
112
103
  end
@@ -116,7 +107,7 @@ class Pry
116
107
  end
117
108
 
118
109
  def command_name
119
- self.options[:listing]
110
+ options[:listing]
120
111
  end
121
112
 
122
113
  # Create a new command with the given properties.
@@ -161,24 +152,21 @@ class Pry
161
152
  # @return [Fixnum]
162
153
  def match_score(val)
163
154
  if command_regex =~ val
164
- Regexp.last_match.size > 1 ? Regexp.last_match.begin(1) : Regexp.last_match.end(0)
155
+ if Regexp.last_match.size > 1
156
+ Regexp.last_match.begin(1)
157
+ else
158
+ Regexp.last_match.end(0)
159
+ end
165
160
  else
166
161
  -1
167
162
  end
168
163
  end
169
164
 
170
- # @deprecated Replaced with {Pry::Hooks#add_hook}. Left for compatibility.
171
- # Store hooks to be run before or after the command body.
172
- def hooks
173
- Pry.hooks
174
- end
175
-
176
165
  def command_regex
177
- pr = Pry.respond_to?(:config) ? Pry.config.command_prefix : ""
178
- prefix = convert_to_regex(pr)
166
+ prefix = convert_to_regex(Pry.config.command_prefix)
179
167
  prefix = "(?:#{prefix})?" unless options[:use_prefix]
180
168
 
181
- /^#{prefix}#{convert_to_regex(match)}(?!\S)/
169
+ /\A#{prefix}#{convert_to_regex(match)}(?!\S)/
182
170
  end
183
171
 
184
172
  def convert_to_regex(obj)
@@ -195,21 +183,24 @@ class Pry
195
183
  # manually overridden if necessary.
196
184
  # Group should not be changed once it is initialized.
197
185
  def group(name = nil)
198
- @group ||= if name
199
- name
200
- else
201
- case Pry::Method(block).source_file
202
- when %r{/pry/.*_commands/(.*).rb}
203
- $1.capitalize.gsub(/_/, " ")
204
- when %r{(pry-\w+)-([\d\.]+([\w\.]+)?)}
205
- name, version = $1, $2
206
- "#{name} (v#{version})"
207
- when /pryrc/
208
- "pryrc"
209
- else
210
- "(other)"
211
- end
212
- end
186
+ @group ||= begin
187
+ name || case Pry::Method(block).source_file
188
+ when %r{/pry/.*_commands/(.*).rb}
189
+ Regexp.last_match(1).capitalize.tr('_', " ")
190
+ when /(pry-\w+)-([\d\.]+([\w\.]+)?)/
191
+ name = Regexp.last_match(1)
192
+ version = Regexp.last_match(2)
193
+ "#{name} (v#{version})"
194
+ when /pryrc/
195
+ "pryrc"
196
+ else
197
+ "(other)"
198
+ end
199
+ end
200
+ end
201
+
202
+ def state
203
+ Pry::CommandState.default.state_for(match)
213
204
  end
214
205
  end
215
206
 
@@ -223,7 +214,8 @@ class Pry
223
214
  attr_accessor :context
224
215
  attr_accessor :command_set
225
216
  attr_accessor :hooks
226
- attr_accessor :_pry_
217
+ attr_accessor :pry_instance
218
+ alias _pry_= pry_instance=
227
219
 
228
220
  # The block we pass *into* a command so long as `:takes_block` is
229
221
  # not equal to `false`
@@ -233,6 +225,47 @@ class Pry
233
225
  # end
234
226
  attr_accessor :command_block
235
227
 
228
+ # Instantiate a command, in preparation for calling it.
229
+ # @param [Hash] context The runtime context to use with this command.
230
+ def initialize(context = {})
231
+ self.context = context
232
+ self.target = context[:target]
233
+ self.output = context[:output]
234
+ self.eval_string = context[:eval_string]
235
+ self.command_set = context[:command_set]
236
+ self.hooks = context[:hooks]
237
+ self.pry_instance = context[:pry_instance]
238
+ end
239
+
240
+ # Make those properties accessible to instances
241
+ def name
242
+ self.class.name
243
+ end
244
+
245
+ def match
246
+ self.class.match
247
+ end
248
+
249
+ def description
250
+ self.class.description
251
+ end
252
+
253
+ def block
254
+ self.class.block
255
+ end
256
+
257
+ def command_options
258
+ self.class.options
259
+ end
260
+
261
+ def command_name
262
+ self.class.command_name
263
+ end
264
+
265
+ def source
266
+ self.class.source
267
+ end
268
+
236
269
  # Run a command from another command.
237
270
  # @param [String] command_string The string that invokes the command
238
271
  # @param [Array] args Further arguments to pass to the command
@@ -243,8 +276,8 @@ class Pry
243
276
  # @example
244
277
  # run "amend-line", "5", 'puts "hello world"'
245
278
  def run(command_string, *args)
246
- command_string = _pry_.config.command_prefix.to_s + command_string
247
- complete_string = "#{command_string} #{args.join(" ")}".rstrip
279
+ command_string = pry_instance.config.command_prefix.to_s + command_string
280
+ complete_string = "#{command_string} #{args.join(' ')}".rstrip
248
281
  command_set.process_line(complete_string, context)
249
282
  end
250
283
 
@@ -252,40 +285,19 @@ class Pry
252
285
  command_set.to_hash
253
286
  end
254
287
 
255
- #
256
- # @deprecated
257
- # Please use black(), white(), etc directly instead (as you would with helper
258
- # functions from BaseHelpers and CommandHelpers)
259
- #
260
- # @return [Module]
261
- # Returns Pry::Helpers::Text
262
- #
263
- def text
264
- Pry::Helpers::Text
265
- end
266
-
267
288
  def void
268
289
  VOID_VALUE
269
290
  end
270
291
 
271
- include Pry::Helpers::BaseHelpers
272
- include Pry::Helpers::CommandHelpers
273
- include Pry::Helpers::Text
274
-
275
- # Instantiate a command, in preparation for calling it.
276
- # @param [Hash] context The runtime context to use with this command.
277
- def initialize(context = {})
278
- self.context = context
279
- self.target = context[:target]
280
- self.output = context[:output]
281
- self.eval_string = context[:eval_string]
282
- self.command_set = context[:command_set]
283
- self.hooks = context[:hooks]
284
- self._pry_ = context[:pry_instance]
292
+ def _pry_
293
+ Pry::Warning.warn('_pry_ is deprecated, use pry_instance instead')
294
+ pry_instance
285
295
  end
286
296
 
287
297
  # @return [Object] The value of `self` inside the `target` binding.
288
- def target_self; target.eval('self'); end
298
+ def target_self
299
+ target.eval('self')
300
+ end
289
301
 
290
302
  # @return [Hash] Pry commands can store arbitrary state
291
303
  # here. This state persists between subsequent command invocations.
@@ -295,7 +307,7 @@ class Pry
295
307
  # state.my_state = "my state" # this will not conflict with any
296
308
  # # `state.my_state` used in another command.
297
309
  def state
298
- _pry_.command_state[match] ||= Pry::Config.from_hash({})
310
+ self.class.state
299
311
  end
300
312
 
301
313
  # Revaluate the string (str) and perform interpolation.
@@ -316,12 +328,15 @@ class Pry
316
328
  # the current scope.
317
329
  def check_for_command_collision(command_match, arg_string)
318
330
  collision_type = target.eval("defined?(#{command_match})")
319
- collision_type ||= 'local-variable' if arg_string.match(%r{\A\s*[-+*/%&|^]*=})
331
+ collision_type ||= 'local-variable' if arg_string =~ %r{\A\s*[-+*/%&|^]*=}
320
332
 
321
333
  if collision_type
322
- output.puts "#{text.bold('WARNING:')} Calling Pry command '#{command_match}', which conflicts with a #{collision_type}.\n\n"
334
+ output.puts(
335
+ "#{Helpers::Text.bold('WARNING:')} Calling Pry command '#{command_match}', " \
336
+ "which conflicts with a #{collision_type}.\n\n"
337
+ )
323
338
  end
324
- rescue Pry::RescuableException
339
+ rescue Pry::RescuableException # rubocop:disable Lint/HandleExceptions
325
340
  end
326
341
 
327
342
  # Extract necessary information from a line that Command.matches? this
@@ -339,12 +354,14 @@ class Pry
339
354
  # @param [String] val The line of input
340
355
  # @return [Array]
341
356
  def tokenize(val)
342
- val.replace(interpolate_string(val)) if command_options[:interpolate]
357
+ val = interpolate_string(val) if command_options[:interpolate]
343
358
 
344
359
  self.class.command_regex =~ val
345
360
 
346
361
  # please call Command.matches? before Command#call_safely
347
- raise CommandError, "fatal: called a command which didn't match?!" unless Regexp.last_match
362
+ unless Regexp.last_match
363
+ raise CommandError, "fatal: called a command which didn't match?!"
364
+ end
348
365
 
349
366
  captures = Regexp.last_match.captures
350
367
  pos = Regexp.last_match.end(0)
@@ -357,11 +374,16 @@ class Pry
357
374
  # process and pass a block if one is found
358
375
  pass_block(arg_string) if command_options[:takes_block]
359
376
 
360
- if arg_string
361
- args = command_options[:shellwords] ? Shellwords.shellwords(arg_string) : arg_string.split(" ")
362
- else
363
- args = []
364
- end
377
+ args =
378
+ if arg_string
379
+ if command_options[:shellwords]
380
+ Shellwords.shellwords(arg_string)
381
+ else
382
+ arg_string.split(" ")
383
+ end
384
+ else
385
+ []
386
+ end
365
387
 
366
388
  [val[0..pos].rstrip, arg_string, captures, args]
367
389
  end
@@ -372,7 +394,9 @@ class Pry
372
394
  def process_line(line)
373
395
  command_match, arg_string, captures, args = tokenize(line)
374
396
 
375
- check_for_command_collision(command_match, arg_string) if Pry.config.collision_warning
397
+ if Pry.config.collision_warning
398
+ check_for_command_collision(command_match, arg_string)
399
+ end
376
400
 
377
401
  self.arg_string = arg_string
378
402
  self.captures = captures
@@ -380,38 +404,15 @@ class Pry
380
404
  call_safely(*(captures + args))
381
405
  end
382
406
 
383
- # Pass a block argument to a command.
384
- # @param [String] arg_string The arguments (as a string) passed to the command.
385
- # We inspect these for a '| do' or a '| {' and if we find it we use it
386
- # to start a block input sequence. Once we have a complete
387
- # block, we save it to an accessor that can be retrieved from the command context.
388
- # Note that if we find the '| do' or '| {' we delete this and the
389
- # elements following it from `arg_string`.
390
- def pass_block(arg_string)
391
- # Workaround for weird JRuby bug where rindex in this case can return nil
392
- # even when there's a match.
393
- arg_string.scan(/\| *(?:do|\{)/)
394
- block_index = $~ && $~.offset(0)[0]
395
-
396
- return if !block_index
397
-
398
- block_init_string = arg_string.slice!(block_index..-1)[1..-1]
399
- prime_string = "proc #{block_init_string}\n"
400
-
401
- if !Pry::Code.complete_expression?(prime_string)
402
- block_string = _pry_.r(target, prime_string)
403
- else
404
- block_string = prime_string
405
- end
406
-
407
- begin
408
- self.command_block = target.eval(block_string)
409
- rescue Pry::RescuableException
410
- raise CommandError, "Incomplete block definition."
411
- end
407
+ # Generate completions for this command
408
+ #
409
+ # @param [String] _search The line typed so far
410
+ # @return [Array<String>] Completion words
411
+ def complete(_search)
412
+ []
412
413
  end
413
414
 
414
- private :pass_block
415
+ private
415
416
 
416
417
  # Run the command with the given `args`.
417
418
  #
@@ -422,15 +423,6 @@ class Pry
422
423
  # @return [Object] The return value of the `#call` method, or
423
424
  # {Command::VOID_VALUE}.
424
425
  def call_safely(*args)
425
- unless dependencies_met?
426
- gems_needed = Array(command_options[:requires_gem])
427
- gems_not_installed = gems_needed.select { |g| !Rubygem.installed?(g) }
428
- output.puts "\nThe command '#{command_name}' is #{text.bold("unavailable")} because it requires the following gems to be installed: #{(gems_not_installed.join(", "))}"
429
- output.puts "-"
430
- output.puts "Type `install-command #{command_name}` to install the required gems and activate this command."
431
- return void
432
- end
433
-
434
426
  if command_options[:argument_required] && args.empty?
435
427
  raise CommandError, "The command '#{command_name}' requires an argument."
436
428
  end
@@ -449,26 +441,41 @@ class Pry
449
441
  Symbol.instance_eval { define_method(:call, call_method) } if call_method
450
442
  end
451
443
 
452
- # Are all the gems required to use this command installed?
453
- #
454
- # @return Boolean
455
- def dependencies_met?
456
- @dependencies_met ||= command_dependencies_met?(command_options)
457
- end
444
+ # Pass a block argument to a command.
445
+ # @param [String] arg_string The arguments (as a string) passed to the command.
446
+ # We inspect these for a '| do' or a '| {' and if we find it we use it
447
+ # to start a block input sequence. Once we have a complete
448
+ # block, we save it to an accessor that can be retrieved from the command context.
449
+ # Note that if we find the '| do' or '| {' we delete this and the
450
+ # elements following it from `arg_string`.
451
+ def pass_block(arg_string)
452
+ # Workaround for weird JRuby bug where rindex in this case can return nil
453
+ # even when there's a match.
454
+ arg_string.scan(/\| *(?:do|\{)/)
455
+ block_index = $LAST_MATCH_INFO && $LAST_MATCH_INFO.offset(0)[0]
458
456
 
459
- # Generate completions for this command
460
- #
461
- # @param [String] _search The line typed so far
462
- # @return [Array<String>] Completion words
463
- def complete(_search)
464
- []
465
- end
457
+ return unless block_index
466
458
 
467
- private
459
+ block_init_string = arg_string.slice!(block_index..-1)[1..-1]
460
+ prime_string = "proc #{block_init_string}\n"
461
+
462
+ block_string =
463
+ if !Pry::Code.complete_expression?(prime_string)
464
+ pry_instance.r(target, prime_string)
465
+ else
466
+ prime_string
467
+ end
468
+
469
+ begin
470
+ self.command_block = target.eval(block_string)
471
+ rescue Pry::RescuableException
472
+ raise CommandError, "Incomplete block definition."
473
+ end
474
+ end
468
475
 
469
476
  def find_hooks(event)
470
477
  event_name = "#{event}_#{command_name}"
471
- (self.hooks || self.class.hooks).get_hooks(event_name).values
478
+ (hooks || Pry.hooks || self.class.hooks).get_hooks(event_name).values
472
479
  end
473
480
 
474
481
  def before_hooks
@@ -494,230 +501,20 @@ class Pry
494
501
  ret
495
502
  end
496
503
 
497
- # Fix the number of arguments we pass to a block to avoid arity warnings.
498
- # @param [Fixnum] arity The arity of the block
499
- # @param [Array] args The arguments to pass
500
- # @return [Array] A (possibly shorter) array of the arguments to pass
501
- def correct_arg_arity(arity, args)
502
- case
503
- when arity < 0
504
+ # Normalize method arguments according to its arity.
505
+ #
506
+ # @param [Integer] method
507
+ # @param [Array] args
508
+ # @return [Array] a (possibly shorter) array of the arguments to pass
509
+ def normalize_method_args(method, args)
510
+ case method.arity
511
+ when -1
504
512
  args
505
- when arity == 0
513
+ when 0
506
514
  []
507
- when arity > 0
508
- args.values_at(*(0..(arity - 1)).to_a)
509
- end
510
- end
511
- end
512
-
513
- # A super-class for Commands that are created with a single block.
514
- #
515
- # This class ensures that the block is called with the correct number of arguments
516
- # and the right context.
517
- #
518
- # Create subclasses using {Pry::CommandSet#command}.
519
- class BlockCommand < Command
520
- # backwards compatibility
521
- alias_method :opts, :context
522
-
523
- # Call the block that was registered with this command.
524
- # @param [Array<String>] args The arguments passed
525
- # @return [Object] The return value of the block
526
- def call(*args)
527
- instance_exec(*correct_arg_arity(block.arity, args), &block)
528
- end
529
-
530
- def help
531
- "#{command_options[:listing].to_s.ljust(18)} #{description}"
532
- end
533
- end
534
-
535
- # A super-class of Commands with structure.
536
- #
537
- # This class implements the bare-minimum functionality that a command should
538
- # have, namely a --help switch, and then delegates actual processing to its
539
- # subclasses.
540
- #
541
- # Create subclasses using {Pry::CommandSet#create_command}, and override the
542
- # `options(opt)` method to set up an instance of Pry::Slop, and the `process`
543
- # method to actually run the command. If necessary, you can also override
544
- # `setup` which will be called before `options`, for example to require any
545
- # gems your command needs to run, or to set up state.
546
- class ClassCommand < Command
547
- class << self
548
- # Ensure that subclasses inherit the options, description and
549
- # match from a ClassCommand super class.
550
- def inherited(klass)
551
- klass.match match
552
- klass.description description
553
- klass.command_options options
554
- end
555
-
556
- def source
557
- source_object.source
558
- end
559
-
560
- def doc
561
- new.help
562
- end
563
-
564
- def source_location
565
- source_object.source_location
566
- end
567
-
568
- def source_file
569
- source_object.source_file
570
- end
571
- alias_method :file, :source_file
572
-
573
- def source_line
574
- source_object.source_line
575
- end
576
- alias_method :line, :source_line
577
-
578
- private
579
-
580
- # The object used to extract the source for the command.
581
- #
582
- # This should be a `Pry::Method(block)` for a command made with `create_command`
583
- # and a `Pry::WrappedModule(self)` for a command that's a standard class.
584
- # @return [Pry::WrappedModule, Pry::Method]
585
- def source_object
586
- @source_object ||= if name =~ /^[A-Z]/
587
- Pry::WrappedModule(self)
588
- else
589
- Pry::Method(block)
590
- end
591
- end
592
- end
593
-
594
- attr_accessor :opts
595
- attr_accessor :args
596
-
597
- # Set up `opts` and `args`, and then call `process`.
598
- #
599
- # This method will display help if necessary.
600
- #
601
- # @param [Array<String>] args The arguments passed
602
- # @return [Object] The return value of `process` or VOID_VALUE
603
- def call(*args)
604
- setup
605
-
606
- self.opts = slop
607
- self.args = self.opts.parse!(args)
608
-
609
- if opts.present?(:help)
610
- output.puts slop.help
611
- void
612
515
  else
613
- process(*correct_arg_arity(method(:process).arity, args))
516
+ args.values_at(*(0..(method.arity - 1)).to_a)
614
517
  end
615
518
  end
616
-
617
- # Return the help generated by Pry::Slop for this command.
618
- def help
619
- slop.help
620
- end
621
-
622
- # Return an instance of Pry::Slop that can parse either subcommands or the
623
- # options that this command accepts.
624
- def slop
625
- Pry::Slop.new do |opt|
626
- opt.banner(unindent(self.class.banner))
627
- subcommands(opt)
628
- options(opt)
629
- opt.on :h, :help, 'Show this message.'
630
- end
631
- end
632
-
633
- # Generate shell completions
634
- # @param [String] search The line typed so far
635
- # @return [Array<String>] the words to complete
636
- def complete(search)
637
- slop.flat_map do |opt|
638
- [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
639
- end.compact + super
640
- end
641
-
642
- # A method called just before `options(opt)` as part of `call`.
643
- #
644
- # This method can be used to set up any context your command needs to run,
645
- # for example requiring gems, or setting default values for options.
646
- #
647
- # @example
648
- # def setup
649
- # require 'gist'
650
- # @action = :method
651
- # end
652
- def setup; end
653
-
654
- # A method to setup Pry::Slop commands so it can parse the subcommands your
655
- # command expects. If you need to set up default values, use `setup`
656
- # instead.
657
- #
658
- # @example A minimal example
659
- # def subcommands(cmd)
660
- # cmd.command :download do |opt|
661
- # description 'Downloads a content from a server'
662
- #
663
- # opt.on :verbose, 'Use verbose output'
664
- #
665
- # run do |options, arguments|
666
- # ContentDownloader.download(options, arguments)
667
- # end
668
- # end
669
- # end
670
- #
671
- # @example Define the invokation block anywhere you want
672
- # def subcommands(cmd)
673
- # cmd.command :download do |opt|
674
- # description 'Downloads a content from a server'
675
- #
676
- # opt.on :verbose, 'Use verbose output'
677
- # end
678
- # end
679
- #
680
- # def process
681
- # # Perform calculations...
682
- # opts.fetch_command(:download).run do |options, arguments|
683
- # ContentDownloader.download(options, arguments)
684
- # end
685
- # # More calculations...
686
- # end
687
- def subcommands(cmd); end
688
-
689
- # A method to setup Pry::Slop so it can parse the options your command expects.
690
- #
691
- # @note Please don't do anything side-effecty in the main part of this
692
- # method, as it may be called by Pry at any time for introspection reasons.
693
- # If you need to set up default values, use `setup` instead.
694
- #
695
- # @example
696
- # def options(opt)
697
- # opt.banner "Gists methods or classes"
698
- # opt.on(:c, :class, "gist a class") do
699
- # @action = :class
700
- # end
701
- # end
702
- def options(opt); end
703
-
704
- # The actual body of your command should go here.
705
- #
706
- # The `opts` mehod can be called to get the options that Pry::Slop has passed,
707
- # and `args` gives the remaining, unparsed arguments.
708
- #
709
- # The return value of this method is discarded unless the command was
710
- # created with `:keep_retval => true`, in which case it is returned to the
711
- # repl.
712
- #
713
- # @example
714
- # def process
715
- # if opts.present?(:class)
716
- # gist_class
717
- # else
718
- # gist_method
719
- # end
720
- # end
721
- def process; raise CommandError, "command '#{command_name}' not implemented" end
722
519
  end
723
520
  end