pry 0.10.pre.1-i386-mswin32 → 0.10.0.pre3-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +702 -0
  3. data/LICENSE +2 -2
  4. data/{README.markdown → README.md} +41 -35
  5. data/lib/pry.rb +82 -139
  6. data/lib/pry/cli.rb +77 -30
  7. data/lib/pry/code.rb +122 -183
  8. data/lib/pry/code/code_file.rb +103 -0
  9. data/lib/pry/code/code_range.rb +71 -0
  10. data/lib/pry/code/loc.rb +92 -0
  11. data/lib/pry/code_object.rb +172 -0
  12. data/lib/pry/color_printer.rb +55 -0
  13. data/lib/pry/command.rb +184 -28
  14. data/lib/pry/command_set.rb +113 -59
  15. data/lib/pry/commands.rb +4 -27
  16. data/lib/pry/commands/amend_line.rb +99 -0
  17. data/lib/pry/commands/bang.rb +20 -0
  18. data/lib/pry/commands/bang_pry.rb +17 -0
  19. data/lib/pry/commands/cat.rb +62 -0
  20. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  21. data/lib/pry/commands/cat/exception_formatter.rb +77 -0
  22. data/lib/pry/commands/cat/file_formatter.rb +67 -0
  23. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  24. data/lib/pry/commands/cd.rb +41 -0
  25. data/lib/pry/commands/change_inspector.rb +27 -0
  26. data/lib/pry/commands/change_prompt.rb +26 -0
  27. data/lib/pry/commands/code_collector.rb +165 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/disabled_commands.rb +2 -0
  30. data/lib/pry/commands/easter_eggs.rb +112 -0
  31. data/lib/pry/commands/edit.rb +195 -0
  32. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +36 -0
  34. data/lib/pry/commands/exit.rb +42 -0
  35. data/lib/pry/commands/exit_all.rb +29 -0
  36. data/lib/pry/commands/exit_program.rb +23 -0
  37. data/lib/pry/commands/find_method.rb +193 -0
  38. data/lib/pry/commands/fix_indent.rb +19 -0
  39. data/lib/pry/commands/gem_cd.rb +26 -0
  40. data/lib/pry/commands/gem_install.rb +32 -0
  41. data/lib/pry/commands/gem_list.rb +33 -0
  42. data/lib/pry/commands/gem_open.rb +29 -0
  43. data/lib/pry/commands/gist.rb +101 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +180 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +53 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/list_inspectors.rb +35 -0
  50. data/lib/pry/commands/list_prompts.rb +35 -0
  51. data/lib/pry/commands/ls.rb +114 -0
  52. data/lib/pry/commands/ls/constants.rb +47 -0
  53. data/lib/pry/commands/ls/formatter.rb +49 -0
  54. data/lib/pry/commands/ls/globals.rb +48 -0
  55. data/lib/pry/commands/ls/grep.rb +21 -0
  56. data/lib/pry/commands/ls/instance_vars.rb +39 -0
  57. data/lib/pry/commands/ls/interrogatable.rb +18 -0
  58. data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
  59. data/lib/pry/commands/ls/local_names.rb +35 -0
  60. data/lib/pry/commands/ls/local_vars.rb +39 -0
  61. data/lib/pry/commands/ls/ls_entity.rb +70 -0
  62. data/lib/pry/commands/ls/methods.rb +57 -0
  63. data/lib/pry/commands/ls/methods_helper.rb +46 -0
  64. data/lib/pry/commands/ls/self_methods.rb +32 -0
  65. data/lib/pry/commands/nesting.rb +25 -0
  66. data/lib/pry/commands/play.rb +103 -0
  67. data/lib/pry/commands/pry_backtrace.rb +25 -0
  68. data/lib/pry/commands/pry_version.rb +17 -0
  69. data/lib/pry/commands/raise_up.rb +32 -0
  70. data/lib/pry/commands/reload_code.rb +62 -0
  71. data/lib/pry/commands/reset.rb +18 -0
  72. data/lib/pry/commands/ri.rb +60 -0
  73. data/lib/pry/commands/save_file.rb +61 -0
  74. data/lib/pry/commands/shell_command.rb +48 -0
  75. data/lib/pry/commands/shell_mode.rb +25 -0
  76. data/lib/pry/commands/show_doc.rb +83 -0
  77. data/lib/pry/commands/show_info.rb +195 -0
  78. data/lib/pry/commands/show_input.rb +17 -0
  79. data/lib/pry/commands/show_source.rb +50 -0
  80. data/lib/pry/commands/simple_prompt.rb +22 -0
  81. data/lib/pry/commands/stat.rb +40 -0
  82. data/lib/pry/commands/switch_to.rb +23 -0
  83. data/lib/pry/commands/toggle_color.rb +24 -0
  84. data/lib/pry/commands/watch_expression.rb +105 -0
  85. data/lib/pry/commands/watch_expression/expression.rb +38 -0
  86. data/lib/pry/commands/whereami.rb +190 -0
  87. data/lib/pry/commands/wtf.rb +57 -0
  88. data/lib/pry/config.rb +20 -229
  89. data/lib/pry/config/behavior.rb +139 -0
  90. data/lib/pry/config/convenience.rb +26 -0
  91. data/lib/pry/config/default.rb +165 -0
  92. data/lib/pry/core_extensions.rb +59 -38
  93. data/lib/pry/editor.rb +133 -0
  94. data/lib/pry/exceptions.rb +77 -0
  95. data/lib/pry/helpers.rb +1 -0
  96. data/lib/pry/helpers/base_helpers.rb +40 -154
  97. data/lib/pry/helpers/command_helpers.rb +19 -130
  98. data/lib/pry/helpers/documentation_helpers.rb +21 -11
  99. data/lib/pry/helpers/table.rb +109 -0
  100. data/lib/pry/helpers/text.rb +8 -9
  101. data/lib/pry/history.rb +61 -45
  102. data/lib/pry/history_array.rb +11 -1
  103. data/lib/pry/hooks.rb +10 -32
  104. data/lib/pry/indent.rb +110 -38
  105. data/lib/pry/input_completer.rb +242 -0
  106. data/lib/pry/input_lock.rb +132 -0
  107. data/lib/pry/inspector.rb +27 -0
  108. data/lib/pry/last_exception.rb +61 -0
  109. data/lib/pry/method.rb +199 -200
  110. data/lib/pry/method/disowned.rb +53 -0
  111. data/lib/pry/method/patcher.rb +125 -0
  112. data/lib/pry/method/weird_method_locator.rb +186 -0
  113. data/lib/pry/module_candidate.rb +39 -33
  114. data/lib/pry/object_path.rb +82 -0
  115. data/lib/pry/output.rb +50 -0
  116. data/lib/pry/pager.rb +234 -0
  117. data/lib/pry/plugins.rb +4 -3
  118. data/lib/pry/prompt.rb +26 -0
  119. data/lib/pry/pry_class.rb +199 -227
  120. data/lib/pry/pry_instance.rb +344 -403
  121. data/lib/pry/rbx_path.rb +1 -1
  122. data/lib/pry/repl.rb +202 -0
  123. data/lib/pry/repl_file_loader.rb +20 -26
  124. data/lib/pry/rubygem.rb +82 -0
  125. data/lib/pry/terminal.rb +79 -0
  126. data/lib/pry/test/helper.rb +170 -0
  127. data/lib/pry/version.rb +1 -1
  128. data/lib/pry/wrapped_module.rb +133 -48
  129. metadata +132 -197
  130. data/.document +0 -2
  131. data/.gemtest +0 -0
  132. data/.gitignore +0 -16
  133. data/.travis.yml +0 -17
  134. data/.yardopts +0 -1
  135. data/CHANGELOG +0 -387
  136. data/CONTRIBUTORS +0 -36
  137. data/Gemfile +0 -2
  138. data/Rakefile +0 -137
  139. data/TODO +0 -117
  140. data/examples/example_basic.rb +0 -15
  141. data/examples/example_command_override.rb +0 -32
  142. data/examples/example_commands.rb +0 -36
  143. data/examples/example_hooks.rb +0 -9
  144. data/examples/example_image_edit.rb +0 -67
  145. data/examples/example_input.rb +0 -7
  146. data/examples/example_input2.rb +0 -29
  147. data/examples/example_output.rb +0 -11
  148. data/examples/example_print.rb +0 -6
  149. data/examples/example_prompt.rb +0 -9
  150. data/examples/helper.rb +0 -6
  151. data/lib/pry/completion.rb +0 -221
  152. data/lib/pry/custom_completions.rb +0 -6
  153. data/lib/pry/default_commands/cd.rb +0 -81
  154. data/lib/pry/default_commands/commands.rb +0 -62
  155. data/lib/pry/default_commands/context.rb +0 -98
  156. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  157. data/lib/pry/default_commands/editing.rb +0 -420
  158. data/lib/pry/default_commands/find_method.rb +0 -169
  159. data/lib/pry/default_commands/gems.rb +0 -84
  160. data/lib/pry/default_commands/gist.rb +0 -187
  161. data/lib/pry/default_commands/help.rb +0 -127
  162. data/lib/pry/default_commands/hist.rb +0 -120
  163. data/lib/pry/default_commands/input_and_output.rb +0 -306
  164. data/lib/pry/default_commands/introspection.rb +0 -410
  165. data/lib/pry/default_commands/ls.rb +0 -272
  166. data/lib/pry/default_commands/misc.rb +0 -38
  167. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  168. data/lib/pry/default_commands/whereami.rb +0 -92
  169. data/lib/pry/extended_commands/experimental.rb +0 -7
  170. data/lib/pry/rbx_method.rb +0 -13
  171. data/man/pry.1 +0 -195
  172. data/man/pry.1.html +0 -204
  173. data/man/pry.1.ronn +0 -141
  174. data/pry.gemspec +0 -46
  175. data/test/candidate_helper1.rb +0 -11
  176. data/test/candidate_helper2.rb +0 -8
  177. data/test/helper.rb +0 -223
  178. data/test/test_cli.rb +0 -78
  179. data/test/test_code.rb +0 -201
  180. data/test/test_command.rb +0 -712
  181. data/test/test_command_helpers.rb +0 -9
  182. data/test/test_command_integration.rb +0 -668
  183. data/test/test_command_set.rb +0 -610
  184. data/test/test_completion.rb +0 -62
  185. data/test/test_control_d_handler.rb +0 -45
  186. data/test/test_default_commands/example.erb +0 -5
  187. data/test/test_default_commands/test_cd.rb +0 -318
  188. data/test/test_default_commands/test_context.rb +0 -280
  189. data/test/test_default_commands/test_documentation.rb +0 -314
  190. data/test/test_default_commands/test_find_method.rb +0 -50
  191. data/test/test_default_commands/test_gems.rb +0 -18
  192. data/test/test_default_commands/test_help.rb +0 -57
  193. data/test/test_default_commands/test_input.rb +0 -428
  194. data/test/test_default_commands/test_introspection.rb +0 -511
  195. data/test/test_default_commands/test_ls.rb +0 -151
  196. data/test/test_default_commands/test_shell.rb +0 -343
  197. data/test/test_default_commands/test_show_source.rb +0 -432
  198. data/test/test_exception_whitelist.rb +0 -21
  199. data/test/test_history_array.rb +0 -65
  200. data/test/test_hooks.rb +0 -521
  201. data/test/test_indent.rb +0 -277
  202. data/test/test_input_stack.rb +0 -86
  203. data/test/test_method.rb +0 -401
  204. data/test/test_pry.rb +0 -463
  205. data/test/test_pry_defaults.rb +0 -419
  206. data/test/test_pry_history.rb +0 -84
  207. data/test/test_pry_output.rb +0 -41
  208. data/test/test_sticky_locals.rb +0 -155
  209. data/test/test_syntax_checking.rb +0 -65
  210. data/test/test_wrapped_module.rb +0 -174
  211. data/test/testrc +0 -2
  212. data/test/testrcbad +0 -2
  213. data/wiki/Customizing-pry.md +0 -397
  214. 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,100 +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, :input_stack, :extra_sticky_locals
