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

Extracted out editor-related code to Pry::Editor

And updated commands to use the new API
This commit is contained in:
John Mair 2012-12-29 23:34:59 +01:00
parent cf1137932f
commit 9bda7a17a7
5 changed files with 112 additions and 90 deletions

View file

@ -248,3 +248,4 @@ require 'pry/pry_instance'
require 'pry/cli'
require 'pry/pager'
require 'pry/terminal_info'
require 'pry/editor'

View file

@ -60,7 +60,7 @@ class Pry
f.puts lines
f.flush
f.close(false)
invoke_editor(f.path, 0, true)
Pry::Editor.invoke_editor(f.path, 0, true)
source = wrap_for_nesting(wrap_for_owner(File.read(f.path)))
@ -79,7 +79,7 @@ class Pry
file, line = extract_file_and_line
reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
invoke_editor(file, opts["no-jump"] ? 0 : line, reload)
Pry::Editor.invoke_editor(file, opts["no-jump"] ? 0 : line, reload)
silence_warnings do
load file if reload
end

View file

@ -14,7 +14,7 @@ class Pry
def process(gem)
Dir.chdir(gem_spec(gem).full_gem_path) do
invoke_editor(".", 0, false)
Pry::Editor.invoke_editor(".", 0, false)
end
end

107
lib/pry/editor.rb Normal file
View file

@ -0,0 +1,107 @@
class Pry
class Editor
extend Pry::Helpers::BaseHelpers
extend Pry::Helpers::CommandHelpers
class << self
def edit_tempfile_with_content(initial_content, line=1)
temp_file do |f|
f.puts(initial_content)
f.flush
f.close(false)
invoke_editor(f.path, line, false)
File.read(f.path)
end
end
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)
args = [file, line, reloading][0...(Pry.config.editor.arity)]
editor_invocation = Pry.config.editor.call(*args)
else
editor_invocation = "#{Pry.config.editor} #{blocking_flag_for_editor(reloading)} #{start_line_syntax_for_editor(file, line)}"
end
return nil unless editor_invocation
if jruby?
begin
require 'spoon'
pid = Spoon.spawnp(*editor_invocation.split)
Process.waitpid(pid)
rescue FFI::NotFoundError
system(editor_invocation)
end
else
# Note we dont want to use Pry.config.system here as that
# may be invoked non-interactively (i.e via Open4), whereas we want to
# ensure the editor is always interactive
system(editor_invocation) or raise CommandError, "`#{editor_invocation}` gave exit status: #{$?.exitstatus}"
end
end
private
# 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 editor_name
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)
if windows?
file_name = file_name.gsub(/\//, '\\')
end
# special case for 1st line
return file_name if line_number <= 1
case editor_name
when /^[gm]?vi/, /^emacs/, /^nano/, /^pico/, /^gedit/, /^kate/
"+#{line_number} #{file_name}"
when /^mate/, /^geany/
"-l #{line_number} #{file_name}"
when /^subl/
"#{file_name}:#{line_number}"
when /^uedit32/
"#{file_name}/#{line_number}"
when /^jedit/
"#{file_name} +line:#{line_number}"
else
if windows?
"#{file_name}"
else
"+#{line_number} #{file_name}"
end
end
end
# Get the name of the binary that Pry.config.editor points to.
#
# This is useful for deciding which flags we pass to the editor as
# we can just use the program's name and ignore any absolute paths.
#
# @example
# Pry.config.editor="/home/conrad/bin/textmate -w"
# editor_name
# # => textmate
#
def editor_name
File.basename(Pry.config.editor).split(" ").first
end
end
end
end

View file

@ -101,92 +101,6 @@ class Pry
header << "#{Pry::Helpers::Text.bold("Number of lines:")} #{content.each_line.count.to_s}\n"
end
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)
args = [file, line, reloading][0...(Pry.config.editor.arity)]
editor_invocation = Pry.config.editor.call(*args)
else
editor_invocation = "#{Pry.config.editor} #{blocking_flag_for_editor(reloading)} #{start_line_syntax_for_editor(file, line)}"
end
return nil unless editor_invocation
if jruby?
begin
require 'spoon'
pid = Spoon.spawnp(*editor_invocation.split)
Process.waitpid(pid)
rescue FFI::NotFoundError
system(editor_invocation)
end
else
# Note we dont want to use Pry.config.system here as that
# may be invoked non-interactively (i.e via Open4), whereas we want to
# ensure the editor is always interactive
system(editor_invocation) or raise CommandError, "`#{editor_invocation}` gave exit status: #{$?.exitstatus}"
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 editor_name
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)
if windows?
file_name = file_name.gsub(/\//, '\\')
end
# special case for 1st line
return file_name if line_number <= 1
case editor_name
when /^[gm]?vi/, /^emacs/, /^nano/, /^pico/, /^gedit/, /^kate/
"+#{line_number} #{file_name}"
when /^mate/, /^geany/
"-l #{line_number} #{file_name}"
when /^subl/
"#{file_name}:#{line_number}"
when /^uedit32/
"#{file_name}/#{line_number}"
when /^jedit/
"#{file_name} +line:#{line_number}"
else
if windows?
"#{file_name}"
else
"+#{line_number} #{file_name}"
end
end
end
# Get the name of the binary that Pry.config.editor points to.
#
# This is useful for deciding which flags we pass to the editor as
# we can just use the program's name and ignore any absolute paths.
#
# @example
# Pry.config.editor="/home/conrad/bin/textmate -w"
# editor_name
# # => textmate
#
def editor_name
File.basename(Pry.config.editor).split(" ").first
end
# Remove any common leading whitespace from every line in `text`.
#
# This can be used to make a HEREDOC line up with the left margin, without