pry 0.11.3 → 0.14.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +346 -2
  3. data/LICENSE +1 -1
  4. data/README.md +351 -292
  5. data/bin/pry +1 -0
  6. data/lib/pry/basic_object.rb +8 -4
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +58 -76
  10. data/lib/pry/code/code_file.rb +33 -28
  11. data/lib/pry/code/code_range.rb +7 -5
  12. data/lib/pry/code/loc.rb +26 -13
  13. data/lib/pry/code.rb +37 -33
  14. data/lib/pry/code_object.rb +41 -39
  15. data/lib/pry/color_printer.rb +47 -45
  16. data/lib/pry/command.rb +178 -374
  17. data/lib/pry/command_set.rb +89 -121
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +71 -59
  24. data/lib/pry/commands/cat/file_formatter.rb +55 -49
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +62 -54
  27. data/lib/pry/commands/cd.rb +40 -35
  28. data/lib/pry/commands/change_inspector.rb +29 -22
  29. data/lib/pry/commands/change_prompt.rb +48 -23
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -133
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +19 -30
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  36. data/lib/pry/commands/edit.rb +184 -160
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -16
  40. data/lib/pry/commands/find_method.rb +168 -162
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +151 -149
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +35 -28
  47. data/lib/pry/commands/ls/constants.rb +60 -43
  48. data/lib/pry/commands/ls/formatter.rb +42 -37
  49. data/lib/pry/commands/ls/globals.rb +38 -36
  50. data/lib/pry/commands/ls/grep.rb +17 -15
  51. data/lib/pry/commands/ls/instance_vars.rb +29 -28
  52. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  53. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  54. data/lib/pry/commands/ls/local_names.rb +26 -24
  55. data/lib/pry/commands/ls/local_vars.rb +38 -30
  56. data/lib/pry/commands/ls/ls_entity.rb +47 -52
  57. data/lib/pry/commands/ls/methods.rb +49 -51
  58. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  59. data/lib/pry/commands/ls/self_methods.rb +23 -23
  60. data/lib/pry/commands/ls.rb +124 -105
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +92 -82
  63. data/lib/pry/commands/pry_backtrace.rb +22 -17
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +60 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +56 -45
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +52 -51
  71. data/lib/pry/commands/shell_mode.rb +22 -18
  72. data/lib/pry/commands/show_doc.rb +80 -70
  73. data/lib/pry/commands/show_info.rb +192 -167
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +110 -42
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +20 -16
  79. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  80. data/lib/pry/commands/watch_expression.rb +89 -84
  81. data/lib/pry/commands/whereami.rb +156 -148
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +312 -9
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +13 -10
  90. data/lib/pry/editor.rb +56 -34
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +13 -18
  94. data/lib/pry/forwardable.rb +5 -1
  95. data/lib/pry/helpers/base_helpers.rb +58 -58
  96. data/lib/pry/helpers/command_helpers.rb +52 -62
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  98. data/lib/pry/helpers/options_helpers.rb +14 -7
  99. data/lib/pry/helpers/platform.rb +55 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +20 -18
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +65 -60
  104. data/lib/pry/hooks.rb +24 -15
  105. data/lib/pry/indent.rb +72 -70
  106. data/lib/pry/input_completer.rb +251 -228
  107. data/lib/pry/input_lock.rb +7 -10
  108. data/lib/pry/inspector.rb +36 -24
  109. data/lib/pry/last_exception.rb +45 -45
  110. data/lib/pry/method/disowned.rb +18 -5
  111. data/lib/pry/method/patcher.rb +14 -5
  112. data/lib/pry/method/weird_method_locator.rb +74 -51
  113. data/lib/pry/method.rb +175 -121
  114. data/lib/pry/object_path.rb +37 -28
  115. data/lib/pry/output.rb +122 -35
  116. data/lib/pry/pager.rb +187 -180
  117. data/lib/pry/prompt.rb +213 -25
  118. data/lib/pry/pry_class.rb +91 -96
  119. data/lib/pry/pry_instance.rb +258 -223
  120. data/lib/pry/repl.rb +49 -25
  121. data/lib/pry/repl_file_loader.rb +27 -22
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/commands.rb +164 -170
  124. data/lib/pry/slop/option.rb +172 -170
  125. data/lib/pry/slop.rb +567 -556
  126. data/lib/pry/syntax_highlighter.rb +26 -0
  127. data/lib/pry/system_command_handler.rb +17 -0
  128. data/lib/pry/testable/evalable.rb +21 -12
  129. data/lib/pry/testable/mockable.rb +18 -10
  130. data/lib/pry/testable/pry_tester.rb +71 -56
  131. data/lib/pry/testable/utility.rb +29 -21
  132. data/lib/pry/testable/variables.rb +49 -43
  133. data/lib/pry/testable.rb +59 -61
  134. data/lib/pry/version.rb +3 -1
  135. data/lib/pry/warning.rb +20 -0
  136. data/lib/pry/wrapped_module/candidate.rb +31 -23
  137. data/lib/pry/wrapped_module.rb +63 -59
  138. data/lib/pry.rb +132 -149
  139. metadata +39 -51
  140. data/lib/pry/commands/disabled_commands.rb +0 -2
  141. data/lib/pry/commands/gem_cd.rb +0 -26
  142. data/lib/pry/commands/gem_install.rb +0 -32
  143. data/lib/pry/commands/gem_list.rb +0 -33
  144. data/lib/pry/commands/gem_open.rb +0 -29
  145. data/lib/pry/commands/gem_readme.rb +0 -25
  146. data/lib/pry/commands/gem_search.rb +0 -40
  147. data/lib/pry/commands/gist.rb +0 -101
  148. data/lib/pry/commands/install_command.rb +0 -53
  149. data/lib/pry/commands/list_prompts.rb +0 -35
  150. data/lib/pry/commands/simple_prompt.rb +0 -22
  151. data/lib/pry/commands.rb +0 -6
  152. data/lib/pry/config/behavior.rb +0 -231
  153. data/lib/pry/config/convenience.rb +0 -25
  154. data/lib/pry/config/default.rb +0 -149
  155. data/lib/pry/config/memoization.rb +0 -44
  156. data/lib/pry/history_array.rb +0 -121
  157. data/lib/pry/platform.rb +0 -93
  158. data/lib/pry/plugins.rb +0 -118
  159. data/lib/pry/rbx_path.rb +0 -22
  160. data/lib/pry/rubygem.rb +0 -84
  161. data/lib/pry/terminal.rb +0 -90