101
- ]
102
-
103
- attributes.each do |attribute|
104
- defaults[attribute] = Pry.send attribute
105
- end
106
-
107
- defaults.merge!(options).each do |key, value|
108
- send("#{key}=", value) if respond_to?("#{key}=")
109
- end
110
-
111
- true
112
- end
113
-
114
- # The currently active `Binding`.
115
- # @return [Binding] The currently active `Binding` for the session.
116
- def current_context
117
- 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)
118
82
  end
119
83
 
120
84
  # The current prompt.
@@ -137,16 +101,70 @@ class Pry
137
101
  end
138
102
  end
139
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
+ #
140
142
  # Injects a local variable into the provided binding.
141
- # @param [String] name The name of the local to inject.
142
- # @param [Object] value The value to set the local to.
143
- # @param [Binding] b The binding to set the local on.
144
- # @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
+ #
145
156
  def inject_local(name, value, b)
146
- Thread.current[:__pry_local__] = value.is_a?(Proc) ? value.call : value
147
- b.eval("#{name} = ::Thread.current[:__pry_local__]")
148
- ensure
149
- Thread.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
150
168
  end
151
169
 
152
170
  # @return [Integer] The maximum amount of objects remembered by the inp and
@@ -160,11 +178,10 @@ class Pry
160
178
  @output_array = Pry::HistoryArray.new(size)
