Sha256: 5fb67584f1bc8d1fdef597b1655804afcdbb2549a61f77a4c5e656af4cd1b233

Contents?: true

Size: 1.42 KB

Versions: 13

Compression:

Stored size: 1.42 KB

Contents

require 'byebug/command'

module Byebug
  #
  # Edit a file from byebug's prompt.
  #
  class EditCommand < Command
    self.allow_in_control = true
    self.allow_in_post_mortem = true

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

    def self.description
      <<-EOD
        edit[ file:lineno]

        #{short_description}

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

    def self.short_description
      'Edits source files'
    end

    def execute
      file, line = location(@match[1])
      return edit_error('not_exist', file) unless File.exist?(file)
      return edit_error('not_readable', file) unless File.readable?(file)

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

      system(cmd)
    end

    private

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

      [File.expand_path(file), line]
    end

    def editor
      ENV['EDITOR'] || 'vim'
    end

    def edit_error(type, file)
      errmsg(pr("edit.errors.#{type}", file: file))
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
byebug-8.2.5 lib/byebug/commands/edit.rb
byebug-8.2.4 lib/byebug/commands/edit.rb
byebug-8.2.3 lib/byebug/commands/edit.rb
byebug-8.2.2 lib/byebug/commands/edit.rb
byebug-8.2.1 lib/byebug/commands/edit.rb
byebug-8.2.0 lib/byebug/commands/edit.rb
byebug-8.1.0 lib/byebug/commands/edit.rb
byebug-8.0.1 lib/byebug/commands/edit.rb
byebug-8.0.0 lib/byebug/commands/edit.rb
byebug-7.0.0 lib/byebug/commands/edit.rb
byebug-6.0.2 lib/byebug/commands/edit.rb
byebug-6.0.1 lib/byebug/commands/edit.rb
byebug-6.0.0 lib/byebug/commands/edit.rb