pry 0.12.1 → 0.14.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +166 -1
  3. data/LICENSE +1 -1
  4. data/README.md +331 -269
  5. data/bin/pry +5 -0
  6. data/lib/pry.rb +132 -118
  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 +43 -54
  11. data/lib/pry/code.rb +40 -28
  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 +162 -360
  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 +22 -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 +80 -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 +110 -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 +307 -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 +58 -59
  98. data/lib/pry/helpers/command_helpers.rb +50 -61
  99. data/lib/pry/helpers/documentation_helpers.rb +21 -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 +22 -19
  104. data/lib/pry/history.rb +48 -56
  105. data/lib/pry/hooks.rb +17 -8
  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 -85
  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/prompt.rb +108 -46
  119. data/lib/pry/pry_class.rb +61 -103
  120. data/lib/pry/pry_instance.rb +217 -185
  121. data/lib/pry/repl.rb +18 -22
  122. data/lib/pry/repl_file_loader.rb +27 -21
  123. data/lib/pry/ring.rb +11 -6
  124. data/lib/pry/slop.rb +574 -563
  125. data/lib/pry/slop/commands.rb +164 -169
  126. data/lib/pry/slop/option.rb +172 -168
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable.rb +59 -61
  130. data/lib/pry/testable/evalable.rb +21 -12
  131. data/lib/pry/testable/mockable.rb +18 -10
  132. data/lib/pry/testable/pry_tester.rb +71 -56
  133. data/lib/pry/testable/utility.rb +29 -21
  134. data/lib/pry/testable/variables.rb +49 -43
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +27 -0
  137. data/lib/pry/wrapped_module.rb +51 -42
  138. data/lib/pry/wrapped_module/candidate.rb +21 -14
  139. metadata +34 -33
  140. data/lib/pry/commands.rb +0 -6
  141. data/lib/pry/commands/disabled_commands.rb +0 -2
  142. data/lib/pry/commands/gem_cd.rb +0 -26
  143. data/lib/pry/commands/gem_install.rb +0 -32
  144. data/lib/pry/commands/gem_list.rb +0 -33
  145. data/lib/pry/commands/gem_open.rb +0 -29
  146. data/lib/pry/commands/gem_readme.rb +0 -25
  147. data/lib/pry/commands/gem_search.rb +0 -40
  148. data/lib/pry/commands/gem_stats.rb +0 -83
  149. data/lib/pry/commands/gist.rb +0 -102
  150. data/lib/pry/commands/install_command.rb +0 -54
  151. data/lib/pry/config/behavior.rb +0 -255
  152. data/lib/pry/config/convenience.rb +0 -28
  153. data/lib/pry/config/default.rb +0 -159
  154. data/lib/pry/config/memoization.rb +0 -48
  155. data/lib/pry/plugins.rb +0 -122
  156. data/lib/pry/rubygem.rb +0 -84
  157. data/lib/pry/terminal.rb +0 -91
@@ -1,4 +1,8 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
+
3
+ require 'method_source'
4
+ require 'ostruct'
5
+
2
6
  ##
3
7
  # Pry is a powerful alternative to the standard IRB shell for Ruby. It
4
8
  # features syntax highlighting, a flexible plugin architecture, runtime
@@ -23,6 +27,8 @@
23
27
 
24
28
  # rubocop:disable Metrics/ClassLength
25
29
  class Pry
30
+ extend Pry::Forwardable
31
+
26
32
  attr_accessor :binding_stack
27
33
  attr_accessor :custom_completions
28
34
  attr_accessor :eval_string
@@ -33,7 +39,6 @@ class Pry
33
39
  attr_accessor :last_dir
34
40
 
35
41
  attr_reader :last_exception
36
- attr_reader :command_state
37
42
  attr_reader :exit_value
38
43
 
39
44
  # @since v0.12.0
@@ -44,8 +49,13 @@ class Pry
44
49
 
45
50
  attr_reader :config
46
51
 