161
179
  end
162
180
 
163
- # Inject all the sticky locals into the `target` binding.
164
- # @param [Binding] target
165
- def inject_sticky_locals(target)
181
+ # Inject all the sticky locals into the current binding.
182
+ def inject_sticky_locals!
166
183
  sticky_locals.each_pair do |name, value|
167
- inject_local(name, value, target)
184
+ inject_local(name, value, current_binding)
168
185
  end
169
186
  end
170
187
 
@@ -174,158 +191,180 @@ class Pry
174
191
  # @yield The block that defines the content of the local. The local
175
192
  # will be refreshed at each tick of the repl loop.
176
193
  def add_sticky_local(name, &block)
177
- sticky_locals[name] = block
194
+ config.extra_sticky_locals[name] = block
178
195
  end
179
196
 
180
- # @return [Hash] The currently defined sticky locals.
181
197
  def sticky_locals
182
- @sticky_locals ||= {
183
- :_in_ => proc { @input_array },
184
- :_out_ => proc { @output_array },
185
- :_pry_ => self,
186
- :_ex_ => proc { last_exception },
187
- :_file_ => proc { last_file },
188
- :_dir_ => proc { last_dir },
189
- :_ => proc { last_result }
190
- }.merge(extra_sticky_locals)
191
- end
192
-
193
- # Initialize the repl session.
194
- # @param [Binding] target The target binding for the session.
195
- def repl_prologue(target)
196
- exec_hook :before_session, output, target, self
197
- set_last_result(nil, target)
198
-
199
-
200
- @input_array << nil # add empty input so _in_ and _out_ match
201
-
202
- binding_stack.push target
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)
203
207
  end
