pry 0.9.12.6-java → 0.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +702 -0
  3. data/LICENSE +2 -2
  4. data/{README.markdown → README.md} +37 -31
  5. data/lib/pry.rb +38 -151
  6. data/lib/pry/cli.rb +35 -17
  7. data/lib/pry/code.rb +19 -63
  8. data/lib/pry/code/code_file.rb +103 -0
  9. data/lib/pry/code/code_range.rb +2 -1
  10. data/lib/pry/code/loc.rb +2 -2
  11. data/lib/pry/code_object.rb +40 -21
  12. data/lib/pry/color_printer.rb +55 -0
  13. data/lib/pry/command.rb +12 -9
  14. data/lib/pry/command_set.rb +81 -38
  15. data/lib/pry/commands.rb +1 -1
  16. data/lib/pry/commands/amend_line.rb +2 -2
  17. data/lib/pry/commands/bang.rb +1 -1
  18. data/lib/pry/commands/cat.rb +11 -2
  19. data/lib/pry/commands/cat/exception_formatter.rb +6 -7
  20. data/lib/pry/commands/cat/file_formatter.rb +15 -32
  21. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  22. data/lib/pry/commands/cd.rb +14 -3
  23. data/lib/pry/commands/change_inspector.rb +27 -0
  24. data/lib/pry/commands/change_prompt.rb +26 -0
  25. data/lib/pry/commands/code_collector.rb +4 -4
  26. data/lib/pry/commands/easter_eggs.rb +3 -3
  27. data/lib/pry/commands/edit.rb +10 -22
  28. data/lib/pry/commands/edit/exception_patcher.rb +2 -2
  29. data/lib/pry/commands/edit/file_and_line_locator.rb +0 -2
  30. data/lib/pry/commands/exit_program.rb +0 -1
  31. data/lib/pry/commands/find_method.rb +16 -22
  32. data/lib/pry/commands/gem_install.rb +5 -2
  33. data/lib/pry/commands/gem_open.rb +1 -1
  34. data/lib/pry/commands/gist.rb +10 -11
  35. data/lib/pry/commands/help.rb +14 -14
  36. data/lib/pry/commands/hist.rb +27 -8
  37. data/lib/pry/commands/install_command.rb +14 -12
  38. data/lib/pry/commands/list_inspectors.rb +35 -0
  39. data/lib/pry/commands/list_prompts.rb +35 -0
  40. data/lib/pry/commands/ls.rb +72 -296
  41. data/lib/pry/commands/ls/constants.rb +47 -0
  42. data/lib/pry/commands/ls/formatter.rb +49 -0
  43. data/lib/pry/commands/ls/globals.rb +48 -0
  44. data/lib/pry/commands/ls/grep.rb +21 -0
  45. data/lib/pry/commands/ls/instance_vars.rb +39 -0
  46. data/lib/pry/commands/ls/interrogatable.rb +18 -0
  47. data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
  48. data/lib/pry/commands/ls/local_names.rb +35 -0
  49. data/lib/pry/commands/ls/local_vars.rb +39 -0
  50. data/lib/pry/commands/ls/ls_entity.rb +70 -0
  51. data/lib/pry/commands/ls/methods.rb +57 -0
  52. data/lib/pry/commands/ls/methods_helper.rb +46 -0
  53. data/lib/pry/commands/ls/self_methods.rb +32 -0
  54. data/lib/pry/commands/play.rb +44 -10
  55. data/lib/pry/commands/pry_backtrace.rb +1 -2
  56. data/lib/pry/commands/raise_up.rb +2 -2
  57. data/lib/pry/commands/reload_code.rb +16 -19
  58. data/lib/pry/commands/ri.rb +7 -3
  59. data/lib/pry/commands/shell_command.rb +18 -13
  60. data/lib/pry/commands/shell_mode.rb +2 -4
  61. data/lib/pry/commands/show_doc.rb +5 -0
  62. data/lib/pry/commands/show_info.rb +8 -13
  63. data/lib/pry/commands/show_source.rb +15 -3
  64. data/lib/pry/commands/simple_prompt.rb +1 -1
  65. data/lib/pry/commands/toggle_color.rb +8 -4
  66. data/lib/pry/commands/watch_expression.rb +105 -0
  67. data/lib/pry/commands/watch_expression/expression.rb +38 -0
  68. data/lib/pry/commands/whereami.rb +18 -10
  69. data/lib/pry/commands/wtf.rb +3 -3
  70. data/lib/pry/config.rb +20 -254
  71. data/lib/pry/config/behavior.rb +139 -0
  72. data/lib/pry/config/convenience.rb +26 -0
  73. data/lib/pry/config/default.rb +165 -0
  74. data/lib/pry/core_extensions.rb +31 -21
  75. data/lib/pry/editor.rb +107 -103
  76. data/lib/pry/exceptions.rb +77 -0
  77. data/lib/pry/helpers/base_helpers.rb +22 -109
  78. data/lib/pry/helpers/command_helpers.rb +10 -8
  79. data/lib/pry/helpers/documentation_helpers.rb +1 -2
  80. data/lib/pry/helpers/text.rb +4 -5
  81. data/lib/pry/history.rb +46 -45
  82. data/lib/pry/history_array.rb +6 -1
  83. data/lib/pry/hooks.rb +9 -29
  84. data/lib/pry/indent.rb +6 -6
  85. data/lib/pry/input_completer.rb +242 -0
  86. data/lib/pry/input_lock.rb +132 -0
  87. data/lib/pry/inspector.rb +27 -0
  88. data/lib/pry/last_exception.rb +61 -0
  89. data/lib/pry/method.rb +82 -87
  90. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +41 -38
  91. data/lib/pry/module_candidate.rb +4 -14
  92. data/lib/pry/object_path.rb +82 -0
  93. data/lib/pry/output.rb +50 -0
  94. data/lib/pry/pager.rb +193 -48
  95. data/lib/pry/plugins.rb +1 -1
  96. data/lib/pry/prompt.rb +26 -0
  97. data/lib/pry/pry_class.rb +149 -230
  98. data/lib/pry/pry_instance.rb +302 -413
  99. data/lib/pry/rbx_path.rb +1 -1
  100. data/lib/pry/repl.rb +202 -0
  101. data/lib/pry/repl_file_loader.rb +20 -26
  102. data/lib/pry/rubygem.rb +13 -5
  103. data/lib/pry/terminal.rb +2 -1
  104. data/lib/pry/test/helper.rb +26 -41
  105. data/lib/pry/version.rb +1 -1
  106. data/lib/pry/wrapped_module.rb +45 -59
  107. metadata +62 -225
  108. data/.document +0 -2
  109. data/.gitignore +0 -16
  110. data/.travis.yml +0 -25
  111. data/.yardopts +0 -1
  112. data/CHANGELOG +0 -534
  113. data/CONTRIBUTORS +0 -55
  114. data/Gemfile +0 -12
  115. data/Rakefile +0 -140
  116. data/TODO +0 -117
  117. data/lib/pry/completion.rb +0 -321
  118. data/lib/pry/custom_completions.rb +0 -6
  119. data/lib/pry/rbx_method.rb +0 -13
  120. data/man/pry.1 +0 -195
  121. data/man/pry.1.html +0 -204
  122. data/man/pry.1.ronn +0 -141
  123. data/pry.gemspec +0 -29
  124. data/spec/Procfile +0 -3
  125. data/spec/cli_spec.rb +0 -78
  126. data/spec/code_object_spec.rb +0 -277
  127. data/spec/code_spec.rb +0 -219
  128. data/spec/command_helpers_spec.rb +0 -29
  129. data/spec/command_integration_spec.rb +0 -644
  130. data/spec/command_set_spec.rb +0 -627
  131. data/spec/command_spec.rb +0 -821
  132. data/spec/commands/amend_line_spec.rb +0 -247
  133. data/spec/commands/bang_spec.rb +0 -19
  134. data/spec/commands/cat_spec.rb +0 -164
  135. data/spec/commands/cd_spec.rb +0 -250
  136. data/spec/commands/disable_pry_spec.rb +0 -25
  137. data/spec/commands/edit_spec.rb +0 -727
  138. data/spec/commands/exit_all_spec.rb +0 -34
  139. data/spec/commands/exit_program_spec.rb +0 -19
  140. data/spec/commands/exit_spec.rb +0 -34
  141. data/spec/commands/find_method_spec.rb +0 -70
  142. data/spec/commands/gem_list_spec.rb +0 -26
  143. data/spec/commands/gist_spec.rb +0 -79
  144. data/spec/commands/help_spec.rb +0 -56
  145. data/spec/commands/hist_spec.rb +0 -181
  146. data/spec/commands/jump_to_spec.rb +0 -15
  147. data/spec/commands/ls_spec.rb +0 -181
  148. data/spec/commands/play_spec.rb +0 -140
  149. data/spec/commands/raise_up_spec.rb +0 -56
  150. data/spec/commands/save_file_spec.rb +0 -177
  151. data/spec/commands/show_doc_spec.rb +0 -510
  152. data/spec/commands/show_input_spec.rb +0 -17
  153. data/spec/commands/show_source_spec.rb +0 -782
  154. data/spec/commands/whereami_spec.rb +0 -203
  155. data/spec/completion_spec.rb +0 -241
  156. data/spec/control_d_handler_spec.rb +0 -58
  157. data/spec/documentation_helper_spec.rb +0 -73
  158. data/spec/editor_spec.rb +0 -79
  159. data/spec/exception_whitelist_spec.rb +0 -21
  160. data/spec/fixtures/candidate_helper1.rb +0 -11
  161. data/spec/fixtures/candidate_helper2.rb +0 -8
  162. data/spec/fixtures/example.erb +0 -5
  163. data/spec/fixtures/example_nesting.rb +0 -33
  164. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  165. data/spec/fixtures/testrc +0 -2
  166. data/spec/fixtures/testrcbad +0 -2
  167. data/spec/fixtures/whereami_helper.rb +0 -6
  168. data/spec/helper.rb +0 -34
  169. data/spec/helpers/bacon.rb +0 -86
  170. data/spec/helpers/mock_pry.rb +0 -43
  171. data/spec/helpers/table_spec.rb +0 -105
  172. data/spec/history_array_spec.rb +0 -67
  173. data/spec/hooks_spec.rb +0 -522
  174. data/spec/indent_spec.rb +0 -301
  175. data/spec/input_stack_spec.rb +0 -90
  176. data/spec/method_spec.rb +0 -482
  177. data/spec/prompt_spec.rb +0 -60
  178. data/spec/pry_defaults_spec.rb +0 -419
  179. data/spec/pry_history_spec.rb +0 -99
  180. data/spec/pry_output_spec.rb +0 -95
  181. data/spec/pry_spec.rb +0 -515
  182. data/spec/run_command_spec.rb +0 -25
  183. data/spec/sticky_locals_spec.rb +0 -157
  184. data/spec/syntax_checking_spec.rb +0 -81
  185. data/spec/wrapped_module_spec.rb +0 -261
  186. data/wiki/Customizing-pry.md +0 -397
  187. data/wiki/Home.md +0 -4
