pry 0.9.12.2 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1141 -0
  3. data/LICENSE +2 -2
  4. data/README.md +466 -0
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +97 -92
  10. data/lib/pry/code/code_file.rb +114 -0
  11. data/lib/pry/code/code_range.rb +7 -4
  12. data/lib/pry/code/loc.rb +27 -14
  13. data/lib/pry/code.rb +62 -90
  14. data/lib/pry/code_object.rb +83 -39
  15. data/lib/pry/color_printer.rb +66 -0
  16. data/lib/pry/command.rb +202 -371
  17. data/lib/pry/command_set.rb +151 -133
  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 +85 -73
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -63
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +64 -47
  27. data/lib/pry/commands/cd.rb +42 -26
  28. data/lib/pry/commands/change_inspector.rb +34 -0
  29. data/lib/pry/commands/change_prompt.rb +51 -0
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +33 -24
  36. data/lib/pry/commands/edit.rb +183 -167
  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 -17
  40. data/lib/pry/commands/find_method.rb +167 -167
  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 +153 -132
  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 +42 -0
  47. data/lib/pry/commands/ls/constants.rb +75 -0
  48. data/lib/pry/commands/ls/formatter.rb +55 -0
  49. data/lib/pry/commands/ls/globals.rb +50 -0
  50. data/lib/pry/commands/ls/grep.rb +23 -0
  51. data/lib/pry/commands/ls/instance_vars.rb +40 -0
  52. data/lib/pry/commands/ls/interrogatable.rb +24 -0
  53. data/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  54. data/lib/pry/commands/ls/local_names.rb +37 -0
  55. data/lib/pry/commands/ls/local_vars.rb +47 -0
  56. data/lib/pry/commands/ls/ls_entity.rb +65 -0
  57. data/lib/pry/commands/ls/methods.rb +55 -0
  58. data/lib/pry/commands/ls/methods_helper.rb +50 -0
  59. data/lib/pry/commands/ls/self_methods.rb +34 -0
  60. data/lib/pry/commands/ls.rb +100 -303
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +93 -49
  63. data/lib/pry/commands/pry_backtrace.rb +22 -18
  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 +57 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -38
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +66 -34
  71. data/lib/pry/commands/shell_mode.rb +22 -20
  72. data/lib/pry/commands/show_doc.rb +80 -65
  73. data/lib/pry/commands/show_info.rb +193 -159
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +113 -33
  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 +21 -13
  79. data/lib/pry/commands/watch_expression/expression.rb +43 -0
  80. data/lib/pry/commands/watch_expression.rb +110 -0
  81. data/lib/pry/commands/whereami.rb +157 -134
  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 +290 -220
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +50 -27
  90. data/lib/pry/editor.rb +130 -102
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +73 -0
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +22 -151
  96. data/lib/pry/helpers/command_helpers.rb +55 -63
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  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 +96 -86
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +101 -70
  104. data/lib/pry/hooks.rb +67 -137
  105. data/lib/pry/indent.rb +79 -73
  106. data/lib/pry/input_completer.rb +283 -0
  107. data/lib/pry/input_lock.rb +129 -0
  108. data/lib/pry/inspector.rb +39 -0
  109. data/lib/pry/last_exception.rb +61 -0
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +51 -42
  112. data/lib/pry/method/weird_method_locator.rb +80 -44
  113. data/lib/pry/method.rb +225 -176
  114. data/lib/pry/object_path.rb +91 -0
  115. data/lib/pry/output.rb +136 -0
  116. data/lib/pry/pager.rb +227 -68
  117. data/lib/pry/prompt.rb +214 -0
  118. data/lib/pry/pry_class.rb +216 -289
  119. data/lib/pry/pry_instance.rb +438 -500
  120. data/lib/pry/repl.rb +256 -0
  121. data/lib/pry/repl_file_loader.rb +34 -35
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/LICENSE +20 -0
  124. data/lib/pry/slop/commands.rb +190 -0
  125. data/lib/pry/slop/option.rb +210 -0
  126. data/lib/pry/slop.rb +672 -0
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable/evalable.rb +24 -0
  130. data/lib/pry/testable/mockable.rb +22 -0
  131. data/lib/pry/testable/pry_tester.rb +88 -0
  132. data/lib/pry/testable/utility.rb +34 -0
  133. data/lib/pry/testable/variables.rb +52 -0
  134. data/lib/pry/testable.rb +68 -0
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +20 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +36 -43
  138. data/lib/pry/wrapped_module.rb +102 -103
  139. data/lib/pry.rb +135 -261
  140. metadata +94 -283
  141. data/.document +0 -2
  142. data/.gitignore +0 -16
  143. data/.travis.yml +0 -21
  144. data/.yardopts +0 -1
  145. data/CHANGELOG +0 -534
  146. data/CONTRIBUTORS +0 -55
  147. data/Gemfile +0 -9
  148. data/Guardfile +0 -62
  149. data/README.markdown +0 -400
  150. data/Rakefile +0 -140
  151. data/TODO +0 -117
  152. data/lib/pry/commands/disabled_commands.rb +0 -2
  153. data/lib/pry/commands/gem_cd.rb +0 -26
  154. data/lib/pry/commands/gem_install.rb +0 -29
  155. data/lib/pry/commands/gem_list.rb +0 -33
  156. data/lib/pry/commands/gem_open.rb +0 -29
  157. data/lib/pry/commands/gist.rb +0 -102
  158. data/lib/pry/commands/install_command.rb +0 -51
  159. data/lib/pry/commands/simple_prompt.rb +0 -22
  160. data/lib/pry/commands.rb +0 -6
  161. data/lib/pry/completion.rb +0 -304
  162. data/lib/pry/custom_completions.rb +0 -6
  163. data/lib/pry/history_array.rb +0 -116
  164. data/lib/pry/plugins.rb +0 -103
  165. data/lib/pry/rbx_method.rb +0 -13
  166. data/lib/pry/rbx_path.rb +0 -22
  167. data/lib/pry/rubygem.rb +0 -74
  168. data/lib/pry/terminal.rb +0 -78
  169. data/lib/pry/test/helper.rb +0 -185
  170. data/man/pry.1 +0 -195
  171. data/man/pry.1.html +0 -204
  172. data/man/pry.1.ronn +0 -141
  173. data/pry.gemspec +0 -30
  174. data/spec/Procfile +0 -3
  175. data/spec/cli_spec.rb +0 -78
  176. data/spec/code_object_spec.rb +0 -277
  177. data/spec/code_spec.rb +0 -219
  178. data/spec/command_helpers_spec.rb +0 -29
  179. data/spec/command_integration_spec.rb +0 -644
  180. data/spec/command_set_spec.rb +0 -627
  181. data/spec/command_spec.rb +0 -821
  182. data/spec/commands/amend_line_spec.rb +0 -247
  183. data/spec/commands/bang_spec.rb +0 -19
  184. data/spec/commands/cat_spec.rb +0 -164
  185. data/spec/commands/cd_spec.rb +0 -250
  186. data/spec/commands/disable_pry_spec.rb +0 -25
  187. data/spec/commands/edit_spec.rb +0 -727
  188. data/spec/commands/exit_all_spec.rb +0 -34
  189. data/spec/commands/exit_program_spec.rb +0 -19
  190. data/spec/commands/exit_spec.rb +0 -34
  191. data/spec/commands/find_method_spec.rb +0 -70
  192. data/spec/commands/gem_list_spec.rb +0 -26
  193. data/spec/commands/gist_spec.rb +0 -79
  194. data/spec/commands/help_spec.rb +0 -56
  195. data/spec/commands/hist_spec.rb +0 -181
  196. data/spec/commands/jump_to_spec.rb +0 -15
  197. data/spec/commands/ls_spec.rb +0 -181
  198. data/spec/commands/play_spec.rb +0 -140
  199. data/spec/commands/raise_up_spec.rb +0 -56
  200. data/spec/commands/save_file_spec.rb +0 -177
  201. data/spec/commands/show_doc_spec.rb +0 -510
  202. data/spec/commands/show_input_spec.rb +0 -17
  203. data/spec/commands/show_source_spec.rb +0 -782
  204. data/spec/commands/whereami_spec.rb +0 -203
  205. data/spec/completion_spec.rb +0 -239
  206. data/spec/control_d_handler_spec.rb +0 -58
  207. data/spec/documentation_helper_spec.rb +0 -73
  208. data/spec/editor_spec.rb +0 -79
  209. data/spec/exception_whitelist_spec.rb +0 -21
  210. data/spec/fixtures/candidate_helper1.rb +0 -11
  211. data/spec/fixtures/candidate_helper2.rb +0 -8
  212. data/spec/fixtures/example.erb +0 -5
  213. data/spec/fixtures/example_nesting.rb +0 -33
  214. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  215. data/spec/fixtures/testrc +0 -2
  216. data/spec/fixtures/testrcbad +0 -2
  217. data/spec/fixtures/whereami_helper.rb +0 -6
  218. data/spec/helper.rb +0 -34
  219. data/spec/helpers/bacon.rb +0 -86
  220. data/spec/helpers/mock_pry.rb +0 -43
  221. data/spec/helpers/table_spec.rb +0 -105
  222. data/spec/history_array_spec.rb +0 -67
  223. data/spec/hooks_spec.rb +0 -522
  224. data/spec/indent_spec.rb +0 -301
  225. data/spec/input_stack_spec.rb +0 -90
  226. data/spec/method_spec.rb +0 -482
  227. data/spec/prompt_spec.rb +0 -60
  228. data/spec/pry_defaults_spec.rb +0 -419
  229. data/spec/pry_history_spec.rb +0 -99
  230. data/spec/pry_output_spec.rb +0 -95
  231. data/spec/pry_spec.rb +0 -504
  232. data/spec/run_command_spec.rb +0 -25
  233. data/spec/sticky_locals_spec.rb +0 -157
  234. data/spec/syntax_checking_spec.rb +0 -81
  235. data/spec/wrapped_module_spec.rb +0 -261
  236. data/wiki/Customizing-pry.md +0 -397
  237. data/wiki/Home.md +0 -4
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,35 +32,35 @@ 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
31
39
  @match = arg
