Allow `Pry.config.editor` procs that take a `reloading` arg

This allow the user to implement an editor proc that only blocks the Pry
process if Pry is going to reload code when it returns:

    Pry.config.editor = proc do |file, line, reloading|
      "subl #{'--wait' if reloading} #{file}:#{line}"
    end
This commit is contained in:
misfo 2012-04-03 21:48:09 -05:00
parent f78ed73443
commit cd8ca7b82b
4 changed files with 58 additions and 11 deletions

View File

@ -116,13 +116,17 @@ class Pry
# for the platform.
# If `editor` is a String then that string is used as the shell
# command to invoke the editor. If `editor` is callable (e.g a
# Proc) then `file` and `line` are passed in as parameters and the
# Proc) then `file`, `line`, and `reloading` are passed in as parameters and the
# return value of that callable invocation is used as the exact
# shell command to invoke the editor.
# shell command to invoke the editor. `reloading` indicates whether Pry will be
# reloading code after the shell command returns. Any or all of these parameters
# can be omitted from the callable's signature.
# @example String
# Pry.config.editor = "emacsclient"
# @example Callable
# Pry.config.editor = proc { |file, line| "emacsclient #{file} +#{line}" }
# @example Callable waiting only if reloading
# Pry.config.editor = proc { |file, line, reloading| "subl #{'--wait' if reloading} #{file}:#{line}" }
# @return [String, #call]
attr_accessor :editor

View File

@ -88,8 +88,9 @@ class Pry
temp_file do |f|
f.puts(content)
f.flush
invoke_editor(f.path, line)
if !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
invoke_editor(f.path, line, reload)
if reload
silence_warnings do
eval_string.replace(File.read(f.path))
end
@ -138,10 +139,11 @@ class Pry
line = opts[:l].to_i if opts.present?(:line)
invoke_editor(file_name, line)
reload = opts.present?(:reload) || ((opts.present?(:ex) || file_name.end_with?(".rb")) && !opts.present?(:'no-reload')) && !Pry.config.disable_auto_reload
invoke_editor(file_name, line, reload)
set_file_and_dir_locals(file_name)
if opts.present?(:reload) || ((opts.present?(:ex) || file_name.end_with?(".rb")) && !opts.present?(:'no-reload')) && !Pry.config.disable_auto_reload
if reload
silence_warnings do
TOPLEVEL_BINDING.eval(File.read(file_name), file_name)
end
@ -207,7 +209,7 @@ class Pry
temp_file do |f|
f.puts lines.join
f.flush
invoke_editor(f.path, 0)
invoke_editor(f.path, 0, true)
if @method.alias?
with_method_transaction(original_name, @method.owner) do
@ -223,9 +225,10 @@ class Pry
def process_file
file, line = extract_file_and_line
invoke_editor(file, opts["no-jump"] ? 0 : line)
reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
invoke_editor(file, opts["no-jump"] ? 0 : line, reload)
silence_warnings do
load file unless opts.present?(:'no-reload') || Pry.config.disable_auto_reload
load file if reload
end
end

View File

@ -113,10 +113,11 @@ class Pry
process_yardoc process_rdoc(comment, code_type)
end
def invoke_editor(file, line)
def invoke_editor(file, line, reloading)
raise CommandError, "Please set Pry.config.editor or export $VISUAL or $EDITOR" unless Pry.config.editor
if Pry.config.editor.respond_to?(:call)
editor_invocation = Pry.config.editor.call(file, line)
args = [file, line, reloading][0...(Pry.config.editor.arity)]
editor_invocation = Pry.config.editor.call(*args)
else
editor_invocation = "#{Pry.config.editor} #{start_line_syntax_for_editor(file, line)}"
end

View File

@ -83,6 +83,23 @@ describe "Pry::DefaultCommands::Introspection" do
tf.close(true)
end
end
describe do
before do
@reloading = nil
Pry.config.editor = lambda do |file, line, reloading|
@file = file; @line = line; @reloading = reloading
nil
end
end
it "should pass the editor a reloading arg" do
mock_pry("edit foo.rb")
@reloading.should == true
mock_pry("edit -n foo.rb")
@reloading.should == false
end
end
end
describe "with --ex" do
@ -610,6 +627,28 @@ describe "Pry::DefaultCommands::Introspection" do
X.new.c.should == :kindaaa
end
end
describe 'with three-arg editor' do
before do
@old_editor = Pry.config.editor
@file, @line, @reloading = nil, nil, nil
Pry.config.editor = lambda do |file, line, reloading|
@file = file; @line = line; @reloading = reloading
nil
end
end
after do
Pry.config.editor = @old_editor
end
it "should pass the editor a reloading arg" do
mock_pry('edit-method X.x')
@reloading.should == true
mock_pry('edit-method -n X.x')
@reloading.should == false
end
end
end
end