Sha256: dcbd01ba84e76dfd73aac28c1480ea4235a548728b007033c5e673ea1571535c

Contents?: true

Size: 1.48 KB

Versions: 6770

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require_relative "../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

6,770 entries across 6,767 versions & 30 rubygems

Version Path
cybrid_api_organization_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_bank_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_id_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_organization_ruby-0.123.117 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_bank_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_organization_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_id_ruby-0.123.116 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_bank_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_id_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_organization_ruby-0.123.115 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
ory-client-1.15.17 vendor/bundle/ruby/3.1.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_bank_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_organization_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_id_ruby-0.123.114 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_organization_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_bank_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_id_ruby-0.123.113 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_organization_ruby-0.123.112 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb
cybrid_api_bank_ruby-0.123.112 vendor/bundle/ruby/3.3.0/gems/byebug-11.1.3/lib/byebug/commands/edit.rb