32
40
  end
33
- @match
41
+ @match ||= nil
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
- @description
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
- @banner || description
63
+ @banner ||= description
56
64
  end
57
65
 
58
66
  def block
@@ -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
100
 
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
-
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,25 +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
- # Store hooks to be run before or after the command body.
171
- # @see {Pry::CommandSet#before_command}
172
- # @see {Pry::CommandSet#after_command}
173
- def hooks
174
- @hooks ||= {:before => [], :after => []}
175
- end
176
-
177
165
  def command_regex
178
- pr = defined?(Pry.config.command_prefix) ? Pry.config.command_prefix : ""
179
- prefix = convert_to_regex(pr)
166
+ prefix = convert_to_regex(Pry.config.command_prefix)
180
167
  prefix = "(?:#{prefix})?" unless options[:use_prefix]
181
168
 
182
- /^#{prefix}#{convert_to_regex(match)}(?!\S)/
169
+ /\A#{prefix}#{convert_to_regex(match)}(?!\S)/
183
170
  end
184
171
 
185
172
  def convert_to_regex(obj)
@@ -194,22 +181,26 @@ class Pry
194
181
  # The group in which the command should be displayed in "help" output.
195
182
  # This is usually auto-generated from directory naming, but it can be
196
183
  # manually overridden if necessary.
