Class: Milkode::FastGotoline

Inherits:
Object
  • Object
show all
Defined in:
lib/milkode/grep/fast_gotoline.rb

Instance Method Summary collapse

Constructor Details

#initialize(gotolines, yaml) ⇒ FastGotoline

Returns a new instance of FastGotoline.



12
13
14
15
# File 'lib/milkode/grep/fast_gotoline.rb', line 12

def initialize(gotolines, yaml)
  @gotolines = gotolines
  @yaml      = yaml
end

Instance Method Details

#get_text_lineno(path, no) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/milkode/grep/fast_gotoline.rb', line 35

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

#search_and_print(stdout) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/milkode/grep/fast_gotoline.rb', line 17

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