pry 0.9.12.6 → 0.10.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +702 -0
- data/LICENSE +2 -2
- data/{README.markdown → README.md} +37 -31
- data/lib/pry.rb +38 -151
- data/lib/pry/cli.rb +35 -17
- data/lib/pry/code.rb +24 -63
- data/lib/pry/code/code_file.rb +103 -0
- data/lib/pry/code/code_range.rb +2 -1
- data/lib/pry/code/loc.rb +2 -2
- data/lib/pry/code_object.rb +40 -21
- data/lib/pry/color_printer.rb +55 -0
- data/lib/pry/command.rb +12 -9
- data/lib/pry/command_set.rb +81 -38
- data/lib/pry/commands.rb +1 -1
- data/lib/pry/commands/amend_line.rb +2 -2
- data/lib/pry/commands/bang.rb +1 -1
- data/lib/pry/commands/cat.rb +11 -2
- data/lib/pry/commands/cat/exception_formatter.rb +5 -6
- data/lib/pry/commands/cat/file_formatter.rb +15 -32
- data/lib/pry/commands/cd.rb +14 -3
- data/lib/pry/commands/change_inspector.rb +27 -0
- data/lib/pry/commands/change_prompt.rb +26 -0
- data/lib/pry/commands/code_collector.rb +4 -4
- data/lib/pry/commands/easter_eggs.rb +3 -3
- data/lib/pry/commands/edit.rb +10 -22
- data/lib/pry/commands/edit/exception_patcher.rb +2 -2
- data/lib/pry/commands/edit/file_and_line_locator.rb +0 -2
- data/lib/pry/commands/exit_program.rb +0 -1
- data/lib/pry/commands/find_method.rb +16 -22
- data/lib/pry/commands/gem_install.rb +5 -2
- data/lib/pry/commands/gem_open.rb +1 -1
- data/lib/pry/commands/gist.rb +10 -11
- data/lib/pry/commands/help.rb +14 -14
- data/lib/pry/commands/hist.rb +27 -8
- data/lib/pry/commands/install_command.rb +14 -12
- data/lib/pry/commands/list_inspectors.rb +35 -0
- data/lib/pry/commands/list_prompts.rb +35 -0
- data/lib/pry/commands/ls.rb +72 -296
- data/lib/pry/commands/ls/constants.rb +47 -0
- data/lib/pry/commands/ls/formatter.rb +49 -0
- data/lib/pry/commands/ls/globals.rb +48 -0
- data/lib/pry/commands/ls/grep.rb +21 -0
- data/lib/pry/commands/ls/instance_vars.rb +39 -0
- data/lib/pry/commands/ls/interrogatable.rb +18 -0
- data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
- data/lib/pry/commands/ls/local_names.rb +35 -0
- data/lib/pry/commands/ls/local_vars.rb +39 -0
- data/lib/pry/commands/ls/ls_entity.rb +70 -0
- data/lib/pry/commands/ls/methods.rb +57 -0
- data/lib/pry/commands/ls/methods_helper.rb +46 -0
- data/lib/pry/commands/ls/self_methods.rb +32 -0
- data/lib/pry/commands/play.rb +44 -10
- data/lib/pry/commands/pry_backtrace.rb +1 -2
- data/lib/pry/commands/raise_up.rb +2 -2
- data/lib/pry/commands/reload_code.rb +16 -19
- data/lib/pry/commands/ri.rb +7 -3
- data/lib/pry/commands/shell_command.rb +18 -13
- data/lib/pry/commands/shell_mode.rb +2 -4
- data/lib/pry/commands/show_doc.rb +5 -0
- data/lib/pry/commands/show_info.rb +8 -13
- data/lib/pry/commands/show_source.rb +15 -3
- data/lib/pry/commands/simple_prompt.rb +1 -1
- data/lib/pry/commands/toggle_color.rb +8 -4
- data/lib/pry/commands/watch_expression.rb +105 -0
- data/lib/pry/commands/watch_expression/expression.rb +38 -0
- data/lib/pry/commands/whereami.rb +18 -10
- data/lib/pry/commands/wtf.rb +3 -3
- data/lib/pry/config.rb +20 -254
- data/lib/pry/config/behavior.rb +139 -0
- data/lib/pry/config/convenience.rb +26 -0
- data/lib/pry/config/default.rb +165 -0
- data/lib/pry/core_extensions.rb +31 -21
- data/lib/pry/editor.rb +107 -103
- data/lib/pry/exceptions.rb +77 -0
- data/lib/pry/helpers/base_helpers.rb +22 -109
- data/lib/pry/helpers/command_helpers.rb +10 -8
- data/lib/pry/helpers/documentation_helpers.rb +1 -2
- data/lib/pry/helpers/text.rb +4 -5
- data/lib/pry/history.rb +46 -45
- data/lib/pry/history_array.rb +6 -1
- data/lib/pry/hooks.rb +9 -29
- data/lib/pry/indent.rb +6 -6
- data/lib/pry/input_completer.rb +242 -0
- data/lib/pry/input_lock.rb +132 -0
- data/lib/pry/inspector.rb +27 -0
- data/lib/pry/last_exception.rb +61 -0
- data/lib/pry/method.rb +82 -87
- data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +41 -38
- data/lib/pry/module_candidate.rb +4 -14
- data/lib/pry/object_path.rb +82 -0
- data/lib/pry/output.rb +50 -0
- data/lib/pry/pager.rb +191 -47
- data/lib/pry/plugins.rb +1 -1
- data/lib/pry/prompt.rb +26 -0
- data/lib/pry/pry_class.rb +149 -230
- data/lib/pry/pry_instance.rb +302 -413
- data/lib/pry/rbx_path.rb +1 -1
- data/lib/pry/repl.rb +202 -0
- data/lib/pry/repl_file_loader.rb +20 -26
- data/lib/pry/rubygem.rb +13 -5
- data/lib/pry/terminal.rb +2 -1
- data/lib/pry/test/helper.rb +26 -41
- data/lib/pry/version.rb +1 -1
- data/lib/pry/wrapped_module.rb +45 -59
- metadata +61 -224
- data/.document +0 -2
- data/.gitignore +0 -16
- data/.travis.yml +0 -25
- data/.yardopts +0 -1
- data/CHANGELOG +0 -534
- data/CONTRIBUTORS +0 -55
- data/Gemfile +0 -12
- data/Rakefile +0 -140
- data/TODO +0 -117
- data/lib/pry/completion.rb +0 -321
- data/lib/pry/custom_completions.rb +0 -6
- data/lib/pry/rbx_method.rb +0 -13
- data/man/pry.1 +0 -195
- data/man/pry.1.html +0 -204
- data/man/pry.1.ronn +0 -141
- data/pry.gemspec +0 -29
- data/spec/Procfile +0 -3
- data/spec/cli_spec.rb +0 -78
- data/spec/code_object_spec.rb +0 -277
- data/spec/code_spec.rb +0 -219
- data/spec/command_helpers_spec.rb +0 -29
- data/spec/command_integration_spec.rb +0 -644
- data/spec/command_set_spec.rb +0 -627
- data/spec/command_spec.rb +0 -821
- data/spec/commands/amend_line_spec.rb +0 -247
- data/spec/commands/bang_spec.rb +0 -19
- data/spec/commands/cat_spec.rb +0 -164
- data/spec/commands/cd_spec.rb +0 -250
- data/spec/commands/disable_pry_spec.rb +0 -25
- data/spec/commands/edit_spec.rb +0 -727
- data/spec/commands/exit_all_spec.rb +0 -34
- data/spec/commands/exit_program_spec.rb +0 -19
- data/spec/commands/exit_spec.rb +0 -34
- data/spec/commands/find_method_spec.rb +0 -70
- data/spec/commands/gem_list_spec.rb +0 -26
- data/spec/commands/gist_spec.rb +0 -79
- data/spec/commands/help_spec.rb +0 -56
- data/spec/commands/hist_spec.rb +0 -181
- data/spec/commands/jump_to_spec.rb +0 -15
- data/spec/commands/ls_spec.rb +0 -181
- data/spec/commands/play_spec.rb +0 -140
- data/spec/commands/raise_up_spec.rb +0 -56
- data/spec/commands/save_file_spec.rb +0 -177
- data/spec/commands/show_doc_spec.rb +0 -510
- data/spec/commands/show_input_spec.rb +0 -17
- data/spec/commands/show_source_spec.rb +0 -782
- data/spec/commands/whereami_spec.rb +0 -203
- data/spec/completion_spec.rb +0 -241
- data/spec/control_d_handler_spec.rb +0 -58
- data/spec/documentation_helper_spec.rb +0 -73
- data/spec/editor_spec.rb +0 -79
- data/spec/exception_whitelist_spec.rb +0 -21
- data/spec/fixtures/candidate_helper1.rb +0 -11
- data/spec/fixtures/candidate_helper2.rb +0 -8
- data/spec/fixtures/example.erb +0 -5
- data/spec/fixtures/example_nesting.rb +0 -33
- data/spec/fixtures/show_source_doc_examples.rb +0 -15
- data/spec/fixtures/testrc +0 -2
- data/spec/fixtures/testrcbad +0 -2
- data/spec/fixtures/whereami_helper.rb +0 -6
- data/spec/helper.rb +0 -34
- data/spec/helpers/bacon.rb +0 -86
- data/spec/helpers/mock_pry.rb +0 -43
- data/spec/helpers/table_spec.rb +0 -105
- data/spec/history_array_spec.rb +0 -67
- data/spec/hooks_spec.rb +0 -522
- data/spec/indent_spec.rb +0 -301
- data/spec/input_stack_spec.rb +0 -90
- data/spec/method_spec.rb +0 -482
- data/spec/prompt_spec.rb +0 -60
- data/spec/pry_defaults_spec.rb +0 -419
- data/spec/pry_history_spec.rb +0 -99
- data/spec/pry_output_spec.rb +0 -95
- data/spec/pry_spec.rb +0 -515
- data/spec/run_command_spec.rb +0 -25
- data/spec/sticky_locals_spec.rb +0 -157
- data/spec/syntax_checking_spec.rb +0 -81
- data/spec/wrapped_module_spec.rb +0 -261
- data/wiki/Customizing-pry.md +0 -397
- data/wiki/Home.md +0 -4
data/lib/pry/pry_instance.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
#
|
58
|
-
#
|
59
|
-
|
60
|
-
|
61
|
-
#
|
62
|
-
#
|
63
|
-
# @
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
-
#
|
144
|
-
# @param [
|
145
|
-
#
|
146
|
-
#
|
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
|
-
|
149
|
-
b.
|
150
|
-
|
151
|
-
|
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
|
166
|
-
|
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,
|
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
|
-
|
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
|
-
|
185
|
-
:
|
186
|
-
:
|
187
|
-
:
|
188
|
-
:
|
189
|
-
:
|
190
|
-
:
|
191
|
-
:
|
192
|
-
|
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
|
-
#
|
197
|
-
#
|
198
|
-
def
|
199
|
-
|
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
|
-
#
|
208
|
-
#
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
#
|
217
|
-
#
|
218
|
-
#
|
219
|
-
#
|
220
|
-
#
|
221
|
-
#
|
222
|
-
# @
|
223
|
-
#
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
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
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
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
|
-
|
250
|
+
@stopped = true
|
251
|
+
@exit_value = exit_value
|
241
252
|
|
242
|
-
|
243
|
-
|
244
|
-
|
253
|
+
# TODO: make this configurable?
|
254
|
+
raise exception if exception
|
255
|
+
return false
|
245
256
|
end
|
246
257
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
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
|
-
|
257
|
-
|
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
|
-
|
262
|
-
|
263
|
-
|
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
|
-
|
273
|
-
|
274
|
-
|
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
|
-
|
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
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
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
|
-
#
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
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
|
-
|
308
|
-
|
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
|
316
|
-
|
317
|
-
|
338
|
+
throw(:breakout) if current_binding.nil?
|
339
|
+
end
|
340
|
+
private :handle_line
|
318
341
|
|
319
|
-
|
320
|
-
|
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
|
324
|
-
|
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 =
|
329
|
-
set_last_result(result,
|
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
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
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
|
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
|
400
|
+
def process_command(val)
|
444
401
|
val = val.chomp
|
445
|
-
result = commands.process_line(val,
|
446
|
-
: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
|
445
|
+
def run_command(val)
|
479
446
|
commands.process_line(val,
|
480
|
-
:eval_string => eval_string,
|
481
|
-
: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,
|
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
|
-
#
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
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 <<
|
539
|
-
@last_exception =
|
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
|
652
|
-
|
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 =
|
658
|
-
:object =>
|
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 =>
|
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.
|