supercharged the `edit` command

(1) It now dumps contents of input buffer in an editor when you type 'edit' (or edit -t) inside a non-empty input buffer
(2) it accepts the file:line syntax. This closes #204

When it evals files it now (by default) just replaces the content of eval_string with the content of the file. This is particularly useful if content of file was an incomplete expression as no error is raised instead
the input buffer just awaits more input.
This commit is contained in:
John Mair 2011-09-05 16:14:24 +12:00
parent 1b98ac8eb2
commit 0ca33cdf16
1 changed files with 10 additions and 12 deletions

View File

@ -108,16 +108,16 @@ class Pry
end end
end end
command "edit", "Invoke the default editor on a file. Type `edit --help` for more info", :keep_retval => true do |*args| command "edit", "Invoke the default editor on a file. Type `edit --help` for more info" do |*args|
opts = Slop.parse!(args) do |opt| opts = Slop.parse!(args) do |opt|
opt.banner "Usage: edit [OPTIONS] [FILE]\n" \ opt.banner "Usage: edit [OPTIONS] [FILE]\n" \
"Edit the method FILE in an editor.\n" \ "Edit the method FILE in an editor.\nWhen no file given, opens editor with contents of input buffer and evals after closing." \
"Ensure #{text.bold("Pry.config.editor")} is set to your editor of choice.\n" \ "\nEnsure #{text.bold("Pry.config.editor")} is set to your editor of choice.\n" \
"e.g: edit sample.rb" "e.g: edit sample.rb"
opt.on :r, "reload", "Eval file content after editing (evals at top level)" opt.on :r, "reload", "Eval file content after editing (evals at top level)"
opt.on :ex, "Open an editor at the line and file that generated the most recent Exception." opt.on :ex, "Open an editor at the line and file that generated the most recent Exception."
opt.on :t, "temp", "Open a temporary file in an editor and eval it in current context after closing." opt.on :t, "temp", "Open a temporary file in an editor with contents of input buffer and eval it in current context after closing (same as `edit` with no args)"
opt.on :p, "play", "Use the pry `play` command to eval the file content after editing." opt.on :p, "play", "Use the pry `play` command to eval the file content after editing."
opt.on :l, "line", "Specify line number to jump to in file", true, :as => Integer opt.on :l, "line", "Specify line number to jump to in file", true, :as => Integer
opt.on :h, :help, "This message." do opt.on :h, :help, "This message." do
@ -127,7 +127,6 @@ class Pry
next if opts.h? next if opts.h?
# the context the file will be eval'd in after closing # the context the file will be eval'd in after closing
context = TOPLEVEL_BINDING
should_reload = opts[:r] should_reload = opts[:r]
if opts.ex? if opts.ex?
@ -142,15 +141,14 @@ class Pry
line = _pry_.last_exception.line line = _pry_.last_exception.line
next output.puts "Exception has no associated file." if file_name.nil? next output.puts "Exception has no associated file." if file_name.nil?
next output.puts "Cannot edit exceptions raised in REPL." if Pry.eval_path == file_name next output.puts "Cannot edit exceptions raised in REPL." if Pry.eval_path == file_name
elsif opts.t? elsif opts.t? || arg_string.empty?
file_name = Tempfile.new(["tmp", ".rb"]).tap(&:close).path file_name = Tempfile.new(["tmp", ".rb"]).tap(&:close).path
line = 0 File.open(file_name, "w") { |f| f.puts eval_string } if !eval_string.empty?
line = eval_string.lines.count + 1
should_reload = true should_reload = true
context = target
else else
next output.puts("Need to specify a file.") if !args.first file_name, line = File.expand_path(args.first).split(/:/)
file_name = File.expand_path(args.first) line = line ? line.to_i : opts[:l].to_i
line = opts[:l].to_i
end end
invoke_editor(file_name, line) invoke_editor(file_name, line)
@ -162,7 +160,7 @@ class Pry
end end
elsif should_reload elsif should_reload
silence_warnings do silence_warnings do
context.eval(File.read(file_name), file_name) eval_string.replace(File.read(file_name))
end end
end end
end end