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

refactored the f*ck out of edit.rb (edit command)

This commit is contained in:
John Mair 2012-12-29 23:37:55 +01:00
parent 9bda7a17a7
commit b6bd28139f

View file

@ -39,21 +39,33 @@ class Pry
super + Bond::Rc.files(search.split(" ").last || '') super + Bond::Rc.files(search.split(" ").last || '')
end end
def bad_option_combination?
[opts.present?(:ex), opts.present?(:temp),
opts.present?(:in), !args.empty?].count(true) > 1
end
def local_edit?
!opts.present?(:ex) && !opts.present?(:current) && args.empty?
end
def process def process
if [opts.present?(:ex), opts.present?(:temp), opts.present?(:in), !args.empty?].count(true) > 1 if bad_option_combination?
raise CommandError, "Only one of --ex, --temp, --in and FILE may be specified." raise CommandError, "Only one of --ex, --temp, --in and FILE may be specified."
end end
if !opts.present?(:ex) && !opts.present?(:current) && args.empty? if local_edit?
# edit of local code, eval'd within pry. # edit of local code, eval'd within pry.
process_local_edit process_local_edit
elsif patch_exception?
# patch an exception
apply_runtime_patch_to_exception
else else
# edit of remote code, eval'd at top-level # edit of remote code, eval'd at top-level
process_remote_edit process_remote_edit
end end
end end
def process_i def retrieve_input_expression
case opts[:i] case opts[:i]
when Range when Range
(_pry_.input_array[opts[:i]] || []).join (_pry_.input_array[opts[:i]] || []).join
@ -64,114 +76,121 @@ class Pry
end end
end end
def process_local_edit def reloadable?
content = case opts.present?(:reload) || opts.present?(:ex)
when opts.present?(:temp) end
""
when opts.present?(:in) def never_reload?
process_i opts.present?(:'no-reload') || Pry.config.disable_auto_reload
when eval_string.strip != "" end
eval_string
else # conditions much less strict than for reload? (which is for remote reloads)
_pry_.input_array.reverse_each.find{ |x| x && x.strip != "" } || "" def local_reload?
!never_reload?
end
def reload?(file_name="")
(reloadable? || file_name.end_with?(".rb")) && !never_reload?
end
def initial_temp_file_content
case
when opts.present?(:temp)
""
when opts.present?(:in)
retrieve_input_expression
when eval_string.strip != ""
eval_string
else
_pry_.input_array.reverse_each.find{ |x| x && x.strip != "" } || ""
end end
end
def process_local_edit
content = initial_temp_file_content
line = content.lines.count line = content.lines.count
source = Pry::Editor.edit_tempfile_with_content(content, line)
temp_file do |f| if local_reload?
f.puts(content) silence_warnings do
f.flush eval_string.replace(source)
reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
f.close(false)
invoke_editor(f.path, line, reload)
if reload
silence_warnings do
eval_string.replace(File.read(f.path))
end
end end
end end
end end
def probably_a_file?(str) def probably_a_file?(str)
[".rb", ".c", ".py", ".yml", ".gemspec"].include? File.extname(str) || [".rb", ".c", ".py", ".yml", ".gemspec"].include? File.extname(str) ||
str =~ /\/|\\/ str =~ /\/|\\/
end end
def process_remote_edit def file_and_line_for_exception
if opts.present?(:ex) raise CommandError, "No exception found." if _pry_.last_exception.nil?
if _pry_.last_exception.nil?
raise CommandError, "No exception found."
end
ex = _pry_.last_exception file_name, line = _pry_.last_exception.bt_source_location_for(opts[:ex].to_i)
bt_index = 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
ex_file, ex_line = ex.bt_source_location_for(bt_index) file_name = RbxPath.convert_path_to_full(file_name) if RbxPath.is_core_path?(file_name)
if ex_file && RbxPath.is_core_path?(ex_file)
file_name = RbxPath.convert_path_to_full(ex_file)
else
file_name = ex_file
end
line = ex_line [file_name, line]
end
if file_name.nil? def current_file_and_line
raise CommandError, "Exception has no associated file." [target.eval("__FILE__"), target.eval("__LINE__")]
end end
if Pry.eval_path == file_name def object_file_and_line
raise CommandError, "Cannot edit exceptions raised in REPL." if !probably_a_file?(args.first) && code_object = Pry::CodeObject.lookup(args.first, target, _pry_)
end [code_object.source_file, code_object.source_line]
elsif opts.present?(:current)
file_name = target.eval("__FILE__")
line = target.eval("__LINE__")
else else
# break up into file:line
if !probably_a_file?(args.first) && code_object = Pry::CodeObject.lookup(args.first, target, _pry_) file_name = File.expand_path(args.first)
file_name = code_object.source_file line = file_name.sub!(/:(\d+)$/, "") ? $1.to_i : 1
line = code_object.source_line [file_name, line]
else
# break up into file:line
file_name = File.expand_path(args.first)
line = file_name.sub!(/:(\d+)$/, "") ? $1.to_i : 1
end
end end
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
if not_a_real_file?(file_name) if not_a_real_file?(file_name)
raise CommandError, "#{file_name} is not a valid file name, cannot edit!" raise CommandError, "#{file_name} is not a valid file name, cannot edit!"
end end
line = opts[:l].to_i if opts.present?(:line) [file_name, opts.present?(:line) ? opts[:l].to_i : line]
end
reload = opts.present?(:reload) || ((opts.present?(:ex) || file_name.end_with?(".rb")) && !opts.present?(:'no-reload')) && !Pry.config.disable_auto_reload def patch_exception?
opts.present?(:ex) && opts.present?(:patch)
end
if opts.present?(:ex) && opts.present?(:patch) def apply_runtime_patch_to_exception
lines = state.dynamical_ex_file || File.open(ex_file).read file_name, line = file_and_line_for_exception
lines = state.dynamical_ex_file || File.read(file_name)
temp_file do |f| source = Pry::Editor.edit_tempfile_with_content(lines)
f.puts lines _pry_.evaluate_ruby source
f.flush state.dynamical_ex_file = source.split("\n")
f.close(false) end
tempfile_path = f.path def process_remote_edit
invoke_editor(tempfile_path, line, reload) file_name, line = retrieve_file_and_line
source = File.read(tempfile_path)
_pry_.evaluate_ruby source
state.dynamical_ex_file = source.split("\n") # Sanitize blanks.
end sanitized_file_name = Shellwords.escape(file_name)
else
# Sanitize blanks.
sanitized_file_name = Shellwords.escape(file_name)
invoke_editor(sanitized_file_name, line, reload) Pry::Editor.invoke_editor(sanitized_file_name, line, reload?(file_name))
set_file_and_dir_locals(sanitized_file_name) set_file_and_dir_locals(sanitized_file_name)
if reload if reload?(file_name)
silence_warnings do silence_warnings do
TOPLEVEL_BINDING.eval(File.read(file_name), file_name) TOPLEVEL_BINDING.eval(File.read(file_name), file_name)
end
end end
end end
end end