Sha256: 36f7d214fc827079826388ba49595f8c82f9faf4c4ddc94686591015e167dddc

Contents?: true

Size: 1.5 KB

Versions: 13

Compression:

Stored size: 1.5 KB

Contents

require 'shellwords'

module Looksee
  class Editor
    def initialize(command)
      @command = command.dup
      infer_arguments
    end

    attr_reader :command

    #
    # Run the editor command for the +method_name+ of +object+.
    #
    def edit(object, method_name)
      method = LookupPath.new(object).find(method_name.to_s) or
        raise NoMethodError, "no method `#{method_name}' in lookup path of #{object.class} instance"
      file, line = Looksee.adapter.source_location(method)
      if !file
        raise NoSourceLocationError, "no source location for #{method.owner}##{method.name}"
      elsif !File.exist?(file)
        raise NoSourceFileError, "cannot find source file: #{file}"
      else
        run(file, line)
      end
    end

    #
    # Run the editor command for the given file and line.
    #
    def run(file, line)
      system *command_for(file, line)
    end

    #
    # Return the editor command for the given file and line.
    #
    # This is an array of the command with its arguments.
    #
    def command_for(file, line)
      line = line.to_s
      words = Shellwords.shellwords(command)
      words.map! do |word|
        word.gsub!(/%f/, file)
        word.gsub!(/%l/, line)
        word.gsub!(/%%/, '%')
        word
      end
    end

    private

    def infer_arguments
      return if command =~ /%[fl]/

      case command[/\S+/]
      when /\A(?:g?vim?|.*macs|pico|nano)\z/
        command << " +%l %f"
      when 'mate'
        command << " -l%l %f"
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
looksee-3.1.1 lib/looksee/editor.rb
looksee-3.1.0-universal-java-1.8 lib/looksee/editor.rb
looksee-3.1.0 lib/looksee/editor.rb
looksee-3.0.1-universal-java-1.8 lib/looksee/editor.rb
looksee-3.0.1 lib/looksee/editor.rb
looksee-3.0.0-universal-java-1.8 lib/looksee/editor.rb
looksee-3.0.0 lib/looksee/editor.rb
looksee-2.1.1-universal-java-1.6 lib/looksee/editor.rb
looksee-2.1.1 lib/looksee/editor.rb
looksee-2.1.0-universal-java-1.6 lib/looksee/editor.rb
looksee-2.1.0 lib/looksee/editor.rb
looksee-2.0.0-universal-java-1.6 lib/looksee/editor.rb
looksee-2.0.0 lib/looksee/editor.rb