204
208
 
205
- # Clean-up after the repl session.
206
- # @param [Binding] target The target binding for the session.
207
- def repl_epilogue(target)
208
- exec_hook :after_session, output, target, self
209
-
210
- binding_stack.pop
211
- Pry.save_history if Pry.config.history.should_save
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 = ""
212
213
  end
213
214
 
214
- # Start a read-eval-print-loop.
215
- # If no parameter is given, default to top-level (main).
216
- # @param [Object, Binding] target The receiver of the Pry session
217
- # @return [Object] The target of the Pry session or an explictly given
218
- # return value. If given return value is `nil` or no return value
219
- # is specified then `target` will be returned.
220
- # @example
221
- # Pry.new.repl(Object.new)
222
- def repl(target=TOPLEVEL_BINDING)
223
- target = Pry.binding_for(target)
224
-
225
- repl_prologue(target)
226
-
227
- 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
228
239
  exception = catch(:raise_up) do
229
- break_data = catch(:breakout) do
230
- loop do
231
- rep(binding_stack.last)
232
- 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
233
246
  end
234
247
  exception = false
235
248
  end
236
249
 
237
- raise exception if exception
250
+ @stopped = true
251
+ @exit_value = exit_value
238
252
 
239
- break_data
240
- ensure
241
- repl_epilogue(target)
253
+ # TODO: make this configurable?
254
+ raise exception if exception
255
+ return false
242
256
  end
243
257
 
