Class: RubyIndexer::IndexVisitor

Inherits:
YARP::Visitor
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_indexer/lib/ruby_indexer/visitor.rb

Instance Method Summary collapse

Constructor Details

#initialize(index, parse_result, file_path) ⇒ IndexVisitor

Returns a new instance of IndexVisitor.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 9

def initialize(index, parse_result, file_path)
  @index = index
  @parse_result = parse_result
  @file_path = file_path
  @stack = T.let([], T::Array[String])
  @comments_by_line = T.let(
    parse_result.comments.to_h do |c|
      [c.location.start_line, c]
    end,
    T::Hash[Integer, YARP::Comment],
  )

  super()
end

Instance Method Details

#runObject



25
26
27
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 25

def run
  visit(@parse_result.value)
end

#visit(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 30

def visit(node)
  case node
  when YARP::ProgramNode, YARP::StatementsNode
    visit_child_nodes(node)
  when YARP::ClassNode
    add_index_entry(node, Index::Entry::Class)
  when YARP::ModuleNode
    add_index_entry(node, Index::Entry::Module)
  when YARP::ConstantWriteNode, YARP::ConstantOrWriteNode
    name = fully_qualify_name(node.name.to_s)
    add_constant(node, name)
  when YARP::ConstantPathWriteNode, YARP::ConstantPathOrWriteNode, YARP::ConstantPathOperatorWriteNode,
    YARP::ConstantPathAndWriteNode

    # ignore variable constants like `var::FOO` or `self.class::FOO`
    return unless node.target.parent.nil? || node.target.parent.is_a?(YARP::ConstantReadNode)

    name = fully_qualify_name(node.target.location.slice)
    add_constant(node, name)
  when YARP::CallNode
    message = node.message
    handle_private_constant(node) if message == "private_constant"
  end
end

#visit_all(nodes) ⇒ Object



57
58
59
# File 'lib/ruby_indexer/lib/ruby_indexer/visitor.rb', line 57

def visit_all(nodes)
  nodes.each { |node| visit(node) }
end