Class: RubynCode::Index::PrismExtractor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/rubyn_code/index/prism_extractor.rb

Overview

Prism-based symbol and call extractor for a single Ruby file. Returns classes/modules and methods with real line spans, plus method-to-method call sites the regex pass can't see. Used by CodebaseIndex, with the regex extractors as a parse-error fallback.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrismExtractor

Returns a new instance of PrismExtractor.



28
29
30
31
32
33
34
35
# File 'lib/rubyn_code/index/prism_extractor.rb', line 28

def initialize
  super
  @namespace = []
  @current_def = nil
  @classes = [] # { name:, kind:, line:, end_line: }
  @defs    = [] # { name:, owner:, line:, end_line:, params: }
  @calls   = [] # { from:, to:, line: }
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



26
27
28
# File 'lib/rubyn_code/index/prism_extractor.rb', line 26

def calls
  @calls
end

#classesObject (readonly)

Returns the value of attribute classes.



26
27
28
# File 'lib/rubyn_code/index/prism_extractor.rb', line 26

def classes
  @classes
end

#defsObject (readonly)

Returns the value of attribute defs.



26
27
28
# File 'lib/rubyn_code/index/prism_extractor.rb', line 26

def defs
  @defs
end

Class Method Details

.extract(content) ⇒ Result?

Returns nil when the file doesn't parse.

Returns:

  • (Result, nil)

    nil when the file doesn't parse



15
16
17
18
19
20
21
22
23
24
# File 'lib/rubyn_code/index/prism_extractor.rb', line 15

def self.extract(content)
  parsed = Prism.parse(content)
  return nil unless parsed.success?

  visitor = new
  visitor.visit(parsed.value)
  Result.new(visitor.classes, visitor.defs, visitor.calls)
rescue StandardError
  nil
end

Instance Method Details

#visit_call_node(node) ⇒ Object

ponytail: resolution is by method name, not receiver type — a call to user.save links to any save defined in the project. Real receiver inference needs type analysis; name matching covers the common case.



62
63
64
65
# File 'lib/rubyn_code/index/prism_extractor.rb', line 62

def visit_call_node(node)
  @calls << { from: @current_def, to: node.name.to_s, line: node.location.start_line } if @current_def
  super
end

#visit_class_node(node) ⇒ Object



37
38
39
# File 'lib/rubyn_code/index/prism_extractor.rb', line 37

def visit_class_node(node)
  record_namespace(node, 'class') { super }
end

#visit_def_node(node) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubyn_code/index/prism_extractor.rb', line 45

def visit_def_node(node)
  @defs << {
    name: node.name.to_s,
    owner: @namespace.join('::'),
    line: node.location.start_line,
    end_line: node.location.end_line,
    params: node.parameters ? "(#{node.parameters.slice})" : nil
  }
  previous = @current_def
  @current_def = node.name.to_s
  super
  @current_def = previous
end

#visit_module_node(node) ⇒ Object



41
42
43
# File 'lib/rubyn_code/index/prism_extractor.rb', line 41

def visit_module_node(node)
  record_namespace(node, 'module') { super }
end