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/hooks.rb
CHANGED
@@ -1,37 +1,33 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
# provided by Pry, these
|
6
|
-
# A hook must
|
7
|
-
# `Pry::Hooks#add_hook`
|
3
|
+
class Pry
|
4
|
+
# Implements a hooks system for Pry. A hook is a callable that is associated
|
5
|
+
# with an event. A number of events are currently provided by Pry, these
|
6
|
+
# include: `:when_started`, `:before_session`, `:after_session`. A hook must
|
7
|
+
# have a name, and is connected with an event by the `Pry::Hooks#add_hook`
|
8
|
+
# method.
|
9
|
+
#
|
8
10
|
# @example Adding a hook for the `:before_session` event.
|
9
11
|
# Pry.config.hooks.add_hook(:before_session, :say_hi) do
|
10
12
|
# puts "hello"
|
11
13
|
# end
|
12
14
|
class Hooks
|
15
|
+
def self.default
|
16
|
+
hooks = new
|
17
|
+
hooks.add_hook(:before_session, :default) do |_out, _target, pry_instance|
|
18
|
+
next if pry_instance.quiet?
|
13
19
|
|
14
|
-
|
15
|
-
# this way are anonymous. This functionality is primarily to
|
16
|
-
# provide backwards-compatibility with the old hash-based hook
|
17
|
-
# system in Pry versions < 0.9.8
|
18
|
-
# @param [Hash] hash The hash to convert to `Pry::Hooks`.
|
19
|
-
# @return [Pry::Hooks] The resulting `Pry::Hooks` instance.
|
20
|
-
def self.from_hash(hash)
|
21
|
-
instance = new
|
22
|
-
hash.each do |k, v|
|
23
|
-
instance.add_hook(k, nil, v)
|
20
|
+
pry_instance.run_command('whereami --quiet')
|
24
21
|
end
|
25
|
-
|
26
|
-
instance
|
22
|
+
hooks
|
27
23
|
end
|
28
24
|
|
29
25
|
def initialize
|
30
|
-
@hooks = {}
|
26
|
+
@hooks = Hash.new { |h, k| h[k] = [] }
|
31
27
|
end
|
32
28
|
|
33
|
-
# Ensure that duplicates have their @hooks object
|
34
|
-
def initialize_copy(
|
29
|
+
# Ensure that duplicates have their @hooks object.
|
30
|
+
def initialize_copy(_orig)
|
35
31
|
hooks_dup = @hooks.dup
|
36
32
|
@hooks.each do |k, v|
|
37
33
|
hooks_dup[k] = v.dup
|
@@ -40,79 +36,50 @@ class Pry
|
|
40
36
|
@hooks = hooks_dup
|
41
37
|
end
|
42
38
|
|
43
|
-
def hooks
|
44
|
-
@hooks
|
45
|
-
end
|
46
|
-
protected :hooks
|
47
|
-
|
48
39
|
def errors
|
49
40
|
@errors ||= []
|
50
41
|
end
|
51
42
|
|
52
|
-
# FIXME:
|
53
|
-
# This is a hack to alert people of the new API.
|
54
|
-
def [](event_name)
|
55
|
-
warn "`Pry.hooks[]` is deprecated! Please use the new `Pry::Hooks` API! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
|
56
|
-
|
57
|
-
get_hook(event_name, nil)
|
58
|
-
end
|
59
|
-
|
60
|
-
# FIXME:
|
61
|
-
# This is a hack to alert people of the new API.
|
62
|
-
def []=(event_name, callable)
|
63
|
-
warn "`Pry.hooks[]=` is deprecated! Please use the new `Pry::Hooks` API! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
|
64
|
-
|
65
|
-
add_hook(event_name, nil, callable)
|
66
|
-
end
|
67
|
-
|
68
43
|
# Destructively merge the contents of two `Pry:Hooks` instances.
|
44
|
+
#
|
69
45
|
# @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
|
70
|
-
# @return [Pry:Hooks]
|
71
|
-
# @
|
72
|
-
# hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
|
73
|
-
# Pry::Hooks.new.merge!(hooks)
|
46
|
+
# @return [Pry:Hooks] The receiver.
|
47
|
+
# @see #merge
|
74
48
|
def merge!(other)
|
75
|
-
@hooks.merge!(other.dup.hooks) do |
|
76
|
-
|
77
|
-
|
49
|
+
@hooks.merge!(other.dup.hooks) do |_key, array, other_array|
|
50
|
+
temp_hash = {}
|
51
|
+
output = []
|
78
52
|
|
79
|
-
|
80
|
-
|
53
|
+
(array + other_array).reverse_each do |pair|
|
54
|
+
temp_hash[pair.first] ||= output.unshift(pair)
|
55
|
+
end
|
81
56
|
|
82
|
-
|
83
|
-
|
84
|
-
end
|
85
|
-
private :merge_arrays
|
57
|
+
output
|
58
|
+
end
|
86
59
|
|
87
|
-
|
88
|
-
hash, output = {}, []
|
89
|
-
input.reverse.each{ |i| hash[block[i]] ||= (output.unshift i) }
|
90
|
-
output
|
60
|
+
self
|
91
61
|
end
|
92
|
-
private :uniq_keeping_last
|
93
62
|
|
94
|
-
# Return a new `Pry::Hooks` instance containing a merge of the contents of two `Pry:Hooks` instances,
|
95
|
-
# @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
|
96
|
-
# @return [Pry::Hooks] The new hash.
|
97
63
|
# @example
|
98
64
|
# hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
|
99
65
|
# Pry::Hooks.new.merge(hooks)
|
66
|
+
# @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
|
67
|
+
# @return [Pry::Hooks] a new `Pry::Hooks` instance containing a merge of the
|
68
|
+
# contents of two `Pry:Hooks` instances.
|
100
69
|
def merge(other)
|
101
|
-
|
70
|
+
dup.tap do |v|
|
102
71
|
v.merge!(other)
|
103
72
|
end
|
104
73
|
end
|
105
74
|
|
106
|
-
# Add a new hook to be executed for the `
|
75
|
+
# Add a new hook to be executed for the `event_name` event.
|
107
76
|
# @param [Symbol] event_name The name of the event.
|
108
77
|
# @param [Symbol] hook_name The name of the hook.
|
109
78
|
# @param [#call] callable The callable.
|
110
|
-
# @yield The block to use as the callable (if `callable`
|
111
|
-
# @return [Pry:Hooks]
|
112
|
-
|
113
|
-
|
114
|
-
def add_hook(event_name, hook_name, callable=nil, &block)
|
115
|
-
@hooks[event_name] ||= []
|
79
|
+
# @yield The block to use as the callable (if no `callable` provided).
|
80
|
+
# @return [Pry:Hooks] The receiver.
|
81
|
+
def add_hook(event_name, hook_name, callable = nil, &block)
|
82
|
+
event_name = event_name.to_s
|
116
83
|
|
117
84
|
# do not allow duplicates, but allow multiple `nil` hooks
|
118
85
|
# (anonymous hooks)
|
@@ -120,12 +87,10 @@ class Pry
|
|
120
87
|
raise ArgumentError, "Hook with name '#{hook_name}' already defined!"
|
121
88
|
end
|
122
89
|
|
123
|
-
if !block && !callable
|
124
|
-
raise ArgumentError, "Must provide a block or callable."
|
125
|
-
end
|
90
|
+
raise ArgumentError, "Must provide a block or callable." if !block && !callable
|
126
91
|
|
127
92
|
# ensure we only have one anonymous hook
|
128
|
-
@hooks[event_name].delete_if { |h,
|
93
|
+
@hooks[event_name].delete_if { |h, _k| h.nil? } if hook_name.nil?
|
129
94
|
|
130
95
|
if block
|
131
96
|
@hooks[event_name] << [hook_name, block]
|
@@ -140,76 +105,49 @@ class Pry
|
|
140
105
|
# @param [Symbol] event_name The name of the event.
|
141
106
|
# @param [Array] args The arguments to pass to each hook function.
|
142
107
|
# @return [Object] The return value of the last executed hook.
|
143
|
-
# @example
|
144
|
-
# my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
|
145
|
-
# my_hooks.exec_hook(:before_session) #=> OUTPUT: "hi!"
|
146
108
|
def exec_hook(event_name, *args, &block)
|
147
|
-
@hooks[event_name]
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
rescue RescuableException => e
|
156
|
-
errors << e
|
157
|
-
e
|
158
|
-
end
|
159
|
-
end.last
|
160
|
-
end
|
109
|
+
@hooks[event_name.to_s].map do |_hook_name, callable|
|
110
|
+
begin
|
111
|
+
callable.call(*args, &block)
|
112
|
+
rescue RescuableException => e
|
113
|
+
errors << e
|
114
|
+
e
|
115
|
+
end
|
116
|
+
end.last
|
161
117
|
end
|
162
118
|
|
163
|
-
# Return the number of hook functions registered for the `event_name` event.
|
164
119
|
# @param [Symbol] event_name The name of the event.
|
165
120
|
# @return [Fixnum] The number of hook functions for `event_name`.
|
166
|
-
# @example
|
167
|
-
# my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
|
168
|
-
# my_hooks.count(:before_session) #=> 1
|
169
121
|
def hook_count(event_name)
|
170
|
-
@hooks[event_name]
|
171
|
-
@hooks[event_name].size
|
122
|
+
@hooks[event_name.to_s].size
|
172
123
|
end
|
173
124
|
|
174
|
-
# Return a specific hook for a given event.
|
175
125
|
# @param [Symbol] event_name The name of the event.
|
176
126
|
# @param [Symbol] hook_name The name of the hook
|
177
|
-
# @return [#call]
|
178
|
-
# @example
|
179
|
-
# my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
|
180
|
-
# my_hooks.get_hook(:before_session, :say_hi).call #=> "hi!"
|
127
|
+
# @return [#call] a specific hook for a given event.
|
181
128
|
def get_hook(event_name, hook_name)
|
182
|
-
@hooks[event_name]
|
183
|
-
|
129
|
+
hook = @hooks[event_name.to_s].find do |current_hook_name, _callable|
|
130
|
+
current_hook_name == hook_name
|
131
|
+
end
|
184
132
|
hook.last if hook
|
185
133
|
end
|
186
134
|
|
187
|
-
# Return the hash of hook names / hook functions for a
|
188
|
-
# given event. (Note that modifying the returned hash does not
|
189
|
-
# alter the hooks, use add_hook/delete_hook for that).
|
190
135
|
# @param [Symbol] event_name The name of the event.
|
191
136
|
# @return [Hash] The hash of hook names / hook functions.
|
192
|
-
# @
|
193
|
-
#
|
194
|
-
# my_hooks.get_hooks(:before_session) #=> {:say_hi=>#<Proc:0x00000101645e18@(pry):9>}
|
137
|
+
# @note Modifying the returned hash does not alter the hooks, use
|
138
|
+
# `add_hook`/`delete_hook` for that.
|
195
139
|
def get_hooks(event_name)
|
196
|
-
@hooks[event_name]
|
197
|
-
Hash[@hooks[event_name]]
|
140
|
+
Hash[@hooks[event_name.to_s]]
|
198
141
|
end
|
199
142
|
|
200
|
-
# Delete a hook for an event.
|
201
143
|
# @param [Symbol] event_name The name of the event.
|
202
144
|
# @param [Symbol] hook_name The name of the hook.
|
203
145
|
# to delete.
|
204
146
|
# @return [#call] The deleted hook.
|
205
|
-
# @example
|
206
|
-
# my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
|
207
|
-
# my_hooks.delete_hook(:before_session, :say_hi)
|
208
147
|
def delete_hook(event_name, hook_name)
|
209
|
-
@hooks[event_name] ||= []
|
210
148
|
deleted_callable = nil
|
211
149
|
|
212
|
-
@hooks[event_name].delete_if do |current_hook_name, callable|
|
150
|
+
@hooks[event_name.to_s].delete_if do |current_hook_name, callable|
|
213
151
|
if current_hook_name == hook_name
|
214
152
|
deleted_callable = callable
|
215
153
|
true
|
@@ -221,30 +159,22 @@ class Pry
|
|
221
159
|
end
|
222
160
|
|
223
161
|
# Clear all hooks functions for a given event.
|
162
|
+
#
|
224
163
|
# @param [String] event_name The name of the event.
|
225
|
-
# @
|
226
|
-
|
227
|
-
|
228
|
-
def delete_hooks(event_name)
|
229
|
-
@hooks[event_name] = []
|
230
|
-
end
|
231
|
-
|
232
|
-
alias_method :clear, :delete_hooks
|
233
|
-
|
234
|
-
# Remove all events and hooks, clearing out the Pry::Hooks
|
235
|
-
# instance completely.
|
236
|
-
# @example
|
237
|
-
# my_hooks = Pry::Hooks.new.add_hook(:before_session, :say_hi) { puts "hi!" }
|
238
|
-
# my_hooks.clear_all
|
239
|
-
def clear_all
|
240
|
-
@hooks = {}
|
164
|
+
# @return [void]
|
165
|
+
def clear_event_hooks(event_name)
|
166
|
+
@hooks[event_name.to_s] = []
|
241
167
|
end
|
242
168
|
|
243
169
|
# @param [Symbol] event_name Name of the event.
|
244
170
|
# @param [Symbol] hook_name Name of the hook.
|
245
|
-
# @return [Boolean] Whether the hook by the name `hook_name
|
171
|
+
# @return [Boolean] Whether the hook by the name `hook_name`.
|
246
172
|
def hook_exists?(event_name, hook_name)
|
247
|
-
|
173
|
+
@hooks[event_name.to_s].map(&:first).include?(hook_name)
|
248
174
|
end
|
175
|
+
|
176
|
+
protected
|
177
|
+
|
178
|
+
attr_reader :hooks
|
249
179
|
end
|
250
180
|
end
|
data/lib/pry/indent.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Pry
|
4
4
|
##
|
@@ -21,35 +21,35 @@ class Pry
|
|
21
21
|
attr_reader :stack
|
22
22
|
|
23
23
|
# The amount of spaces to insert for each indent level.
|
24
|
-
SPACES = ' '
|
24
|
+
SPACES = ' '.freeze
|
25
25
|
|
26
26
|
# Hash containing all the tokens that should increase the indentation
|
27
27
|
# level. The keys of this hash are open tokens, the values the matching
|
28
28
|
# tokens that should prevent a line from being indented if they appear on
|
29
29
|
# the same line.
|
30
30
|
OPEN_TOKENS = {
|
31
|
-
'def'
|
32
|
-
'class'
|
31
|
+
'def' => 'end',
|
32
|
+
'class' => 'end',
|
33
33
|
'module' => 'end',
|
34
|
-
'do'
|
35
|
-
'if'
|
34
|
+
'do' => 'end',
|
35
|
+
'if' => 'end',
|
36
36
|
'unless' => 'end',
|
37
|
-
'while'
|
38
|
-
'until'
|
39
|
-
'for'
|
40
|
-
'case'
|
41
|
-
'begin'
|
42
|
-
'['
|
43
|
-
'{'
|
44
|
-
'('
|
45
|
-
}
|
37
|
+
'while' => 'end',
|
38
|
+
'until' => 'end',
|
39
|
+
'for' => 'end',
|
40
|
+
'case' => 'end',
|
41
|
+
'begin' => 'end',
|
42
|
+
'[' => ']',
|
43
|
+
'{' => '}',
|
44
|
+
'(' => ')'
|
45
|
+
}.freeze
|
46
46
|
|
47
47
|
# Which tokens can either be open tokens, or appear as modifiers on
|
48
48
|
# a single-line.
|
49
|
-
SINGLELINE_TOKENS = %w
|
49
|
+
SINGLELINE_TOKENS = %w[if while until unless rescue].freeze
|
50
50
|
|
51
51
|
# Which tokens can be followed by an optional "do" keyword.
|
52
|
-
OPTIONAL_DO_TOKENS = %w
|
52
|
+
OPTIONAL_DO_TOKENS = %w[for while until].freeze
|
53
53
|
|
54
54
|
# Collection of token types that should be ignored. Without this list
|
55
55
|
# keywords such as "class" inside strings would cause the code to be
|
@@ -57,8 +57,8 @@ class Pry
|
|
57
57
|
#
|
58
58
|
# :pre_constant and :preserved_constant are the CodeRay 0.9.8 and 1.0.0
|
59
59
|
# classifications of "true", "false", and "nil".
|
60
|
-
IGNORE_TOKENS = [
|
61
|
-
|
60
|
+
IGNORE_TOKENS = %i[space content string method ident
|
61
|
+
constant pre_constant predefined_constant].freeze
|
62
62
|
|
63
63
|
# Tokens that indicate the end of a statement (i.e. that, if they appear
|
64
64
|
# directly before an "if" indicates that that if applies to the same line,
|
@@ -66,12 +66,14 @@ class Pry
|
|
66
66
|
#
|
67
67
|
# :reserved and :keywords are the CodeRay 0.9.8 and 1.0.0 respectively
|
68
68
|
# classifications of "super", "next", "return", etc.
|
69
|
-
STATEMENT_END_TOKENS = IGNORE_TOKENS + [
|
70
|
-
|
69
|
+
STATEMENT_END_TOKENS = IGNORE_TOKENS + %i[regexp integer float
|
70
|
+
keyword delimiter reserved
|
71
|
+
instance_variable
|
72
|
+
class_variable global_variable]
|
71
73
|
|
72
74
|
# Collection of tokens that should appear dedented even though they
|
73
75
|
# don't affect the surrounding code.
|
74
|
-
MIDWAY_TOKENS = %w
|
76
|
+
MIDWAY_TOKENS = %w[when else elsif ensure rescue].freeze
|
75
77
|
|
76
78
|
# Clean the indentation of a fragment of ruby.
|
77
79
|
#
|
@@ -94,19 +96,20 @@ class Pry
|
|
94
96
|
indent = new
|
95
97
|
lines = str.split("\n")
|
96
98
|
n = line_number - 1
|
97
|
-
to_indent = lines[0...n]
|
98
|
-
indent.indent(to_indent.join("\n")
|
99
|
+
to_indent = lines[0...n] << (lines[n] || "").split("def").first(1)
|
100
|
+
indent.indent(to_indent.join("\n") << "\n")
|
99
101
|
indent.module_nesting
|
100
102
|
end
|
101
103
|
|
102
|
-
def initialize
|
104
|
+
def initialize(pry_instance = Pry.new)
|
105
|
+
@pry_instance = pry_instance
|
103
106
|
reset
|
104
107
|
end
|
105
108
|
|
106
109
|
# reset internal state
|
107
110
|
def reset
|
108
111
|
@stack = []
|
109
|
-
@indent_level =
|
112
|
+
@indent_level = String.new # rubocop:disable Style/EmptyLiteral
|
110
113
|
@heredoc_queue = []
|
111
114
|
@close_heredocs = {}
|
112
115
|
@string_start = nil
|
@@ -141,10 +144,11 @@ class Pry
|
|
141
144
|
prefix = indent_level
|
142
145
|
|
143
146
|
input.lines.each do |line|
|
144
|
-
|
145
147
|
if in_string?
|
146
148
|
tokens = tokenize("#{open_delimiters_line}\n#{line}")
|
147
|
-
tokens = tokens.drop_while
|
149
|
+
tokens = tokens.drop_while do |token, _type|
|
150
|
+
!(token.is_a?(String) && token.include?("\n"))
|
151
|
+
end
|
148
152
|
previously_in_string = true
|
149
153
|
else
|
150
154
|
tokens = tokenize(line)
|
@@ -153,7 +157,7 @@ class Pry
|
|
153
157
|
|
154
158
|
before, after = indentation_delta(tokens)
|
155
159
|
|
156
|
-
before.times{ prefix.sub! SPACES, '' }
|
160
|
+
before.times { prefix.sub! SPACES, '' }
|
157
161
|
new_prefix = prefix + SPACES * after
|
158
162
|
|
159
163
|
line = prefix + line.lstrip unless previously_in_string
|
@@ -165,7 +169,7 @@ class Pry
|
|
165
169
|
|
166
170
|
@indent_level = prefix
|
167
171
|
|
168
|
-
|
172
|
+
output
|
169
173
|
end
|
170
174
|
|
171
175
|
# Get the indentation for the start of the next line.
|
@@ -192,7 +196,6 @@ class Pry
|
|
192
196
|
# @return [Array[Integer]]
|
193
197
|
#
|
194
198
|
def indentation_delta(tokens)
|
195
|
-
|
196
199
|
# We need to keep track of whether we've seen a "for" on this line because
|
197
200
|
# if the line ends with "do" then that "do" should be discounted (i.e. we're
|
198
201
|
# only opening one level not two) To do this robustly we want to keep track
|
@@ -203,29 +206,37 @@ class Pry
|
|
203
206
|
# When deciding whether an "if" token is the start of a multiline statement,
|
204
207
|
# or just the middle of a single-line if statement, we just look at the
|
205
208
|
# preceding token, which is tracked here.
|
206
|
-
last_token
|
209
|
+
last_token = nil
|
210
|
+
last_kind = nil
|
207
211
|
|
208
212
|
# delta keeps track of the total difference from the start of each line after
|
209
213
|
# the given token, 0 is just the level at which the current line started for
|
210
214
|
# reference.
|
211
|
-
remove_before
|
215
|
+
remove_before = 0
|
216
|
+
add_after = 0
|
212
217
|
|
213
218
|
# If the list of tokens contains a matching closing token the line should
|
214
219
|
# not be indented (and thus we should return true).
|
215
220
|
tokens.each do |token, kind|
|
216
|
-
is_singleline_if
|
221
|
+
is_singleline_if =
|
222
|
+
SINGLELINE_TOKENS.include?(token) && end_of_statement?(last_token, last_kind)
|
217
223
|
is_optional_do = (token == "do" && seen_for_at.include?(add_after - 1))
|
218
224
|
|
219
|
-
|
225
|
+
unless kind == :space
|
226
|
+
last_token = token
|
227
|
+
last_kind = kind
|
228
|
+
end
|
220
229
|
next if IGNORE_TOKENS.include?(kind)
|
221
230
|
|
222
231
|
track_module_nesting(token, kind)
|
223
232
|
|
224
233
|
seen_for_at << add_after if OPTIONAL_DO_TOKENS.include?(token)
|
225
234
|
|
235
|
+
next if is_singleline_if
|
236
|
+
|
226
237
|
if kind == :delimiter
|
227
238
|
track_delimiter(token)
|
228
|
-
elsif OPEN_TOKENS.
|
239
|
+
elsif OPEN_TOKENS.key?(token) && !is_optional_do && !is_singleline_if
|
229
240
|
@stack << token
|
230
241
|
add_after += 1
|
231
242
|
elsif token == OPEN_TOKENS[@stack.last]
|
@@ -244,20 +255,21 @@ class Pry
|
|
244
255
|
end
|
245
256
|
end
|
246
257
|
|
247
|
-
|
258
|
+
[remove_before, add_after]
|
248
259
|
end
|
249
260
|
|
250
|
-
# If the code just before an "if" or "while" token on a line looks like the
|
251
|
-
# then we want to treat that "if" as a singleline, not
|
261
|
+
# If the code just before an "if" or "while" token on a line looks like the
|
262
|
+
# end of a statement, then we want to treat that "if" as a singleline, not
|
263
|
+
# multiline statement.
|
252
264
|
def end_of_statement?(last_token, last_kind)
|
253
|
-
(last_token =~
|
265
|
+
(last_token =~ %r{^[)\]\}/]$} || STATEMENT_END_TOKENS.include?(last_kind))
|
254
266
|
end
|
255
267
|
|
256
268
|
# Are we currently in the middle of a string literal.
|
257
269
|
#
|
258
|
-
# This is used to determine whether to re-indent a given line, we mustn't
|
259
|
-
# within string literals because to do so would actually change
|
260
|
-
# String!
|
270
|
+
# This is used to determine whether to re-indent a given line, we mustn't
|
271
|
+
# re-indent within string literals because to do so would actually change
|
272
|
+
# the value of the String!
|
261
273
|
#
|
262
274
|
# @return Boolean
|
263
275
|
def in_string?
|
@@ -269,16 +281,17 @@ class Pry
|
|
269
281
|
# @param [String] string The Ruby to lex
|
270
282
|
# @return [Array] An Array of pairs of [token_value, token_type]
|
271
283
|
def tokenize(string)
|
272
|
-
tokens =
|
284
|
+
tokens = SyntaxHighlighter.tokenize(string)
|
273
285
|
tokens = tokens.tokens.each_slice(2) if tokens.respond_to?(:tokens) # Coderay 1.0.0
|
274
286
|
tokens.to_a
|
275
287
|
end
|
276
288
|
|
277
289
|
# Update the internal state about what kind of strings are open.
|
278
290
|
#
|
279
|
-
# Most of the complication here comes from the fact that HEREDOCs can be
|
280
|
-
# normal strings (which can't be nested) we assume that CodeRay
|
281
|
-
# open-and-close delimiters so we don't bother checking what
|
291
|
+
# Most of the complication here comes from the fact that HEREDOCs can be
|
292
|
+
# nested. For normal strings (which can't be nested) we assume that CodeRay
|
293
|
+
# correctly pairs open-and-close delimiters so we don't bother checking what
|
294
|
+
# they are.
|
282
295
|
#
|
283
296
|
# @param [String] token The token (of type :delimiter)
|
284
297
|
def track_delimiter(token)
|
@@ -289,11 +302,7 @@ class Pry
|
|
289
302
|
when @close_heredocs[@heredoc_queue.first]
|
290
303
|
@heredoc_queue.shift
|
291
304
|
else
|
292
|
-
|
293
|
-
@string_start = nil
|
294
|
-
else
|
295
|
-
@string_start = token
|
296
|
-
end
|
305
|
+
@string_start = @string_start ? nil : token
|
297
306
|
end
|
298
307
|
end
|
299
308
|
|
@@ -309,7 +318,7 @@ class Pry
|
|
309
318
|
#
|
310
319
|
# @return String
|
311
320
|
def open_delimiters_line
|
312
|
-
"puts #{open_delimiters.join(
|
321
|
+
"puts #{open_delimiters.join(', ')}"
|
313
322
|
end
|
314
323
|
|
315
324
|
# Update the internal state relating to module nesting.
|
@@ -320,7 +329,7 @@ class Pry
|
|
320
329
|
# [ ["class", "Foo"], ["module", "Bar::Baz"], ["class <<", "self"] ]
|
321
330
|
#
|
322
331
|
# A nil value in the @module_nesting array happens in two places: either
|
323
|
-
# when @
|
332
|
+
# when @awaiting_class is true and we're still waiting for the string to
|
324
333
|
# fill that space, or when a parse was rejected.
|
325
334
|
#
|
326
335
|
# At the moment this function is quite restricted about what formats it will
|
@@ -330,7 +339,7 @@ class Pry
|
|
330
339
|
# @param [String] token a token from Coderay
|
331
340
|
# @param [Symbol] kind the kind of that token
|
332
341
|
def track_module_nesting(token, kind)
|
333
|
-
if kind == :keyword &&
|
342
|
+
if kind == :keyword && %w[class module].include?(token)
|
334
343
|
@module_nesting << [token, nil]
|
335
344
|
@awaiting_class = true
|
336
345
|
elsif @awaiting_class
|
@@ -341,7 +350,7 @@ class Pry
|
|
341
350
|
@module_nesting.last[1] = token if kind == :class
|
342
351
|
@awaiting_class = false
|
343
352
|
else
|
344
|
-
# leave @
|
353
|
+
# leave @module_nesting[-1]
|
345
354
|
@awaiting_class = false
|
346
355
|
end
|
347
356
|
end
|
@@ -354,10 +363,8 @@ class Pry
|
|
354
363
|
#
|
355
364
|
# @param [String] token a token from Coderay
|
356
365
|
# @param [Symbol] kind the kind of that token
|
357
|
-
def track_module_nesting_end(token, kind
|
358
|
-
if kind == :keyword &&
|
359
|
-
@module_nesting.pop
|
360
|
-
end
|
366
|
+
def track_module_nesting_end(token, kind = :keyword)
|
367
|
+
@module_nesting.pop if kind == :keyword && %w[class module].include?(token)
|
361
368
|
end
|
362
369
|
|
363
370
|
# Return a list of strings which can be used to re-construct the Module.nesting at
|
@@ -378,25 +385,24 @@ class Pry
|
|
378
385
|
# the correct indentation. Mostly useful for fixing 'end'.
|
379
386
|
#
|
380
387
|
# @param [String] prompt The user's prompt
|
381
|
-
# @param [String] code
|
382
|
-
# @param [
|
383
|
-
# the difference in length between the old line and the new one)
|
384
|
-
#
|
385
|
-
|
388
|
+
# @param [String] code The code the user just typed in
|
389
|
+
# @param [Integer] overhang The number of characters to erase afterwards (the
|
390
|
+
# the difference in length between the old line and the new one)
|
391
|
+
#
|
392
|
+
# @return [String] correctly indented line
|
393
|
+
def correct_indentation(prompt, code, overhang = 0)
|
386
394
|
prompt = prompt.delete("\001\002")
|
387
|
-
|
395
|
+
line_to_measure = Pry::Helpers::Text.strip_color(prompt) << code
|
388
396
|
whitespace = ' ' * overhang
|
389
397
|
|
390
|
-
|
391
|
-
|
392
|
-
cols = cols.to_i
|
393
|
-
lines = cols != 0 ? (full_line.length / cols + 1) : 1
|
398
|
+
cols = @pry_instance.output.width
|
399
|
+
lines = cols == 0 ? 1 : (line_to_measure.length / cols + 1).to_i
|
394
400
|
|
395
|
-
if
|
396
|
-
move_up
|
401
|
+
if Helpers::Platform.windows_ansi?
|
402
|
+
move_up = "\e[#{lines}F"
|
397
403
|
move_down = "\e[#{lines}E"
|
398
404
|
else
|
399
|
-
move_up
|
405
|
+
move_up = "\e[#{lines}A\e[0G"
|
400
406
|
move_down = "\e[#{lines}B\e[0G"
|
401
407
|
end
|
402
408
|
|