pry 0.12.2-java → 0.13.0-java
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 +110 -1
- data/LICENSE +1 -1
- data/README.md +331 -269
- data/bin/pry +5 -0
- data/lib/pry.rb +133 -119
- data/lib/pry/basic_object.rb +8 -4
- data/lib/pry/block_command.rb +22 -0
- data/lib/pry/class_command.rb +194 -0
- data/lib/pry/cli.rb +40 -31
- data/lib/pry/code.rb +39 -27
- data/lib/pry/code/code_file.rb +28 -24
- data/lib/pry/code/code_range.rb +4 -2
- data/lib/pry/code/loc.rb +15 -8
- data/lib/pry/code_object.rb +40 -38
- data/lib/pry/color_printer.rb +47 -46
- data/lib/pry/command.rb +166 -369
- data/lib/pry/command_set.rb +76 -73
- data/lib/pry/command_state.rb +31 -0
- data/lib/pry/commands/amend_line.rb +86 -81
- data/lib/pry/commands/bang.rb +18 -14
- data/lib/pry/commands/bang_pry.rb +15 -11
- data/lib/pry/commands/cat.rb +61 -54
- data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
- data/lib/pry/commands/cat/exception_formatter.rb +71 -60
- data/lib/pry/commands/cat/file_formatter.rb +55 -49
- data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
- data/lib/pry/commands/cd.rb +40 -35
- data/lib/pry/commands/change_inspector.rb +29 -22
- data/lib/pry/commands/change_prompt.rb +44 -39
- data/lib/pry/commands/clear_screen.rb +16 -10
- data/lib/pry/commands/code_collector.rb +148 -133
- data/lib/pry/commands/disable_pry.rb +23 -19
- data/lib/pry/commands/easter_eggs.rb +19 -30
- data/lib/pry/commands/edit.rb +184 -161
- data/lib/pry/commands/edit/exception_patcher.rb +21 -17
- data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
- data/lib/pry/commands/exit.rb +39 -35
- data/lib/pry/commands/exit_all.rb +24 -20
- data/lib/pry/commands/exit_program.rb +20 -16
- data/lib/pry/commands/find_method.rb +168 -160
- data/lib/pry/commands/fix_indent.rb +16 -12
- data/lib/pry/commands/help.rb +140 -133
- data/lib/pry/commands/hist.rb +151 -150
- data/lib/pry/commands/import_set.rb +20 -16
- data/lib/pry/commands/jump_to.rb +25 -21
- data/lib/pry/commands/list_inspectors.rb +35 -28
- data/lib/pry/commands/ls.rb +124 -102
- data/lib/pry/commands/ls/constants.rb +59 -42
- data/lib/pry/commands/ls/formatter.rb +50 -46
- data/lib/pry/commands/ls/globals.rb +38 -34
- data/lib/pry/commands/ls/grep.rb +17 -13
- data/lib/pry/commands/ls/instance_vars.rb +29 -27
- data/lib/pry/commands/ls/interrogatable.rb +18 -12
- data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
- data/lib/pry/commands/ls/local_names.rb +26 -22
- data/lib/pry/commands/ls/local_vars.rb +38 -28
- data/lib/pry/commands/ls/ls_entity.rb +47 -51
- data/lib/pry/commands/ls/methods.rb +44 -43
- data/lib/pry/commands/ls/methods_helper.rb +46 -42
- data/lib/pry/commands/ls/self_methods.rb +23 -22
- data/lib/pry/commands/nesting.rb +21 -17
- data/lib/pry/commands/play.rb +93 -82
- data/lib/pry/commands/pry_backtrace.rb +24 -17
- data/lib/pry/commands/pry_version.rb +15 -11
- data/lib/pry/commands/raise_up.rb +27 -22
- data/lib/pry/commands/reload_code.rb +60 -48
- data/lib/pry/commands/reset.rb +16 -12
- data/lib/pry/commands/ri.rb +55 -45
- data/lib/pry/commands/save_file.rb +45 -43
- data/lib/pry/commands/shell_command.rb +51 -51
- data/lib/pry/commands/shell_mode.rb +21 -17
- data/lib/pry/commands/show_doc.rb +81 -68
- data/lib/pry/commands/show_info.rb +189 -171
- data/lib/pry/commands/show_input.rb +16 -11
- data/lib/pry/commands/show_source.rb +109 -45
- data/lib/pry/commands/stat.rb +35 -31
- data/lib/pry/commands/switch_to.rb +21 -15
- data/lib/pry/commands/toggle_color.rb +20 -16
- data/lib/pry/commands/watch_expression.rb +89 -86
- data/lib/pry/commands/watch_expression/expression.rb +32 -27
- data/lib/pry/commands/whereami.rb +156 -148
- data/lib/pry/commands/wtf.rb +75 -50
- data/lib/pry/config.rb +311 -25
- 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/control_d_handler.rb +28 -0
- data/lib/pry/core_extensions.rb +9 -7
- data/lib/pry/editor.rb +48 -21
- data/lib/pry/env.rb +18 -0
- data/lib/pry/exception_handler.rb +43 -0
- data/lib/pry/exceptions.rb +13 -16
- data/lib/pry/forwardable.rb +5 -1
- data/lib/pry/helpers.rb +2 -0
- data/lib/pry/helpers/base_helpers.rb +68 -197
- data/lib/pry/helpers/command_helpers.rb +50 -61
- data/lib/pry/helpers/documentation_helpers.rb +20 -13
- data/lib/pry/helpers/options_helpers.rb +14 -7
- data/lib/pry/helpers/platform.rb +7 -5
- data/lib/pry/helpers/table.rb +33 -26
- data/lib/pry/helpers/text.rb +17 -14
- data/lib/pry/history.rb +48 -56
- data/lib/pry/hooks.rb +21 -12
- data/lib/pry/indent.rb +54 -50
- data/lib/pry/input_completer.rb +248 -230
- data/lib/pry/input_lock.rb +8 -9
- data/lib/pry/inspector.rb +36 -24
- data/lib/pry/last_exception.rb +45 -45
- data/lib/pry/method.rb +141 -94
- data/lib/pry/method/disowned.rb +16 -4
- data/lib/pry/method/patcher.rb +12 -3
- data/lib/pry/method/weird_method_locator.rb +68 -44
- data/lib/pry/object_path.rb +33 -25
- data/lib/pry/output.rb +121 -35
- data/lib/pry/pager.rb +41 -42
- data/lib/pry/plugins.rb +25 -8
- data/lib/pry/prompt.rb +123 -54
- data/lib/pry/pry_class.rb +61 -98
- data/lib/pry/pry_instance.rb +217 -215
- data/lib/pry/repl.rb +18 -22
- data/lib/pry/repl_file_loader.rb +27 -21
- data/lib/pry/ring.rb +11 -6
- data/lib/pry/slop.rb +574 -563
- data/lib/pry/slop/commands.rb +164 -169
- data/lib/pry/slop/option.rb +172 -168
- data/lib/pry/syntax_highlighter.rb +26 -0
- data/lib/pry/system_command_handler.rb +17 -0
- data/lib/pry/testable.rb +59 -61
- data/lib/pry/testable/evalable.rb +21 -12
- data/lib/pry/testable/mockable.rb +18 -10
- data/lib/pry/testable/pry_tester.rb +71 -56
- data/lib/pry/testable/utility.rb +29 -21
- data/lib/pry/testable/variables.rb +49 -43
- data/lib/pry/version.rb +3 -1
- data/lib/pry/warning.rb +27 -0
- data/lib/pry/wrapped_module.rb +51 -42
- data/lib/pry/wrapped_module/candidate.rb +21 -14
- metadata +31 -30
- data/lib/pry/commands.rb +0 -6
- 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 -32
- data/lib/pry/commands/gem_list.rb +0 -33
- data/lib/pry/commands/gem_open.rb +0 -29
- data/lib/pry/commands/gem_readme.rb +0 -25
- data/lib/pry/commands/gem_search.rb +0 -40
- data/lib/pry/commands/gem_stats.rb +0 -83
- data/lib/pry/commands/gist.rb +0 -102
- data/lib/pry/commands/install_command.rb +0 -54
- data/lib/pry/config/behavior.rb +0 -255
- data/lib/pry/config/convenience.rb +0 -28
- data/lib/pry/config/default.rb +0 -159
- data/lib/pry/config/memoization.rb +0 -48
- data/lib/pry/platform.rb +0 -91
- data/lib/pry/rubygem.rb +0 -84
- data/lib/pry/terminal.rb +0 -91
data/lib/pry/command_set.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Pry
|
2
4
|
class NoCommandError < StandardError
|
3
5
|
def initialize(match, owner)
|
@@ -29,16 +31,12 @@ class Pry
|
|
29
31
|
# @option options [Boolean] :keep_retval Whether or not to use return value
|
30
32
|
# of the block for return of `command` or just to return `nil`
|
31
33
|
# (the default).
|
32
|
-
# @option options [Array<String>] :requires_gem Whether the command has
|
33
|
-
# any gem dependencies, if it does and dependencies not met then
|
34
|
-
# command is disabled and a stub proc giving instructions to
|
35
|
-
# install command is provided.
|
36
34
|
# @option options [Boolean] :interpolate Whether string #{} based
|
37
35
|
# interpolation is applied to the command arguments before
|
38
36
|
# executing the command. Defaults to true.
|
39
37
|
# @option options [String] :listing The listing name of the
|
40
38
|
# command. That is the name by which the command is looked up by
|
41
|
-
# help and by show-
|
39
|
+
# help and by show-source. Necessary for commands with regex matches.
|
42
40
|
# @option options [Boolean] :use_prefix Whether the command uses
|
43
41
|
# `Pry.config.command_prefix` prefix (if one is defined). Defaults
|
44
42
|
# to true.
|
@@ -57,31 +55,38 @@ class Pry
|
|
57
55
|
# end
|
58
56
|
#
|
59
57
|
# # From pry:
|
60
|
-
# # pry(main)>
|
58
|
+
# # pry(main)> pry_instance.commands = MyCommands
|
61
59
|
# # pry(main)> greet john
|
62
60
|
# # Good afternoon John!
|
63
61
|
# # pry(main)> help greet
|
64
62
|
# # Greet somebody
|
65
63
|
# @example Regexp command
|
66
64
|
# MyCommands = Pry::CommandSet.new do
|
67
|
-
# command
|
65
|
+
# command(
|
66
|
+
# /number-(\d+)/, "number-N regex command", :listing => "number"
|
67
|
+
# ) do |num, name|
|
68
68
|
# puts "hello #{name}, nice number: #{num}"
|
69
69
|
# end
|
70
70
|
# end
|
71
71
|
#
|
72
72
|
# # From pry:
|
73
|
-
# # pry(main)>
|
73
|
+
# # pry(main)> pry_instance.commands = MyCommands
|
74
74
|
# # pry(main)> number-10 john
|
75
75
|
# # hello john, nice number: 10
|
76
76
|
# # pry(main)> help number
|
77
77
|
# # number-N regex command
|
78
78
|
def block_command(match, description = "No description.", options = {}, &block)
|
79
|
-
|
79
|
+
if description.is_a?(Hash)
|
80
|
+
options = description
|
81
|
+
description = "No description."
|
82
|
+
end
|
80
83
|
options = Pry::Command.default_options(match).merge!(options)
|
81
84
|
|
82
|
-
@commands[match] = Pry::BlockCommand.subclass(
|
85
|
+
@commands[match] = Pry::BlockCommand.subclass(
|
86
|
+
match, description, options, helper_module, &block
|
87
|
+
)
|
83
88
|
end
|
84
|
-
|
89
|
+
alias command block_command
|
85
90
|
|
86
91
|
# Defines a new Pry command class.
|
87
92
|
#
|
@@ -99,7 +104,9 @@ class Pry
|
|
99
104
|
# end
|
100
105
|
#
|
101
106
|
# def process
|
102
|
-
#
|
107
|
+
# if opts.present?(:u) && opts.present?(:d)
|
108
|
+
# raise Pry::CommandError, "-u and -d makes no sense"
|
109
|
+
# end
|
103
110
|
# result = args.join(" ")
|
104
111
|
# result.downcase! if opts.present?(:downcase)
|
105
112
|
# result.upcase! if opts.present?(:upcase)
|
@@ -108,10 +115,15 @@ class Pry
|
|
108
115
|
# end
|
109
116
|
#
|
110
117
|
def create_command(match, description = "No description.", options = {}, &block)
|
111
|
-
|
118
|
+
if description.is_a?(Hash)
|
119
|
+
options = description
|
120
|
+
description = "No description."
|
121
|
+
end
|
112
122
|
options = Pry::Command.default_options(match).merge!(options)
|
113
123
|
|
114
|
-
@commands[match] = Pry::ClassCommand.subclass(
|
124
|
+
@commands[match] = Pry::ClassCommand.subclass(
|
125
|
+
match, description, options, helper_module, &block
|
126
|
+
)
|
115
127
|
@commands[match].class_eval(&block)
|
116
128
|
@commands[match]
|
117
129
|
end
|
@@ -121,7 +133,8 @@ class Pry
|
|
121
133
|
end
|
122
134
|
|
123
135
|
# Removes some commands from the set
|
124
|
-
# @param [Array<String>] searches the matches or listings of the commands
|
136
|
+
# @param [Array<String>] searches the matches or listings of the commands
|
137
|
+
# to remove
|
125
138
|
def delete(*searches)
|
126
139
|
searches.each do |search|
|
127
140
|
cmd = find_command_by_match_or_listing(search)
|
@@ -160,7 +173,7 @@ class Pry
|
|
160
173
|
def find_command_by_match_or_listing(match_or_listing)
|
161
174
|
cmd = (@commands[match_or_listing] ||
|
162
175
|
Pry::Helpers::BaseHelpers.find_command(match_or_listing, @commands))
|
163
|
-
cmd
|
176
|
+
cmd || raise(ArgumentError, "cannot find a command: '#{match_or_listing}'")
|
164
177
|
end
|
165
178
|
|
166
179
|
# Aliases a command
|
@@ -175,13 +188,13 @@ class Pry
|
|
175
188
|
# @example Pass explicit description (overriding default).
|
176
189
|
# Pry.config.commands.alias_command "lM", "ls -M", :desc => "cutiepie"
|
177
190
|
def alias_command(match, action, options = {})
|
178
|
-
cmd = find_command(action)
|
191
|
+
(cmd = find_command(action)) || raise("command: '#{action}' not found")
|
179
192
|
original_options = cmd.options.dup
|
180
193
|
|
181
|
-
options = original_options.merge!(
|
182
|
-
|
183
|
-
|
184
|
-
|
194
|
+
options = original_options.merge!(
|
195
|
+
desc: "Alias for `#{action}`",
|
196
|
+
listing: match
|
197
|
+
).merge!(options)
|
185
198
|
|
186
199
|
# ensure default description is used if desc is nil
|
187
200
|
desc = options.delete(:desc).to_s
|
@@ -190,6 +203,7 @@ class Pry
|
|
190
203
|
run action, *args
|
191
204
|
end
|
192
205
|
|
206
|
+
# TODO: untested. What's this about?
|
193
207
|
c.class_eval do
|
194
208
|
define_method(:complete) do |input|
|
195
209
|
cmd.new(context).complete(input)
|
@@ -225,17 +239,6 @@ class Pry
|
|
225
239
|
@commands.delete(cmd.match)
|
226
240
|
end
|
227
241
|
|
228
|
-
def disabled_command(name_of_disabled_command, message, matcher = name_of_disabled_command)
|
229
|
-
create_command name_of_disabled_command do
|
230
|
-
match matcher
|
231
|
-
description ""
|
232
|
-
|
233
|
-
define_method(:process) do
|
234
|
-
output.puts "DISABLED: #{message}"
|
235
|
-
end
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
242
|
# Sets or gets the description for a command (replacing the old
|
240
243
|
# description). Returns current description if no description
|
241
244
|
# parameter provided.
|
@@ -249,50 +252,33 @@ class Pry
|
|
249
252
|
# Pry.config.commands.desc "amend-line"
|
250
253
|
def desc(search, description = nil)
|
251
254
|
cmd = find_command_by_match_or_listing(search)
|
252
|
-
return cmd.description
|
255
|
+
return cmd.description unless description
|
253
256
|
|
254
257
|
cmd.description = description
|
255
258
|
end
|
256
259
|
|
257
|
-
# Defines helpers methods for this command sets.
|
258
|
-
# Those helpers are only defined in this command set.
|
259
|
-
#
|
260
|
-
# @yield A block defining helper methods
|
261
|
-
# @example
|
262
|
-
# helpers do
|
263
|
-
# def hello
|
264
|
-
# puts "Hello!"
|
265
|
-
# end
|
266
|
-
#
|
267
|
-
# include OtherModule
|
268
|
-
# end
|
269
|
-
def helpers(&block)
|
270
|
-
helper_module.class_eval(&block)
|
271
|
-
end
|
272
|
-
|
273
260
|
# @return [Array]
|
274
261
|
# The list of commands provided by the command set.
|
275
262
|
def list_commands
|
276
263
|
@commands.keys
|
277
264
|
end
|
278
|
-
|
265
|
+
alias keys list_commands
|
279
266
|
|
280
267
|
def to_hash
|
281
268
|
@commands.dup
|
282
269
|
end
|
283
|
-
|
270
|
+
alias to_h to_hash
|
284
271
|
|
285
272
|
# Find a command that matches the given line
|
286
273
|
# @param [String] pattern The line that might be a command invocation
|
287
274
|
# @return [Pry::Command, nil]
|
288
275
|
def [](pattern)
|
289
|
-
@commands.values.select do |command|
|
276
|
+
commands = @commands.values.select do |command|
|
290
277
|
command.matches?(pattern)
|
291
|
-
end
|
292
|
-
|
293
|
-
end.last
|
278
|
+
end
|
279
|
+
commands.max_by { |command| command.match_score(pattern) }
|
294
280
|
end
|
295
|
-
|
281
|
+
alias find_command []
|
296
282
|
|
297
283
|
#
|
298
284
|
# Re-assign the command found at _pattern_ with _command_.
|
@@ -311,9 +297,11 @@ class Pry
|
|
311
297
|
#
|
312
298
|
def []=(pattern, command)
|
313
299
|
if command.equal?(nil)
|
314
|
-
|
300
|
+
@commands.delete(pattern)
|
301
|
+
return
|
315
302
|
end
|
316
|
-
|
303
|
+
|
304
|
+
unless command.is_a?(Class) && command < Pry::Command
|
317
305
|
raise TypeError, "command is not a subclass of Pry::Command"
|
318
306
|
end
|
319
307
|
|
@@ -341,11 +329,12 @@ class Pry
|
|
341
329
|
# @param [String] search The user's search.
|
342
330
|
# @return [Pry::Command?]
|
343
331
|
def find_command_for_help(search)
|
344
|
-
find_command(search) ||
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
332
|
+
find_command(search) ||
|
333
|
+
(begin
|
334
|
+
find_command_by_match_or_listing(search)
|
335
|
+
rescue ArgumentError
|
336
|
+
nil
|
337
|
+
end)
|
349
338
|
end
|
350
339
|
|
351
340
|
# Is the given line a command invocation?
|
@@ -369,12 +358,6 @@ class Pry
|
|
369
358
|
end
|
370
359
|
end
|
371
360
|
|
372
|
-
# @private (used for testing)
|
373
|
-
def run_command(context, match, *args)
|
374
|
-
command = @commands[match] or raise NoCommandError.new(match, self)
|
375
|
-
command.new(context).call_safely(*args)
|
376
|
-
end
|
377
|
-
|
378
361
|
# Generate completions for the user's search.
|
379
362
|
# @param [String] search The line to search for
|
380
363
|
# @param [Hash] context The context to create the command with
|
@@ -383,11 +366,30 @@ class Pry
|
|
383
366
|
if (command = find_command(search))
|
384
367
|
command.new(context).complete(search)
|
385
368
|
else
|
386
|
-
@commands.keys.select do |key|
|
387
|
-
String
|
388
|
-
end
|
369
|
+
keys = @commands.keys.select do |key|
|
370
|
+
key.is_a?(String) && key.start_with?(search)
|
371
|
+
end
|
372
|
+
keys.map { |key| key + " " }
|
389
373
|
end
|
390
374
|
end
|
375
|
+
|
376
|
+
private
|
377
|
+
|
378
|
+
# Defines helpers methods for this command sets.
|
379
|
+
# Those helpers are only defined in this command set.
|
380
|
+
#
|
381
|
+
# @yield A block defining helper methods
|
382
|
+
# @example
|
383
|
+
# helpers do
|
384
|
+
# def hello
|
385
|
+
# puts "Hello!"
|
386
|
+
# end
|
387
|
+
#
|
388
|
+
# include OtherModule
|
389
|
+
# end
|
390
|
+
def helpers(&block)
|
391
|
+
helper_module.class_eval(&block)
|
392
|
+
end
|
391
393
|
end
|
392
394
|
|
393
395
|
# Wraps the return result of process_commands, indicates if the
|
@@ -396,7 +398,8 @@ class Pry
|
|
396
398
|
attr_reader :retval
|
397
399
|
|
398
400
|
def initialize(is_command, retval = nil)
|
399
|
-
@is_command
|
401
|
+
@is_command = is_command
|
402
|
+
@retval = retval
|
400
403
|
end
|
401
404
|
|
402
405
|
# Is the result a command?
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
class Pry
|
6
|
+
# CommandState is a data structure to hold per-command state.
|
7
|
+
#
|
8
|
+
# Pry commands can store arbitrary state here. This state persists between
|
9
|
+
# subsequent command invocations. All state saved here is unique to the
|
10
|
+
# command.
|
11
|
+
#
|
12
|
+
# @since v0.13.0
|
13
|
+
# @api private
|
14
|
+
class CommandState
|
15
|
+
def self.default
|
16
|
+
@default ||= new
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@command_state = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
def state_for(command_name)
|
24
|
+
@command_state[command_name] ||= OpenStruct.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def reset(command_name)
|
28
|
+
@command_state[command_name] = OpenStruct.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,98 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Pry
|
2
|
-
class Command
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
4
|
+
class Command
|
5
|
+
class AmendLine < Pry::ClassCommand
|
6
|
+
match(/amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/)
|
7
|
+
group 'Editing'
|
8
|
+
description 'Amend a line of input in multi-line mode.'
|
9
|
+
command_options interpolate: false, listing: 'amend-line'
|
10
|
+
|
11
|
+
banner <<-'BANNER'
|
12
|
+
Amend a line of input in multi-line mode. `amend-line N`, where the N represents
|
13
|
+
line to replace. Can also specify a range of lines using `amend-line N..M`
|
14
|
+
syntax. Passing "!" as replacement content deletes the line(s) instead.
|
15
|
+
|
16
|
+
amend-line 1 puts 'new' # replace line 1
|
17
|
+
amend-line 1..4 ! # delete lines 1..4
|
18
|
+
amend-line 3 >puts 'bye' # insert before line 3
|
19
|
+
amend-line puts 'appended' # no line number modifies immediately preceding line
|
20
|
+
BANNER
|
21
|
+
|
22
|
+
def process
|
23
|
+
raise CommandError, "No input to amend." if eval_string.empty?
|
24
|
+
|
25
|
+
eval_string.replace(amend_input)
|
26
|
+
run "fix-indent"
|
27
|
+
run "show-input"
|
28
|
+
end
|
26
29
|
|
27
|
-
|
30
|
+
private
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
# @return [String] A new string with the amendments applied to it.
|
33
|
+
def amend_input
|
34
|
+
input_array = eval_string.each_line.to_a
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
if arg_string == "!"
|
37
|
+
delete_from_array(input_array, line_range)
|
38
|
+
elsif arg_string.start_with?(">")
|
39
|
+
insert_into_array(input_array, line_range)
|
40
|
+
else
|
41
|
+
replace_in_array(input_array, line_range)
|
42
|
+
end
|
40
43
|
|
41
|
-
|
42
|
-
|
44
|
+
input_array.join
|
45
|
+
end
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
+
def delete_from_array(array, range)
|
48
|
+
array.slice!(range)
|
49
|
+
end
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
51
|
+
def insert_into_array(array, range)
|
52
|
+
insert_slot = Array(range).first
|
53
|
+
array.insert(insert_slot, arg_string[1..-1] << "\n")
|
54
|
+
end
|
52
55
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
+
def replace_in_array(array, range)
|
57
|
+
array[range] = arg_string + "\n"
|
58
|
+
end
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
60
|
+
# @return [Fixnum] The number of lines currently in `eval_string` (the
|
61
|
+
# input buffer)
|
62
|
+
def line_count
|
63
|
+
eval_string.lines.count
|
64
|
+
end
|
61
65
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
66
|
+
# Returns the (one-indexed) start and end lines given by the user.
|
67
|
+
# The lines in this range will be affected by the `amend-line`.
|
68
|
+
# Returns `nil` if no lines were specified by the user.
|
69
|
+
# @return [Array<Fixnum>, nil]
|
70
|
+
def start_and_end_line_number
|
71
|
+
start_line_number, end_line_number = args
|
72
|
+
end_line_number ||= start_line_number.to_i
|
69
73
|
|
70
|
-
|
71
|
-
|
74
|
+
[start_line_number.to_i, end_line_number.to_i] if start_line_number
|
75
|
+
end
|
72
76
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
77
|
+
# Takes two numbers that are 1-indexed, and returns a range (or
|
78
|
+
# number) that is 0-indexed. 1-indexed means the first element is
|
79
|
+
# indentified by 1 rather than by 0 (as is the case for Ruby arrays).
|
80
|
+
# @param [Fixnum] start_line_number One-indexed number.
|
81
|
+
# @param [Fixnum] end_line_number One-indexed number.
|
82
|
+
# @return [Range] The zero-indexed range.
|
83
|
+
def zero_indexed_range_from_one_indexed_numbers(start_line_number, end_line_number)
|
84
|
+
# FIXME: one_index_number is a horrible name for this method
|
85
|
+
one_index_number(start_line_number)..one_index_number(end_line_number)
|
86
|
+
end
|
83
87
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
88
|
+
# The lines (or line) that will be modified by the `amend-line`.
|
89
|
+
# @return [Range, Fixnum] The lines or line.
|
90
|
+
def line_range
|
91
|
+
start_line_number, end_line_number = start_and_end_line_number
|
92
|
+
if start_line_number
|
93
|
+
zero_indexed_range_from_one_indexed_numbers(start_line_number,
|
94
|
+
end_line_number)
|
95
|
+
else
|
96
|
+
line_count - 1
|
97
|
+
end
|
93
98
|
end
|
94
99
|
end
|
95
|
-
end
|
96
100
|
|
97
|
-
|
101
|
+
Pry::Commands.add_command(Pry::Command::AmendLine)
|
102
|
+
end
|
98
103
|
end
|