super-smart-mod 0.0.1

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 (143) hide show
  1. checksums.yaml +7 -0
  2. data/pry-0.16.0/CHANGELOG.md +1211 -0
  3. data/pry-0.16.0/LICENSE +25 -0
  4. data/pry-0.16.0/README.md +469 -0
  5. data/pry-0.16.0/bin/pry +13 -0
  6. data/pry-0.16.0/lib/pry/basic_object.rb +10 -0
  7. data/pry-0.16.0/lib/pry/block_command.rb +22 -0
  8. data/pry-0.16.0/lib/pry/class_command.rb +194 -0
  9. data/pry-0.16.0/lib/pry/cli.rb +211 -0
  10. data/pry-0.16.0/lib/pry/code/code_file.rb +114 -0
  11. data/pry-0.16.0/lib/pry/code/code_range.rb +73 -0
  12. data/pry-0.16.0/lib/pry/code/loc.rb +105 -0
  13. data/pry-0.16.0/lib/pry/code.rb +357 -0
  14. data/pry-0.16.0/lib/pry/code_object.rb +197 -0
  15. data/pry-0.16.0/lib/pry/color_printer.rb +66 -0
  16. data/pry-0.16.0/lib/pry/command.rb +520 -0
  17. data/pry-0.16.0/lib/pry/command_set.rb +418 -0
  18. data/pry-0.16.0/lib/pry/command_state.rb +36 -0
  19. data/pry-0.16.0/lib/pry/commands/amend_line.rb +103 -0
  20. data/pry-0.16.0/lib/pry/commands/bang.rb +24 -0
  21. data/pry-0.16.0/lib/pry/commands/bang_pry.rb +21 -0
  22. data/pry-0.16.0/lib/pry/commands/cat/abstract_formatter.rb +32 -0
  23. data/pry-0.16.0/lib/pry/commands/cat/exception_formatter.rb +90 -0
  24. data/pry-0.16.0/lib/pry/commands/cat/file_formatter.rb +77 -0
  25. data/pry-0.16.0/lib/pry/commands/cat/input_expression_formatter.rb +48 -0
  26. data/pry-0.16.0/lib/pry/commands/cat.rb +70 -0
  27. data/pry-0.16.0/lib/pry/commands/cd.rb +48 -0
  28. data/pry-0.16.0/lib/pry/commands/change_inspector.rb +34 -0
  29. data/pry-0.16.0/lib/pry/commands/change_prompt.rb +51 -0
  30. data/pry-0.16.0/lib/pry/commands/clear_screen.rb +20 -0
  31. data/pry-0.16.0/lib/pry/commands/code_collector.rb +182 -0
  32. data/pry-0.16.0/lib/pry/commands/disable_pry.rb +31 -0
  33. data/pry-0.16.0/lib/pry/commands/easter_eggs.rb +101 -0
  34. data/pry-0.16.0/lib/pry/commands/edit/exception_patcher.rb +29 -0
  35. data/pry-0.16.0/lib/pry/commands/edit/file_and_line_locator.rb +47 -0
  36. data/pry-0.16.0/lib/pry/commands/edit.rb +225 -0
  37. data/pry-0.16.0/lib/pry/commands/exit.rb +47 -0
  38. data/pry-0.16.0/lib/pry/commands/exit_all.rb +33 -0
  39. data/pry-0.16.0/lib/pry/commands/exit_program.rb +27 -0
  40. data/pry-0.16.0/lib/pry/commands/find_method.rb +199 -0
  41. data/pry-0.16.0/lib/pry/commands/fix_indent.rb +23 -0
  42. data/pry-0.16.0/lib/pry/commands/help.rb +171 -0
  43. data/pry-0.16.0/lib/pry/commands/hist.rb +182 -0
  44. data/pry-0.16.0/lib/pry/commands/import_set.rb +27 -0
  45. data/pry-0.16.0/lib/pry/commands/jump_to.rb +33 -0
  46. data/pry-0.16.0/lib/pry/commands/list_inspectors.rb +42 -0
  47. data/pry-0.16.0/lib/pry/commands/ls/config.rb +54 -0
  48. data/pry-0.16.0/lib/pry/commands/ls/constants.rb +75 -0
  49. data/pry-0.16.0/lib/pry/commands/ls/formatter.rb +55 -0
  50. data/pry-0.16.0/lib/pry/commands/ls/globals.rb +50 -0
  51. data/pry-0.16.0/lib/pry/commands/ls/grep.rb +23 -0
  52. data/pry-0.16.0/lib/pry/commands/ls/instance_vars.rb +40 -0
  53. data/pry-0.16.0/lib/pry/commands/ls/interrogatable.rb +24 -0
  54. data/pry-0.16.0/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  55. data/pry-0.16.0/lib/pry/commands/ls/local_names.rb +37 -0
  56. data/pry-0.16.0/lib/pry/commands/ls/local_vars.rb +47 -0
  57. data/pry-0.16.0/lib/pry/commands/ls/ls_entity.rb +65 -0
  58. data/pry-0.16.0/lib/pry/commands/ls/methods.rb +55 -0
  59. data/pry-0.16.0/lib/pry/commands/ls/methods_helper.rb +50 -0
  60. data/pry-0.16.0/lib/pry/commands/ls/self_methods.rb +34 -0
  61. data/pry-0.16.0/lib/pry/commands/ls.rb +114 -0
  62. data/pry-0.16.0/lib/pry/commands/nesting.rb +29 -0
  63. data/pry-0.16.0/lib/pry/commands/play.rb +113 -0
  64. data/pry-0.16.0/lib/pry/commands/pry_backtrace.rb +30 -0
  65. data/pry-0.16.0/lib/pry/commands/pry_version.rb +21 -0
  66. data/pry-0.16.0/lib/pry/commands/raise_up.rb +38 -0
  67. data/pry-0.16.0/lib/pry/commands/reload_code.rb +74 -0
  68. data/pry-0.16.0/lib/pry/commands/reset.rb +22 -0
  69. data/pry-0.16.0/lib/pry/commands/ri.rb +75 -0
  70. data/pry-0.16.0/lib/pry/commands/save_file.rb +63 -0
  71. data/pry-0.16.0/lib/pry/commands/shell_command.rb +75 -0
  72. data/pry-0.16.0/lib/pry/commands/shell_mode.rb +30 -0
  73. data/pry-0.16.0/lib/pry/commands/show_doc.rb +84 -0
  74. data/pry-0.16.0/lib/pry/commands/show_info.rb +234 -0
  75. data/pry-0.16.0/lib/pry/commands/show_input.rb +22 -0
  76. data/pry-0.16.0/lib/pry/commands/show_source.rb +57 -0
  77. data/pry-0.16.0/lib/pry/commands/stat.rb +44 -0
  78. data/pry-0.16.0/lib/pry/commands/switch_to.rb +29 -0
  79. data/pry-0.16.0/lib/pry/commands/toggle_color.rb +28 -0
  80. data/pry-0.16.0/lib/pry/commands/watch_expression/expression.rb +43 -0
  81. data/pry-0.16.0/lib/pry/commands/watch_expression.rb +108 -0
  82. data/pry-0.16.0/lib/pry/commands/whereami.rb +205 -0
  83. data/pry-0.16.0/lib/pry/commands/wtf.rb +95 -0
  84. data/pry-0.16.0/lib/pry/config/attributable.rb +22 -0
  85. data/pry-0.16.0/lib/pry/config/lazy_value.rb +29 -0
  86. data/pry-0.16.0/lib/pry/config/memoized_value.rb +34 -0
  87. data/pry-0.16.0/lib/pry/config/value.rb +24 -0
  88. data/pry-0.16.0/lib/pry/config.rb +321 -0
  89. data/pry-0.16.0/lib/pry/control_d_handler.rb +28 -0
  90. data/pry-0.16.0/lib/pry/core_extensions.rb +144 -0
  91. data/pry-0.16.0/lib/pry/editor.rb +157 -0
  92. data/pry-0.16.0/lib/pry/env.rb +18 -0
  93. data/pry-0.16.0/lib/pry/exception_handler.rb +48 -0
  94. data/pry-0.16.0/lib/pry/exceptions.rb +73 -0
  95. data/pry-0.16.0/lib/pry/forwardable.rb +27 -0
  96. data/pry-0.16.0/lib/pry/helpers/base_helpers.rb +71 -0
  97. data/pry-0.16.0/lib/pry/helpers/command_helpers.rb +146 -0
  98. data/pry-0.16.0/lib/pry/helpers/documentation_helpers.rb +84 -0
  99. data/pry-0.16.0/lib/pry/helpers/options_helpers.rb +34 -0
  100. data/pry-0.16.0/lib/pry/helpers/platform.rb +55 -0
  101. data/pry-0.16.0/lib/pry/helpers/table.rb +121 -0
  102. data/pry-0.16.0/lib/pry/helpers/text.rb +118 -0
  103. data/pry-0.16.0/lib/pry/helpers.rb +8 -0
  104. data/pry-0.16.0/lib/pry/history.rb +153 -0
  105. data/pry-0.16.0/lib/pry/hooks.rb +180 -0
  106. data/pry-0.16.0/lib/pry/indent.rb +414 -0
  107. data/pry-0.16.0/lib/pry/input/simple_stdio.rb +13 -0
  108. data/pry-0.16.0/lib/pry/input_completer.rb +283 -0
  109. data/pry-0.16.0/lib/pry/input_lock.rb +129 -0
  110. data/pry-0.16.0/lib/pry/inspector.rb +39 -0
  111. data/pry-0.16.0/lib/pry/last_exception.rb +61 -0
  112. data/pry-0.16.0/lib/pry/method/disowned.rb +67 -0
  113. data/pry-0.16.0/lib/pry/method/patcher.rb +131 -0
  114. data/pry-0.16.0/lib/pry/method/weird_method_locator.rb +222 -0
  115. data/pry-0.16.0/lib/pry/method.rb +599 -0
  116. data/pry-0.16.0/lib/pry/object_path.rb +91 -0
  117. data/pry-0.16.0/lib/pry/output.rb +136 -0
  118. data/pry-0.16.0/lib/pry/pager.rb +249 -0
  119. data/pry-0.16.0/lib/pry/prompt.rb +214 -0
  120. data/pry-0.16.0/lib/pry/pry_class.rb +371 -0
  121. data/pry-0.16.0/lib/pry/pry_instance.rb +663 -0
  122. data/pry-0.16.0/lib/pry/repl.rb +326 -0
  123. data/pry-0.16.0/lib/pry/repl_file_loader.rb +79 -0
  124. data/pry-0.16.0/lib/pry/ring.rb +89 -0
  125. data/pry-0.16.0/lib/pry/slop/LICENSE +20 -0
  126. data/pry-0.16.0/lib/pry/slop/commands.rb +190 -0
  127. data/pry-0.16.0/lib/pry/slop/option.rb +210 -0
  128. data/pry-0.16.0/lib/pry/slop.rb +672 -0
  129. data/pry-0.16.0/lib/pry/syntax_highlighter.rb +26 -0
  130. data/pry-0.16.0/lib/pry/system_command_handler.rb +17 -0
  131. data/pry-0.16.0/lib/pry/testable/evalable.rb +24 -0
  132. data/pry-0.16.0/lib/pry/testable/mockable.rb +22 -0
  133. data/pry-0.16.0/lib/pry/testable/pry_tester.rb +88 -0
  134. data/pry-0.16.0/lib/pry/testable/utility.rb +34 -0
  135. data/pry-0.16.0/lib/pry/testable/variables.rb +52 -0
  136. data/pry-0.16.0/lib/pry/testable.rb +68 -0
  137. data/pry-0.16.0/lib/pry/version.rb +5 -0
  138. data/pry-0.16.0/lib/pry/warning.rb +20 -0
  139. data/pry-0.16.0/lib/pry/wrapped_module/candidate.rb +145 -0
  140. data/pry-0.16.0/lib/pry/wrapped_module.rb +382 -0
  141. data/pry-0.16.0/lib/pry.rb +148 -0
  142. data/super-smart-mod.gemspec +12 -0
  143. metadata +182 -0
