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

Merge pull request #513 from misfo/editor-reloading-arg

Allow `Pry.config.editor` procs that take a `reloading` arg
This commit is contained in:
John Mair 2012-04-10 07:02:04 -07:00
commit 663be7be29
4 changed files with 79 additions and 16 deletions

View file

@ -112,17 +112,21 @@ class Pry
# Pry.config.prompt = proc { |obj, nest_level, _pry_| "#{obj}:#{nest_level}> " }
attr_accessor :prompt
# The default editor to use. Defaults to $EDITOR or nano if
# $EDITOR is not defined.
# The default editor to use. Defaults to $VISUAL, $EDITOR, or a sensible fallback
# 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,12 +113,13 @@ class Pry
process_yardoc process_rdoc(comment, code_type)
end
def invoke_editor(file, line)
raise CommandError, "Please set Pry.config.editor or export $EDITOR" unless Pry.config.editor
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)}"
editor_invocation = "#{Pry.config.editor} #{blocking_flag_for_editor(reloading)} #{start_line_syntax_for_editor(file, line)}"
end
return nil unless editor_invocation
@ -138,6 +139,22 @@ class Pry
end
end
# Some editors that run outside the terminal allow you to control whether or
# not to block the process from which they were launched (in this case, Pry).
# For those editors, return the flag that produces the desired behavior.
def blocking_flag_for_editor(block)
case Pry.config.editor
when /^emacsclient/
'--no-wait' unless block
when /^[gm]vim/
'--nofork' if block
when /^jedit/
'-wait' if block
when /^mate/, /^subl/
'-w' if block
end
end
# Return the syntax for a given editor for starting the editor
# and moving to a particular line within that file
def start_line_syntax_for_editor(file_name, line_number)
@ -154,7 +171,7 @@ class Pry
when /^mate/, /^geany/
"-l #{line_number} #{file_name}"
when /^subl/
"-w #{ file_name }:#{ line_number }"
"#{file_name}:#{line_number}"
when /^uedit32/
"#{file_name}/#{line_number}"
when /^jedit/

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