244
- # Perform a read-eval-print.
245
- # If no parameter is given, default to top-level (main).
246
- # @param [Object, Binding] target The receiver of the read-eval-print
247
- # @example
248
- # Pry.new.rep(Object.new)
249
- def rep(target=TOPLEVEL_BINDING)
250
- target = Pry.binding_for(target)
251
- result = re(target)
252
-
253
- show_result(result) if should_print?
254
- end
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
- # Perform a read-eval
257
- # If no parameter is given, default to top-level (main).
258
- # @param [Object, Binding] target The receiver of the read-eval-print
259
- # @return [Object] The result of the eval or an `Exception` object in case of
260
- # error. In the latter case, you can check whether the exception was raised
261
- # or is just the result of the expression using #last_result_is_exception?
262
- # @example
263
- # Pry.new.re(Object.new)
264
- def re(target=TOPLEVEL_BINDING)
265
- target = Pry.binding_for(target)
264
+ ensure_correct_encoding!(line)
265
+ Pry.history << line unless options[:generated]
266
266
 
267
- # It's not actually redundant to inject them continually as we may have
268
- # moved into the scope of a new Binding (e.g the user typed `cd`)
269
- inject_sticky_locals(target)
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
270
276
 
271
- code = r(target)
277
+ Pry.critical_section do
278
+ show_result(result)
279
+ end
280
+ return
281
+ end
272
282
 
273
- exec_hook :before_eval, code, self
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
274
286
 
275
- result = target.eval(code, Pry.eval_path, Pry.current_line)
276
- set_last_result(result, target, code)
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
277
293
 
278
- result
279
- rescue RescuableException => e
280
- self.last_exception = e
281
- e
282
- ensure
283
- update_input_history(code)
284
- exec_hook :after_eval, result, self
285
- end
294
+ if complete_expr
295
+ if @eval_string =~ /;\Z/ || @eval_string.empty? || @eval_string =~ /\A *#.*\n\z/
296
+ @suppress_output = true
297
+ end
286
298
 
287
- # Perform a read.
288
- # If no parameter is given, default to top-level (main).
289
- # This is a multi-line read; so the read continues until a valid
290
- # Ruby expression is received.
291
- # Pry commands are also accepted here and operate on the target.
292
- # @param [Object, Binding] target The receiver of the read.
293
- # @param [String] eval_string Optionally Prime `eval_string` with a start value.
294
- # @return [String] The Ruby expression.
295
- # @example
296
- # Pry.new.r(Object.new)
297
- def r(target=TOPLEVEL_BINDING, eval_string="")
298
- target = Pry.binding_for(target)
299
- @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
300
314
 
301
- loop do
302
315
  begin
303
- # eval_string will probably be mutated by this method
304
- retrieve_line(eval_string, target)
305
- rescue CommandError, Slop::InvalidOptionError, MethodSource::SourceNotFoundError => e
306
- 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
307
331
  end
308
332
 
309
- begin
310
- break if Pry::Code.complete_expression?(eval_string)
311
- rescue SyntaxError => e
312
- output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
313
- eval_string = ""
333
+ Pry.critical_section do
334
+ show_result(result)
314
335
  end
315
336
  end
316
337
 
317
- @suppress_output = true if eval_string =~ /;\Z/ || eval_string.empty?
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
347
+ end
348
+
349
+ def evaluate_ruby(code)
350
+ inject_sticky_locals!
351
+ exec_hook :before_eval, code, self
352
+
353
+ result = current_binding.eval(code, Pry.eval_path, Pry.current_line)
354
+ set_last_result(result, code)
355
+ ensure
356
+ update_input_history(code)
357
+ exec_hook :after_eval, result, self
321
358
  end
322
359
 
323
360
  # Output the result or pass to an exception handler (if result is an exception).
324
361
  def show_result(result)
325
362
  if last_result_is_exception?
326
- exception_handler.call output, result, self
363
+ exception_handler.call(output, result, self)
364
+ elsif should_print?
365
+ print.call(output, result, self)
327
366
  else
328
- print.call output, result
367
+ # nothin'
329
368
  end
330
369
  rescue RescuableException => e
331
370
  # Being uber-paranoid here, given that this exception arose because we couldn't
@@ -340,95 +379,35 @@ class Pry
340
379
  output.puts "(pry) output error: failed to show result"
341
380
  end
342
381
  end
382
+ ensure
383
+ output.flush if output.respond_to?(:flush)
343
384
  end
344
385
 
345
- def should_force_encoding?(eval_string, val)
346
- eval_string.empty? && val.respond_to?(:encoding) && val.encoding != eval_string.encoding
347
- end
348
- private :should_force_encoding?
349
-
350
- # Read and process a line of input -- check for ^D, determine which prompt to
351
- # use, rewrite the indentation if `Pry.config.auto_indent` is enabled, and,
352
- # if the line is a command, process it and alter the eval_string accordingly.
353
- # This method should not need to be invoked directly.
354
- #
355
- # @param [String] eval_string The cumulative lines of input.
356
- # @param [Binding] target The target of the session.
357
- # @return [String] The line received.
358
- def retrieve_line(eval_string, target)
359
- @indent.reset if eval_string.empty?
360
-
361
- current_prompt = select_prompt(eval_string, target)
362
- completion_proc = Pry::InputCompleter.build_completion_proc(target,
363
- instance_eval(&custom_completions))
364
-
365
-
366
- indentation = Pry.config.auto_indent ? @indent.current_prefix : ''
367
-
368
- begin
369
- val = readline("#{current_prompt}#{indentation}", completion_proc)
370
-
371
- # Handle <Ctrl+C> like Bash, empty the current input buffer but do not quit.
372
- # This is only for ruby-1.9; other versions of ruby do not let you send Interrupt
373
- # from within Readline.
374
- rescue Interrupt
375
- output.puts ""
376
- eval_string.replace("")
377
- return
378
- end
379
-
380
- # invoke handler if we receive EOF character (^D)
381
- if !val
382
- output.puts ""
383
- Pry.config.control_d_handler.call(eval_string, self)
384
- return
385
- end
386
-
387
- # Change the eval_string into the input encoding (Issue 284)
388
- # TODO: This wouldn't be necessary if the eval_string was constructed from
389
- # input strings only.
390
- if should_force_encoding?(eval_string, val)
391
- eval_string.force_encoding(val.encoding)
392
- end
393
-
394
- if Pry.config.auto_indent && !input.is_a?(StringIO)
395
- original_val = "#{indentation}#{val}"
396
- indented_val = @indent.indent(val)
397
-
398
- if output.tty? && Pry::Helpers::BaseHelpers.use_ansi_codes? && Pry.config.correct_indent
399
- output.print @indent.correct_indentation(current_prompt, indented_val, original_val.length - indented_val.length)
400
- output.flush
401
- end
402
- else
403
- indented_val = val
404
- end
405
-
406
- begin
407
- if !process_command(val, eval_string, target)
408
- eval_string << "#{indented_val.rstrip}\n" unless val.empty?
409
- end
410
- ensure
411
- Pry.history << indented_val unless input.is_a?(StringIO)
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)
412
392
  end
413
393
  end
394
+ private :ensure_correct_encoding!
414
395
 
415
396
  # If the given line is a valid command, process it in the context of the
416
- # current `eval_string` and context.
417
- # This method should not need to be invoked directly.
397
+ # current `eval_string` and binding.
418
398
  # @param [String] val The line to process.
419
- # @param [String] eval_string The cumulative lines of input.
420
- # @param [Binding] target The target of the Pry session.
421
399
  # @return [Boolean] `true` if `val` is a command, `false` otherwise
422
- def process_command(val, eval_string, target)
423
- result = commands.process_line(val, {
424
- :target => target,
400
+ def process_command(val)
401
+ val = val.chomp
402
+ result = commands.process_line(val,
403
+ :target => current_binding,
425
404
  :output => output,
426
- :eval_string => eval_string,
405
+ :eval_string => @eval_string,
427
406
  :pry_instance => self
428
- })
407
+ )
429
408
 
430
409
  # set a temporary (just so we can inject the value we want into eval_string)
431
- Thread.current[:__pry_cmd_result__] = result
410
+ Pry.current[:pry_cmd_result] = result
432
411
 
433
412
  # note that `result` wraps the result of command processing; if a
434
413
  # command was matched and invoked then `result.command?` returns true,
