Class: RubyIndexer::ReferenceFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb

Defined Under Namespace

Classes: ConstTarget, InstanceVariableTarget, MethodTarget, Reference, Target

Instance Method Summary collapse

Constructor Details

#initialize(target, index, dispatcher, uri, include_declarations: true) ⇒ ReferenceFinder

: (Target target, RubyIndexer::Index index, Prism::Dispatcher dispatcher, URI::Generic uri, ?include_declarations: bool) -> void



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 65

def initialize(target, index, dispatcher, uri, include_declarations: true)
  @target = target
  @index = index
  @uri = uri
  @include_declarations = include_declarations
  @stack = [] #: Array[String]
  @references = [] #: Array[Reference]

  dispatcher.register(
    self,
    :on_class_node_enter,
    :on_class_node_leave,
    :on_module_node_enter,
    :on_module_node_leave,
    :on_singleton_class_node_enter,
    :on_singleton_class_node_leave,
    :on_def_node_enter,
    :on_def_node_leave,
    :on_multi_write_node_enter,
    :on_constant_path_write_node_enter,
    :on_constant_path_or_write_node_enter,
    :on_constant_path_operator_write_node_enter,
    :on_constant_path_and_write_node_enter,
    :on_constant_or_write_node_enter,
    :on_constant_path_node_enter,
    :on_constant_read_node_enter,
    :on_constant_write_node_enter,
    :on_constant_or_write_node_enter,
    :on_constant_and_write_node_enter,
    :on_constant_operator_write_node_enter,
    :on_instance_variable_read_node_enter,
    :on_instance_variable_write_node_enter,
    :on_instance_variable_and_write_node_enter,
    :on_instance_variable_operator_write_node_enter,
    :on_instance_variable_or_write_node_enter,
    :on_instance_variable_target_node_enter,
    :on_call_node_enter,
  )
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object

: (Prism::CallNode node) -> void



284
285
286
287
288
289
290
291
292
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 284

def on_call_node_enter(node)
  if @target.is_a?(MethodTarget) && (name = node.name.to_s) == @target.method_name
    @references << Reference.new(
      name,
      node.message_loc, #: as !nil
      declaration: false,
    )
  end
end

#on_class_node_enter(node) ⇒ Object

: (Prism::ClassNode node) -> void



113
114
115
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 113

def on_class_node_enter(node)
  @stack << node.constant_path.slice
end

#on_class_node_leave(node) ⇒ Object

: (Prism::ClassNode node) -> void



118
119
120
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 118

def on_class_node_leave(node)
  @stack.pop
end

#on_constant_and_write_node_enter(node) ⇒ Object

: (Prism::ConstantAndWriteNode node) -> void



226
227
228
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 226

def on_constant_and_write_node_enter(node)
  collect_constant_references(node.name.to_s, node.name_loc)
end

#on_constant_operator_write_node_enter(node) ⇒ Object

: (Prism::ConstantOperatorWriteNode node) -> void



231
232
233
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 231

def on_constant_operator_write_node_enter(node)
  collect_constant_references(node.name.to_s, node.name_loc)
end

#on_constant_or_write_node_enter(node) ⇒ Object

: (Prism::ConstantOrWriteNode node) -> void



221
222
223
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 221

def on_constant_or_write_node_enter(node)
  collect_constant_references(node.name.to_s, node.name_loc)
end

#on_constant_path_and_write_node_enter(node) ⇒ Object

: (Prism::ConstantPathAndWriteNode node) -> void



205
206
207
208
209
210
211
212
213
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 205

def on_constant_path_and_write_node_enter(node)
  target = node.target
  return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

  name = Index.constant_name(target)
  return unless name

  collect_constant_references(name, target.location)
end

#on_constant_path_node_enter(node) ⇒ Object

: (Prism::ConstantPathNode node) -> void



146
147
148
149
150
151
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 146

def on_constant_path_node_enter(node)
  name = Index.constant_name(node)
  return unless name

  collect_constant_references(name, node.location)
end

#on_constant_path_operator_write_node_enter(node) ⇒ Object

: (Prism::ConstantPathOperatorWriteNode node) -> void



194
195
196
197
198
199
200
201
202
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 194

def on_constant_path_operator_write_node_enter(node)
  target = node.target
  return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

  name = Index.constant_name(target)
  return unless name

  collect_constant_references(name, target.location)
end

#on_constant_path_or_write_node_enter(node) ⇒ Object

: (Prism::ConstantPathOrWriteNode node) -> void



183
184
185
186
187
188
189
190
191
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 183

def on_constant_path_or_write_node_enter(node)
  target = node.target
  return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

  name = Index.constant_name(target)
  return unless name

  collect_constant_references(name, target.location)
end

#on_constant_path_write_node_enter(node) ⇒ Object