@@ -1,5 +1,4 @@
1
- require "pry/indent"
2
-
1
+ # -*- coding: utf-8 -*-
3
2
  ##
4
3
  # Pry is a powerful alternative to the standard IRB shell for Ruby. It
5
4
  # features syntax highlighting, a flexible plugin architecture, runtime
@@ -21,102 +20,65 @@ require "pry/indent"
21
20
  # * https://github.com/pry/pry
22
21
  # * the IRC channel, which is #pry on the Freenode network
23
22
  #
24
- class Pry
25
-
26
- attr_accessor :input
27
- attr_accessor :output
28
- attr_accessor :commands
29
- attr_accessor :print
30
- attr_accessor :exception_handler
31
- attr_accessor :input_stack
32
- attr_accessor :quiet
33
- alias :quiet? :quiet
34
-
35
- attr_accessor :custom_completions
36
23
 
24
+ class Pry
37
25
  attr_accessor :binding_stack
38
-
26
+ attr_accessor :custom_completions
27
+ attr_accessor :eval_string
28
+ attr_accessor :backtrace
29
+ attr_accessor :suppress_output
39
30
  attr_accessor :last_result
40
31
  attr_accessor :last_file
41
32
  attr_accessor :last_dir
42
33
 
43
34
  attr_reader :last_exception