47
- extend Pry::Config::Convenience
48
- config_shortcut(*Pry::Config.shortcuts)
52
+ def_delegators(
53
+ :@config, :input, :input=, :output, :output=, :commands,
54
+ :commands=, :print, :print=, :exception_handler, :exception_handler=,
55
+ :hooks, :hooks=, :color, :color=, :pager, :pager=, :editor, :editor=,
56
+ :memory_size, :memory_size=, :extra_sticky_locals, :extra_sticky_locals=
57
+ )
58
+
49
59
  EMPTY_COMPLETIONS = [].freeze
50
60
 
51
61
  # Create a new {Pry} instance.
@@ -58,7 +68,7 @@ class Pry
58
68
  # The object to use for commands.
59
69
  # @option options [Hash] :hooks
60
70
  # The defined hook Procs.
61
- # @option options [Array<Proc>] :prompt
71
+ # @option options [Pry::Prompt] :prompt
62
72
  # The array of Procs to use for prompts.
63
73
  # @option options [Proc] :print
64
74
  # The Proc to use for printing return values.
@@ -70,13 +80,11 @@ class Pry
70
80
  # The initial context for this session.
71
81
  def initialize(options = {})
72
82
  @binding_stack = []
73
- @indent = Pry::Indent.new
74
- @command_state = {}
75
- @eval_string = ""
83
+ @indent = Pry::Indent.new(self)
84
+ @eval_string = ''.dup
76
85
  @backtrace = options.delete(:backtrace) || caller
77
86
  target = options.delete(:target)
78
- @config = Pry::Config.new
79
- config.merge!(options)
87
+ @config = self.class.config.merge(options)
80
88
  push_prompt(config.prompt)
81
89
  @input_ring = Pry::Ring.new(config.memory_size)
82
90
  @output_ring = Pry::Ring.new(config.memory_size)
@@ -85,14 +93,18 @@ class Pry
85
93
  @input_ring << nil
86
94
  push_initial_binding(target)
87
95
  exec_hook(:when_started, target, options, self)
96
+ @prompt_warn = false
88
97
  end
89
98
 
90
99
  # This is the prompt at the top of the prompt stack.
91
- # @return [Array<Proc>] the current prompt
100
+ # @return [Pry::Prompt] the current prompt
92
101
  def prompt
93
102
  prompt_stack.last
94
103
  end
95
104
 
105
+ # Sets the Pry prompt.
106
+ # @param [Pry::Prompt] new_prompt
107
+ # @return [void]
96
108
  def prompt=(new_prompt)
97
109
  if prompt_stack.empty?
98
110
  push_prompt new_prompt
@@ -135,7 +147,11 @@ class Pry
135
147
 
136
148
  Pry.critical_section do
137
149
  completer = config.completer.new(config.input, self)
138
- completer.call str, target: current_binding, custom_completions: custom_completions.call.push(*sticky_locals.keys)
150
+ completer.call(
151
+ str,
152
+ target: current_binding,
153
+ custom_completions: custom_completions.call.push(*sticky_locals.keys)
154
+ )
139
155
  end
140
156
  end
141
157
 
@@ -148,20 +164,20 @@ class Pry
148
164
  # @param [Object] value
149
165
  # The value to set the local to.
150
166
  #
151
- # @param [Binding] b
167
+ # @param [Binding] binding
152
168
  # The binding to set the local on.
153
169
  #
154
170
  # @return [Object]
155
171
  # The value the local was set to.
156
172
  #
157
- def inject_local(name, value, b)
158
- value = Proc === value ? value.call : value
159
- if b.respond_to?(:local_variable_set)
160
- b.local_variable_set name, value
173
+ def inject_local(name, value, binding)
174
+ value = value.is_a?(Proc) ? value.call : value
175
+ if binding.respond_to?(:local_variable_set)
176
+ binding.local_variable_set name, value
161
177
  else # < 2.1
162
178
  begin
163
179
  Pry.current[:pry_local] = value
164
- b.eval "#{name} = ::Pry.current[:pry_local]"
180
+ binding.eval "#{name} = ::Pry.current[:pry_local]"
165
181
  ensure
166
182
  Pry.current[:pry_local] = nil
167
183
  end
@@ -198,9 +214,10 @@ class Pry
198
214
  end
199
215
 
200
216
  def sticky_locals