: (Prism::ConstantPathWriteNode node) -> void



172
173
174
175
176
177
178
179
180
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 172

def on_constant_path_write_node_enter(node)
  target = node.target
  return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

  name = Index.constant_name(target)
  return unless name

  collect_constant_references(name, target.location)
end

#on_constant_read_node_enter(node) ⇒ Object

: (Prism::ConstantReadNode node) -> void



154
155
156
157
158
159
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 154

def on_constant_read_node_enter(node)
  name = Index.constant_name(node)
  return unless name

  collect_constant_references(name, node.location)
end

#on_constant_write_node_enter(node) ⇒ Object

: (Prism::ConstantWriteNode node) -> void



216
217
218
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 216

def on_constant_write_node_enter(node)
  collect_constant_references(node.name.to_s, node.name_loc)
end

#on_def_node_enter(node) ⇒ Object

: (Prism::DefNode node) -> void



236
237
238
239
240
241
242
243
244
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 236

def on_def_node_enter(node)
  if @target.is_a?(MethodTarget) && (name = node.name.to_s) == @target.method_name
    @references << Reference.new(name, node.name_loc, declaration: true)
  end

  if node.receiver.is_a?(Prism::SelfNode)
    @stack << "<Class:#{@stack.last}>"
  end
end

#on_def_node_leave(node) ⇒ Object

: (Prism::DefNode node) -> void



247
248
249
250
251
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 247

def on_def_node_leave(node)
  if node.receiver.is_a?(Prism::SelfNode)
    @stack.pop
  end
end

#on_instance_variable_and_write_node_enter(node) ⇒ Object

: (Prism::InstanceVariableAndWriteNode node) -> void



264
265
266
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 264

def on_instance_variable_and_write_node_enter(node)
  collect_instance_variable_references(node.name.to_s, node.name_loc, true)
end

#on_instance_variable_operator_write_node_enter(node) ⇒ Object

: (Prism::InstanceVariableOperatorWriteNode node) -> void



269
270
271
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 269

def on_instance_variable_operator_write_node_enter(node)
  collect_instance_variable_references(node.name.to_s, node.name_loc, true)
end

#on_instance_variable_or_write_node_enter(node) ⇒ Object

: (Prism::InstanceVariableOrWriteNode node) -> void



274
275
276
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 274

def on_instance_variable_or_write_node_enter(node)
  collect_instance_variable_references(node.name.to_s, node.name_loc, true)
end

#on_instance_variable_read_node_enter(node) ⇒ Object

: (Prism::InstanceVariableReadNode node) -> void



254
255
256
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 254

def on_instance_variable_read_node_enter(node)
  collect_instance_variable_references(node.name.to_s, node.location, false)
end

#on_instance_variable_target_node_enter(node) ⇒ Object

: (Prism::InstanceVariableTargetNode node) -> void



279
280
281
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 279

def on_instance_variable_target_node_enter(node)
  collect_instance_variable_references(node.name.to_s, node.location, true)
end

#on_instance_variable_write_node_enter(node) ⇒ Object

: (Prism::InstanceVariableWriteNode node) -> void



259
260
261
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 259

def on_instance_variable_write_node_enter(node)
  collect_instance_variable_references(node.name.to_s, node.name_loc, true)
end

#on_module_node_enter(node) ⇒ Object

: (Prism::ModuleNode node) -> void



123
124
125
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 123

def on_module_node_enter(node)
  @stack << node.constant_path.slice
end

#on_module_node_leave(node) ⇒ Object

: (Prism::ModuleNode node) -> void



128
129
130
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 128

def on_module_node_leave(node)
  @stack.pop
end

#on_multi_write_node_enter(node) ⇒ Object

: (Prism::MultiWriteNode node) -> void



162
163
164
165
166
167
168
169
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 162

def on_multi_write_node_enter(node)
  [*node.lefts, *node.rest, *node.rights].each do |target|
    case target
    when Prism::ConstantTargetNode, Prism::ConstantPathTargetNode
      collect_constant_references(target.name.to_s, target.location)
    end
  end
end

#on_singleton_class_node_enter(node) ⇒ Object

: (Prism::SingletonClassNode node) -> void



133
134
135
136
137
138
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 133

def on_singleton_class_node_enter(node)
  expression = node.expression
  return unless expression.is_a?(Prism::SelfNode)

  @stack << "<Class:#{@stack.last}>"
end

#on_singleton_class_node_leave(node) ⇒ Object

: (Prism::SingletonClassNode node) -> void



141
142
143
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 141

def on_singleton_class_node_leave(node)
  @stack.pop
end

#referencesObject

: -> Array



106
107
108
109
110
# File 'lib/ruby_indexer/lib/ruby_indexer/reference_finder.rb', line 106

def references
  return @references if @include_declarations

  @references.reject(&:declaration)
end