44
-
35
+ attr_reader :command_state
36
+ attr_reader :exit_value
45
37
  attr_reader :input_array
46
38
  attr_reader :output_array
47
-
48
- attr_accessor :backtrace
49
-
50
- attr_accessor :extra_sticky_locals
51
-
52
- attr_accessor :suppress_output
53
-
54
- # This is exposed via Pry::Command#state.
55
- attr_reader :command_state
56
-
57
- # Special treatment for hooks as we want to alert people of the
58
- # changed API
59
- attr_reader :hooks
60
-
61
- # FIXME:
62
- # This is a hack to alert people of the new API.
63
- # @param [Pry::Hooks] v Only accept `Pry::Hooks` now!
64
- def hooks=(v)
65
- if v.is_a?(Hash)
66
- warn "Hash-based hooks are now deprecated! Use a `Pry::Hooks` object instead! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
67
- @hooks = Pry::Hooks.from_hash(v)
68
- else
69
- @hooks = v
70
- end
71
- end
72
-
73
- # Create a new `Pry` object.
74
- # @param [Hash] options The optional configuration parameters.
75
- # @option options [#readline] :input The object to use for input.
76
- # @option options [#puts] :output The object to use for output.
77
- # @option options [Pry::CommandBase] :commands The object to use for commands.
78
- # @option options [Hash] :hooks The defined hook Procs
79
- # @option options [Array<Proc>] :prompt The array of Procs to use for the prompts.
80
- # @option options [Proc] :print The Proc to use for the 'print'
81
- # @option options [Boolean] :quiet If true, omit the whereami banner when starting.
82
- # component of the REPL. (see print.rb)
39
+ attr_reader :config
40
+
41
+ extend Pry::Config::Convenience
42
+ config_shortcut *Pry::Config.shortcuts
43
+ EMPTY_COMPLETIONS = [].freeze
44
+
45
+ # Create a new {Pry} instance.
46
+ # @param [Hash] options
47
+ # @option options [#readline] :input
48
+ # The object to use for input.
49
+ # @option options [#puts] :output
50
+ # The object to use for output.
51
+ # @option options [Pry::CommandBase] :commands
52
+ # The object to use for commands.
53
+ # @option options [Hash] :hooks
54
+ # The defined hook Procs.
55
+ # @option options [Array<Proc>] :prompt
56
+ # The array of Procs to use for prompts.
57
+ # @option options [Proc] :print
58
+ # The Proc to use for printing return values.
59
+ # @option options [Boolean] :quiet
60
+ # Omit the `whereami` banner when starting.
61
+ # @option options [Array<String>] :backtrace
62
+ # The backtrace of the session's `binding.pry` line, if applicable.
63
+ # @option options [Object] :target
64
+ # The initial context for this session.
83
65
  def initialize(options={})
84
- refresh(options)
85
-
86
66
  @binding_stack = []
87
67
  @indent = Pry::Indent.new
88
68
  @command_state = {}
89
- end
90
-
91
- # Refresh the Pry instance settings from the Pry class.
92
- # Allows options to be specified to override settings from Pry class.
93
- # @param [Hash] options The options to override Pry class settings
94
- # for this instance.
95
- def refresh(options={})
96
- defaults = {}
97
- attributes = [
98
- :input, :output, :commands, :print, :quiet,
99
- :exception_handler, :hooks, :custom_completions,
100
- :prompt, :memory_size, :extra_sticky_locals
101
- ]
102
-
103
- attributes.each do |attribute|
104
- defaults[attribute] = Pry.send attribute
105
- end
106
-
107
- defaults[:input_stack] = Pry.input_stack.dup
108
-
109
- defaults.merge!(options).each do |key, value|
110
- send("#{key}=", value) if respond_to?("#{key}=")
111
- end
112
-
113
- true
114
- end
115
-
116
- # The currently active `Binding`.
117
- # @return [Binding] The currently active `Binding` for the session.
118
- def current_context
119
- binding_stack.last
69
+ @eval_string = ""
70
+ @backtrace = options.delete(:backtrace) || caller
71
+ target = options.delete(:target)
72
+ @config = Pry::Config.new
73
+ config.merge!(options)
74
+ push_prompt(config.prompt)
75
+ @input_array = Pry::HistoryArray.new config.memory_size
76
+ @output_array = Pry::HistoryArray.new config.memory_size
77
+ @custom_completions = config.command_completions
78
+ set_last_result nil
79
+ @input_array << nil
80
+ push_initial_binding(target)
81
+ exec_hook(:when_started, target, options, self)
120
82
  end
