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:
commit
663be7be29
4 changed files with 79 additions and 16 deletions
|
@ -112,17 +112,21 @@ class Pry
|
||||||
# Pry.config.prompt = proc { |obj, nest_level, _pry_| "#{obj}:#{nest_level}> " }
|
# Pry.config.prompt = proc { |obj, nest_level, _pry_| "#{obj}:#{nest_level}> " }
|
||||||
attr_accessor :prompt
|
attr_accessor :prompt
|
||||||
|
|
||||||
# The default editor to use. Defaults to $EDITOR or nano if
|
# The default editor to use. Defaults to $VISUAL, $EDITOR, or a sensible fallback
|
||||||
# $EDITOR is not defined.
|
# for the platform.
|
||||||
# If `editor` is a String then that string is used as the shell
|
# 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
|
# 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
|
# 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
|
# @example String
|
||||||
# Pry.config.editor = "emacsclient"
|
# Pry.config.editor = "emacsclient"
|
||||||
# @example Callable
|
# @example Callable
|
||||||
# Pry.config.editor = proc { |file, line| "emacsclient #{file} +#{line}" }
|
# 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]
|
# @return [String, #call]
|
||||||
attr_accessor :editor
|
attr_accessor :editor
|
||||||
|
|
||||||
|
|
|
@ -88,8 +88,9 @@ class Pry
|
||||||
temp_file do |f|
|
temp_file do |f|
|
||||||
f.puts(content)
|
f.puts(content)
|
||||||
f.flush
|
f.flush
|
||||||
invoke_editor(f.path, line)
|
reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
|
||||||
if !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
|
invoke_editor(f.path, line, reload)
|
||||||
|
if reload
|
||||||
silence_warnings do
|
silence_warnings do
|
||||||
eval_string.replace(File.read(f.path))
|
eval_string.replace(File.read(f.path))
|
||||||
end
|
end
|
||||||
|
@ -138,10 +139,11 @@ class Pry
|
||||||
|
|
||||||
line = opts[:l].to_i if opts.present?(:line)
|
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)
|
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
|
silence_warnings do
|
||||||
TOPLEVEL_BINDING.eval(File.read(file_name), file_name)
|
TOPLEVEL_BINDING.eval(File.read(file_name), file_name)
|
||||||
end
|
end
|
||||||
|
@ -207,7 +209,7 @@ class Pry
|
||||||
temp_file do |f|
|
temp_file do |f|
|
||||||
f.puts lines.join
|
f.puts lines.join
|
||||||
f.flush
|
f.flush
|
||||||
invoke_editor(f.path, 0)
|
invoke_editor(f.path, 0, true)
|
||||||
|
|
||||||
if @method.alias?
|
if @method.alias?
|
||||||
with_method_transaction(original_name, @method.owner) do
|
with_method_transaction(original_name, @method.owner) do
|
||||||
|
@ -223,9 +225,10 @@ class Pry
|
||||||
def process_file
|
def process_file
|
||||||
file, line = extract_file_and_line
|
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
|
silence_warnings do
|
||||||
load file unless opts.present?(:'no-reload') || Pry.config.disable_auto_reload
|
load file if reload
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -113,12 +113,13 @@ class Pry
|
||||||
process_yardoc process_rdoc(comment, code_type)
|
process_yardoc process_rdoc(comment, code_type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoke_editor(file, line)
|
def invoke_editor(file, line, reloading)
|
||||||
raise CommandError, "Please set Pry.config.editor or export $EDITOR" unless Pry.config.editor
|
raise CommandError, "Please set Pry.config.editor or export $VISUAL or $EDITOR" unless Pry.config.editor
|
||||||
if Pry.config.editor.respond_to?(:call)
|
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
|
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
|
end
|
||||||
return nil unless editor_invocation
|
return nil unless editor_invocation
|
||||||
|
|
||||||
|
@ -138,6 +139,22 @@ class Pry
|
||||||
end
|
end
|
||||||
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
|
# Return the syntax for a given editor for starting the editor
|
||||||
# and moving to a particular line within that file
|
# and moving to a particular line within that file
|
||||||
def start_line_syntax_for_editor(file_name, line_number)
|
def start_line_syntax_for_editor(file_name, line_number)
|
||||||
|
@ -154,7 +171,7 @@ class Pry
|
||||||
when /^mate/, /^geany/
|
when /^mate/, /^geany/
|
||||||
"-l #{line_number} #{file_name}"
|
"-l #{line_number} #{file_name}"
|
||||||
when /^subl/
|
when /^subl/
|
||||||
"-w #{ file_name }:#{ line_number }"
|
"#{file_name}:#{line_number}"
|
||||||
when /^uedit32/
|
when /^uedit32/
|
||||||
"#{file_name}/#{line_number}"
|
"#{file_name}/#{line_number}"
|
||||||
when /^jedit/
|
when /^jedit/
|
||||||
|
|
|
@ -83,6 +83,23 @@ describe "Pry::DefaultCommands::Introspection" do
|
||||||
tf.close(true)
|
tf.close(true)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
describe "with --ex" do
|
describe "with --ex" do
|
||||||
|
@ -610,6 +627,28 @@ describe "Pry::DefaultCommands::Introspection" do
|
||||||
X.new.c.should == :kindaaa
|
X.new.c.should == :kindaaa
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue