Class: Pry::Editor

Inherits:
Object show all
Includes:
Helpers::CommandHelpers
Defined in:
lib/pry/editor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CommandHelpers

#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent

Methods included from Helpers::OptionsHelpers

method_object, method_options

Constructor Details

#initialize(pry_instance) ⇒ Editor

Returns a new instance of Editor.



27
28
29
# File 'lib/pry/editor.rb', line 27

def initialize(pry_instance)
  @pry_instance = pry_instance
end

Instance Attribute Details

#pry_instanceObject (readonly)

Returns the value of attribute pry_instance.



25
26
27
# File 'lib/pry/editor.rb', line 25

def pry_instance
  @pry_instance
end

Class Method Details

.defaultObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pry/editor.rb', line 7

def self.default
  if (visual = Pry::Env['VISUAL'])
    return visual
  end

  if (editor = Pry::Env['EDITOR'])
    return editor
  end

  return 'notepad' if Helpers::Platform.windows?

  %w[editor nano vi].find do |editor_exe|
    Kernel.system("which #{editor_exe} > /dev/null 2>&1")
  end
end

Instance Method Details

#build_editor_invocation_string(file, line, blocking) ⇒ Object

Generate the string that’s used to start the editor. This includes all the flags we want as well as the file and line number we want to open at.



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pry/editor.rb', line 60

def build_editor_invocation_string(file, line, blocking)
  if pry_instance.config.editor.respond_to?(:call)
    args = [file, line, blocking][0...(pry_instance.config.editor.arity)]
    pry_instance.config.editor.call(*args)
  else
    sanitized_file = Helpers::Platform.windows? ? file : Shellwords.escape(file)
    editor = pry_instance.config.editor
    flag = blocking_flag_for_editor(blocking)
    start_line = start_line_syntax_for_editor(sanitized_file, line)
    "#{editor} #{flag} #{start_line}"
  end
end

#edit_tempfile_with_content(initial_content, line = 1) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/pry/editor.rb', line 31

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, true)
    File.read(f.path)
  end
end

#invoke_editor(file, line, blocking = true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pry/editor.rb', line 41

def invoke_editor(file, line, blocking = true)
  unless pry_instance.config.editor
    raise CommandError,
          "Please set Pry.config.editor or export $VISUAL or $EDITOR"
  end

  editor_invocation = build_editor_invocation_string(file, line, blocking)
  return nil unless editor_invocation

  if Helpers::Platform.jruby?
    open_editor_on_jruby(editor_invocation)
  else
    open_editor(editor_invocation)
  end
end