Class: RubyLsp::Listeners::DocumentSymbol

Inherits:
RubyLsp::Listener show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/listeners/document_symbol.rb

Defined Under Namespace

Classes: SymbolHierarchyRoot

Constant Summary collapse

ResponseType =
type_member { { fixed: T::Array[Interface::DocumentSymbol] } }
ATTR_ACCESSORS =
T.let([:attr_reader, :attr_writer, :attr_accessor].freeze, T::Array[Symbol])

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RubyLsp::Listener

#response

Methods included from Requests::Support::Common

#create_code_lens, #markdown_from_index_entries, #not_in_dependencies?, #range_from_location, #range_from_node, #self_receiver?, #visible?

Constructor Details

#initialize(dispatcher) ⇒ DocumentSymbol

Returns a new instance of DocumentSymbol.



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
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 30

def initialize(dispatcher)
  @root = T.let(SymbolHierarchyRoot.new, SymbolHierarchyRoot)
  @_response = T.let(@root.children, T::Array[Interface::DocumentSymbol])
  @stack = T.let(
    [@root],
    T::Array[T.any(SymbolHierarchyRoot, Interface::DocumentSymbol)],
  )

  super

  dispatcher.register(
    self,
    :on_class_node_enter,
    :on_class_node_leave,
    :on_call_node_enter,
    :on_constant_path_write_node_enter,
    :on_constant_write_node_enter,
    :on_def_node_enter,
    :on_def_node_leave,
    :on_module_node_enter,
    :on_module_node_leave,
    :on_instance_variable_write_node_enter,
    :on_class_variable_write_node_enter,
    :on_singleton_class_node_enter,
    :on_singleton_class_node_leave,
  )
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



27
28
29
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 27

def _response
  @_response
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 91

def on_call_node_enter(node)
  return unless ATTR_ACCESSORS.include?(node.name) && node.receiver.nil?

  arguments = node.arguments
  return unless arguments

  arguments.arguments.each do |argument|
    next unless argument.is_a?(Prism::SymbolNode)

    name = argument.value
    next unless name

    create_document_symbol(
      name: name,
      kind: Constant::SymbolKind::FIELD,
      range_location: argument.location,
      selection_range_location: T.must(argument.value_loc),
    )
  end
end

#on_class_node_enter(node) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 59

def on_class_node_enter(node)
  @stack << create_document_symbol(
    name: node.constant_path.location.slice,
    kind: Constant::SymbolKind::CLASS,
    range_location: node.location,
    selection_range_location: node.constant_path.location,
  )
end

#on_class_node_leave(node) ⇒ Object



69
70
71
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 69

def on_class_node_leave(node)
  @stack.pop
end

#on_class_variable_write_node_enter(node) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 189

def on_class_variable_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::VARIABLE,
    range_location: node.name_loc,
    selection_range_location: node.name_loc,
  )
end

#on_constant_path_write_node_enter(node) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 113

def on_constant_path_write_node_enter(node)
  create_document_symbol(
    name: node.target.location.slice,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.target.location,
  )
end

#on_constant_write_node_enter(node) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 123

def on_constant_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::CONSTANT,
    range_location: node.location,
    selection_range_location: node.name_loc,
  )
end

#on_def_node_enter(node) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 148

def on_def_node_enter(node)
  receiver = node.receiver
  previous_symbol = @stack.last

  if receiver.is_a?(Prism::SelfNode)
    name = "self.#{node.name}"
    kind = Constant::SymbolKind::FUNCTION
  elsif previous_symbol.is_a?(Interface::DocumentSymbol) && previous_symbol.name.start_with?("<<")
    name = node.name.to_s
    kind = Constant::SymbolKind::FUNCTION
  else
    name = node.name.to_s
    kind = name == "initialize" ? Constant::SymbolKind::CONSTRUCTOR : Constant::SymbolKind::METHOD
  end

  symbol = create_document_symbol(
    name: name,
    kind: kind,
    range_location: node.location,
    selection_range_location: node.name_loc,
  )

  @stack << symbol
end

#on_def_node_leave(node) ⇒ Object



133
134
135
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 133

def on_def_node_leave(node)
  @stack.pop
end

#on_instance_variable_write_node_enter(node) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 179

def on_instance_variable_write_node_enter(node)
  create_document_symbol(
    name: node.name.to_s,
    kind: Constant::SymbolKind::VARIABLE,
    range_location: node.name_loc,
    selection_range_location: node.name_loc,
  )
end

#on_module_node_enter(node) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 138

def on_module_node_enter(node)
  @stack << create_document_symbol(
    name: node.constant_path.location.slice,
    kind: Constant::SymbolKind::MODULE,
    range_location: node.location,
    selection_range_location: node.constant_path.location,
  )
end

#on_module_node_leave(node) ⇒ Object



174
175
176
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 174

def on_module_node_leave(node)
  @stack.pop
end

#on_singleton_class_node_enter(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 74

def on_singleton_class_node_enter(node)
  expression = node.expression

  @stack << create_document_symbol(
    name: "<< #{expression.slice}",
    kind: Constant::SymbolKind::NAMESPACE,
    range_location: node.location,
    selection_range_location: expression.location,
  )
end

#on_singleton_class_node_leave(node) ⇒ Object



86
87
88
# File 'lib/ruby_lsp/listeners/document_symbol.rb', line 86

def on_singleton_class_node_leave(node)
  @stack.pop
end