197
- 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\d\.]+)?)}
205
- name, version = $1, $2
206
- "#{name.to_s} (v#{version.to_s})"
207
- when /pryrc/
208
- "~/.pryrc"
209
- else
210
- "(other)"
211
- end
212
- end
184
+ # Group should not be changed once it is initialized.
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)
213
204
  end
214
205
  end
215
206
 
@@ -222,7 +213,9 @@ class Pry
222
213
  attr_accessor :arg_string
223
214
  attr_accessor :context
224
215
  attr_accessor :command_set
225
- attr_accessor :_pry_
216
+ attr_accessor :hooks
217
+ attr_accessor :pry_instance
218
+ alias _pry_= pry_instance=
226
219
 
227
220
  # The block we pass *into* a command so long as `:takes_block` is
228
221
  # not equal to `false`
@@ -232,6 +225,47 @@ class Pry
232
225
  # end
233
226
  attr_accessor :command_block
234
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
+
235
269
  # Run a command from another command.
236
270
  # @param [String] command_string The string that invokes the command
237
271
  # @param [Array] args Further arguments to pass to the command
@@ -242,38 +276,28 @@ class Pry
242
276
  # @example
243
277
  # run "amend-line", "5", 'puts "hello world"'
244
278
  def run(command_string, *args)
