1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

edit command now supports -p for methods

This commit is contained in:
John Mair 2012-12-31 03:29:05 +01:00
parent 6d0b6b9a14
commit 621e01e3f0
2 changed files with 122 additions and 24 deletions

View file

@ -56,15 +56,23 @@ class Pry
if local_edit?
# edit of local code, eval'd within pry.
process_local_edit
elsif patch_exception?
elsif runtime_patch?
# patch an exception
apply_runtime_patch_to_exception
apply_runtime_patch
else
# edit of remote code, eval'd at top-level
process_remote_edit
end
end
def retrieve_code_object
!probably_a_file?(args.first) && Pry::CodeObject.lookup(args.first, target, _pry_)
end
def runtime_patch?
opts.present?(:patch)
end
def retrieve_input_expression
case opts[:i]
when Range
@ -109,11 +117,9 @@ class Pry
def process_local_edit
content = initial_temp_file_content
line = content.lines.count
source = Pry::Editor.edit_tempfile_with_content(content, line)
if local_reload?
silence_warnings do
eval_string.replace(source)
eval_string.replace Pry::Editor.edit_tempfile_with_content(content, content.lines.count)
end
end
end
@ -140,7 +146,7 @@ class Pry
end
def object_file_and_line
if !probably_a_file?(args.first) && code_object = Pry::CodeObject.lookup(args.first, target, _pry_)
if code_object = retrieve_code_object
[code_object.source_file, code_object.source_line]
else
# break up into file:line
@ -179,6 +185,35 @@ class Pry
state.dynamical_ex_file = source.split("\n")
end
def apply_runtime_patch_to_method(method_object)
lines = method_object.source.lines.to_a
lines[0] = definition_line_for_owner(lines[0], method_object.original_name)
source = wrap_for_nesting(wrap_for_owner(Pry::Editor.edit_tempfile_with_content(lines), method_object.owner), method_object)
if method_object.alias?
with_method_transaction(method_object.original_name, method_object.owner) do
_pry_.evaluate_ruby source
Pry.binding_for(method_object.owner).eval("alias #{method_object.name} #{original_name}")
end
else
_pry_.evaluate_ruby source
end
end
def apply_runtime_patch
if patch_exception?
apply_runtime_patch_to_exception
else
code_object = retrieve_code_object
if code_object.is_a?(Pry::Method)
apply_runtime_patch_to_method(code_object)
else
raise NotImplementedError, "Cannot yet patch #{code_object} objects!"
end
end
end
def process_remote_edit
file_name, line = retrieve_file_and_line
@ -194,6 +229,81 @@ class Pry
end
end
end
# Run some code ensuring that at the end target#meth_name will not have changed.
#
# When we're redefining aliased methods we will overwrite the method at the
# unaliased name (so that super continues to work). By wrapping that code in a
# transation we make that not happen, which means that alias_method_chains, etc.
# continue to work.
#
# @param [String] meth_name The method name before aliasing
# @param [Module] target The owner of the method
def with_method_transaction(meth_name, target)
target = Pry.binding_for(target)
temp_name = "__pry_#{meth_name}__"
target.eval("alias #{temp_name} #{meth_name}")
yield
target.eval("alias #{meth_name} #{temp_name}")
ensure
target.eval("undef #{temp_name}") rescue nil
end
# Update the definition line so that it can be eval'd directly on the Method's
# owner instead of from the original context.
#
# In particular this takes `def self.foo` and turns it into `def foo` so that we
# don't end up creating the method on the singleton class of the singleton class
# by accident.
#
# This is necessarily done by String manipulation because we can't find out what
# syntax is needed for the argument list by ruby-level introspection.
#
# @param String The original definition line. e.g. def self.foo(bar, baz=1)
# @return String The new definition line. e.g. def foo(bar, baz=1)
def definition_line_for_owner(line, original_name)
if line =~ /^def (?:.*?\.)?#{Regexp.escape(original_name)}(?=[\(\s;]|$)/
"def #{original_name}#{$'}"
else
raise CommandError, "Could not find original `def #{original_name}` line to patch."
end
end
# Update the source code so that when it has the right owner when eval'd.
#
# This (combined with definition_line_for_owner) is backup for the case that
# wrap_for_nesting fails, to ensure that the method will stil be defined in
# the correct place.
#
# @param [String] source The source to wrap
# @return [String]
def wrap_for_owner(source, owner)
Thread.current[:__pry_owner__] = owner
source = "Thread.current[:__pry_owner__].class_eval do\n#{source}\nend"
end
# Update the new source code to have the correct Module.nesting.
#
# This method uses syntactic analysis of the original source file to determine
# the new nesting, so that we can tell the difference between:
#
# class A; def self.b; end; end
# class << A; def b; end; end
#
# The resulting code should be evaluated in the TOPLEVEL_BINDING.
#
# @param [String] source The source to wrap.
# @return [String]
def wrap_for_nesting(source, method_object)
nesting = Pry::Code.from_file(method_object.source_file).nesting_at(method_object.source_line)
(nesting + [source] + nesting.map{ "end" } + [""]).join("\n")
rescue Pry::Indent::UnparseableNestingError => e
source
end
end
Pry::Commands.add_command(Pry::Command::Edit)

View file

@ -27,10 +27,6 @@ class Pry
end
def process
if !Pry.config.editor
raise CommandError, "No editor set!\nEnsure that #{text.bold("Pry.config.editor")} is set to your editor of choice."
end
begin
@method = method_object
rescue MethodNotFound => err
@ -53,25 +49,17 @@ class Pry
def process_patch
lines = @method.source.lines.to_a
lines[0] = definition_line_for_owner(lines[0])
temp_file do |f|
f.puts lines
f.flush
f.close(false)
Pry::Editor.invoke_editor(f.path, 0, true)
source = wrap_for_nesting(wrap_for_owner(Pry::Editor.edit_tempfile_with_content(lines)))
source = wrap_for_nesting(wrap_for_owner(File.read(f.path)))
if @method.alias?
with_method_transaction(original_name, @method.owner) do
_pry_.evaluate_ruby source
Pry.binding_for(@method.owner).eval("alias #{@method.name} #{original_name}")
end
else
if @method.alias?
with_method_transaction(original_name, @method.owner) do
_pry_.evaluate_ruby source
Pry.binding_for(@method.owner).eval("alias #{@method.name} #{original_name}")
end
else
_pry_.evaluate_ruby source
end
end