2012-08-11 20:22:29 -04:00
|
|
|
class Pry
|
2012-10-21 20:00:51 -04:00
|
|
|
# Uses the following state variables:
|
|
|
|
# - dynamical_ex_file [Array<String>]
|
|
|
|
# Utilised in `edit --ex --patch` operations. Contains the source code
|
|
|
|
# of a monkey patched file, in which an exception was raised. We store
|
|
|
|
# the entire source code because an exception may happen anywhere in the
|
|
|
|
# code and there is no way to predict that. So we simply superimpose
|
|
|
|
# everything (admittedly, doing extra job).
|
2012-12-25 16:35:17 -05:00
|
|
|
class Command::Edit < Pry::ClassCommand
|
2012-12-30 23:15:25 -05:00
|
|
|
require 'pry/commands/edit/method_patcher'
|
|
|
|
|
2012-12-25 16:35:17 -05:00
|
|
|
match 'edit'
|
2012-08-11 21:26:59 -04:00
|
|
|
group 'Editing'
|
2012-12-25 16:35:17 -05:00
|
|
|
description 'Invoke the default editor on a file.'
|
2012-08-11 20:22:29 -04:00
|
|
|
|
|
|
|
banner <<-BANNER
|
2012-12-30 23:48:48 -05:00
|
|
|
Usage: edit [--no-reload|--reload|--patch] [--line LINE] [--temp|--ex|FILE[:LINE]|OBJECT|--in N]
|
2012-08-11 20:22:29 -04:00
|
|
|
|
|
|
|
Open a text editor. When no FILE is given, edits the pry input buffer.
|
|
|
|
Ensure Pry.config.editor is set to your editor of choice.
|
|
|
|
|
|
|
|
e.g: `edit sample.rb`
|
|
|
|
e.g: `edit sample.rb --line 105`
|
2012-12-30 23:48:48 -05:00
|
|
|
e.g: `edit MyClass#my_method`
|
|
|
|
e.g: `edit -p MyClass#my_method`
|
|
|
|
e.g: `edit YourClass`
|
2012-08-11 20:22:29 -04:00
|
|
|
e.g: `edit --ex`
|
|
|
|
|
|
|
|
https://github.com/pry/pry/wiki/Editor-integration#wiki-Edit_command
|
|
|
|
BANNER
|
|
|
|
|
|
|
|
def options(opt)
|
|
|
|
opt.on :e, :ex, "Open the file that raised the most recent exception (_ex_.file)", :optional_argument => true, :as => Integer
|
|
|
|
opt.on :i, :in, "Open a temporary file containing the Nth input expression. N may be a range.", :optional_argument => true, :as => Range, :default => -1..-1
|
|
|
|
opt.on :t, :temp, "Open an empty temporary file"
|
|
|
|
opt.on :l, :line, "Jump to this line in the opened file", :argument => true, :as => Integer
|
|
|
|
opt.on :n, :"no-reload", "Don't automatically reload the edited code"
|
2012-10-21 20:00:51 -04:00
|
|
|
opt.on :c, :current, "Open the current __FILE__ and at __LINE__ (as returned by `whereami`)."
|
2012-08-11 20:22:29 -04:00
|
|
|
opt.on :r, :reload, "Reload the edited code immediately (default for ruby files)"
|
2012-12-30 23:48:48 -05:00
|
|
|
opt.on :p, :patch, "Instead of editing the object's file, try to edit in a tempfile and apply as a monkey patch."
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
|
2012-08-21 01:12:03 -04:00
|
|
|
def complete(search)
|
|
|
|
super + Bond::Rc.files(search.split(" ").last || '')
|
|
|
|
end
|
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
def bad_option_combination?
|
|
|
|
[opts.present?(:ex), opts.present?(:temp),
|
|
|
|
opts.present?(:in), !args.empty?].count(true) > 1
|
|
|
|
end
|
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def process
|
2012-12-29 17:37:55 -05:00
|
|
|
if bad_option_combination?
|
2012-08-11 20:22:29 -04:00
|
|
|
raise CommandError, "Only one of --ex, --temp, --in and FILE may be specified."
|
|
|
|
end
|
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
if local_edit?
|
2012-08-11 20:22:29 -04:00
|
|
|
# edit of local code, eval'd within pry.
|
|
|
|
process_local_edit
|
2012-12-30 21:29:05 -05:00
|
|
|
elsif runtime_patch?
|
2012-12-30 23:15:25 -05:00
|
|
|
# patch code without persisting changes
|
2012-12-30 21:29:05 -05:00
|
|
|
apply_runtime_patch
|
2012-08-11 20:22:29 -04:00
|
|
|
else
|
|
|
|
# edit of remote code, eval'd at top-level
|
|
|
|
process_remote_edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-30 23:48:48 -05:00
|
|
|
def process_local_edit
|
|
|
|
content = initial_temp_file_content
|
|
|
|
|
|
|
|
if local_reload?
|
|
|
|
silence_warnings do
|
|
|
|
eval_string.replace Pry::Editor.edit_tempfile_with_content(content, content.lines.count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_runtime_patch
|
|
|
|
if patch_exception?
|
|
|
|
apply_runtime_patch_to_exception
|
|
|
|
else
|
|
|
|
if code_object.is_a?(Pry::Method)
|
|
|
|
MethodPatcher.new(code_object, target, _pry_).perform_patch
|
|
|
|
else
|
|
|
|
raise NotImplementedError, "Cannot yet patch #{code_object} objects!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_remote_edit
|
|
|
|
file_name, line = retrieve_file_and_line
|
|
|
|
|
|
|
|
# Sanitize blanks.
|
|
|
|
sanitized_file_name = Shellwords.escape(file_name)
|
|
|
|
|
|
|
|
Pry::Editor.invoke_editor(sanitized_file_name, line, reload?(file_name))
|
|
|
|
set_file_and_dir_locals(sanitized_file_name)
|
|
|
|
|
|
|
|
if reload?(file_name)
|
|
|
|
silence_warnings do
|
|
|
|
TOPLEVEL_BINDING.eval(File.read(file_name), file_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-30 23:15:25 -05:00
|
|
|
def code_object
|
2012-12-30 23:48:48 -05:00
|
|
|
@code_object ||= args.first && !probably_a_file?(args.first) &&
|
|
|
|
Pry::CodeObject.lookup(args.first, target, _pry_)
|
|
|
|
end
|
|
|
|
|
|
|
|
def local_edit?
|
|
|
|
!opts.present?(:ex) && !opts.present?(:current) && args.empty?
|
2012-12-30 21:29:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def runtime_patch?
|
2012-12-30 23:15:25 -05:00
|
|
|
opts.present?(:patch) || dynamically_defined_method?
|
|
|
|
end
|
|
|
|
|
|
|
|
def dynamically_defined_method?
|
2012-12-30 23:48:48 -05:00
|
|
|
code_object.is_a?(Pry::Method) &&
|
|
|
|
code_object.dynamically_defined?
|
2012-12-30 21:29:05 -05:00
|
|
|
end
|
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
def retrieve_input_expression
|
2012-08-11 20:22:29 -04:00
|
|
|
case opts[:i]
|
|
|
|
when Range
|
|
|
|
(_pry_.input_array[opts[:i]] || []).join
|
|
|
|
when Fixnum
|
|
|
|
_pry_.input_array[opts[:i]] || ""
|
|
|
|
else
|
2012-08-18 23:58:48 -04:00
|
|
|
raise Pry::CommandError, "Not a valid range: #{opts[:i]}"
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
def reloadable?
|
|
|
|
opts.present?(:reload) || opts.present?(:ex)
|
|
|
|
end
|
|
|
|
|
|
|
|
def never_reload?
|
|
|
|
opts.present?(:'no-reload') || Pry.config.disable_auto_reload
|
|
|
|
end
|
|
|
|
|
|
|
|
# conditions much less strict than for reload? (which is for remote reloads)
|
|
|
|
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 != "" } || ""
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-12-29 17:37:55 -05:00
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-22 16:56:59 -05:00
|
|
|
def probably_a_file?(str)
|
|
|
|
[".rb", ".c", ".py", ".yml", ".gemspec"].include? File.extname(str) ||
|
2012-12-29 17:37:55 -05:00
|
|
|
str =~ /\/|\\/
|
2012-12-22 16:56:59 -05:00
|
|
|
end
|
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
def file_and_line_for_exception
|
|
|
|
raise CommandError, "No exception found." if _pry_.last_exception.nil?
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
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
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
file_name = RbxPath.convert_path_to_full(file_name) if RbxPath.is_core_path?(file_name)
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
[file_name, line]
|
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
def current_file_and_line
|
|
|
|
[target.eval("__FILE__"), target.eval("__LINE__")]
|
|
|
|
end
|
2012-10-21 20:00:51 -04:00
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
def object_file_and_line
|
2012-12-30 23:15:25 -05:00
|
|
|
if code_object
|
2012-12-29 17:37:55 -05:00
|
|
|
[code_object.source_file, code_object.source_line]
|
2012-08-11 20:22:29 -04:00
|
|
|
else
|
2012-12-29 17:37:55 -05:00
|
|
|
# break up into file:line
|
|
|
|
file_name = File.expand_path(args.first)
|
|
|
|
line = file_name.sub!(/:(\d+)$/, "") ? $1.to_i : 1
|
|
|
|
[file_name, line]
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-12-29 17:37:55 -05:00
|
|
|
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
|
2012-08-11 20:22:29 -04:00
|
|
|
|
|
|
|
if not_a_real_file?(file_name)
|
|
|
|
raise CommandError, "#{file_name} is not a valid file name, cannot edit!"
|
|
|
|
end
|
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
[file_name, opts.present?(:line) ? opts[:l].to_i : line]
|
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
def patch_exception?
|
|
|
|
opts.present?(:ex) && opts.present?(:patch)
|
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
def apply_runtime_patch_to_exception
|
|
|
|
file_name, line = file_and_line_for_exception
|
|
|
|
lines = state.dynamical_ex_file || File.read(file_name)
|
2012-10-21 20:00:51 -04:00
|
|
|
|
2012-12-29 17:37:55 -05:00
|
|
|
source = Pry::Editor.edit_tempfile_with_content(lines)
|
|
|
|
_pry_.evaluate_ruby source
|
|
|
|
state.dynamical_ex_file = source.split("\n")
|
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-12-25 16:35:17 -05:00
|
|
|
|
|
|
|
Pry::Commands.add_command(Pry::Command::Edit)
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|