245
- 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
246
281
  command_set.process_line(complete_string, context)
247
282
  end
248
283
 
249
284
  def commands
250
- command_set.commands
251
- end
252
-
253
- def text
254
- Pry::Helpers::Text
285
+ command_set.to_hash
255
286
  end
256
287
 
257
288
  def void
258
289
  VOID_VALUE
259
290
  end
260
291
 
261
- include Pry::Helpers::BaseHelpers
262
- include Pry::Helpers::CommandHelpers
263
-
264
- # Instantiate a command, in preparation for calling it.
265
- # @param [Hash] context The runtime context to use with this command.
266
- def initialize(context={})
267
- self.context = context
268
- self.target = context[:target]
269
- self.output = context[:output]
270
- self.eval_string = context[:eval_string]
271
- self.command_set = context[:command_set]
272
- self._pry_ = context[:pry_instance]
292
+ def _pry_
293
+ Pry::Warning.warn('_pry_ is deprecated, use pry_instance instead')
294
+ pry_instance
273
295
  end
274
296
 
275
297
  # @return [Object] The value of `self` inside the `target` binding.
276
- def target_self; target.eval('self'); end
298
+ def target_self
299
+ target.eval('self')
300
+ end
277
301
 
278
302
  # @return [Hash] Pry commands can store arbitrary state
279
303
  # here. This state persists between subsequent command invocations.
@@ -283,7 +307,7 @@ class Pry
283
307
  # state.my_state = "my state" # this will not conflict with any
284
308
  # # `state.my_state` used in another command.
285
309
  def state
286
- _pry_.command_state[match] ||= OpenStruct.new
310
+ self.class.state
287
311
  end
288
312
 
289
313
  # Revaluate the string (str) and perform interpolation.
@@ -304,13 +328,15 @@ class Pry
304
328
  # the current scope.
305
329
  def check_for_command_collision(command_match, arg_string)
306
330
  collision_type = target.eval("defined?(#{command_match})")
307
- collision_type ||= 'local-variable' if arg_string.match(%r{\A\s*[-+*/%&|^]*=})
331
+ collision_type ||= 'local-variable' if arg_string =~ %r{\A\s*[-+*/%&|^]*=}
308
332
 
309
333
  if collision_type
310
- output.puts "#{text.bold('WARNING:')} Calling Pry command '#{command_match}'," +
311
- "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
+ )
312
338
  end
313
- rescue Pry::RescuableException
339
+ rescue Pry::RescuableException # rubocop:disable Lint/HandleExceptions
314
340
  end
315
341
 
316
342
  # Extract necessary information from a line that Command.matches? this
@@ -328,12 +354,15 @@ class Pry
328
354
  # @param [String] val The line of input
329
355
  # @return [Array]
330
356
  def tokenize(val)
331
- val.replace(interpolate_string(val)) if command_options[:interpolate]
357
+ val = interpolate_string(val) if command_options[:interpolate]
332
358
 
333
359
  self.class.command_regex =~ val
334
360
 
335
361
  # please call Command.matches? before Command#call_safely
336
- 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
+
337
366
  captures = Regexp.last_match.captures
338
367
  pos = Regexp.last_match.end(0)
339
368
 
@@ -345,11 +374,16 @@ class Pry
345
374
  # process and pass a block if one is found
346
375
  pass_block(arg_string) if command_options[:takes_block]
347
376
 
348
- if arg_string
349
- args = command_options[:shellwords] ? Shellwords.shellwords(arg_string) : arg_string.split(" ")
350
- else
351
- args = []
352
- 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
353
387
 
354
388
  [val[0..pos].rstrip, arg_string, captures, args]
355
389
  end
