Class: RubyLsp::Requests::DocumentSymbol

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

Overview

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 opened 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 =
{
  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
ATTR_ACCESSORS =
["attr_reader", "attr_writer", "attr_accessor"].freeze

Instance Method Summary collapse

Methods inherited from BaseRequest

#range_from_syntax_tree_node, run

Constructor Details

#initialize(document) ⇒ DocumentSymbol

Returns a new instance of DocumentSymbol.



67
68
69
70
71
72
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 67

def initialize(document)
  super

  @root = SymbolHierarchyRoot.new
  @stack = [@root]
end

Instance Method Details

#runObject



74
75
76
77
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 74

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

#visit_class(node) ⇒ Object



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

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

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

#visit_command(node) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 92

def visit_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: :field,
      range_node: argument,
      selection_range_node: argument.value
    )
  end
end

#visit_const_path_field(node) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 107

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 116

def visit_def(node)
  name = node.name.value

  symbol = create_document_symbol(
    name: name,
    kind: name == "initialize" ? :constructor : :method,
    range_node: node,
    selection_range_node: node.name
  )

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

#visit_def_endless(node) ⇒ Object



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

def visit_def_endless(node)
  name = node.name.value

  symbol = create_document_symbol(
    name: name,
    kind: name == "initialize" ? :constructor : :method,
    range_node: node,
    selection_range_node: node.name
  )

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

#visit_defs(node) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 146

def visit_defs(node)
  symbol = create_document_symbol(
    name: "self.#{node.name.value}",
    kind: :method,
    range_node: node,
    selection_range_node: node.name
  )

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

#visit_module(node) ⇒ Object



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

def visit_module(node)
  symbol = create_document_symbol(
    name: node.constant.constant.value,
    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



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

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