201
- { _in_: input_ring,
217
+ {
218
+ _in_: input_ring,
202
219
  _out_: output_ring,
203
- _pry_: self,
220
+ pry_instance: self,
204
221
  _ex_: last_exception && last_exception.wrapped_exception,
205
222
  _file_: last_file,
206
223
  _dir_: last_dir,
@@ -212,7 +229,7 @@ class Pry
212
229
  # Reset the current eval string. If the user has entered part of a multiline
213
230
  # expression, this discards that input.
214
231
  def reset_eval_string
215
- @eval_string = ""
232
+ @eval_string = ''.dup
216
233
  end
217
234
 
218
235
  # Pass a line of input to Pry.
@@ -243,7 +260,7 @@ class Pry
243
260
  exit_value = catch(:breakout) do
244
261
  handle_line(line, options)
245
262
  # We use 'return !@stopped' here instead of 'return true' so that if
246
- # handle_line has stopped this pry instance (e.g. by opening _pry_.repl and
263
+ # handle_line has stopped this pry instance (e.g. by opening pry_instance.repl and
247
264
  # then popping all the bindings) we still exit immediately.
248
265
  return !@stopped
249
266
  end
@@ -256,94 +273,10 @@ class Pry
256
273
  # TODO: make this configurable?
257
274
  raise exception if exception
258
275
 
259
- return false
276
+ false
260
277
  end
261
278
 
262
- def handle_line(line, options)
263
- if line.nil?
264
- config.control_d_handler.call(@eval_string, self)
265
- return
266
- end
267
-
268
- ensure_correct_encoding!(line)
269
- Pry.history << line unless options[:generated]
270
-
271
- @suppress_output = false
272
- inject_sticky_locals!
273
- begin
274
- if !process_command_safely(line)
275
- @eval_string << "#{line.chomp}\n" if !line.empty? || !@eval_string.empty?
276
- end
277
- rescue RescuableException => e
278
- self.last_exception = e
279
- result = e
280
-
281
- Pry.critical_section do
282
- show_result(result)
283
- end
284
- return
285
- end
286
-
287
- # This hook is supposed to be executed after each line of ruby code
288
- # has been read (regardless of whether eval_string is yet a complete expression)
289
- exec_hook :after_read, eval_string, self
290
-
291
- begin
292
- complete_expr = Pry::Code.complete_expression?(@eval_string)
293
- rescue SyntaxError => e
294
- output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
295
- reset_eval_string
296
- end
297
-
298
- if complete_expr
299
- if @eval_string =~ /;\Z/ || @eval_string.empty? || @eval_string =~ /\A *#.*\n\z/
300
- @suppress_output = true
301
- end
302
-
303
- # A bug in jruby makes java.lang.Exception not rescued by
304
- # `rescue Pry::RescuableException` clause.
305
- #
306
- # * https://github.com/pry/pry/issues/854
307
- # * https://jira.codehaus.org/browse/JRUBY-7100
308
- #
309
- # Until that gets fixed upstream, treat java.lang.Exception
310
- # as an additional exception to be rescued explicitly.
311
- #
312
- # This workaround has a side effect: java exceptions specified
313
- # in `Pry.config.exception_whitelist` are ignored.
314
- jruby_exceptions = []
315
- if Helpers::Platform.jruby?
316
- jruby_exceptions << Java::JavaLang::Exception
317
- end
318
-
319
- begin
320
- # Reset eval string, in case we're evaluating Ruby that does something
321
- # like open a nested REPL on this instance.
322
- eval_string = @eval_string
323
- reset_eval_string
324
-
325
- result = evaluate_ruby(eval_string)
326
- rescue RescuableException, *jruby_exceptions => e
327
- # Eliminate following warning:
328
- # warning: singleton on non-persistent Java type X
329
- # (http://wiki.jruby.org/Persistence)
330
- if Helpers::Platform.jruby? && e.class.respond_to?('__persistent__')
331
- e.class.__persistent__ = true
332
- end
333
- self.last_exception = e
334
- result = e
335
- end
336
-
337
- Pry.critical_section do
338
- show_result(result)
339
- end
340
- end
341
-
342
- throw(:breakout) if current_binding.nil?
343
- end
344
- private :handle_line
345
-
346
- # Potentially deprecated — Use `Pry::REPL.new(pry, :target => target).start`
279
+ # Potentially deprecated. Use `Pry::REPL.new(pry, :target => target).start`
347
280
  # (If nested sessions are going to exist, this method is fine, but a goal is
348
281
  # to come up with an alternative to nested sessions altogether.)
349
282
  def repl(target = nil)
@@ -367,8 +300,6 @@ class Pry
367
300
  exception_handler.call(output, result, self)
368
301
  elsif should_print?
369
302
  print.call(output, result, self)
370
- else
371
- # nothin'
372
303
  end
373
304
  rescue RescuableException => e
374
305
  # Being uber-paranoid here, given that this exception arose because we couldn't
@@ -376,7 +307,7 @@ class Pry
376
307
  # the exception either.
377
308
  begin
378
309
  output.puts "(pry) output error: #{e.inspect}\n#{e.backtrace.join("\n")}"
379
- rescue RescuableException => e
310
+ rescue RescuableException
380
311
  if last_result_is_exception?
381
312
  output.puts "(pry) output error: failed to show exception"
382
313
  else
@@ -387,16 +318,6 @@ class Pry
387
318
  output.flush if output.respond_to?(:flush)
388
319
  end
389
320
 
390
- # Force `eval_string` into the encoding of `val`. [Issue #284]
391
- def ensure_correct_encoding!(val)
392
- if @eval_string.empty? &&
393
- val.respond_to?(:encoding) &&
394
- val.encoding != @eval_string.encoding
395
- @eval_string.force_encoding(val.encoding)
396
- end
397
- end
398
- private :ensure_correct_encoding!
399
-
400
321
  # If the given line is a valid command, process it in the context of the
401
322
  # current `eval_string` and binding.
402
323
  # @param [String] val The line to process.
@@ -404,7 +325,8 @@ class Pry
404
325
  def process_command(val)
405
326
  val = val.lstrip if /^\s\S/ !~ val
406
327
  val = val.chomp
407
- result = commands.process_line(val,
328
+ result = commands.process_line(
329
+ val,
408
330
  target: current_binding,
409
331
  output: output,
410
332
  eval_string: @eval_string,
@@ -419,11 +341,11 @@ class Pry
419
341
  # command was matched and invoked then `result.command?` returns true,
420
342
  # otherwise it returns false.
421
343
  if result.command?
422
- if !result.void_command?
344
+ unless result.void_command?
423
345
  # the command that was invoked was non-void (had a return value) and so we make
424
346
  # the value of the current expression equal to the return value
425
347
  # of the command.
426
- @eval_string.replace "::Pry.current[:pry_cmd_result].retval\n"
348
+ @eval_string = "::Pry.current[:pry_cmd_result].retval\n"
427
349
  end
428
350
  true
429
351
  else
@@ -437,7 +359,9 @@ class Pry
437
359
  # @return [Boolean] `true` if `val` is a command, `false` otherwise
438
360
  def process_command_safely(val)
439
361
  process_command(val)
440
- rescue CommandError, Pry::Slop::InvalidOptionError, MethodSource::SourceNotFoundError => e
362
+ rescue CommandError,
363
+ Pry::Slop::InvalidOptionError,
364
+ MethodSource::SourceNotFoundError => e
441
365
  Pry.last_internal_error = e
442
366
  output.puts "Error: #{e.message}"
443
367
  true
@@ -449,7 +373,8 @@ class Pry
449
373
  # @example
450
374
  # pry_instance.run_command("ls -m")
451
375
  def run_command(val)
452
- commands.process_line(val,
376
+ commands.process_line(
377
+ val,
453
378
  eval_string: @eval_string,
454
379
  target: current_binding,
455
380
  pry_instance: self,
@@ -471,8 +396,8 @@ class Pry
471
396
  hooks.exec_hook(name, *args, &block).tap do
472
397
  hooks.errors[e_before..-1].each do |e|
473
398
  output.puts "#{name} hook failed: #{e.class}: #{e.message}"
474
- output.puts "#{e.backtrace.first}"
475
- output.puts "(see _pry_.hooks.errors to debug)"
399
+ output.puts e.backtrace.first.to_s
400
+ output.puts "(see pry_instance.hooks.errors to debug)"
476
401
  end
477
402
  end
478
403
  end
@@ -488,15 +413,11 @@ class Pry
488
413
  self.last_result = result unless code =~ /\A\s*\z/
489
414
  end
490
415
 
491
- #
492
416
  # Set the last exception for a session.
493
- #
494
- # @param [Exception] e
495
- # the last exception.
496
- #
497
- def last_exception=(e)
498
- last_exception = Pry::LastException.new(e)
417
+ # @param [Exception] exception The last exception.
418
+ def last_exception=(exception)
499
419
  @last_result_is_exception = true
420
+ last_exception = Pry::LastException.new(exception)
500
421
  @output_ring << last_exception
501
422
  @last_exception = last_exception
502
423
  end
@@ -507,10 +428,10 @@ class Pry
507
428
  def update_input_history(code)
508
429
  # Always push to the @input_ring as the @output_ring is always pushed to.
509
430
  @input_ring << code
510
- if code
511
- Pry.line_buffer.push(*code.each_line)
512
- Pry.current_line += code.lines.count
513
- end
431
+ return unless code
432
+
433
+ Pry.line_buffer.push(*code.each_line)
434
+ Pry.current_line += code.lines.count
514
435
  end
515
436
 
516
437
  # @return [Boolean] True if the last result is an exception that was raised,
@@ -533,21 +454,37 @@ class Pry
533
454
  object = current_binding.eval('self')
534
455
  open_token = @indent.open_delimiters.last || @indent.stack.last
535
456
 
536
- c = Pry::Config.assign({
537
- object: object,
538
- nesting_level: binding_stack.size - 1,
539
- open_token: open_token,
540
- session_line: Pry.history.session_line_count + 1,
541
- history_line: Pry.history.history_line_count + 1,
542
- expr_number: input_ring.count,
543
- _pry_: self,
544
- binding_stack: binding_stack,
545
- input_ring: input_ring,
546
- eval_string: @eval_string,
547
- cont: !@eval_string.empty?
548
- })
457
+ c = OpenStruct.new(
458
+ object: object,
459
+ nesting_level: binding_stack.size - 1,
460
+ open_token: open_token,
461
+ session_line: Pry.history.session_line_count + 1,
462
+ history_line: Pry.history.history_line_count + 1,
463
+ expr_number: input_ring.count,
464
+ pry_instance: self,
465
+ binding_stack: binding_stack,
466
+ input_ring: input_ring,
467
+ eval_string: @eval_string,
468
+ cont: !@eval_string.empty?
469
+ )
549
470
 
550
471
  Pry.critical_section do
472
+ # If input buffer is empty, then use normal prompt. Otherwise use the wait
473
+ # prompt (indicating multi-line expression).
474
+ if prompt.is_a?(Pry::Prompt)
475
+ prompt_proc = eval_string.empty? ? prompt.wait_proc : prompt.incomplete_proc
476
+ return prompt_proc.call(c.object, c.nesting_level, c.pry_instance)
477
+ end
478
+
479
+ unless @prompt_warn
480
+ @prompt_warn = true
481
+ Kernel.warn(
482
+ "warning: setting prompt with help of " \
483
+ "`Pry.config.prompt = [proc {}, proc {}]` is deprecated. " \
484
+ "Use Pry::Prompt API instead"
485
+ )
486
+ end
487
+
551
488
  # If input buffer is empty then use normal prompt
552
489
  if eval_string.empty?
553
490
  generate_prompt(Array(prompt).first, c)
@@ -558,44 +495,30 @@ class Pry
558
495
  end
559
496
  end
560
497
 
561
- def generate_prompt(prompt_proc, conf)
562
- if prompt_proc.arity == 1
563
- prompt_proc.call(conf)
564
- else
565
- prompt_proc.call(conf.object, conf.nesting_level, conf._pry_)
566
- end
567
- end
568
- private :generate_prompt
569
-
570
- # the array that the prompt stack is stored in
571
- def prompt_stack
572
- @prompt_stack ||= Array.new
573
- end
574
- private :prompt_stack
575
-
576
498
  # Pushes the current prompt onto a stack that it can be restored from later.
577
499
  # Use this if you wish to temporarily change the prompt.
578
- # @param [Array<Proc>] new_prompt
579
- # @return [Array<Proc>] new_prompt
500
+ #
580
501
  # @example
581
- # new_prompt = [ proc { '>' }, proc { '>>' } ]
582
- # push_prompt(new_prompt) # => new_prompt
502
+ # push_prompt(Pry::Prompt[:my_prompt])
503
+ #
504
+ # @param [Pry::Prompt] new_prompt
505
+ # @return [Pry::Prompt] new_prompt
583
506
  def push_prompt(new_prompt)
584
507
  prompt_stack.push new_prompt
585
508
  end
586
509
 
587
- # Pops the current prompt off of the prompt stack.
588
- # If the prompt you are popping is the last prompt, it will not be popped.
589
- # Use this to restore the previous prompt.
590
- # @return [Array<Proc>] Prompt being popped.
510
+ # Pops the current prompt off of the prompt stack. If the prompt you are
511
+ # popping is the last prompt, it will not be popped. Use this to restore the
512
+ # previous prompt.
513
+ #
591
514
  # @example
592
- # prompt1 = [ proc { '>' }, proc { '>>' } ]
593
- # prompt2 = [ proc { '$' }, proc { '>' } ]
594
- # pry = Pry.new :prompt => prompt1
595
- # pry.push_prompt(prompt2)
515
+ # pry = Pry.new(prompt: Pry::Prompt[:my_prompt1])
516
+ # pry.push_prompt(Pry::Prompt[:my_prompt2])
596
517
  # pry.pop_prompt # => prompt2
597
518
  # pry.pop_prompt # => prompt1
598
519
  # pry.pop_prompt # => prompt1
520
+ #
521
+ # @return [Pry::Prompt] the prompt being popped
599
522
  def pop_prompt
600
523
  prompt_stack.size > 1 ? prompt_stack.pop : prompt
601
524
  end
@@ -603,7 +526,7 @@ class Pry
603
526
  undef :pager if method_defined? :pager
604
527
  # Returns the currently configured pager
605
528
  # @example
606
- # _pry_.pager.page text
529
+ # pry_instance.pager.page text
607
530
  def pager
608
531
  Pry::Pager.new(self)
609
532
  end
@@ -611,7 +534,7 @@ class Pry
611
534
  undef :output if method_defined? :output
612
535
  # Returns an output device
613
536
  # @example
614
- # _pry_.output.puts "ohai!"
537
+ # pry_instance.output.puts "ohai!"
615
538
  def output
616
539
  Pry::Output.new(self)
617
540
  end
@@ -637,7 +560,7 @@ class Pry
637
560
  raise ArgumentError, "wrong number of arguments"
638
561
  elsif !args.first.respond_to?(:exception)
639
562
  raise TypeError, "exception class/object expected"
640
- elsif args.length === 1
563
+ elsif args.size == 1
641
564
  args.first.exception
642
565
  else
643
566
  args.first.exception(args[1])
@@ -645,7 +568,7 @@ class Pry
645
568
 
646
569
  raise TypeError, "exception object expected" unless exception.is_a? Exception
647
570
 
648
- exception.set_backtrace(args.length === 3 ? args[2] : caller(1))
571
+ exception.set_backtrace(args.size == 3 ? args[2] : caller(1))
649
572
 
650
573
  if force || binding_stack.one?
651
574
  binding_stack.clear
@@ -656,14 +579,123 @@ class Pry
656
579
  end
657
580
  end
658
581
 
659
- def raise_up(*args); raise_up_common(false, *args); end
582
+ def raise_up(*args)
583
+ raise_up_common(false, *args)
584
+ end
660
585
 
661
- def raise_up!(*args); raise_up_common(true, *args); end
586
+ def raise_up!(*args)
587
+ raise_up_common(true, *args)
588
+ end
662
589
 
663
590
  # Convenience accessor for the `quiet` config key.
664
591
  # @return [Boolean]
665
592
  def quiet?
666
593
  config.quiet
667
594
  end
595
+
596
+ private
597
+
598
+ def handle_line(line, options)
599
+ if line.nil?
600
+ config.control_d_handler.call(self)
601
+ return
602
+ end
603
+
604
+ ensure_correct_encoding!(line)
605
+ Pry.history << line unless options[:generated]
606
+
607
+ @suppress_output = false
608
+ inject_sticky_locals!
609
+ begin
610
+ unless process_command_safely(line)
611
+ @eval_string += "#{line.chomp}\n" if !line.empty? || !@eval_string.empty?
612
+ end
613
+ rescue RescuableException => e
614
+ self.last_exception = e
615
+ result = e
616
+
617
+ Pry.critical_section do
618
+ show_result(result)
619
+ end
620
+ return
621
+ end
622
+
623
+ # This hook is supposed to be executed after each line of ruby code
624
+ # has been read (regardless of whether eval_string is yet a complete expression)
625
+ exec_hook :after_read, eval_string, self
626
+
627
+ begin
628
+ complete_expr = Pry::Code.complete_expression?(@eval_string)
629
+ rescue SyntaxError => e
630
+ output.puts e.message.gsub(/^.*syntax error, */, "SyntaxError: ")
631
+ reset_eval_string
632
+ end
633
+
634
+ if complete_expr
635
+ if @eval_string =~ /;\Z/ || @eval_string.empty? || @eval_string =~ /\A *#.*\n\z/
636
+ @suppress_output = true
637
+ end
638
+
639
+ # A bug in jruby makes java.lang.Exception not rescued by
640
+ # `rescue Pry::RescuableException` clause.
641
+ #
642
+ # * https://github.com/pry/pry/issues/854
643
+ # * https://jira.codehaus.org/browse/JRUBY-7100
644
+ #
645
+ # Until that gets fixed upstream, treat java.lang.Exception
646
+ # as an additional exception to be rescued explicitly.
647
+ #
648
+ # This workaround has a side effect: java exceptions specified
649
+ # in `Pry.config.unrescued_exceptions` are ignored.
650
+ jruby_exceptions = []
651
+ jruby_exceptions << Java::JavaLang::Exception if Helpers::Platform.jruby?
652
+
653
+ begin
654
+ # Reset eval string, in case we're evaluating Ruby that does something
655
+ # like open a nested REPL on this instance.
656
+ eval_string = @eval_string
657
+ reset_eval_string
658
+
659
+ result = evaluate_ruby(eval_string)
660
+ rescue RescuableException, *jruby_exceptions => e
661
+ # Eliminate following warning:
662
+ # warning: singleton on non-persistent Java type X
663
+ # (http://wiki.jruby.org/Persistence)
664
+ if Helpers::Platform.jruby? && e.class.respond_to?('__persistent__')
665
+ e.class.__persistent__ = true
666
+ end
667
+ self.last_exception = e
668
+ result = e
669
+ end
670
+
671
+ Pry.critical_section do
672
+ show_result(result)
673
+ end
674
+ end
675
+
676
+ throw(:breakout) if current_binding.nil?
677
+ end
678
+
679
+ # Force `eval_string` into the encoding of `val`. [Issue #284]
680
+ def ensure_correct_encoding!(val)
681
+ if @eval_string.empty? &&
682
+ val.respond_to?(:encoding) &&
683
+ val.encoding != @eval_string.encoding
684
+ @eval_string.force_encoding(val.encoding)
685
+ end
686
+ end
687
+
688
+ def generate_prompt(prompt_proc, conf)
689
+ if prompt_proc.arity == 1
690
+ prompt_proc.call(conf)
691
+ else
692
+ prompt_proc.call(conf.object, conf.nesting_level, conf.pry_instance)
693
+ end
694
+ end
695
+
696
+ # the array that the prompt stack is stored in
697
+ def prompt_stack
698
+ @prompt_stack ||= []
699
+ end
668
700
  end
669
701
  # rubocop:enable Metrics/ClassLength