Class: RubyLsp::Requests::DocumentSymbol

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

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

run

Constructor Details

#initialize(document) ⇒ DocumentSymbol

Returns a new instance of DocumentSymbol.



45
46
47
48
49
50
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 45

def initialize(document)
  super

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

Instance Method Details

#runObject



52
53
54
55
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 52

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

#visit_class(node) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 57

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 70

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



85
86
87
88
89
90
91
92
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 85

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



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

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



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

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



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

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



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ruby_lsp/requests/document_symbol.rb', line 137

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



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

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



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

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