mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
fix edit-method for single-line definitions
This commit is contained in:
parent
509e908688
commit
c2c6a30814
2 changed files with 12 additions and 2 deletions
|
@ -226,7 +226,7 @@ class Pry
|
|||
lines = meth.source.lines.to_a
|
||||
|
||||
if ((original_name = meth.original_name) &&
|
||||
lines[0] =~ /^def (?:.*?\.)?#{original_name}(?=[\( ]|$)/)
|
||||
lines[0] =~ /^def (?:.*?\.)?#{original_name}(?=[\(\s;]|$)/)
|
||||
lines[0] = "def #{original_name}#{$'}"
|
||||
else
|
||||
raise CommandError, "Pry can only patch methods created with the `def` keyword."
|
||||
|
|
|
@ -288,7 +288,10 @@ class Pry
|
|||
if RUBY_VERSION =~ /^1\.9/ && RUBY_ENGINE == "ruby"
|
||||
require 'ripper'
|
||||
|
||||
# Ripper is ok with an extraneous end, so we don't need to worry about
|
||||
# whether it's a one-liner.
|
||||
tree = Ripper::SexpBuilder.new(first_line + ";end").parse
|
||||
|
||||
name = tree.flatten(2).each do |lst|
|
||||
break lst[1] if lst[0] == :@ident
|
||||
end
|
||||
|
@ -297,7 +300,14 @@ class Pry
|
|||
else
|
||||
require 'ruby_parser'
|
||||
|
||||
tree = RubyParser.new.parse(first_line + ";end")
|
||||
# RubyParser breaks if there's an extra end, so we'll just rescue
|
||||
# and try again.
|
||||
tree = begin
|
||||
RubyParser.new.parse(first_line + ";end")
|
||||
rescue Racc::ParseError
|
||||
RubyParser.new.parse(first_line)
|
||||
end
|
||||
|
||||
name = tree.each_cons(2) do |a, b|
|
||||
break a if b.is_a?(Array) && b.first == :args
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue