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