@@ -438,7 +417,7 @@ class Pry
438
417
  # the command that was invoked was non-void (had a return value) and so we make
439
418
  # the value of the current expression equal to the return value
440
419
  # of the command.
441
- eval_string.replace "Thread.current[:__pry_cmd_result__].retval\n"
420
+ @eval_string.replace "::Pry.current[:pry_cmd_result].retval\n"
442
421
  end
443
422
  true
444
423
  else
@@ -446,17 +425,27 @@ class Pry
446
425
  end
447
426
  end
448
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
+
449
440
  # Run the specified command.
450
441
  # @param [String] val The command (and its params) to execute.
451
- # @param [String] eval_string The current input buffer.
452
- # @param [Binding] target The binding to use..
453
442
  # @return [Pry::Command::VOID_VALUE]
454
443
  # @example
455
444
  # pry_instance.run_command("ls -m")
456
- def run_command(val, eval_string = "", target = binding_stack.last)
445
+ def run_command(val)
457
446
  commands.process_line(val,
458
- :eval_string => eval_string,
459
- :target => target,
447
+ :eval_string => @eval_string,
448
+ :target => current_binding,
460
449
  :pry_instance => self,
461
450
  :output => output
462
451
  )
@@ -485,36 +474,25 @@ class Pry
485
474
  # Set the last result of an eval.
486
475
  # This method should not need to be invoked directly.
487
476
  # @param [Object] result The result.
488
- # @param [Binding] target The binding to set `_` on.
489
477
  # @param [String] code The code that was run.
490
- def set_last_result(result, target, code="")
478
+ def set_last_result(result, code="")
491
479
  @last_result_is_exception = false
492
480
  @output_array << result
493
481
 
494
482
  self.last_result = result unless code =~ /\A\s*\z/
495
483
  end
496
484
 
485
+ #
497
486
  # Set the last exception for a session.
498
- # @param [Exception] ex
499
- def last_exception=(ex)
500
- class << ex
501
- attr_accessor :file, :line, :bt_index
502
- def bt_source_location_for(index)
503
- backtrace[index] =~ /(.*):(\d+)/
504
- [$1, $2.to_i]
505
- end
506
-
507
- def inc_bt_index
508
- @bt_index = (@bt_index + 1) % backtrace.size
509
- end
510
- end
511
-
512
- ex.bt_index = 0
513
- ex.file, ex.line = ex.bt_source_location_for(0)
514
-
487
+ #
488
+ # @param [Exception] e
489
+ # the last exception.
490
+ #
491
+ def last_exception=(e)
492
+ last_exception = Pry::LastException.new(e)
515
493
  @last_result_is_exception = true
516
- @output_array << ex
517
- @last_exception = ex
494
+ @output_array << last_exception
495
+ @last_exception = last_exception
518
496
  end
519
497
 
520
498
  # Update Pry's internal state after evalling code.
@@ -536,105 +514,54 @@ class Pry
536
514
  @last_result_is_exception
537
515
  end
538
516
 
