Sha256: 6af3564c87ee0d97054a0df07fe789c2b4436561dc93db0e10a1542fe1840aef

Contents?: true

Size: 1.47 KB

Versions: 12

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

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
      <<-DESCRIPTION
        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
      DESCRIPTION
    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}"

      Kernel.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

12 entries across 12 versions & 5 rubygems

Version Path
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/byebug-10.0.2/lib/byebug/commands/edit.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/byebug-10.0.2/lib/byebug/commands/edit.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/byebug-10.0.2/lib/byebug/commands/edit.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/byebug-10.0.2/lib/byebug/commands/edit.rb
jets-0.5.5 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/edit.rb
jets-0.5.4 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/edit.rb
jets-0.5.3 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/edit.rb
jets-0.5.2 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/edit.rb
jets-0.5.1 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/edit.rb
byebug-10.0.2 lib/byebug/commands/edit.rb
byebug-10.0.1 lib/byebug/commands/edit.rb
byebug-10.0.0 lib/byebug/commands/edit.rb