Skip to content

Commit ef77ed6

Browse files
Handle self receiver and string arguments for attribute accessors in document symbol (#1366)
* Handle attr_accessors when receiver is a SelfNode * Handle StringNode arguments on attr_accessors
1 parent f3b2c26 commit ef77ed6

File tree

3 files changed

+233
-18
lines changed

3 files changed

+233
-18
lines changed

lib/ruby_lsp/listeners/document_symbol.rb

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,34 @@ def create_document_symbol(name:, kind:, range_location:, selection_range_locati
206206

207207
sig { params(node: Prism::CallNode).void }
208208
def handle_attr_accessor(node)
209-
return unless node.receiver.nil?
209+
receiver = node.receiver
210+
return if receiver && !receiver.is_a?(Prism::SelfNode)
210211

211212
arguments = node.arguments
212213
return unless arguments
213214

214215
arguments.arguments.each do |argument|
215-
next unless argument.is_a?(Prism::SymbolNode)
216-
217-
name = argument.value
218-
next unless name
219-
220-
create_document_symbol(
221-
name: name,
222-
kind: Constant::SymbolKind::FIELD,
223-
range_location: argument.location,
224-
selection_range_location: T.must(argument.value_loc),
225-
)
216+
if argument.is_a?(Prism::SymbolNode)
217+
name = argument.value
218+
next unless name
219+
220+
create_document_symbol(
221+
name: name,
222+
kind: Constant::SymbolKind::FIELD,
223+
range_location: argument.location,
224+
selection_range_location: T.must(argument.value_loc),
225+
)
226+
elsif argument.is_a?(Prism::StringNode)
227+
name = argument.content
228+
next if name.empty?
229+
230+
create_document_symbol(
231+
name: name,
232+
kind: Constant::SymbolKind::FIELD,
233+
range_location: argument.location,
234+
selection_range_location: argument.content_loc,
235+
)
236+
end
226237
end
227238
end
228239

test/expectations/document_symbol/attr_accessor.exp.json

Lines changed: 203 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@
2828
{
2929
"name": "b",
3030
"kind": 8,
31+
"range": {
32+
"start": {
33+
"line": 0,
34+
"character": 16
35+
},
36+
"end": {
37+
"line": 0,
38+
"character": 19
39+
}
40+
},
41+
"selectionRange": {
42+
"start": {
43+
"line": 0,
44+
"character": 17
45+
},
46+
"end": {
47+
"line": 0,
48+
"character": 18
49+
}
50+
},
51+
"children": []
52+
},
53+
{
54+
"name": "c",
55+
"kind": 8,
3156
"range": {
3257
"start": {
3358
"line": 1,
@@ -51,7 +76,7 @@
5176
"children": []
5277
},
5378
{
54-
"name": "c",
79+
"name": "d",
5580
"kind": 8,
5681
"range": {
5782
"start": {
@@ -60,7 +85,7 @@
6085
},
6186
"end": {
6287
"line": 1,
63-
"character": 18
88+
"character": 19
6489
}
6590
},
6691
"selectionRange": {
@@ -76,7 +101,7 @@
76101
"children": []
77102
},
78103
{
79-
"name": "d",
104+
"name": "e",
80105
"kind": 8,
81106
"range": {
82107
"start": {
@@ -99,6 +124,181 @@
99124
}
100125
},
101126
"children": []
127+
},
128+
{
129+
"name": "f",
130+
"kind": 8,
131+
"range": {
132+
"start": {
133+
"line": 2,
134+
"character": 18
135+
},
136+
"end": {
137+
"line": 2,
138+
"character": 21
139+
}
140+
},
141+
"selectionRange": {
142+
"start": {
143+
"line": 2,
144+
"character": 19
145+
},
146+
"end": {
147+
"line": 2,
148+
"character": 20
149+
}
150+
},
151+
"children": []
152+
},
153+
{
154+
"name": "a",
155+
"kind": 8,
156+
"range": {
157+
"start": {
158+
"line": 4,
159+
"character": 17
160+
},
161+
"end": {
162+
"line": 4,
163+
"character": 19
164+
}
165+
},
166+
"selectionRange": {
167+
"start": {
168+
"line": 4,
169+
"character": 18
170+
},
171+
"end": {
172+
"line": 4,
173+
"character": 19
174+
}
175+
},
176+
"children": []
177+
},
178+
{
179+
"name": "b",
180+
"kind": 8,
181+
"range": {
182+
"start": {
183+
"line": 4,
184+
"character": 21
185+
},
186+
"end": {
187+
"line": 4,
188+
"character": 24
189+
}
190+
},
191+
"selectionRange": {
192+
"start": {
193+
"line": 4,
194+
"character": 22
195+
},
196+
"end": {
197+
"line": 4,
198+
"character": 23
199+
}
200+
},
201+
"children": []
202+
},
203+
{
204+
"name": "c",
205+
"kind": 8,
206+
"range": {
207+
"start": {
208+
"line": 5,
209+
"character": 17
210+
},
211+
"end": {
212+
"line": 5,
213+
"character": 19
214+
}
215+
},
216+
"selectionRange": {
217+
"start": {
218+
"line": 5,
219+
"character": 18
220+
},
221+
"end": {
222+
"line": 5,
223+
"character": 19
224+
}
225+
},
226+
"children": []
227+
},
228+
{
229+
"name": "d",
230+
"kind": 8,
231+
"range": {
232+
"start": {
233+
"line": 5,
234+
"character": 21
235+
},
236+
"end": {
237+
"line": 5,
238+
"character": 24
239+
}
240+
},
241+
"selectionRange": {
242+
"start": {
243+
"line": 5,
244+
"character": 22
245+
},
246+
"end": {
247+
"line": 5,
248+
"character": 23
249+
}
250+
},
251+
"children": []
252+
},
253+
{
254+
"name": "e",
255+
"kind": 8,
256+
"range": {
257+
"start": {
258+
"line": 6,
259+
"character": 19
260+
},
261+
"end": {
262+
"line": 6,
263+
"character": 21
264+
}
265+
},
266+
"selectionRange": {
267+
"start": {
268+
"line": 6,
269+
"character": 20
270+
},
271+
"end": {
272+
"line": 6,
273+
"character": 21
274+
}
275+
},
276+
"children": []
277+
},
278+
{
279+
"name": "f",
280+
"kind": 8,
281+
"range": {
282+
"start": {
283+
"line": 6,
284+
"character": 23
285+
},
286+
"end": {
287+
"line": 6,
288+
"character": 26
289+
}
290+
},
291+
"selectionRange": {
292+
"start": {
293+
"line": 6,
294+
"character": 24
295+
},
296+
"end": {
297+
"line": 6,
298+
"character": 25
299+
}
300+
},
301+
"children": []
102302
}
103303
]
104304
}

test/fixtures/attr_accessor.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
attr_reader :a
2-
attr_writer :b, :c
3-
attr_accessor :d
1+
attr_reader :a, "b", some_variable
2+
attr_writer :c, "d", some_variable
3+
attr_accessor :e, "f", some_variable
4+
5+
self.attr_reader :a, "b", some_variable
6+
self.attr_writer :c, "d", some_variable
7+
self.attr_accessor :e, "f", some_variable

0 commit comments

Comments
 (0)