121
83
 
122
84
  # The current prompt.
@@ -139,16 +101,70 @@ class Pry
139
101
  end
140
102
  end
141
103
 
104
+ # Initialize this instance by pushing its initial context into the binding
105
+ # stack. If no target is given, start at the top level.
106
+ def push_initial_binding(target=nil)
107
+ push_binding(target || Pry.toplevel_binding)
108
+ end
109
+
110
+ # The currently active `Binding`.
111
+ # @return [Binding] The currently active `Binding` for the session.
112
+ def current_binding
113
+ binding_stack.last
114
+ end
115
+ alias current_context current_binding # support previous API
116
+
117
+ # Push a binding for the given object onto the stack. If this instance is
118
+ # currently stopped, mark it as usable again.
119
+ def push_binding(object)
120
+ @stopped = false
121
+ binding_stack << Pry.binding_for(object)
122
+ end
123
+
124
+ #
125
+ # Generate completions.
126
+ #
127
+ # @param [String] input
128
+ # What the user has typed so far
129
+ #
130
+ # @return [Array<String>]
131
+ # Possible completions
132
+ #
133
+ def complete(str)
134
+ return EMPTY_COMPLETIONS unless config.completer
135
+ Pry.critical_section do
136
+ completer = config.completer.new(config.input, self)
137
+ completer.call str, target: current_binding, custom_completions: custom_completions.call.push(*sticky_locals.keys)
138
+ end
139
+ end
140
+
141
+ #
142
142
  # Injects a local variable into the provided binding.
143
- # @param [String] name The name of the local to inject.
144
- # @param [Object] value The value to set the local to.
145
- # @param [Binding] b The binding to set the local on.
146
- # @return [Object] The value the local was set to.
143
+ #
144
+ # @param [String] name
145
+ # The name of the local to inject.
146
+ #
147
+ # @param [Object] value
148
+ # The value to set the local to.
149
+ #
150
+ # @param [Binding] b
151
+ # The binding to set the local on.
152
+ #
153
+ # @return [Object]
154
+ # The value the local was set to.
155
+ #
147
156
  def inject_local(name, value, b)
148
- Pry.current[:pry_local] = value.is_a?(Proc) ? value.call : value
149
- b.eval("#{name} = ::Pry.current[:pry_local]")
150
- ensure
151
- Pry.current[:pry_local] = nil
157
+ value = Proc === value ? value.call : value
158
+ if b.respond_to?(:local_variable_set)
159
+ b.local_variable_set name, value
160
+ else # < 2.1
161
+ begin
162
+ Pry.current[:pry_local] = value
163
+ b.eval "#{name} = ::Pry.current[:pry_local]"
164
+ ensure
165
+ Pry.current[:pry_local] = nil
166
+ end
167
+ end
152
168
  end
153
169
 
154
170
  # @return [Integer] The maximum amount of objects remembered by the inp and
@@ -162,11 +178,10 @@ class Pry
162
178
  @output_array = Pry::HistoryArray.new(size)
163
179
  end
164
180
 
165
- # Inject all the sticky locals into the `target` binding.
166
- # @param [Binding] target
167
- def inject_sticky_locals(target)
181
+ # Inject all the sticky locals into the current binding.
182
+ def inject_sticky_locals!
168
183
  sticky_locals.each_pair do |name, value|
169
- inject_local(name, value, target)
184
+ inject_local(name, value, current_binding)
170
185
  end
171
186
  end
172
187
 
@@ -176,157 +191,167 @@ class Pry
176
191
  # @yield The block that defines the content of the local. The local
177
192
  # will be refreshed at each tick of the repl loop.
178
193
  def add_sticky_local(name, &block)
179
- sticky_locals[name] = block
194
+ config.extra_sticky_locals[name] = block
180
195
  end
181
196
 
182
- # @return [Hash] The currently defined sticky locals.
183
197
  def sticky_locals
184
- @sticky_locals ||= {
185
- :_in_ => proc { @input_array },
186
- :_out_ => proc { @output_array },
187
- :_pry_ => self,
188
- :_ex_ => proc { last_exception },
189
- :_file_ => proc { last_file },
190
- :_dir_ => proc { last_dir },
191
- :_ => proc { last_result },
192
- :__ => proc { @output_array[-2] }
193
- }.merge(extra_sticky_locals)
198
+ { _in_: input_array,
199
+ _out_: output_array,
200
+ _pry_: self,
201
+ _ex_: last_exception && last_exception.wrapped_exception,
202
+ _file_: last_file,
203
+ _dir_: last_dir,
204
+ _: proc { last_result },
205
+ __: proc { output_array[-2] }
206
+ }.merge(config.extra_sticky_locals)
194
207
  end
195
208
 
196
- # Initialize the repl session.
197
- # @param [Binding] target The target binding for the session.
198
- def repl_prologue(target)
199
- exec_hook :before_session, output, target, self
200
- set_last_result(nil, target)
201
-
202
- @input_array << nil # add empty input so _in_ and _out_ match
203
-
204
- binding_stack.push target
209
+ # Reset the current eval string. If the user has entered part of a multiline
210
+ # expression, this discards that input.
211
+ def reset_eval_string
212
+ @eval_string = ""
205
213
  end
206
214
 