data/lib/pry/command.rb CHANGED
@@ -1,20 +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
-
6
7
  # The super-class of all commands, new commands should be created by calling
7
- # {Pry::CommandSet#command} which creates a BlockCommand or {Pry::CommandSet#create_command}
8
- # 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.
9
11
  class Command
10
12
  extend Helpers::DocumentationHelpers
11
13
  extend CodeObject::Helpers
12
14
 
15
+ include Pry::Helpers::BaseHelpers
16
+ include Pry::Helpers::CommandHelpers
17
+ include Pry::Helpers::Text
18
+
13
19
  # represents a void return value for a command
14
20
  VOID_VALUE = Object.new
15
21
 
16
22
  # give it a nice inspect
17
- def VOID_VALUE.inspect() "void" end
23
+ def VOID_VALUE.inspect
24
+ "void"
25
+ end
18
26
 
19
27
  # Properties of the command itself (as passed as arguments to
20
28
  # {CommandSet#command} or {CommandSet#create_command}).
@@ -24,7 +32,7 @@ class Pry
24
32
  attr_writer :command_options
25
33
  attr_writer :match
26
34
 
27
- def match(arg=nil)
35
+ def match(arg = nil)
28
36
  if arg
29
37
  @command_options ||= default_options(arg)
30
38
  @command_options[:listing] = arg.is_a?(String) ? arg : arg.inspect