@@ -360,7 +394,9 @@ class Pry
360
394
  def process_line(line)
361
395
  command_match, arg_string, captures, args = tokenize(line)
362
396
 
363
- 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
364
400
 
365
401
  self.arg_string = arg_string
366
402
  self.captures = captures
@@ -368,6 +404,43 @@ class Pry
368
404
  call_safely(*(captures + args))
369
405
  end
370
406
 
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
+ []
413
+ end
414
+
415
+ private
416
+
417
+ # Run the command with the given `args`.
418
+ #
419
+ # This is a public wrapper around `#call` which ensures all preconditions
420
+ # are met.
421
+ #
422
+ # @param [Array<String>] args The arguments to pass to this command.
423
+ # @return [Object] The return value of the `#call` method, or
424
+ # {Command::VOID_VALUE}.
425
+ def call_safely(*args)
426
+ if command_options[:argument_required] && args.empty?
427
+ raise CommandError, "The command '#{command_name}' requires an argument."
428
+ end
429
+
430
+ ret = use_unpatched_symbol do
431
+ call_with_hooks(*args)
432
+ end
433
+ command_options[:keep_retval] ? ret : void
434
+ end
435
+
436
+ def use_unpatched_symbol
437
+ call_method = Symbol.method_defined?(:call) && Symbol.instance_method(:call)
438
+ Symbol.class_eval { undef :call } if call_method
439
+ yield
440
+ ensure
441
+ Symbol.instance_eval { define_method(:call, call_method) } if call_method
442
+ end
443
+
371
444
  # Pass a block argument to a command.
372
445
  # @param [String] arg_string The arguments (as a string) passed to the command.
373
446
  # We inspect these for a '| do' or a '| {' and if we find it we use it
@@ -379,18 +452,19 @@ class Pry
379
452
  # Workaround for weird JRuby bug where rindex in this case can return nil
380
453
  # even when there's a match.
