Class: RubyLsp::Requests::DocumentSymbol

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

Overview

![Document symbol demo](../../misc/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

SYMBOL_KIND =
T.let(
  {
    file: 1,
    module: 2,
    namespace: 3,
    package: 4,
    class: 5,
    method: 6,
    property: 7,
    field: 8,
    constructor: 9,
    enum: 10,
    interface: 11,
    function: 12,
    variable: 13,
    constant: 14,
    string: 15,
    number: 16,
    boolean: 17,
    array: 18,
    object: 19,
    key: 20,
    null: 21,
    enummember: 22,
    struct: 23,
    event: 24,
    operator: 25,
    typeparameter: 26,
  }.freeze,
  T::Hash[Symbol, Integer],
)
ATTR_ACCESSORS =
T.let(["attr_reader", "attr_writer", "attr_accessor"].freeze, T::Array[String])

Instance Method Summary collapse

Methods inherited from BaseRequest

#full_constant_name, #locate, #range_from_syntax_tree_node, #visible?, #visit_all

Constructor Details

#initialize(document) ⇒ DocumentSymbol

Returns a new instance of DocumentSymbol.



79
80
81
82
83
84
85
86
87
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 79

def initialize(document)
  super

  @root = T.let(SymbolHierarchyRoot.new, SymbolHierarchyRoot)
  @stack = T.let(
    [@root],
    T::Array[T.any(SymbolHierarchyRoot, LanguageServer::Protocol::Interface::DocumentSymbol)],
  )
end

Instance Method Details

#runObject



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

def run
  visit(@document.tree) if @document.parsed?
  @root.children
end

#visit_class(node) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 96

def visit_class(node)
  symbol = create_document_symbol(
    name: full_constant_name(node.constant),
    kind: :class,
    range_node: node,
    selection_range_node: node.constant,
  )

  @stack << symbol
  visit(node.bodystmt)
  @stack.pop
end

#visit_command(node) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 110

def visit_command(node)
  return visit(node.arguments) 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: :field,
      range_node: argument,
      selection_range_node: argument.value,
    )
  end
end

#visit_const_path_field(node) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 126

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

#visit_def(node) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 136

def visit_def(node)
  if node.target&.value&.value == "self"
    name = "self.#{node.name.value}"
    kind = :method
  else
    name = node.name.value
    kind = name == "initialize" ? :constructor : :method
  end

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

  @stack << symbol
  visit(node.bodystmt)
  @stack.pop
end

#visit_module(node) ⇒ Object



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

def visit_module(node)
  symbol = create_document_symbol(
    name: full_constant_name(node.constant),
    kind: :module,
    range_node: node,
    selection_range_node: node.constant,
  )

  @stack << symbol
  visit(node.bodystmt)
  @stack.pop
end

#visit_top_const_field(node) ⇒ Object



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

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

#visit_var_field(node) ⇒ Object



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

def visit_var_field(node)
  kind = case node.value
  when SyntaxTree::Const
    :constant
  when SyntaxTree::CVar, SyntaxTree::IVar
    :variable
  else
    return
  end

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