207
- # Clean-up after the repl session.
208
- # @param [Binding] target The target binding for the session.
209
- def repl_epilogue(target)
210
- exec_hook :after_session, output, target, self
211
-
212
- binding_stack.pop
213
- Pry.save_history if Pry.config.history.should_save
214
- end
215
-
216
- # Start a read-eval-print-loop.
217
- # If no parameter is given, default to top-level (main).
218
- # @param [Object, Binding] target The receiver of the Pry session
219
- # @return [Object] The target of the Pry session or an explictly given
220
- # return value. If given return value is `nil` or no return value
221
- # is specified then `target` will be returned.
222
- # @example
223
- # Pry.new.repl(Object.new)
224
- def repl(target=TOPLEVEL_BINDING)
225
- target = Pry.binding_for(target)
226
-
227
- repl_prologue(target)
228
-
229
- break_data = nil
215
+ # Pass a line of input to Pry.
216
+ #
217
+ # This is the equivalent of `Binding#eval` but with extra Pry!
218
+ #
219
+ # In particular:
220
+ # 1. Pry commands will be executed immediately if the line matches.
221
+ # 2. Partial lines of input will be queued up until a complete expression has
222
+ # been accepted.
223
+ # 3. Output is written to `#output` in pretty colours, not returned.
224
+ #
225
+ # Once this method has raised an exception or returned false, this instance
226
+ # is no longer usable. {#exit_value} will return the session's breakout
227
+ # value if applicable.
228
+ #
229
+ # @param [String?] line The line of input; `nil` if the user types `<Ctrl-D>`
230
+ # @option options [Boolean] :generated Whether this line was generated automatically.
231
+ # Generated lines are not stored in history.
232
+ # @return [Boolean] Is Pry ready to accept more input?
233
+ # @raise [Exception] If the user uses the `raise-up` command, this method
234
+ # will raise that exception.
235
+ def eval(line, options={})
236
+ return false if @stopped
237
+
238
+ exit_value = nil
230
239
  exception = catch(:raise_up) do
231
- break_data = catch(:breakout) do
232
- loop do
233
- throw(:breakout) if binding_stack.empty?
234
- rep(binding_stack.last)
235
- end
240
+ exit_value = catch(:breakout) do
241
+ handle_line(line, options)
242
+ # We use 'return !@stopped' here instead of 'return true' so that if
243
+ # handle_line has stopped this pry instance (e.g. by opening _pry_.repl and
244
+ # then popping all the bindings) we still exit immediately.
245
+ return !@stopped
236
246
  end
237
247
  exception = false
238
248
  end
239
249
 
240
- raise exception if exception
250
+ @stopped = true
251
+ @exit_value = exit_value
241
252
 
242
- break_data
243
- ensure
244
- repl_epilogue(target)
253
+ # TODO: make this configurable?
254
+ raise exception if exception
255
+ return false
245
256
  end
246
257
 
247
- # Perform a read-eval-print.
248
- # If no parameter is given, default to top-level (main).
249
- # @param [Object, Binding] target The receiver of the read-eval-print
250
- # @example
251
- # Pry.new.rep(Object.new)
252
- def rep(target=TOPLEVEL_BINDING)
253
- target = Pry.binding_for(target)
254
- result = re(target)
258
+ def handle_line(line, options)
259
+ if line.nil?
260
+ config.control_d_handler.call(@eval_string, self)
261
+ return
262
+ end
255
263
 
256
- Pry.critical_section do
257
- show_result(result)
264
+ ensure_correct_encoding!(line)
265
+ Pry.history << line unless options[:generated]
266
+
267
+ @suppress_output = false
268
+ inject_sticky_locals!
269
+ begin
270
+ if !process_command_safely(line.lstrip)
271
+ @eval_string << "#{line.chomp}\n" if !line.empty? || !@eval_string.empty?
272
+ end
273
+ rescue RescuableException => e
274
+ self.last_exception = e
275
+ result = e
276
+
277
+ Pry.critical_section do
278
+ show_result(result)
279
+ end
280
+ return
258
281
  end
259
- end
260
282
 
261
- # Perform a read-eval
262
- # If no parameter is given, default to top-level (main).
263
- # @param [Object, Binding] target The receiver of the read-eval-print
264
- # @return [Object] The result of the eval or an `Exception` object in case of
265
- # error. In the latter case, you can check whether the exception was raised
266
- # or is just the result of the expression using #last_result_is_exception?
267
- # @example
268
- # Pry.new.re(Object.new)
269
- def re(target=TOPLEVEL_BINDING)
270
- target = Pry.binding_for(target)
283
+ # This hook is supposed to be executed after each line of ruby code
284
+ # has been read (regardless of whether eval_string is yet a complete expression)
285
+ exec_hook :after_read, eval_string, self
271
286
 
272
- # It's not actually redundant to inject them continually as we may have
273
- # moved into the scope of a new Binding (e.g the user typed `cd`).
274
- inject_sticky_locals(target)
287
+ begin
288
+ complete_expr = Pry::Code.complete_expression?(@eval_string)
289
+ rescue SyntaxError => e
290
+ output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
291
+ reset_eval_string
292
+ end
275
293
 
276
- code = r(target)
294
+ if complete_expr
295
+ if @eval_string =~ /;\Z/ || @eval_string.empty? || @eval_string =~ /\A *#.*\n\z/
296
+ @suppress_output = true
297
+ end
277
298
 
