1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/lib/pry/commands/edit/file_and_line_locator.rb
Takashi Kokubun 272b3290b5 Fix Ruby 2.6 warning for Binding#source_location
eval('[__FILE__, __LINE__]') will not work in future Ruby.
Let's stop using it when Binding#source_location is available because it
prints noisy warnings.

Since Binding#source_location is added in Ruby 2.6
https://github.com/ruby/ruby/commit/571e48b7442, this commit is leaving
old code with branching by binding.respond_to?(:source_location).

Fixes #1871
2018-12-11 11:10:55 +09:00

40 lines
1.3 KiB
Ruby

class Pry
class Command::Edit
module FileAndLineLocator
class << self
def from_binding(target)
if target.respond_to?(:source_location)
target.source_location
else
[target.eval("__FILE__"), target.eval("__LINE__")]
end
end
def from_code_object(code_object, filename_argument)
if File.exist?(code_object.source_file.to_s)
[code_object.source_file, code_object.source_line]
else
raise CommandError, "Cannot find a file for #{filename_argument}!"
end
end
def from_exception(exception, backtrace_level)
raise CommandError, "No exception found." if exception.nil?
file_name, line = exception.bt_source_location_for(backtrace_level)
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, line]
end
# when file and line are passed as a single arg, e.g my_file.rb:30
def from_filename_argument(filename_argument)
f = File.expand_path(filename_argument)
l = f.sub!(/:(\d+)$/, "") ? $1.to_i : 1
[f, l]
end
end
end
end
end