381
454
  arg_string.scan(/\| *(?:do|\{)/)
382
- block_index = $~ && $~.offset(0)[0]
455
+ block_index = $LAST_MATCH_INFO && $LAST_MATCH_INFO.offset(0)[0]
383
456
 
384
- return if !block_index
457
+ return unless block_index
385
458
 
386
459
  block_init_string = arg_string.slice!(block_index..-1)[1..-1]
387
460
  prime_string = "proc #{block_init_string}\n"
388
461
 
389
- if !Pry::Code.complete_expression?(prime_string)
390
- block_string = _pry_.r(target, prime_string)
391
- else
392
- block_string = prime_string
393
- end
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
394
468
 
395
469
  begin
396
470
  self.command_block = target.eval(block_string)
@@ -399,291 +473,48 @@ class Pry
399
473
  end
400
474
  end
401
475
 
402
- private :pass_block
403
-
404
- # Run the command with the given `args`.
405
- #
406
- # This is a public wrapper around `#call` which ensures all preconditions
407
- # are met.
408
- #
409
- # @param [Array<String>] args The arguments to pass to this command.
410
- # @return [Object] The return value of the `#call` method, or
411
- # {Command::VOID_VALUE}.
412
- def call_safely(*args)
413
- unless dependencies_met?
414
- gems_needed = Array(command_options[:requires_gem])
415
- gems_not_installed = gems_needed.select { |g| !Rubygem.installed?(g) }
416
- output.puts "\nThe command '#{command_name}' is #{text.bold("unavailable")} because it requires the following gems to be installed: #{(gems_not_installed.join(", "))}"
417
- output.puts "-"
418
- output.puts "Type `install-command #{command_name}` to install the required gems and activate this command."
419
- return void
420
- end
421
-
422
- if command_options[:argument_required] && args.empty?
423
- raise CommandError, "The command '#{command_name}' requires an argument."
424
- end
425
-
426
- ret = call_with_hooks(*args)
427
- command_options[:keep_retval] ? ret : void
476
+ def find_hooks(event)
477
+ event_name = "#{event}_#{command_name}"
478
+ (hooks || Pry.hooks || self.class.hooks).get_hooks(event_name).values
428
479
  end
429
480
 
430
- # Are all the gems required to use this command installed?
431
- #
432
- # @return Boolean
433
- def dependencies_met?
434
- @dependencies_met ||= command_dependencies_met?(command_options)
481
+ def before_hooks
482
+ find_hooks('before')
435
483
  end
436
484
 
437
- # Generate completions for this command
438
- #
439
- # @param [String] search The line typed so far
440
- # @return [Array<String>] Completion words
441
- def complete(search); Bond::DefaultMission.completions; end
442
-
443
- private
485
+ def after_hooks
486
+ find_hooks('after')
487
+ end
444
488
 
445
489
  # Run the `#call` method and all the registered hooks.
446
490
  # @param [Array<String>] args The arguments to `#call`
447
491
  # @return [Object] The return value from `#call`
448
492
  def call_with_hooks(*args)
449
- self.class.hooks[:before].each do |block|
450
- instance_exec(*args, &block)
451
- end
493
+ before_hooks.each { |block| instance_exec(*args, &block) }
452
494
 
453
495
  ret = call(*args)
454
496
 
455
- self.class.hooks[:after].each do |block|
497
+ after_hooks.each do |block|
456
498
  ret = instance_exec(*args, &block)
457
499
  end
458
500
 
459
501
  ret
460
502
  end
461
503
 
462
- # Fix the number of arguments we pass to a block to avoid arity warnings.
463
- # @param [Fixnum] arity The arity of the block
464
- # @param [Array] args The arguments to pass
465
- # @return [Array] A (possibly shorter) array of the arguments to pass
466
- def correct_arg_arity(arity, args)
467
- case
468
- 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
469
512
  args
470
- when arity == 0
513
+ when 0
471
514
  []
472
- when arity > 0
473
- args.values_at(*(0..(arity - 1)).to_a)
474
- end
475
- end
476
- end
477
-
478
- # A super-class for Commands that are created with a single block.
479
- #
480
- # This class ensures that the block is called with the correct number of arguments
481
- # and the right context.
482
- #
483
- # Create subclasses using {Pry::CommandSet#command}.
484
- class BlockCommand < Command
485
- # backwards compatibility
486
- alias_method :opts, :context
487
-
488
- # Call the block that was registered with this command.
489
- # @param [Array<String>] args The arguments passed
490
- # @return [Object] The return value of the block
491
- def call(*args)
492
- instance_exec(*correct_arg_arity(block.arity, args), &block)
493
- end
494
-
495
- def help
496
- "#{command_options[:listing].to_s.ljust(18)} #{description}"
497
- end
498
- end
499
-
500
- # A super-class of Commands with structure.
501
- #
502
- # This class implements the bare-minimum functionality that a command should
503
- # have, namely a --help switch, and then delegates actual processing to its
504
- # subclasses.
505
- #
506
- # Create subclasses using {Pry::CommandSet#create_command}, and override the
507
- # `options(opt)` method to set up an instance of Slop, and the `process`
508
- # method to actually run the command. If necessary, you can also override
509
- # `setup` which will be called before `options`, for example to require any
510
- # gems your command needs to run, or to set up state.
511
- class ClassCommand < Command
512
- class << self
513
-
514
- # Ensure that subclasses inherit the options, description and
515
- # match from a ClassCommand super class.
516
- def inherited(klass)
517
- klass.match match
518
- klass.description description
519
- klass.command_options options
520
- end
521
-
522
- def source
523
- source_object.source
524
- end
525
-
526
- def doc
527
- new.help
528
- end
529
-
530
- def source_location
531
- source_object.source_location
532
- end
533
-
534
- def source_file
535
- source_object.source_file
536
- end
537
- alias_method :file, :source_file
538
-
539
- def source_line
540
- source_object.source_line
541
- end
542
- alias_method :line, :source_line
543
-
544
- private
545
-
546
- # The object used to extract the source for the command.
547
- #
548
- # This should be a `Pry::Method(block)` for a command made with `create_command`
549
- # and a `Pry::WrappedModule(self)` for a command that's a standard class.
550
- # @return [Pry::WrappedModule, Pry::Method]
551
- def source_object
552
- @source_object ||= if name =~ /^[A-Z]/
553
- Pry::WrappedModule(self)
554
- else
555
- Pry::Method(block)
556
- end
557
- end
558
- end
559
-
560
- attr_accessor :opts
561
- attr_accessor :args
562
-
563
- # Set up `opts` and `args`, and then call `process`.
564
- #
565
- # This method will display help if necessary.
566
- #
567
- # @param [Array<String>] args The arguments passed
568
- # @return [Object] The return value of `process` or VOID_VALUE
569
- def call(*args)
570
- setup
571
-
572
- self.opts = slop
573
- self.args = self.opts.parse!(args)
574
-
575
- if opts.present?(:help)
576
- output.puts slop.help
577
- void
578
515
  else
579
- process(*correct_arg_arity(method(:process).arity, args))
516
+ args.values_at(*(0..(method.arity - 1)).to_a)
580
517
  end
581
518
  end
582
-
583
- # Return the help generated by Slop for this command.
584
- def help
585
- slop.help
586
- end
587
-
588
- # Return an instance of Slop that can parse either subcommands or the
589
- # options that this command accepts.
590
- def slop
591
- Slop.new do |opt|
592
- opt.banner(unindent(self.class.banner))
593
- subcommands(opt)
594
- options(opt)
595
- opt.on :h, :help, 'Show this message.'
596
- end
597
- end
598
-
599
- # Generate shell completions
600
- # @param [String] search The line typed so far
601
- # @return [Array<String>] the words to complete
602
- def complete(search)
603
- slop.map do |opt|
604
- [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
605
- end.flatten(1).compact + super
606
- end
607
-
608
- # A method called just before `options(opt)` as part of `call`.
609
- #
610
- # This method can be used to set up any context your command needs to run,
611
- # for example requiring gems, or setting default values for options.
612
- #
613
- # @example
614
- # def setup
615
- # require 'gist'
616
- # @action = :method
617
- # end
618
- def setup; end
619
-
620
- # A method to setup Slop commands so it can parse the subcommands your
621
- # command expects. If you need to set up default values, use `setup`
622
- # instead.
623
- #
624
- # @example A minimal example
625
- # def subcommands(cmd)
626
- # cmd.command :download do |opt|
627
- # description 'Downloads a content from a server'
628
- #
629
- # opt.on :verbose, 'Use verbose output'
630
- #
631
- # run do |options, arguments|
632
- # ContentDownloader.download(options, arguments)
633
- # end
634
- # end
635
- # end
636
- #
637
- # @example Define the invokation block anywhere you want
638
- # def subcommands(cmd)
639
- # cmd.command :download do |opt|
640
- # description 'Downloads a content from a server'
641
- #
642
- # opt.on :verbose, 'Use verbose output'
643
- # end
644
- # end
645
- #
646
- # def process
647
- # # Perform calculations...
648
- # opts.fetch_command(:download).run do |options, arguments|
649
- # ContentDownloader.download(options, arguments)
650
- # end
651
- # # More calculations...
652
- # end
653
- def subcommands(cmd); end
654
-
655
- # A method to setup Slop so it can parse the options your command expects.
656
- #
657
- # @note Please don't do anything side-effecty in the main part of this
658
- # method, as it may be called by Pry at any time for introspection reasons.
659
- # If you need to set up default values, use `setup` instead.
660
- #
661
- # @example
662
- # def options(opt)
663
- # opt.banner "Gists methods or classes"
664
- # opt.on(:c, :class, "gist a class") do
665
- # @action = :class
666
- # end
667
- # end
668
- def options(opt); end
669
-
670
- # The actual body of your command should go here.
671
- #
672
- # The `opts` mehod can be called to get the options that Slop has passed,
673
- # and `args` gives the remaining, unparsed arguments.
674
- #
675
- # The return value of this method is discarded unless the command was
676
- # created with `:keep_retval => true`, in which case it is returned to the
677
- # repl.
678
- #
679
- # @example
680
- # def process
681
- # if opts.present?(:class)
682
- # gist_class
683
- # else
684
- # gist_method
685
- # end
686
- # end
687
- def process; raise CommandError, "command '#{command_name}' not implemented" end
688
519
  end
689
520
  end