Sha256: 853255e881bcb4c791f5ba7c83e984494141f16da5b0098fc403a2a570cef9d3

Contents?: true

Size: 1.16 KB

Versions: 16

Compression:

Stored size: 1.16 KB

Contents

# -*- coding: utf-8 -*-
#
# @file 
# @brief
# @author ongaeshi
# @date   2013/05/03

require 'milkode/common/util'

module Milkode
  class FastGotoline
    def initialize(gotolines, yaml)
      @gotolines = gotolines
      @yaml      = yaml
    end

    def search_and_print(stdout)
      @gotolines.each do |gotoline|
        package_name, restpath = Util.divide_shortpath(gotoline[0][0])
        package                = @yaml.find_name(package_name)

        if package
          path          = File.join(package.directory, restpath)
          relative_path = Util.relative_path(path, Dir.pwd).to_s
          lineno        = gotoline[1]
          content       = get_text_lineno(path, lineno)

          if content          
            stdout.puts "#{relative_path}:#{lineno} #{content}"
          end
        end
      end
    end

    def get_text_lineno(path, no)
      index = no - 1

      begin
        open(path, "r") do |file|
          file.each_with_index do |line, i|
            return line.chomp if i == index
          end
        end
      rescue Errno::ENOENT
        # ファイルが見つからない時もnilを返す
      end

      nil
    end
    
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
milkode-1.8.9.2 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.9.1 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.9 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.8 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.7 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.6 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.5 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.4 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.3 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.2 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.1 lib/milkode/grep/fast_gotoline.rb
milkode-1.8.0 lib/milkode/grep/fast_gotoline.rb
milkode-1.7.1 lib/milkode/grep/fast_gotoline.rb
milkode-1.7.0 lib/milkode/grep/fast_gotoline.rb
milkode-1.6.1 lib/milkode/grep/fast_gotoline.rb
milkode-1.6.0 lib/milkode/grep/fast_gotoline.rb