Class: RubyIndexer::Collector

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

Constant Summary collapse

LEAVE_EVENT =
T.let(Object.new.freeze, Object)

Instance Method Summary collapse

Constructor Details

#initialize(index, parse_result, file_path) ⇒ Collector

Returns a new instance of Collector.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_indexer/lib/ruby_indexer/collector.rb', line 11

def initialize(index, parse_result, file_path)
  @index = index
  @file_path = file_path
  @stack = T.let([], T::Array[String])
   = T.let(
    parse_result.comments.to_h do |c|
      [c.location.start_line, c]
    end,
    T::Hash[Integer, Prism::Comment],
  )
  @queue = T.let([], T::Array[Object])
  @current_owner = T.let(nil, T.nilable(Entry::Namespace))

  super()
end

Instance Method Details

#collect(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_indexer/lib/ruby_indexer/collector.rb', line 28

def collect(node)
  @queue = [node]

  until @queue.empty?
    node_or_event = @queue.shift

    case node_or_event
    when Prism::ProgramNode
      @queue << node_or_event.statements
    when Prism::StatementsNode
      T.unsafe(@queue).prepend(*node_or_event.body)
    when Prism::ClassNode
      add_class_entry(node_or_event)
    when Prism::ModuleNode
      add_module_entry(node_or_event)
    when Prism::MultiWriteNode
      handle_multi_write_node(node_or_event)
    when Prism::ConstantPathWriteNode
      handle_constant_path_write_node(node_or_event)
    when Prism::ConstantPathOrWriteNode
      handle_constant_path_or_write_node(node_or_event)
    when Prism::ConstantPathOperatorWriteNode
      handle_constant_path_operator_write_node(node_or_event)
    when Prism::ConstantPathAndWriteNode
      handle_constant_path_and_write_node(node_or_event)
    when Prism::ConstantWriteNode
      handle_constant_write_node(node_or_event)
    when Prism::ConstantOrWriteNode
      name = fully_qualify_name(node_or_event.name.to_s)
      add_constant(node_or_event, name)
    when Prism::ConstantAndWriteNode
      name = fully_qualify_name(node_or_event.name.to_s)
      add_constant(node_or_event, name)
    when Prism::ConstantOperatorWriteNode
      name = fully_qualify_name(node_or_event.name.to_s)
      add_constant(node_or_event, name)
    when Prism::CallNode
      handle_call_node(node_or_event)
    when Prism::DefNode
      handle_def_node(node_or_event)
    when LEAVE_EVENT
      @stack.pop
    end
  end
end