Class: Byebug::EditCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/byebug/commands/edit.rb

Overview

Edit a file from byebug's prompt.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

commands, find, format_subcmd, format_subcmds, help, inherited, #initialize, #match

Methods included from StringFunctions

#camelize, #prettify

Methods included from FileFunctions

#get_line, #get_lines, #n_lines, #normalize

Methods included from ParseFunctions

#get_int, #parse_steps, #syntax_valid?

Constructor Details

This class inherits a constructor from Byebug::Command

Class Method Details

.descriptionObject


45
46
47
48
49
50
51
52
53
# File 'lib/byebug/commands/edit.rb', line 45

def description
  prettify <<-EOD
    edit[ file:lineno] Edit specified files.

    With no argument, edits file containing most recent line listed.
    Editing targets can also be specified to start editing at a specific
    line in a specific file.
  EOD
end

.namesObject


41
42
43
# File 'lib/byebug/commands/edit.rb', line 41

def names
  %w(edit)
end

Instance Method Details

#executeObject


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/byebug/commands/edit.rb', line 14

def execute
  if !@match[1]
    return errmsg(pr('edit.errors.state')) unless @state.file
    file = @state.file
    line = @state.line if @state.line
  elsif (@pos_match = /([^:]+)[:]([0-9]+)/.match(@match[1]))
    file, line = @pos_match.captures
  else
    file = @match[1]
  end

  editor = ENV['EDITOR'] || 'vim'
  file = File.expand_path(file)

  unless File.exist?(file)
    return errmsg(pr('edit.errors.not_exist', file: file))
  end
  unless File.readable?(file)
    return errmsg(pr('edit.errors.not_readable', file: file))
  end

  cmd = line ? "#{editor} +#{line} #{file}" : "#{editor} #{file}"

  system(cmd)
end

#regexpObject


10
11
12
# File 'lib/byebug/commands/edit.rb', line 10

def regexp
  /^\s* ed(?:it)? (?:\s+(\S+))? \s*$/x
end