@@ -34,23 +42,23 @@ class Pry
34
42
  end
35
43
 
36
44
  # Define or get the command's description
37
- def description(arg=nil)
45
+ def description(arg = nil)
38
46
  @description = arg if arg
39
47
  @description ||= nil
40
48
  end
41
49
 
42
50
  # Define or get the command's options
43
- def command_options(arg=nil)
51
+ def command_options(arg = nil)
44
52
  @command_options ||= default_options(match)
45
53
  @command_options.merge!(arg) if arg
46
54
  @command_options
47
55
  end
48
56
  # backward compatibility
49
- alias_method :options, :command_options
50
- alias_method :options=, :command_options=
57
+ alias options command_options
58
+ alias options= command_options=
51
59
 
52
60
  # Define or get the command's banner
53
- def banner(arg=nil)
61
+ def banner(arg = nil)
54
62
  @banner = arg if arg
55
63
  @banner ||= description
56
64
  end
@@ -68,45 +76,28 @@ class Pry
68
76
  new.help
69
77
  end
70
78
 
71
- def source_location
72
- block.source_location
73
- end
74
-
75
79
  def source_file
76
80
  Array(block.source_location).first
77
81
  end
78
- alias_method :file, :source_file
82
+ alias file source_file
79
83
 
80
84
  def source_line
81
85
  Array(block.source_location).last
82
86
  end
83
- alias_method :line, :source_line
87
+ alias line source_line
84
88
 
85
89
  def default_options(match)
86
90
  {
87
- :requires_gem => [],
88
- :keep_retval => false,
89
- :argument_required => false,
90
- :interpolate => true,
91
- :shellwords => true,
92
- :listing => (String === match ? match : match.inspect),
93
- :use_prefix => true,
94
- :takes_block => false
91
+ keep_retval: false,
92
+ argument_required: false,
93
+ interpolate: true,
94
+ shellwords: true,
95
+ listing: (match.is_a?(String) ? match : match.inspect),
96
+ use_prefix: true,
97
+ takes_block: false
95
98
  }
96
99
  end
97
- end
98
-
99
- # Make those properties accessible to instances
100
- def name; self.class.name; end
101
- def match; self.class.match; end
102
- def description; self.class.description; end
103
- def block; self.class.block; end
104
- def command_options; self.class.options; end
105
- def command_name; self.class.command_name; end
106
- def source; self.class.source; end
107
- def source_location; self.class.source_location; end
108
100
 
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,26 +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
- # @see {Pry::CommandSet#before_command}
173
- # @see {Pry::CommandSet#after_command}
174
- def hooks
175
- Pry.hooks
176
- end
177
-
178
165
  def command_regex
179
- pr = Pry.respond_to?(:config) ? Pry.config.command_prefix : ""
180
- prefix = convert_to_regex(pr)
166
+ prefix = convert_to_regex(Pry.config.command_prefix)
181
167
  prefix = "(?:#{prefix})?" unless options[:use_prefix]
182
168
 
183
- /^#{prefix}#{convert_to_regex(match)}(?!\S)/
169
+ /\A#{prefix}#{convert_to_regex(match)}(?!\S)/
184
170
  end
185
171
 
186
172
  def convert_to_regex(obj)
@@ -196,22 +182,25 @@ class Pry
196
182
  # This is usually auto-generated from directory naming, but it can be
197
183
  # manually overridden if necessary.
198
184
  # Group should not be changed once it is initialized.
199
- def group(name=nil)
200
- @group ||= if name
201
- name
202
- else
203
- case Pry::Method(block).source_file
204
- when %r{/pry/.*_commands/(.*).rb}
205
- $1.capitalize.gsub(/_/, " ")
206
- when %r{(pry-\w+)-([\d\.]+([\w\.]+)?)}
207
- name, version = $1, $2
208
- "#{name.to_s} (v#{version.to_s})"
209
- when /pryrc/
210
- "~/.pryrc"
211
- else
212
- "(other)"
213
- end
214
- end
185
+ def group(name = nil)
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)
215
204
  end
