Skip to content

Commit 4737007

Browse files
authored
Improve document symbols of singleton class operators and methods (#1153)
1 parent e2115db commit 4737007

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

lib/ruby_lsp/requests/document_symbol.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ def initialize(dispatcher, message_queue)
7373
:on_module_node_leave,
7474
:on_instance_variable_write_node_enter,
7575
:on_class_variable_write_node_enter,
76+
:on_singleton_class_node_enter,
77+
:on_singleton_class_node_leave,
7678
)
7779
end
7880

@@ -103,6 +105,23 @@ def on_class_node_leave(node)
103105
@stack.pop
104106
end
105107

108+
sig { params(node: Prism::SingletonClassNode).void }
109+
def on_singleton_class_node_enter(node)
110+
expression = node.expression
111+
112+
@stack << create_document_symbol(
113+
name: "<< #{expression.slice}",
114+
kind: Constant::SymbolKind::NAMESPACE,
115+
range_location: node.location,
116+
selection_range_location: expression.location,
117+
)
118+
end
119+
120+
sig { params(node: Prism::SingletonClassNode).void }
121+
def on_singleton_class_node_leave(node)
122+
@stack.pop
123+
end
124+
106125
sig { params(node: Prism::CallNode).void }
107126
def on_call_node_enter(node)
108127
return unless ATTR_ACCESSORS.include?(node.name) && node.receiver.nil?
@@ -163,10 +182,14 @@ def on_module_node_enter(node)
163182
sig { params(node: Prism::DefNode).void }
164183
def on_def_node_enter(node)
165184
receiver = node.receiver
185+
previous_symbol = @stack.last
166186

167187
if receiver.is_a?(Prism::SelfNode)
168188
name = "self.#{node.name}"
169-
kind = Constant::SymbolKind::METHOD
189+
kind = Constant::SymbolKind::FUNCTION
190+
elsif previous_symbol.is_a?(Interface::DocumentSymbol) && previous_symbol.name.start_with?("<<")
191+
name = node.name.to_s
192+
kind = Constant::SymbolKind::FUNCTION
170193
else
171194
name = node.name.to_s
172195
kind = name == "initialize" ? Constant::SymbolKind::CONSTRUCTOR : Constant::SymbolKind::METHOD

test/expectations/document_symbol/defs.exp.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"result": [
33
{
44
"name": "self.foo",
5-
"kind": 6,
5+
"kind": 12,
66
"range": {
77
"start": {
88
"line": 0,
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"result": [
3+
{
4+
"name": "Foo",
5+
"kind": 5,
6+
"range": {
7+
"start": {
8+
"line": 0,
9+
"character": 0
10+
},
11+
"end": {
12+
"line": 8,
13+
"character": 3
14+
}
15+
},
16+
"selectionRange": {
17+
"start": {
18+
"line": 0,
19+
"character": 6
20+
},
21+
"end": {
22+
"line": 0,
23+
"character": 9
24+
}
25+
},
26+
"children": [
27+
{
28+
"name": "<< self",
29+
"kind": 3,
30+
"range": {
31+
"start": {
32+
"line": 1,
33+
"character": 2
34+
},
35+
"end": {
36+
"line": 3,
37+
"character": 5
38+
}
39+
},
40+
"selectionRange": {
41+
"start": {
42+
"line": 1,
43+
"character": 11
44+
},
45+
"end": {
46+
"line": 1,
47+
"character": 15
48+
}
49+
},
50+
"children": [
51+
{
52+
"name": "bar",
53+
"kind": 12,
54+
"range": {
55+
"start": {
56+
"line": 2,
57+
"character": 4
58+
},
59+
"end": {
60+
"line": 2,
61+
"character": 16
62+
}
63+
},
64+
"selectionRange": {
65+
"start": {
66+
"line": 2,
67+
"character": 8
68+
},
69+
"end": {
70+
"line": 2,
71+
"character": 11
72+
}
73+
},
74+
"children": []
75+
}
76+
]
77+
},
78+
{
79+
"name": "<< baz",
80+
"kind": 3,
81+
"range": {
82+
"start": {
83+
"line": 5,
84+
"character": 2
85+
},
86+
"end": {
87+
"line": 7,
88+
"character": 5
89+
}
90+
},
91+
"selectionRange": {
92+
"start": {
93+
"line": 5,
94+
"character": 11
95+
},
96+
"end": {
97+
"line": 5,
98+
"character": 14
99+
}
100+
},
101+
"children": [
102+
{
103+
"name": "qux",
104+
"kind": 12,
105+
"range": {
106+
"start": {
107+
"line": 6,
108+
"character": 4
109+
},
110+
"end": {
111+
"line": 6,
112+
"character": 16
113+
}
114+
},
115+
"selectionRange": {
116+
"start": {
117+
"line": 6,
118+
"character": 8
119+
},
120+
"end": {
121+
"line": 6,
122+
"character": 11
123+
}
124+
},
125+
"children": []
126+
}
127+
]
128+
}
129+
]
130+
}
131+
]
132+
}

test/fixtures/sclass_operator.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Foo
2+
class << self
3+
def bar; end
4+
end
5+
6+
class << baz
7+
def qux; end
8+
end
9+
end

0 commit comments

Comments
 (0)