278
- evaluate_ruby(code, target)
279
- rescue RescuableException => e
280
- self.last_exception = e
281
- e
282
- end
283
-
284
- # Perform a read.
285
- # If no parameter is given, default to top-level (main).
286
- # This is a multi-line read; so the read continues until a valid
287
- # Ruby expression is received.
288
- # Pry commands are also accepted here and operate on the target.
289
- # @param [Object, Binding] target The receiver of the read.
290
- # @param [String] eval_string Optionally Prime `eval_string` with a start value.
291
- # @return [String] The Ruby expression.
292
- # @example
293
- # Pry.new.r(Object.new)
294
- def r(target=TOPLEVEL_BINDING, eval_string="")
295
- target = Pry.binding_for(target)
296
- @suppress_output = false
299
+ # A bug in jruby makes java.lang.Exception not rescued by
300
+ # `rescue Pry::RescuableException` clause.
301
+ #
302
+ # * https://github.com/pry/pry/issues/854
303
+ # * https://jira.codehaus.org/browse/JRUBY-7100
304
+ #
305
+ # Until that gets fixed upstream, treat java.lang.Exception
306
+ # as an additional exception to be rescued explicitly.
307
+ #
308
+ # This workaround has a side effect: java exceptions specified
309
+ # in `Pry.config.exception_whitelist` are ignored.
310
+ jruby_exceptions = []
311
+ if Pry::Helpers::BaseHelpers.jruby?
312
+ jruby_exceptions << Java::JavaLang::Exception
313
+ end
297
314
 
298
- loop do
299
315
  begin
300
- # eval_string will probably be mutated by this method
301
- retrieve_line(eval_string, target)
302
- rescue CommandError, Slop::InvalidOptionError, MethodSource::SourceNotFoundError => e
303
- Pry.last_internal_error = e
304
- output.puts "Error: #{e.message}"
316
+ # Reset eval string, in case we're evaluating Ruby that does something
317
+ # like open a nested REPL on this instance.
318
+ eval_string = @eval_string
319
+ reset_eval_string
320
+
321
+ result = evaluate_ruby(eval_string)
322
+ rescue RescuableException, *jruby_exceptions => e
323
+ # Eliminate following warning:
324
+ # warning: singleton on non-persistent Java type X
325
+ # (http://wiki.jruby.org/Persistence)
326
+ if Pry::Helpers::BaseHelpers.jruby? && e.class.respond_to?('__persistent__')
327
+ e.class.__persistent__ = true
328
+ end
329
+ self.last_exception = e
330
+ result = e
305
331
  end
306
332
 
307
- begin
308
- break if Pry::Code.complete_expression?(eval_string)
309
- rescue SyntaxError => e
310
- exception_handler.call(output, e.extend(UserError), self)
311
- eval_string = ""
333
+ Pry.critical_section do
334
+ show_result(result)
312
335
  end
313
336
  end
314
337
 
315
- if eval_string =~ /;\Z/ || eval_string.empty? || eval_string =~ /\A *#.*\n\z/
316
- @suppress_output = true
317
- end
338
+ throw(:breakout) if current_binding.nil?
339
+ end
340
+ private :handle_line
318
341
 
319
- exec_hook :after_read, eval_string, self
320
- eval_string
342
+ # Potentially deprecated — Use `Pry::REPL.new(pry, :target => target).start`
343
+ # (If nested sessions are going to exist, this method is fine, but a goal is
344
+ # to come up with an alternative to nested sessions altogether.)
345
+ def repl(target = nil)
346
+ Pry::REPL.new(self, :target => target).start
321
347
  end
322
348
 
323
- def evaluate_ruby(code, target = binding_stack.last)
324
- target = Pry.binding_for(target)
325
- inject_sticky_locals(target)
349
+ def evaluate_ruby(code)
350
+ inject_sticky_locals!
326
351
  exec_hook :before_eval, code, self
327
352
 
328
- result = target.eval(code, Pry.eval_path, Pry.current_line)
329
- set_last_result(result, target, code)
353
+ result = current_binding.eval(code, Pry.eval_path, Pry.current_line)
354
+ set_last_result(result, code)
330
355
  ensure
331
356
  update_input_history(code)
332
357
  exec_hook :after_eval, result, self
@@ -337,7 +362,7 @@ class Pry
337
362
  if last_result_is_exception?
338
363
  exception_handler.call(output, result, self)
339
364
  elsif should_print?
340
- print.call(output, result)
365
+ print.call(output, result, self)
341
366
  else
342
367
  # nothin'
343
368
  end
@@ -354,100 +379,32 @@ class Pry
354
379
  output.puts "(pry) output error: failed to show result"
355
380
  end
356
381
  end
382
+ ensure
383
+ output.flush if output.respond_to?(:flush)
357
384
  end
358
385
 