216
205
  end
217
206
 
@@ -225,7 +214,8 @@ class Pry
225
214
  attr_accessor :context
226
215
  attr_accessor :command_set
227
216
  attr_accessor :hooks
228
- attr_accessor :_pry_
217
+ attr_accessor :pry_instance
218
+ alias _pry_= pry_instance=
229
219
 
230
220
  # The block we pass *into* a command so long as `:takes_block` is
231
221
  # not equal to `false`
@@ -235,6 +225,47 @@ class Pry
235
225
  # end
236
226
  attr_accessor :command_block
237
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
+
238
269
  # Run a command from another command.
239
270
  # @param [String] command_string The string that invokes the command
240
271
  # @param [Array] args Further arguments to pass to the command
@@ -245,8 +276,8 @@ class Pry
245
276
  # @example
246
277
  # run "amend-line", "5", 'puts "hello world"'
247
278
  def run(command_string, *args)
248
- command_string = _pry_.config.command_prefix.to_s + command_string
249
- 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
250
281
  command_set.process_line(complete_string, context)
251
282
  end
252
283
 
@@ -254,31 +285,19 @@ class Pry
254
285
  command_set.to_hash
255
286
  end
256
287
 
257
- def text
258
- Pry::Helpers::Text
259
- end
260
-
261
288
  def void
262
289
  VOID_VALUE
263
290
  end
264
291
 
265
- include Pry::Helpers::BaseHelpers
266
- include Pry::Helpers::CommandHelpers
267
-
268
- # Instantiate a command, in preparation for calling it.
269
- # @param [Hash] context The runtime context to use with this command.
270
- def initialize(context={})
271
- self.context = context
272
- self.target = context[:target]
273
- self.output = context[:output]
274
- self.eval_string = context[:eval_string]
275
- self.command_set = context[:command_set]
276
- self.hooks = context[:hooks]
277
- self._pry_ = context[:pry_instance]
292
+ def _pry_
293
+ Pry::Warning.warn('_pry_ is deprecated, use pry_instance instead')
294
+ pry_instance
278
295
  end
279
296
 
280
297
  # @return [Object] The value of `self` inside the `target` binding.
281
- def target_self; target.eval('self'); end
298
+ def target_self
299
+ target.eval('self')
300
+ end
282
301
 
283
302
  # @return [Hash] Pry commands can store arbitrary state
284
303
  # here. This state persists between subsequent command invocations.
@@ -288,7 +307,7 @@ class Pry
288
307
  # state.my_state = "my state" # this will not conflict with any
289
308
  # # `state.my_state` used in another command.
290
309
  def state
291
- _pry_.command_state[match] ||= Pry::Config.from_hash({})
310
+ self.class.state
292
311
  end
293
312
 
294
313
  # Revaluate the string (str) and perform interpolation.
@@ -309,12 +328,15 @@ class Pry
309
328
  # the current scope.
310
329
  def check_for_command_collision(command_match, arg_string)
311
330
  collision_type = target.eval("defined?(#{command_match})")
312
- collision_type ||= 'local-variable' if arg_string.match(%r{\A\s*[-+*/%&|^]*=})
331
+ collision_type ||= 'local-variable' if arg_string =~ %r{\A\s*[-+*/%&|^]*=}
313
332
 
314
333
  if collision_type
315
- 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
+ )
316
338
  end
317
- rescue Pry::RescuableException
339
+ rescue Pry::RescuableException # rubocop:disable Lint/HandleExceptions
318
340
  end
319
341
 
320
342
  # Extract necessary information from a line that Command.matches? this
@@ -332,12 +354,15 @@ class Pry
332
354
  # @param [String] val The line of input
333
355
  # @return [Array]
334
356
  def tokenize(val)
335
- val.replace(interpolate_string(val)) if command_options[:interpolate]
357
+ val = interpolate_string(val) if command_options[:interpolate]
336
358
 
337
359
  self.class.command_regex =~ val
338
360
 
339
361
  # please call Command.matches? before Command#call_safely
340
- 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
365
+
341
366
  captures = Regexp.last_match.captures
342
367
  pos = Regexp.last_match.end(0)
343
368
 
@@ -349,11 +374,16 @@ class Pry
349
374
  # process and pass a block if one is found
350
375
  pass_block(arg_string) if command_options[:takes_block]
351
376
 
352
- if arg_string
353
- args = command_options[:shellwords] ? Shellwords.shellwords(arg_string) : arg_string.split(" ")
354
- else
355
- args = []
356
- 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
357
387
 
358
388
  [val[0..pos].rstrip, arg_string, captures, args]
359
389
  end
@@ -364,7 +394,9 @@ class Pry
364
394
  def process_line(line)
365
395
  command_match, arg_string, captures, args = tokenize(line)
366
396
 
367
- 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
368
400
 
369
401
  self.arg_string = arg_string
370
402
  self.captures = captures
@@ -372,38 +404,15 @@ class Pry
372
404
  call_safely(*(captures + args))
373
405
  end
374
406
 
375
- # Pass a block argument to a command.
376
- # @param [String] arg_string The arguments (as a string) passed to the command.
377
- # We inspect these for a '| do' or a '| {' and if we find it we use it
378
- # to start a block input sequence. Once we have a complete
379
- # block, we save it to an accessor that can be retrieved from the command context.
380
- # Note that if we find the '| do' or '| {' we delete this and the
381
- # elements following it from `arg_string`.
382
- def pass_block(arg_string)
383
- # Workaround for weird JRuby bug where rindex in this case can return nil
384
- # even when there's a match.
385
- arg_string.scan(/\| *(?:do|\{)/)
386
- block_index = $~ && $~.offset(0)[0]
387
-
388
- return if !block_index
389
-
390
- block_init_string = arg_string.slice!(block_index..-1)[1..-1]
391
- prime_string = "proc #{block_init_string}\n"
392
-
393
- if !Pry::Code.complete_expression?(prime_string)
394
- block_string = _pry_.r(target, prime_string)
395
- else
396
- block_string = prime_string
397
- end
398
-
399
- begin
400
- self.command_block = target.eval(block_string)
401
- rescue Pry::RescuableException
402
- raise CommandError, "Incomplete block definition."
403
- 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
+ []
404
413
  end
405
414
 
406
- private :pass_block
415
+ private
407
416
 
408
417
  # Run the command with the given `args`.
409
418
  #
@@ -414,15 +423,6 @@ class Pry
414
423
  # @return [Object] The return value of the `#call` method, or
415
424
  # {Command::VOID_VALUE}.
416
425
  def call_safely(*args)
417
- unless dependencies_met?
418
- gems_needed = Array(command_options[:requires_gem])
419
- gems_not_installed = gems_needed.select { |g| !Rubygem.installed?(g) }
420
- output.puts "\nThe command '#{command_name}' is #{text.bold("unavailable")} because it requires the following gems to be installed: #{(gems_not_installed.join(", "))}"
421
- output.puts "-"
422
- output.puts "Type `install-command #{command_name}` to install the required gems and activate this command."
423
- return void
424
- end
425
-
426
426
  if command_options[:argument_required] && args.empty?
427
427
  raise CommandError, "The command '#{command_name}' requires an argument."
428
428
  end
@@ -441,26 +441,41 @@ class Pry
441
441
  Symbol.instance_eval { define_method(:call, call_method) } if call_method
442
442
  end
443
443
 
444
- # Are all the gems required to use this command installed?
445
- #
446
- # @return Boolean
447
- def dependencies_met?
448
- @dependencies_met ||= command_dependencies_met?(command_options)
449
- 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]
450
456
 
