Class: RubyLsp::Requests::DocumentSymbol

Inherits:
Listener
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/requests/document_symbol.rb

Overview

![Document symbol demo](../../document_symbol.gif)

The [document symbol](microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol) request informs the editor of all the important symbols, such as classes, variables, and methods, defined in a file. With this information, the editor can populate breadcrumbs, file outline and allow for fuzzy symbol searches.

In VS Code, fuzzy symbol search can be accessed by opening the command palette and inserting an ‘@` symbol.

# Example

“‘ruby class Person # –> document symbol: class

attr_reader :age # --> document symbol: field

def initialize
  @age = 0 # --> document symbol: variable
end

def age # --> document symbol: method
end

end “‘

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[String])

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

#merge_external_listeners_responses!

Methods included from Support::Common

#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?

Constructor Details

#initialize(emitter, message_queue) ⇒ DocumentSymbol

Returns a new instance of DocumentSymbol.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 53

def initialize(emitter, message_queue)
  super

  @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)],
  )

  @external_listeners.concat(
    Extension.extensions.filter_map { |ext| ext.create_document_symbol_listener(emitter, message_queue) },
  )

  emitter.register(
    self,
    :on_class,
    :after_class,
    :on_command,
    :on_const_path_field,
    :on_def,
    :after_def,
    :on_module,
    :after_module,
    :on_top_const_field,
    :on_var_field,
  )
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



50
51
52
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 50

def response
  @response
end

Instance Method Details

#after_class(node) ⇒ Object



100
101
102
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 100

def after_class(node)
  @stack.pop
end

#after_def(node) ⇒ Object



153
154
155
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 153

def after_def(node)
  @stack.pop
end

#after_module(node) ⇒ Object



168
169
170
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 168

def after_module(node)
  @stack.pop
end

#merge_response!(other) ⇒ Object



84
85
86
87
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 84

def merge_response!(other)
  @response.concat(other.response)
  self
end

#on_class(node) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 90

def on_class(node)
  @stack << create_document_symbol(
    name: full_constant_name(node.constant),
    kind: Constant::SymbolKind::CLASS,
    range_node: node,
    selection_range_node: node.constant,
  )
end

#on_command(node) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 105

def on_command(node)
  return unless ATTR_ACCESSORS.include?(node.message.value)

  node.arguments.parts.each do |argument|
    next unless argument.is_a?(SyntaxTree::SymbolLiteral)

    create_document_symbol(
      name: argument.value.value,
      kind: Constant::SymbolKind::FIELD,
      range_node: argument,
      selection_range_node: argument.value,
    )
  end
end

#on_const_path_field(node) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 121

def on_const_path_field(node)
  create_document_symbol(
    name: node.constant.value,
    kind: Constant::SymbolKind::CONSTANT,
    range_node: node,
    selection_range_node: node.constant,
  )
end

#on_def(node) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 131

def on_def(node)
  target = node.target

  if target.is_a?(SyntaxTree::VarRef) && target.value.is_a?(SyntaxTree::Kw) && target.value.value == "self"
    name = "self.#{node.name.value}"
    kind = Constant::SymbolKind::METHOD
  else
    name = node.name.value
    kind = name == "initialize" ? Constant::SymbolKind::CONSTRUCTOR : Constant::SymbolKind::METHOD
  end

  symbol = create_document_symbol(
    name: name,
    kind: kind,
    range_node: node,
    selection_range_node: node.name,
  )

  @stack << symbol
end

#on_module(node) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 158

def on_module(node)
  @stack << create_document_symbol(
    name: full_constant_name(node.constant),
    kind: Constant::SymbolKind::MODULE,
    range_node: node,
    selection_range_node: node.constant,
  )
end

#on_top_const_field(node) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 173

def on_top_const_field(node)
  create_document_symbol(
    name: node.constant.value,
    kind: Constant::SymbolKind::CONSTANT,
    range_node: node,
    selection_range_node: node.constant,
  )
end

#on_var_field(node) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 183

def on_var_field(node)
  value = node.value
  kind = case value
  when SyntaxTree::Const
    Constant::SymbolKind::CONSTANT
  when SyntaxTree::CVar, SyntaxTree::IVar
    Constant::SymbolKind::VARIABLE
  else
    return
  end

  create_document_symbol(
    name: value.value,
    kind: kind,
    range_node: node,
    selection_range_node: value,
  )
end