359
- def should_force_encoding?(eval_string, val)
360
- eval_string.empty? && val.respond_to?(:encoding) && val.encoding != eval_string.encoding
361
- end
362
- private :should_force_encoding?
363
-
364
- # Read and process a line of input -- check for ^D, determine which prompt to
365
- # use, rewrite the indentation if `Pry.config.auto_indent` is enabled, and,
366
- # if the line is a command, process it and alter the eval_string accordingly.
367
- # This method should not need to be invoked directly.
368
- #
369
- # @param [String] eval_string The cumulative lines of input.
370
- # @param [Binding] target The target of the session.
371
- # @return [String] The line received.
372
- def retrieve_line(eval_string, target)
373
- @indent.reset if eval_string.empty?
374
-
375
- current_prompt = select_prompt(eval_string, target)
376
- completion_proc = Pry.config.completer.build_completion_proc(target, self,
377
- instance_eval(&custom_completions))
378
-
379
- safe_completion_proc = proc{ |*a| Pry.critical_section{ completion_proc.call(*a) } }
380
-
381
- indentation = Pry.config.auto_indent ? @indent.current_prefix : ''
382
-
383
- begin
384
- val = readline("#{current_prompt}#{indentation}", safe_completion_proc)
385
-
386
- # Handle <Ctrl+C> like Bash, empty the current input buffer but do not quit.
387
- # This is only for ruby-1.9; other versions of ruby do not let you send Interrupt
388
- # from within Readline.
389
- rescue Interrupt
390
- output.puts ""
391
- eval_string.replace("")
392
- return
393
- end
394
-
395
- # invoke handler if we receive EOF character (^D)
396
- if !val
397
- output.puts ""
398
- Pry.config.control_d_handler.call(eval_string, self)
399
- return
400
- end
401
-
402
- # Change the eval_string into the input encoding (Issue 284)
403
- # TODO: This wouldn't be necessary if the eval_string was constructed from
404
- # input strings only.
405
- if should_force_encoding?(eval_string, val)
406
- eval_string.force_encoding(val.encoding)
407
- end
408
-
409
- if Pry.config.auto_indent && !input.is_a?(StringIO)
410
- original_val = "#{indentation}#{val}"
411
- indented_val = @indent.indent(val)
412
-
413
- if output.tty? && Pry::Helpers::BaseHelpers.use_ansi_codes? && Pry.config.correct_indent
414
- output.print @indent.correct_indentation(current_prompt, indented_val, original_val.length - indented_val.length)
415
- output.flush
416
- end
417
- else
418
- indented_val = val
419
- end
420
-
421
- # Check this before processing the line, because a command might change
422
- # Pry's input.
423
- interactive = !input.is_a?(StringIO)
424
-
425
- begin
426
- if !process_command(val, eval_string, target)
427
- unless [val, eval_string].all?(&:empty?)
428
- eval_string << "#{indented_val.chomp}\n"
429
- end
430
- end
431
- ensure
432
- Pry.history << indented_val if interactive
386
+ # Force `eval_string` into the encoding of `val`. [Issue #284]
387
+ def ensure_correct_encoding!(val)
388
+ if @eval_string.empty? &&
389
+ val.respond_to?(:encoding) &&
390
+ val.encoding != @eval_string.encoding
391
+ @eval_string.force_encoding(val.encoding)
433
392
  end
434
393
  end
394
+ private :ensure_correct_encoding!
435
395
 
436
396
  # If the given line is a valid command, process it in the context of the
437
- # current `eval_string` and context.
438
- # This method should not need to be invoked directly.
397
+ # current `eval_string` and binding.
439
398
  # @param [String] val The line to process.
440
- # @param [String] eval_string The cumulative lines of input.
441
- # @param [Binding] target The target of the Pry session.
442
399
  # @return [Boolean] `true` if `val` is a command, `false` otherwise
443
- def process_command(val, eval_string = '', target = binding_stack.last)
400
+ def process_command(val)
444
401
  val = val.chomp
445
- result = commands.process_line(val, {
446
- :target => target,
402
+ result = commands.process_line(val,
403
+ :target => current_binding,
447
404
  :output => output,
448
- :eval_string => eval_string,
405
+ :eval_string => @eval_string,
449
406
  :pry_instance => self
450
- })
407
+ )
451
408
 
452
409
  # set a temporary (just so we can inject the value we want into eval_string)
453
410
  Pry.current[:pry_cmd_result] = result
@@ -460,7 +417,7 @@ class Pry
460
417
  # the command that was invoked was non-void (had a return value) and so we make
461
418
  # the value of the current expression equal to the return value
462
419
  # of the command.
463
- eval_string.replace "::Pry.current[:pry_cmd_result].retval\n"
420
+ @eval_string.replace "::Pry.current[:pry_cmd_result].retval\n"
464
421
  end
465
422
  true
466
423
  else
@@ -468,17 +425,27 @@ class Pry
468
425
  end
469
426
  end
470
427
 
428
+ # Same as process_command, but outputs exceptions to `#output` instead of
429
+ # raising.
430
+ # @param [String] val The line to process.
431
+ # @return [Boolean] `true` if `val` is a command, `false` otherwise
432
+ def process_command_safely(val)
433
+ process_command(val)
434
+ rescue CommandError, Slop::InvalidOptionError, MethodSource::SourceNotFoundError => e
435
+ Pry.last_internal_error = e
436
+ output.puts "Error: #{e.message}"
437
+ true
438
+ end
439
+
471
440
  # Run the specified command.
472
441
  # @param [String] val The command (and its params) to execute.
473
- # @param [String] eval_string The current input buffer.
474
- # @param [Binding] target The binding to use..
475
442
  # @return [Pry::Command::VOID_VALUE]
476
443
  # @example
477
444
  # pry_instance.run_command("ls -m")
478
- def run_command(val, eval_string = "", target = binding_stack.last)
445
+ def run_command(val)
479
446
  commands.process_line(val,
480
- :eval_string => eval_string,
481
- :target => target,
447
+ :eval_string => @eval_string,
448
+ :target => current_binding,
482
449
  :pry_instance => self,
483
450
  :output => output
484
451
  )
@@ -507,36 +474,25 @@ class Pry
507
474
  # Set the last result of an eval.
508
475
  # This method should not need to be invoked directly.
509
476
  # @param [Object] result The result.
510
- # @param [Binding] target The binding to set `_` on.
511
477
  # @param [String] code The code that was run.
512
- def set_last_result(result, target, code="")
478
+ def set_last_result(result, code="")
513
479
  @last_result_is_exception = false
514
480
  @output_array << result
515
481
 
516
482
  self.last_result = result unless code =~ /\A\s*\z/
517
483
  end
518
484
 
485
+ #
519
486
  # Set the last exception for a session.
