mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
edit command: extracted out ContextLocator class
This commit is contained in:
parent
1a6077a0f9
commit
03d3d99211
3 changed files with 74 additions and 52 deletions
|
@ -9,6 +9,7 @@ class Pry
|
|||
class Command::Edit < Pry::ClassCommand
|
||||
require 'pry/commands/edit/method_patcher'
|
||||
require 'pry/commands/edit/exception_patcher'
|
||||
require 'pry/commands/edit/context_locator'
|
||||
|
||||
match 'edit'
|
||||
group 'Editing'
|
||||
|
@ -41,15 +42,6 @@ class Pry
|
|||
opt.on :p, :patch, "Instead of editing the object's file, try to edit in a tempfile and apply as a monkey patch."
|
||||
end
|
||||
|
||||
def complete(search)
|
||||
super + Bond::Rc.files(search.split(" ").last || '')
|
||||
end
|
||||
|
||||
def bad_option_combination?
|
||||
[opts.present?(:ex), opts.present?(:temp),
|
||||
opts.present?(:in), !args.empty?].count(true) > 1
|
||||
end
|
||||
|
||||
def process
|
||||
if bad_option_combination?
|
||||
raise CommandError, "Only one of --ex, --temp, --in and FILE may be specified."
|
||||
|
@ -90,7 +82,7 @@ class Pry
|
|||
end
|
||||
|
||||
def process_remote_edit
|
||||
file_name, line = retrieve_file_and_line
|
||||
file_name, line = ContextLocator.new(self).file_and_line
|
||||
raise CommandError, "#{file_name} is not a valid file name, cannot edit!" if not_a_real_file?(file_name)
|
||||
|
||||
# Sanitize blanks.
|
||||
|
@ -128,6 +120,11 @@ class Pry
|
|||
opts.present?(:ex) && opts.present?(:patch)
|
||||
end
|
||||
|
||||
def bad_option_combination?
|
||||
[opts.present?(:ex), opts.present?(:temp),
|
||||
opts.present?(:in), !args.empty?].count(true) > 1
|
||||
end
|
||||
|
||||
def input_expression
|
||||
case opts[:i]
|
||||
when Range
|
||||
|
@ -174,43 +171,8 @@ class Pry
|
|||
str =~ /\/|\\/
|
||||
end
|
||||
|
||||
def retrieve_file_and_line
|
||||
file_name, line = if opts.present?(:ex)
|
||||
file_and_line_for_exception
|
||||
elsif opts.present?(:current)
|
||||
current_file_and_line
|
||||
else
|
||||
object_file_and_line
|
||||
end
|
||||
|
||||
[file_name, opts.present?(:line) ? opts[:l].to_i : line]
|
||||
end
|
||||
|
||||
def file_and_line_for_exception
|
||||
raise CommandError, "No exception found." if _pry_.last_exception.nil?
|
||||
|
||||
file_name, line = _pry_.last_exception.bt_source_location_for(opts[:ex].to_i)
|
||||
raise CommandError, "Exception has no associated file." if file_name.nil?
|
||||
raise CommandError, "Cannot edit exceptions raised in REPL." if Pry.eval_path == file_name
|
||||
|
||||
file_name = RbxPath.convert_path_to_full(file_name) if RbxPath.is_core_path?(file_name)
|
||||
|
||||
[file_name, line]
|
||||
end
|
||||
|
||||
def current_file_and_line
|
||||
[target.eval("__FILE__"), target.eval("__LINE__")]
|
||||
end
|
||||
|
||||
def object_file_and_line
|
||||
if code_object
|
||||
[code_object.source_file, code_object.source_line]
|
||||
else
|
||||
# break up into file:line
|
||||
file_name = File.expand_path(args.first)
|
||||
line = file_name.sub!(/:(\d+)$/, "") ? $1.to_i : 1
|
||||
[file_name, line]
|
||||
end
|
||||
def complete(search)
|
||||
super + Bond::Rc.files(search.split(" ").last || '')
|
||||
end
|
||||
end
|
||||
|
||||
|
|
56
lib/pry/commands/edit/context_locator.rb
Normal file
56
lib/pry/commands/edit/context_locator.rb
Normal file
|
@ -0,0 +1,56 @@
|
|||
require 'forwardable'
|
||||
|
||||
class Pry
|
||||
class Command::Edit
|
||||
class ContextLocator
|
||||
extend Forwardable
|
||||
|
||||
def_delegators :@edit_context, :code_object, :target, :_pry_, :opts, :args
|
||||
|
||||
def initialize(edit_context)
|
||||
@edit_context = edit_context
|
||||
end
|
||||
|
||||
def file_and_line
|
||||
file_name, line = if opts.present?(:ex)
|
||||
file_and_line_for_exception
|
||||
elsif opts.present?(:current)
|
||||
current_file_and_line
|
||||
else
|
||||
object_file_and_line
|
||||
end
|
||||
|
||||
[file_name, opts.present?(:line) ? opts[:l].to_i : line]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def file_and_line_for_exception
|
||||
raise CommandError, "No exception found." if _pry_.last_exception.nil?
|
||||
|
||||
file_name, line = _pry_.last_exception.bt_source_location_for(opts[:ex].to_i)
|
||||
raise CommandError, "Exception has no associated file." if file_name.nil?
|
||||
raise CommandError, "Cannot edit exceptions raised in REPL." if Pry.eval_path == file_name
|
||||
|
||||
file_name = RbxPath.convert_path_to_full(file_name) if RbxPath.is_core_path?(file_name)
|
||||
|
||||
[file_name, line]
|
||||
end
|
||||
|
||||
def current_file_and_line
|
||||
[target.eval("__FILE__"), target.eval("__LINE__")]
|
||||
end
|
||||
|
||||
def object_file_and_line
|
||||
if code_object
|
||||
[code_object.source_file, code_object.source_line]
|
||||
else
|
||||
# break up into file:line
|
||||
file_name = File.expand_path(args.first)
|
||||
line = file_name.sub!(/:(\d+)$/, "") ? $1.to_i : 1
|
||||
[file_name, line]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +1,11 @@
|
|||
require 'forwardable'
|
||||
|
||||
class Pry
|
||||
class Command::Edit
|
||||
class ExceptionPatcher
|
||||
attr_accessor :edit_context
|
||||
extend Forwardable
|
||||
|
||||
def_delegators :@edit_context, :state, :_pry_
|
||||
|
||||
def initialize(edit_context)
|
||||
@edit_context = edit_context
|
||||
|
@ -9,12 +13,12 @@ class Pry
|
|||
|
||||
# perform the patch
|
||||
def perform_patch
|
||||
file_name, line = edit_context.retrieve_file_and_line
|
||||
lines = edit_context.state.dynamical_ex_file || File.read(file_name)
|
||||
file_name, line = ContextLocator.new(@edit_context).file_and_line
|
||||
lines = state.dynamical_ex_file || File.read(file_name)
|
||||
|
||||
source = Pry::Editor.edit_tempfile_with_content(lines)
|
||||
edit_context._pry_.evaluate_ruby source
|
||||
edit_context.state.dynamical_ex_file = source.split("\n")
|
||||
_pry_.evaluate_ruby source
|
||||
state.dynamical_ex_file = source.split("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue