Class: Hind::LSIF::DeclarationVisitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/hind/lsif/visitors/declaration_visitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, file_path) ⇒ DeclarationVisitor

Returns a new instance of DeclarationVisitor.



8
9
10
11
12
# File 'lib/hind/lsif/visitors/declaration_visitor.rb', line 8

def initialize(generator, file_path)
  @generator = generator
  @file_path = file_path
  @current_scope = []
end

Instance Attribute Details

#current_scopeObject (readonly)

Returns the value of attribute current_scope.



6
7
8
# File 'lib/hind/lsif/visitors/declaration_visitor.rb', line 6

def current_scope
  @current_scope
end

Instance Method Details

#visit_call_node(node) ⇒ Object



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hind/lsif/visitors/declaration_visitor.rb', line 75

def visit_call_node(node)
  # Handle attr_accessor, attr_reader, attr_writer
  if node.receiver.nil? && %w[attr_reader attr_writer attr_accessor].include?(node.name.to_s)
    helpers = node.arguments&.arguments || []
    helpers.each do |arg|
      next unless arg.is_a?(Prism::SymbolNode)
      
      name = arg.value
      if %w[attr_reader attr_accessor].include?(node.name.to_s)
        # Getter
        qualified_name = @current_scope.empty? ? "##{name}" : "#{current_scope_name}##{name}"
        @generator.register_method_declaration({
          type: :method,
          name: qualified_name,
          node: arg, # Use symbol node as the 'def' site
          scope: current_scope_name
        })
      end

      if %w[attr_writer attr_accessor].include?(node.name.to_s)
        # Setter
        setter_name = "#{name}="
        qualified_name = @current_scope.empty? ? "##{setter_name}" : "#{current_scope_name}##{setter_name}"
        @generator.register_method_declaration({
          type: :method,
          name: qualified_name,
          node: arg,
          scope: current_scope_name
        })
      end
    end
  end

  # Handle include, extend, prepend
  if node.receiver.nil? && %w[include extend prepend].include?(node.name.to_s)
    modules = node.arguments&.arguments || []
    modules.each do |mod_node|
      # Simplified: only handle simple constant names or paths
      next unless mod_node.is_a?(Prism::ConstantReadNode) || mod_node.is_a?(Prism::ConstantPathNode)
      
      mixed_in = mod_node.slice
      GlobalState.instance.add_ancestor(current_scope_name, mixed_in, node.name.to_sym)
    end
  end
  super
end

#visit_class_node(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hind/lsif/visitors/declaration_visitor.rb', line 14

def visit_class_node(node)
  @current_scope.push(node.constant_path.slice)
  class_name = current_scope_name
  target_node = node.constant_path
  target_node = target_node.child while target_node.is_a?(Prism::ConstantPathNode)

  @generator.register_class_declaration({
    type: :class,
    name: class_name,
    node: node,
    range_location: target_node.location,
    scope: @current_scope[0..-2].join('::'),
    superclass: node.superclass&.slice
  })
  super
  @current_scope.pop
end

#visit_constant_write_node(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hind/lsif/visitors/declaration_visitor.rb', line 49

def visit_constant_write_node(node)
  return unless node.name
  constant_name = node.name.to_s
  qualified_name = @current_scope.empty? ? constant_name : "#{current_scope_name}::#{constant_name}"
  @generator.register_constant_declaration({
    type: :constant,
    name: qualified_name,
    node: node,
    scope: current_scope_name
  })
  super
end

#visit_def_node(node) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hind/lsif/visitors/declaration_visitor.rb', line 62

def visit_def_node(node)
  method_name = node.name.to_s
  qualified_name = @current_scope.empty? ? "##{method_name}" : "#{current_scope_name}##{method_name}"
  
  @generator.register_method_declaration({
    type: :method,
    name: qualified_name,
    node: node,
    scope: current_scope_name
  })
  super
end

#visit_module_node(node) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hind/lsif/visitors/declaration_visitor.rb', line 32

def visit_module_node(node)
  @current_scope.push(node.constant_path.slice)
  module_name = current_scope_name
  target_node = node.constant_path
  target_node = target_node.child while target_node.is_a?(Prism::ConstantPathNode)

  @generator.register_module_declaration({
    type: :module,
    name: module_name,
    node: node,
    range_location: target_node.location,
    scope: @current_scope[0..-2].join('::')
  })
  super
  @current_scope.pop
end