Class: CtagsReader::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/ctags_reader/reader.rb

Overview

A Reader can be initialized either with a file (or any IO stream), or with a filename. In the second case, the filename is opened in read-only mode and parsed for tags.

Note that parsing is done immediately upon initialization.

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Reader

Returns a new instance of Reader.



12
13
14
15
16
17
18
19
20
# File 'lib/ctags_reader/reader.rb', line 12

def initialize(file)
  if file.is_a? String
    @filename = File.expand_path(file)
  else
    @file = file
  end

  @tag_index = nil
end

Instance Method Details

#fast_find(query) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/ctags_reader/reader.rb', line 50

def fast_find(query)
  if query.start_with?('^')
    # drop the ^, look for a prefix
    prefix = query[1..-1]
    tags.bsearch { |tag| prefix <=> tag.name[0...prefix.length] }
  else
    # should be a direct match
    (tag_index[query] || []).first
  end
end

#file(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/ctags_reader/reader.rb', line 27

def file(&block)
  if @file
    yield @file
  else
    File.open(@filename, 'r') do |file|
      yield file
    end
  end
end

#find(query) ⇒ Object



66
67
68
# File 'lib/ctags_reader/reader.rb', line 66

def find(query)
  find_all(query).first
end

#find_all(query) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ctags_reader/reader.rb', line 70

def find_all(query)
  if query.include?('#')
    class_name, method_name = query.split('#')
    class_filenames = find_all(class_name).map(&:filename).uniq

    find_all(method_name).select do |tag|
      class_filenames.include?(tag.filename)
    end
  elsif query.include?('::')
    filename = class_to_filename(query)
    class_name = query.split('::').last

    direct_find_all(class_name).select do |tag|
      tag.filename =~ /#{filename}$/
    end
  else
    direct_find_all(query)
  end
end

#namesObject



37
38
39
# File 'lib/ctags_reader/reader.rb', line 37

def names
  @names ||= tag_index.keys
end

#tag_countObject



45
46
47
# File 'lib/ctags_reader/reader.rb', line 45

def tag_count
  tags.count
end

#tag_indexObject



22
23
24
25
# File 'lib/ctags_reader/reader.rb', line 22

def tag_index
  parse if not @tag_index
  @tag_index
end

#tagsObject



41
42
43
# File 'lib/ctags_reader/reader.rb', line 41

def tags
  @tags ||= tag_index.values.flatten
end

#to_sObject



90
91
92
# File 'lib/ctags_reader/reader.rb', line 90

def to_s
  "#<CtagsReader::Reader:#{object_id} (#{tag_count} tags)>"
end