@@ -0,0 +1,663 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'method_source'
4
+
5
+ ##
6
+ # Pry is a powerful alternative to the standard IRB shell for Ruby. It
7
+ # features syntax highlighting, a flexible plugin architecture, runtime
8
+ # invocation and source and documentation browsing.
9
+ #
10
+ # Pry can be started similar to other command line utilities by simply running
11
+ # the following command:
12
+ #
13
+ # pry
14
+ #
15
+ # Once inside Pry you can invoke the help message:
16
+ #
17
+ # help
18
+ #
19
+ # This will show a list of available commands and their usage. For more
20
+ # information about Pry you can refer to the following resources:
21
+ #
22
+ # * https://pry.github.io
23
+ # * https://github.com/pry/pry
24
+ # * the IRC channel, which is #pry on the Freenode network
25
+ #
26
+
27
+ # rubocop:disable Metrics/ClassLength
28
+ class Pry
29
+ extend Pry::Forwardable
30
+
31
+ attr_accessor :binding_stack
32
+ attr_accessor :custom_completions
33
+ attr_accessor :eval_string
34
+ attr_accessor :backtrace
35
+ attr_accessor :suppress_output
36
+ attr_accessor :last_result
37
+ attr_accessor :last_file
38
+ attr_accessor :last_dir
39
+
40
+ attr_reader :last_exception
41
+ attr_reader :exit_value
42
+
43
+ # @since v0.12.0
44
+ attr_reader :input_ring
45
+
46
+ # @since v0.12.0
47
+ attr_reader :output_ring
48
+
49
+ attr_reader :config
50
+
51
+ def_delegators(
52
+ :@config, :input, :input=, :output, :output=, :commands,
53
+ :commands=, :print, :print=, :exception_handler, :exception_handler=,
54
+ :hooks, :hooks=, :color, :color=, :pager, :pager=, :editor, :editor=,
55
+ :memory_size, :memory_size=, :extra_sticky_locals, :extra_sticky_locals=
56
+ )
57
+
58
+ EMPTY_COMPLETIONS = [].freeze
59
+
60
+ # Create a new {Pry} instance.
61
+ # @param [Hash] options
62
+ # @option options [#readline] :input
63
+ # The object to use for input.
64
+ # @option options [#puts] :output
65
+ # The object to use for output.
66
+ # @option options [Pry::CommandBase] :commands
67
+ # The object to use for commands.
68
+ # @option options [Hash] :hooks
69
+ # The defined hook Procs.
70
+ # @option options [Pry::Prompt] :prompt
71
+ # The array of Procs to use for prompts.
72
+ # @option options [Proc] :print
73
+ # The Proc to use for printing return values.
74
+ # @option options [Boolean] :quiet
75
+ # Omit the `whereami` banner when starting.
76
+ # @option options [Array<String>] :backtrace
77
+ # The backtrace of the session's `binding.pry` line, if applicable.
78
+ # @option options [Object] :target
79
+ # The initial context for this session.
80
+ def initialize(options = {})
81
+ @binding_stack = []
82
+ @indent = Pry::Indent.new(self)
83
+ @eval_string = ''.dup
84
+ @backtrace = options.delete(:backtrace) || caller
85
+ target = options.delete(:target)
86
+ @config = self.class.config.merge(options)
87
+ push_prompt(config.prompt)
88
+ @input_ring = Pry::Ring.new(config.memory_size)
89
+ @output_ring = Pry::Ring.new(config.memory_size)
90
+ @custom_completions = config.command_completions
91
+ set_last_result nil
92
+ @input_ring << nil
93
+ push_initial_binding(target)
94
+ exec_hook(:when_started, target, options, self)
95
+ end
96
+
97
+ # This is the prompt at the top of the prompt stack.
98
+ # @return [Pry::Prompt] the current prompt
99
+ def prompt
100
+ prompt_stack.last
101
+ end
102
+
103
+ # Sets the Pry prompt.
104
+ # @param [Pry::Prompt] new_prompt
105
+ # @return [void]
106
+ def prompt=(new_prompt)
107
+ if prompt_stack.empty?
108
+ push_prompt new_prompt
109
+ else
110
+ prompt_stack[-1] = new_prompt
111
+ end
112
+ end
113
+
114
+ # Initialize this instance by pushing its initial context into the binding
115
+ # stack. If no target is given, start at the top level.
116
+ def push_initial_binding(target = nil)
117
+ push_binding(target || Pry.toplevel_binding)
118
+ end
119
+
120
+ # The currently active `Binding`.
121
+ # @return [Binding] The currently active `Binding` for the session.
122
+ def current_binding
123
+ binding_stack.last
124
+ end
125
+ alias current_context current_binding # support previous API
126
+
127
+ # Push a binding for the given object onto the stack. If this instance is
128
+ # currently stopped, mark it as usable again.
129
+ def push_binding(object)
130
+ @stopped = false
131
+ binding_stack << Pry.binding_for(object)
132
+ end
133
+
134
+ #
135
+ # Generate completions.
136
+ #
137
+ # @param [String] str
138
+ # What the user has typed so far
139
+ #
140
+ # @return [Array<String>]
141
+ # Possible completions
142
+ #
143
+ def complete(str)
144
+ return EMPTY_COMPLETIONS unless config.completer
145
+
146
+ Pry.critical_section do
147
+ completer = config.completer.new(config.input, self)
148
+ completer.call(
149
+ str,
150
+ target: current_binding,
151
+ custom_completions: custom_completions.call.push(*sticky_locals.keys)
152
+ )
153
+ end
154
+ end
155
+
156
+ #
157
+ # Injects a local variable into the provided binding.
158
+ #
159
+ # @param [String] name
160
+ # The name of the local to inject.
161
+ #
162
+ # @param [Object] value
163
+ # The value to set the local to.
164
+ #
165
+ # @param [Binding] binding
166
+ # The binding to set the local on.
167
+ #
168
+ # @return [Object]
169
+ # The value the local was set to.
170
+ #
171
+ def inject_local(name, value, binding)
172
+ value = value.is_a?(Proc) ? value.call : value
173
+ if binding.respond_to?(:local_variable_set)
174
+ binding.local_variable_set name, value
175
+ else # < 2.1
176
+ begin
177
+ Pry.current[:pry_local] = value
178
+ binding.eval "#{name} = ::Pry.current[:pry_local]"
179
+ ensure
180
+ Pry.current[:pry_local] = nil
181
+ end
182
+ end
183
+ end
184
+
185
+ undef :memory_size if method_defined? :memory_size
186
+ # @return [Integer] The maximum amount of objects remembered by the inp and
187
+ # out arrays. Defaults to 100.
188
+ def memory_size
189
+ @output_ring.max_size
190
+ end
191
+
192
+ undef :memory_size= if method_defined? :memory_size=
193
+ def memory_size=(size)
194
+ @input_ring = Pry::Ring.new(size)
195
+ @output_ring = Pry::Ring.new(size)
196
+ end
197
+
198
+ # Inject all the sticky locals into the current binding.
199
+ def inject_sticky_locals!
200
+ sticky_locals.each_pair do |name, value|
201
+ inject_local(name, value, current_binding)
202
+ end
203
+ end
204
+
205
+ # Add a sticky local to this Pry instance.
206
+ # A sticky local is a local that persists between all bindings in a session.
207
+ # @param [Symbol] name The name of the sticky local.
208
+ # @yield The block that defines the content of the local. The local
209
+ # will be refreshed at each tick of the repl loop.
210
+ def add_sticky_local(name, &block)
211
+ config.extra_sticky_locals[name] = block
212
+ end
213
+
214
+ def sticky_locals
215
+ {
216
+ _in_: input_ring,
217
+ _out_: output_ring,
218
+ pry_instance: self,
219
+ _ex_: last_exception && last_exception.wrapped_exception,
220
+ _file_: last_file,
221
+ _dir_: last_dir,
222
+ _: proc { last_result },
223
+ __: proc { output_ring[-2] }
224
+ }.merge(config.extra_sticky_locals)
225
+ end
226
+
227
+ # Reset the current eval string. If the user has entered part of a multiline
228
+ # expression, this discards that input.
229
+ def reset_eval_string
230
+ @eval_string = ''.dup
231
+ end
232
+
233
+ # Pass a line of input to Pry.
234
+ #
235
+ # This is the equivalent of `Binding#eval` but with extra Pry!
236
+ #
237
+ # In particular:
238
+ # 1. Pry commands will be executed immediately if the line matches.
239
+ # 2. Partial lines of input will be queued up until a complete expression has
240
+ # been accepted.
241
+ # 3. Output is written to `#output` in pretty colours, not returned.
242
+ #
243
+ # Once this method has raised an exception or returned false, this instance
244
+ # is no longer usable. {#exit_value} will return the session's breakout
245
+ # value if applicable.
246
+ #
247
+ # @param [String?] line The line of input; `nil` if the user types `<Ctrl-D>`
248
+ # @option options [Boolean] :generated Whether this line was generated automatically.
249
+ # Generated lines are not stored in history.
250
+ # @return [Boolean] Is Pry ready to accept more input?
251
+ # @raise [Exception] If the user uses the `raise-up` command, this method
252
+ # will raise that exception.
253
+ def eval(line, options = {})
254
+ return false if @stopped
255
+
256
+ exit_value = nil
257
+ exception = catch(:raise_up) do
258
+ exit_value = catch(:breakout) do
259
+ handle_line(line, options)
260
+ # We use 'return !@stopped' here instead of 'return true' so that if
261
+ # handle_line has stopped this pry instance (e.g. by opening pry_instance.repl and
262
+ # then popping all the bindings) we still exit immediately.
263
+ return !@stopped
264
+ end
265
+ exception = false
266
+ end
267
+
268
+ @stopped = true
269
+ @exit_value = exit_value
270
+
271
+ # TODO: make this configurable?
272
+ raise exception if exception
273
+
274
+ false
275
+ end
276
+
277
+ # Potentially deprecated. Use `Pry::REPL.new(pry, :target => target).start`
278
+ # (If nested sessions are going to exist, this method is fine, but a goal is
279
+ # to come up with an alternative to nested sessions altogether.)
280
+ def repl(target = nil)
281
+ Pry::REPL.new(self, target: target).start
282
+ end
283
+
284
+ def evaluate_ruby(code)
285
+ inject_sticky_locals!
286
+ exec_hook :before_eval, code, self
287
+
288
+ result = current_binding.eval(code, Pry.eval_path, Pry.current_line)
289
+ set_last_result(result, code)
290
+ ensure
291
+ update_input_history(code)
292
+ exec_hook :after_eval, result, self
293
+ end
294
+
295
+ # Output the result or pass to an exception handler (if result is an exception).
296
+ def show_result(result)
297
+ if last_result_is_exception?
298
+ exception_handler.call(output, result, self)
299
+ elsif should_print?
300
+ print.call(output, result, self)
301
+ end
302
+ rescue RescuableException => e
303
+ # Being uber-paranoid here, given that this exception arose because we couldn't
304
+ # serialize something in the user's program, let's not assume we can serialize
305
+ # the exception either.
306
+ begin
307
+ output.puts "(pry) output error: #{e.inspect}\n#{e.backtrace.join("\n")}"
308
+ rescue RescuableException
309
+ if last_result_is_exception?
310
+ output.puts "(pry) output error: failed to show exception"
311
+ else
312
+ output.puts "(pry) output error: failed to show result"
313
+ end
314
+ end
315
+ ensure
316
+ output.flush if output.respond_to?(:flush)
317
+ end
318
+
319
+ # If the given line is a valid command, process it in the context of the
320
+ # current `eval_string` and binding.
321
+ # @param [String] val The line to process.
322
+ # @return [Boolean] `true` if `val` is a command, `false` otherwise
323
+ def process_command(val)
324
+ val = val.lstrip if /^\s\S/ !~ val
325
+ val = val.chomp
326
+ result = commands.process_line(
327
+ val,
328
+ target: current_binding,
329
+ output: output,
330
+ eval_string: @eval_string,
331
+ pry_instance: self,
332
+ hooks: hooks
333
+ )
334
+
335
+ # set a temporary (just so we can inject the value we want into eval_string)
336
+ Pry.current[:pry_cmd_result] = result
337
+
338
+ # note that `result` wraps the result of command processing; if a
339
+ # command was matched and invoked then `result.command?` returns true,
340
+ # otherwise it returns false.
341
+ if result.command?
342
+ unless result.void_command?
343
+ # the command that was invoked was non-void (had a return value) and so we make
344
+ # the value of the current expression equal to the return value
345
+ # of the command.
346
+ @eval_string = "::Pry.current[:pry_cmd_result].retval\n"
347
+ end
348
+ true
349
+ else
350
+ false
351
+ end
352
+ end
353
+
354
+ # Same as process_command, but outputs exceptions to `#output` instead of
355
+ # raising.
356
+ # @param [String] val The line to process.
357
+ # @return [Boolean] `true` if `val` is a command, `false` otherwise
358
+ def process_command_safely(val)
359
+ process_command(val)
360
+ rescue CommandError,
361
+ Pry::Slop::InvalidOptionError,
362
+ MethodSource::SourceNotFoundError => e
363
+ Pry.last_internal_error = e
364
+ output.puts "Error: #{e.message}"
365
+ true
366
+ end
367
+
368
+ # Run the specified command.
369
+ # @param [String] val The command (and its params) to execute.
370
+ # @return [Pry::Command::VOID_VALUE]
371
+ # @example
372
+ # pry_instance.run_command("ls -m")
373
+ def run_command(val)
374
+ commands.process_line(
375
+ val,
376
+ eval_string: @eval_string,
377
+ target: current_binding,
378
+ pry_instance: self,
379
+ output: output
380
+ )
381
+ Pry::Command::VOID_VALUE
382
+ end
383
+
384
+ # Execute the specified hook.
385
+ # @param [Symbol] name The hook name to execute
386
+ # @param [*Object] args The arguments to pass to the hook
387
+ # @return [Object, Exception] The return value of the hook or the exception raised
388
+ #
389
+ # If executing a hook raises an exception, we log that and then continue successfully.
390
+ # To debug such errors, use the global variable $pry_hook_error, which is set as a
391
+ # result.
392
+ def exec_hook(name, *args, &block)
393
+ e_before = hooks.errors.size
394
+ hooks.exec_hook(name, *args, &block).tap do
395
+ hooks.errors[e_before..-1].each do |e|
396
+ output.puts "#{name} hook failed: #{e.class}: #{e.message}"
397
+ output.puts e.backtrace.first.to_s
398
+ output.puts "(see pry_instance.hooks.errors to debug)"
399
+ end
400
+ end
401
+ end
402
+
403
+ # Set the last result of an eval.
404
+ # This method should not need to be invoked directly.
405
+ # @param [Object] result The result.
406
+ # @param [String] code The code that was run.
407
+ def set_last_result(result, code = "")
408
+ @last_result_is_exception = false
409
+ @output_ring << result
410
+
411
+ self.last_result = result unless code =~ /\A\s*\z/
412
+ end
413
+
414
+ # Set the last exception for a session.
415
+ # @param [Exception] exception The last exception.
416
+ def last_exception=(exception)
417
+ @last_result_is_exception = true
418
+ last_exception = Pry::LastException.new(exception)
419
+ @output_ring << last_exception
420
+ @last_exception = last_exception
421
+ end
422
+
423
+ # Update Pry's internal state after evalling code.
424
+ # This method should not need to be invoked directly.
425
+ # @param [String] code The code we just eval'd
426
+ def update_input_history(code)
427
+ # Always push to the @input_ring as the @output_ring is always pushed to.
428
+ @input_ring << code
429
+ return unless code
430
+
431
+ Pry.line_buffer.push(*code.each_line)
432
+ Pry.current_line += code.lines.count
433
+ end
434
+
435
+ # @return [Boolean] True if the last result is an exception that was raised,
436
+ # as opposed to simply an instance of Exception (like the result of
437
+ # Exception.new)
438
+ def last_result_is_exception?
439
+ @last_result_is_exception
440
+ end
441
+
442
+ # Whether the print proc should be invoked.
443
+ # Currently only invoked if the output is not suppressed.
444
+ # @return [Boolean] Whether the print proc should be invoked.
445
+ def should_print?
446
+ !@suppress_output
447
+ end
448
+
449
+ # Returns the appropriate prompt to use.
450
+ # @return [String] The prompt.
451
+ def select_prompt
452
+ object = current_binding.eval('self')
453
+ nesting_level = binding_stack.size - 1
454
+ pry_instance = self
455
+
456
+ Pry.critical_section do
457
+ # If input buffer is empty, then use normal prompt. Otherwise use the wait
458
+ # prompt (indicating multi-line expression).
459
+ if prompt.is_a?(Pry::Prompt)
460
+ prompt_proc = eval_string.empty? ? prompt.wait_proc : prompt.incomplete_proc
461
+ return prompt_proc.call(object, nesting_level, pry_instance)
462
+ else
463
+ output.puts "ERROR: Use Pry::Prompt API."
464
+ end
465
+ end
466
+ end
467
+
468
+ # Pushes the current prompt onto a stack that it can be restored from later.
469
+ # Use this if you wish to temporarily change the prompt.
470
+ #
471
+ # @example
472
+ # push_prompt(Pry::Prompt[:my_prompt])
473
+ #
474
+ # @param [Pry::Prompt] new_prompt
475
+ # @return [Pry::Prompt] new_prompt
476
+ def push_prompt(new_prompt)
477
+ prompt_stack.push new_prompt
478
+ end
479
+
480
+ # Pops the current prompt off of the prompt stack. If the prompt you are
481
+ # popping is the last prompt, it will not be popped. Use this to restore the
482
+ # previous prompt.
483
+ #
484
+ # @example
485
+ # pry = Pry.new(prompt: Pry::Prompt[:my_prompt1])
486
+ # pry.push_prompt(Pry::Prompt[:my_prompt2])
487
+ # pry.pop_prompt # => prompt2
488
+ # pry.pop_prompt # => prompt1
489
+ # pry.pop_prompt # => prompt1
490
+ #
491
+ # @return [Pry::Prompt] the prompt being popped
492
+ def pop_prompt
493
+ prompt_stack.size > 1 ? prompt_stack.pop : prompt
494
+ end
495
+
496
+ undef :pager if method_defined? :pager
497
+ # Returns the currently configured pager
498
+ # @example
499
+ # pry_instance.pager.page text
500
+ def pager
501
+ Pry::Pager.new(self)
502
+ end
503
+
504
+ undef :output if method_defined? :output
505
+ # Returns an output device
506
+ # @example
507
+ # pry_instance.output.puts "ohai!"
508
+ def output
509
+ Pry::Output.new(self)
510
+ end
511
+
512
+ # Raise an exception out of Pry.
513
+ #
514
+ # See Kernel#raise for documentation of parameters.
515
+ # See rb_make_exception for the inbuilt implementation.
516
+ #
517
+ # This is necessary so that the raise-up command can tell the
518
+ # difference between an exception the user has decided to raise,
519
+ # and a mistake in specifying that exception.
520
+ #
521
+ # (i.e. raise-up RunThymeError.new should not be the same as
522
+ # raise-up NameError, "uninitialized constant RunThymeError")
523
+ #
524
+ def raise_up_common(force, *args)
525
+ exception = if args == []
526
+ last_exception || RuntimeError.new
527
+ elsif args.length == 1 && args.first.is_a?(String)
528
+ RuntimeError.new(args.first)
529
+ elsif args.length > 3
530
+ raise ArgumentError, "wrong number of arguments"
531
+ elsif !args.first.respond_to?(:exception)
532
+ raise TypeError, "exception class/object expected"
533
+ elsif args.size == 1
534
+ args.first.exception
535
+ else
536
+ args.first.exception(args[1])
537
+ end
538
+
539
+ raise TypeError, "exception object expected" unless exception.is_a? Exception
540
+
541
+ exception.set_backtrace(args.size == 3 ? args[2] : caller(1))
542
+
543
+ if force || binding_stack.one?
544
+ binding_stack.clear
545
+ throw :raise_up, exception
546
+ else
547
+ binding_stack.pop
548
+ raise exception
549
+ end
550
+ end
551
+
552
+ def raise_up(*args)
553
+ raise_up_common(false, *args)
554
+ end
555
+
556
+ def raise_up!(*args)
557
+ raise_up_common(true, *args)
558
+ end
559
+
560
+ # Convenience accessor for the `quiet` config key.
561
+ # @return [Boolean]
562
+ def quiet?
563
+ config.quiet
564
+ end
565
+
566
+ private
567
+
568
+ def handle_line(line, options)
569
+ if line.nil?
570
+ config.control_d_handler.call(self)
571
+ return
572
+ end
573
+
574
+ ensure_correct_encoding!(line)
575
+ Pry.history << line unless options[:generated]
576
+
577
+ @suppress_output = false
578
+ inject_sticky_locals!
579
+ begin
580
+ unless process_command_safely(line)
581
+ @eval_string += "#{line.chomp}\n" if !line.empty? || !@eval_string.empty?
582
+ end
583
+ rescue RescuableException => e
584
+ self.last_exception = e
585
+ result = e
586
+
587
+ Pry.critical_section do
588
+ show_result(result)
589
+ end
590
+ return
591
+ end
592
+
593
+ # This hook is supposed to be executed after each line of ruby code
594
+ # has been read (regardless of whether eval_string is yet a complete expression)
595
+ exec_hook :after_read, eval_string, self
596
+
597
+ begin
598
+ complete_expr = Pry::Code.complete_expression?(@eval_string)
599
+ rescue SyntaxError => e
600
+ output.puts e.message.gsub(/^.*syntax error, */, "SyntaxError: ")
601
+ reset_eval_string
602
+ end
603
+
604
+ if complete_expr
605
+ if @eval_string =~ /;\Z/ || @eval_string.empty? || @eval_string =~ /\A *#.*\n\z/
606
+ @suppress_output = true
607
+ end
608
+
609
+ # A bug in jruby makes java.lang.Exception not rescued by
610
+ # `rescue Pry::RescuableException` clause.
611
+ #
612
+ # * https://github.com/pry/pry/issues/854
613
+ # * https://jira.codehaus.org/browse/JRUBY-7100
614
+ #
615
+ # Until that gets fixed upstream, treat java.lang.Exception
616
+ # as an additional exception to be rescued explicitly.
617
+ #
618
+ # This workaround has a side effect: java exceptions specified
619
+ # in `Pry.config.unrescued_exceptions` are ignored.
620
+ jruby_exceptions = []
621
+ jruby_exceptions << Java::JavaLang::Exception if Helpers::Platform.jruby?
622
+
623
+ begin
624
+ # Reset eval string, in case we're evaluating Ruby that does something
625
+ # like open a nested REPL on this instance.
626
+ eval_string = @eval_string
627
+ reset_eval_string
628
+
629
+ result = evaluate_ruby(eval_string)
630
+ rescue RescuableException, *jruby_exceptions => e
631
+ # Eliminate following warning:
632
+ # warning: singleton on non-persistent Java type X
633
+ # (http://wiki.jruby.org/Persistence)
634
+ if Helpers::Platform.jruby? && e.class.respond_to?('__persistent__')
635
+ e.class.__persistent__ = true
636
+ end
637
+ self.last_exception = e
638
+ result = e
639
+ end
640
+
641
+ Pry.critical_section do
642
+ show_result(result)
643
+ end
644
+ end
645
+
646
+ throw(:breakout) if current_binding.nil?
647
+ end
648
+
649
+ # Force `eval_string` into the encoding of `val`. [Issue #284]
650
+ def ensure_correct_encoding!(val)
651
+ if @eval_string.empty? &&
652
+ val.respond_to?(:encoding) &&
653
+ val.encoding != @eval_string.encoding
654
+ @eval_string.force_encoding(val.encoding)
655
+ end
656
+ end
657
+
658
+ # the array that the prompt stack is stored in
659
+ def prompt_stack
660
+ @prompt_stack ||= []
661
+ end
662
+ end
663
+ # rubocop:enable Metrics/ClassLength