451
- # Generate completions for this command
452
- #
453
- # @param [String] search The line typed so far
454
- # @return [Array<String>] Completion words
455
- def complete(search)
456
- []
457
- end
457
+ return unless block_index
458
458
 
459
- 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
460
475
 
461
476
  def find_hooks(event)
462
477
  event_name = "#{event}_#{command_name}"
463
- (self.hooks || self.class.hooks).get_hooks(event_name).values
478
+ (hooks || Pry.hooks || self.class.hooks).get_hooks(event_name).values
464
479
  end
465
480
 
466
481
  def before_hooks
@@ -486,231 +501,20 @@ class Pry
486
501
  ret
487
502
  end
488
503
 
489
- # Fix the number of arguments we pass to a block to avoid arity warnings.
490
- # @param [Fixnum] arity The arity of the block
491
- # @param [Array] args The arguments to pass
492
- # @return [Array] A (possibly shorter) array of the arguments to pass
493
- def correct_arg_arity(arity, args)
494
- case
495
- 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
496
512
  args
497
- when arity == 0
513
+ when 0
498
514
  []
499
- when arity > 0
500
- args.values_at(*(0..(arity - 1)).to_a)
501
- end
502
- end
503
- end
504
-
505
- # A super-class for Commands that are created with a single block.
506
- #
507
- # This class ensures that the block is called with the correct number of arguments
508
- # and the right context.
509
- #
510
- # Create subclasses using {Pry::CommandSet#command}.
511
- class BlockCommand < Command
512
- # backwards compatibility
513
- alias_method :opts, :context
514
-
515
- # Call the block that was registered with this command.
516
- # @param [Array<String>] args The arguments passed
517
- # @return [Object] The return value of the block
518
- def call(*args)
519
- instance_exec(*correct_arg_arity(block.arity, args), &block)
520
- end
521
-
522
- def help
523
- "#{command_options[:listing].to_s.ljust(18)} #{description}"
524
- end
525
- end
526
-
527
- # A super-class of Commands with structure.
528
- #
529
- # This class implements the bare-minimum functionality that a command should
530
- # have, namely a --help switch, and then delegates actual processing to its
531
- # subclasses.
532
- #
533
- # Create subclasses using {Pry::CommandSet#create_command}, and override the
534
- # `options(opt)` method to set up an instance of Pry::Slop, and the `process`
535
- # method to actually run the command. If necessary, you can also override
536
- # `setup` which will be called before `options`, for example to require any
537
- # gems your command needs to run, or to set up state.
538
- class ClassCommand < Command
539
- class << self
540
-
541
- # Ensure that subclasses inherit the options, description and
542
- # match from a ClassCommand super class.
543
- def inherited(klass)
544
- klass.match match
545
- klass.description description
546
- klass.command_options options
547
- end
548
-
549
- def source
550
- source_object.source
551
- end
552
-
553
- def doc
554
- new.help
555
- end
556
-
557
- def source_location
558
- source_object.source_location
559
- end
560
-
561
- def source_file
562
- source_object.source_file
563
- end
564
- alias_method :file, :source_file
565
-
566
- def source_line
567
- source_object.source_line
568
- end
569
- alias_method :line, :source_line
570
-
571
- private
572
-
573
- # The object used to extract the source for the command.
574
- #
575
- # This should be a `Pry::Method(block)` for a command made with `create_command`
576
- # and a `Pry::WrappedModule(self)` for a command that's a standard class.
577
- # @return [Pry::WrappedModule, Pry::Method]
578
- def source_object
579
- @source_object ||= if name =~ /^[A-Z]/
580
- Pry::WrappedModule(self)
581
- else
582
- Pry::Method(block)
583
- end
584
- end
585
- end
586
-
587
- attr_accessor :opts
588
- attr_accessor :args
589
-
590
- # Set up `opts` and `args`, and then call `process`.
591
- #
592
- # This method will display help if necessary.
593
- #
594
- # @param [Array<String>] args The arguments passed
595
- # @return [Object] The return value of `process` or VOID_VALUE
596
- def call(*args)
597
- setup
598
-
599
- self.opts = slop
600
- self.args = self.opts.parse!(args)
601
-
602
- if opts.present?(:help)
603
- output.puts slop.help
604
- void
605
515
  else