539
- # Manage switching of input objects on encountering EOFErrors
540
- def handle_read_errors
541
- should_retry = true
542
- exception_count = 0
543
- begin
544
- yield
545
- rescue EOFError
546
- if input_stack.empty?
547
- self.input = Pry.config.input
548
- if !should_retry
549
- output.puts "Error: Pry ran out of things to read from! Attempting to break out of REPL."
550
- throw(:breakout)
551
- end
552
- should_retry = false
553
- else
554
- self.input = input_stack.pop
555
- end
556
-
557
- retry
558
-
559
- # Interrupts are handled in r() because they need to tweak eval_string
560
- # TODO: Refactor this baby.
561
- rescue Interrupt
562
- raise
563
-
564
- # If we get a random error when trying to read a line we don't want to automatically
565
- # retry, as the user will see a lot of error messages scroll past and be unable to do
566
- # anything about it.
567
- rescue RescuableException => e
568
- puts "Error: #{e.message}"
569
- output.puts e.backtrace
570
- exception_count += 1
571
- if exception_count < 5
572
- retry
573
- end
574
- puts "FATAL: Pry failed to get user input using `#{input}`."
575
- puts "To fix this you may be able to pass input and output file descriptors to pry directly. e.g."
576
- puts " Pry.config.input = STDIN"
577
- puts " Pry.config.output = STDOUT"
578
- puts " binding.pry"
579
- throw(:breakout)
580
- end
581
- end
582
- private :handle_read_errors
583
-
584
- # Returns the next line of input to be used by the pry instance.
585
- # This method should not need to be invoked directly.
586
- # @param [String] current_prompt The prompt to use for input.
587
- # @return [String] The next line of input.
588
- def readline(current_prompt="> ", completion_proc=nil)
589
- handle_read_errors do
590
-
591
- if defined? Coolline and input.is_a? Coolline
592
- input.completion_proc = proc do |cool|
593
- completion_proc.call cool.completed_word
594
- end
595
- elsif input.respond_to? :completion_proc=
596
- input.completion_proc = completion_proc
597
- end
598
-
599
- if input == Readline
600
- input.readline(current_prompt, false) # false since we'll add it manually
601
- elsif defined? Coolline and input.is_a? Coolline
602
- input.readline(current_prompt)
603
- else
604
- if input.method(:readline).arity == 1
605
- input.readline(current_prompt)
606
- else
607
- input.readline
608
- end
609
- end
610
- end
611
- end
612
-
613
517
  # Whether the print proc should be invoked.
614
- # Currently only invoked if the output is not suppressed OR the last result
615
- # is an exception regardless of suppression.
518
+ # Currently only invoked if the output is not suppressed.
616
519
  # @return [Boolean] Whether the print proc should be invoked.
617
520
  def should_print?
618
- !@suppress_output || last_result_is_exception?
521
+ !@suppress_output
619
522
  end
620
523
 
621
524
  # Returns the appropriate prompt to use.
622
- # This method should not need to be invoked directly.
623
- # @param [String] eval_string The current input buffer.
624
- # @param [Binding] target The target Binding of the Pry session.
625
525
  # @return [String] The prompt.
626
- def select_prompt(eval_string, target)
627
- target_self = target.eval('self')
628
-
629
- # If input buffer is empty then use normal prompt
630
- if eval_string.empty?
631
- Array(prompt).first.call(target_self, binding_stack.size - 1, self)
526
+ def select_prompt
527
+ object = current_binding.eval('self')
528
+
529
+ open_token = @indent.open_delimiters.any? ? @indent.open_delimiters.last :
530
+ @indent.stack.last
531
+
532
+ c = Pry::Config.from_hash({
533
+ :object => object,
534
+ :nesting_level => binding_stack.size - 1,
535
+ :open_token => open_token,
536
+ :session_line => Pry.history.session_line_count + 1,
537
+ :history_line => Pry.history.history_line_count + 1,
538
+ :expr_number => input_array.count,
539
+ :_pry_ => self,
540
+ :binding_stack => binding_stack,
541
+ :input_array => input_array,
542
+ :eval_string => @eval_string,
543
+ :cont => !@eval_string.empty?})
544
+
545
+ Pry.critical_section do
546
+ # If input buffer is empty then use normal prompt
547
+ if eval_string.empty?
548
+ generate_prompt(Array(prompt).first, c)
549
+
550
+ # Otherwise use the wait prompt (indicating multi-line expression)
551
+ else
552
+ generate_prompt(Array(prompt).last, c)
553
+ end
554
+ end
555
+ end
632
556
 
633
- # Otherwise use the wait prompt (indicating multi-line expression)
557
+ def generate_prompt(prompt_proc, conf)
558
+ if prompt_proc.arity == 1
559
+ prompt_proc.call(conf)
634
560
  else
635
- Array(prompt).last.call(target_self, binding_stack.size - 1, self)
561
+ prompt_proc.call(conf.object, conf.nesting_level, conf._pry_)
636
562
  end
637
563
  end
564
+ private :generate_prompt
638
565
 
639
566
  # the array that the prompt stack is stored in
640
567
  def prompt_stack
@@ -669,6 +596,20 @@ class Pry
669
596
  prompt_stack.size > 1 ? prompt_stack.pop : prompt
670
597
  end
671
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
+
672
613
  # Raise an exception out of Pry.
673
614
  #
674
615
  # See Kernel#raise for documentation of parameters.