520
- # @param [Exception] ex
521
- def last_exception=(ex)
522
- class << ex
523
- attr_accessor :file, :line, :bt_index
524
- def bt_source_location_for(index)
525
- backtrace[index] =~ /(.*):(\d+)/
526
- [$1, $2.to_i]
527
- end
528
-
529
- def inc_bt_index
530
- @bt_index = (@bt_index + 1) % backtrace.size
531
- end
532
- end
533
-
534
- ex.bt_index = 0
535
- ex.file, ex.line = ex.bt_source_location_for(0)
536
-
487
+ #
488
+ # @param [Exception] e
489
+ # the last exception.
490
+ #
491
+ def last_exception=(e)
492
+ last_exception = Pry::LastException.new(e)
537
493
  @last_result_is_exception = true
538
- @output_array << ex
539
- @last_exception = ex
494
+ @output_array << last_exception
495
+ @last_exception = last_exception
540
496
  end
541
497
 
542
498
  # Update Pry's internal state after evalling code.
@@ -558,84 +514,6 @@ class Pry
558
514
  @last_result_is_exception
559
515
  end
560
516
 
561
- # Manage switching of input objects on encountering EOFErrors
562
- def handle_read_errors
563
- should_retry = true
564
- exception_count = 0
565
- begin
566
- yield
567
- rescue EOFError
568
- if input_stack.empty?
569
- self.input = Pry.config.input
570
- if !should_retry
571
- output.puts "Error: Pry ran out of things to read from! Attempting to break out of REPL."
572
- throw(:breakout)
573
- end
574
- should_retry = false
575
- else
576
- self.input = input_stack.pop
577
- end
578
-
579
- retry
580
-
581
- # Interrupts are handled in r() because they need to tweak eval_string
582
- # TODO: Refactor this baby.
583
- rescue Interrupt
584
- raise
585
-
586
- # If we get a random error when trying to read a line we don't want to automatically
587
- # retry, as the user will see a lot of error messages scroll past and be unable to do
588
- # anything about it.
589
- rescue RescuableException => e
590
- puts "Error: #{e.message}"
591
- output.puts e.backtrace
592
- exception_count += 1
593
- if exception_count < 5
594
- retry
595
- end
596
- puts "FATAL: Pry failed to get user input using `#{input}`."
597
- puts "To fix this you may be able to pass input and output file descriptors to pry directly. e.g."
598
- puts " Pry.config.input = STDIN"
599
- puts " Pry.config.output = STDOUT"
600
- puts " binding.pry"
601
- throw(:breakout)
602
- end
603
- end
604
- private :handle_read_errors
605
-
606
- # Returns the next line of input to be used by the pry instance.
607
- # This method should not need to be invoked directly.
608
- # @param [String] current_prompt The prompt to use for input.
609
- # @return [String] The next line of input.
610
- def readline(current_prompt="> ", completion_proc=nil)
611
- handle_read_errors do
612
-
613
- if defined? Coolline and input.is_a? Coolline
614
- input.completion_proc = proc do |cool|
615
- completions = completion_proc.call cool.completed_word
616
- completions.compact
617
- end
618
- elsif input.respond_to? :completion_proc=
619
- input.completion_proc = completion_proc
620
- end
621
-
622
- if defined?(Readline) && input == Readline
623
- if !$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows?
624
- Readline.output = File.open('/dev/tty', 'w')
625
- end
626
- input.readline(current_prompt, false) # false since we'll add it manually
627
- elsif defined? Coolline and input.is_a? Coolline
628
- input.readline(current_prompt)
629
- else
630
- if input.method(:readline).arity == 1
631
- input.readline(current_prompt)
632
- else
633
- input.readline
634
- end
635
- end
636
- end
637
- end
638
-
639
517
  # Whether the print proc should be invoked.
640
518
  # Currently only invoked if the output is not suppressed.
641
519
  # @return [Boolean] Whether the print proc should be invoked.
@@ -644,18 +522,15 @@ class Pry
644
522
  end
645
523
 
646
524
  # Returns the appropriate prompt to use.
647
- # This method should not need to be invoked directly.
648
- # @param [String] eval_string The current input buffer.
649
- # @param [Binding] target The target Binding of the Pry session.
650
525
  # @return [String] The prompt.
651
- def select_prompt(eval_string, target)
652
- target_self = target.eval('self')
526
+ def select_prompt
527
+ object = current_binding.eval('self')
653
528
 
654
529
  open_token = @indent.open_delimiters.any? ? @indent.open_delimiters.last :
655
530
  @indent.stack.last
656
531
 
657
- c = OpenStruct.new(
658
- :object => target_self,
532
+ c = Pry::Config.from_hash({
533
+ :object => object,
659
534
  :nesting_level => binding_stack.size - 1,
660
535
  :open_token => open_token,
661
536
  :session_line => Pry.history.session_line_count + 1,
@@ -664,8 +539,8 @@ class Pry
664
539
  :_pry_ => self,
665
540
  :binding_stack => binding_stack,
666
541
  :input_array => input_array,
667
- :eval_string => eval_string,
668
- :cont => !eval_string.empty?)
542
+ :eval_string => @eval_string,
543
+ :cont => !@eval_string.empty?})
669
544
 
670
545
  Pry.critical_section do
671
546
  # If input buffer is empty then use normal prompt
@@ -721,6 +596,20 @@ class Pry
721
596
  prompt_stack.size > 1 ? prompt_stack.pop : prompt
722
597
  end
723
598
 
599
+ # Returns the currently configured pager
600
+ # @example
601
+ # _pry_.pager.page text
602
+ def pager
603
+ Pry::Pager.new(self)
604
+ end
605
+
606
+ # Returns an output device
607
+ # @example
608
+ # _pry_.output.puts "ohai!"
609
+ def output
610
+ Pry::Output.new(self)
611
+ end
612
+
724
613
  # Raise an exception out of Pry.
725
614
  #
726
615
  # See Kernel#raise for documentation of parameters.