606
- process(*correct_arg_arity(method(:process).arity, args))
607
- end
608
- end
609
-
610
- # Return the help generated by Pry::Slop for this command.
611
- def help
612
- slop.help
613
- end
614
-
615
- # Return an instance of Pry::Slop that can parse either subcommands or the
616
- # options that this command accepts.
617
- def slop
618
- Pry::Slop.new do |opt|
619
- opt.banner(unindent(self.class.banner))
620
- subcommands(opt)
621
- options(opt)
622
- opt.on :h, :help, 'Show this message.'
516
+ args.values_at(*(0..(method.arity - 1)).to_a)
623
517
  end
624
518
  end
625
-
626
- # Generate shell completions
627
- # @param [String] search The line typed so far
628
- # @return [Array<String>] the words to complete
629
- def complete(search)
630
- slop.flat_map do |opt|
631
- [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
632
- end.compact + super
633
- end
634
-
635
- # A method called just before `options(opt)` as part of `call`.
636
- #
637
- # This method can be used to set up any context your command needs to run,
638
- # for example requiring gems, or setting default values for options.
639
- #
640
- # @example
641
- # def setup
642
- # require 'gist'
643
- # @action = :method
644
- # end
645
- def setup; end
646
-
647
- # A method to setup Pry::Slop commands so it can parse the subcommands your
648
- # command expects. If you need to set up default values, use `setup`
649
- # instead.
650
- #
651
- # @example A minimal example
652
- # def subcommands(cmd)
653
- # cmd.command :download do |opt|
654
- # description 'Downloads a content from a server'
655
- #
656
- # opt.on :verbose, 'Use verbose output'
657
- #
658
- # run do |options, arguments|
659
- # ContentDownloader.download(options, arguments)
660
- # end
661
- # end
662
- # end
663
- #
664
- # @example Define the invokation block anywhere you want
665
- # def subcommands(cmd)
666
- # cmd.command :download do |opt|
667
- # description 'Downloads a content from a server'
668
- #
669
- # opt.on :verbose, 'Use verbose output'
670
- # end
671
- # end
672
- #
673
- # def process
674
- # # Perform calculations...
675
- # opts.fetch_command(:download).run do |options, arguments|
676
- # ContentDownloader.download(options, arguments)
677
- # end
678
- # # More calculations...
679
- # end
680
- def subcommands(cmd); end
681
-
682
- # A method to setup Pry::Slop so it can parse the options your command expects.
683
- #
684
- # @note Please don't do anything side-effecty in the main part of this
685
- # method, as it may be called by Pry at any time for introspection reasons.
686
- # If you need to set up default values, use `setup` instead.
687
- #
688
- # @example
689
- # def options(opt)
690
- # opt.banner "Gists methods or classes"
691
- # opt.on(:c, :class, "gist a class") do
692
- # @action = :class
693
- # end
694
- # end
695
- def options(opt); end
696
-
697
- # The actual body of your command should go here.
698
- #
699
- # The `opts` mehod can be called to get the options that Pry::Slop has passed,
700
- # and `args` gives the remaining, unparsed arguments.
701
- #
702
- # The return value of this method is discarded unless the command was
703
- # created with `:keep_retval => true`, in which case it is returned to the
704
- # repl.
705
- #
706
- # @example
707
- # def process
708
- # if opts.present?(:class)
709
- # gist_class
710
- # else
711
- # gist_method
712
- # end
713
- # end
714
- def process; raise CommandError, "command '#{command_name}' not implemented" end
715
519
  end
716
520
  end