@@ -122,6 +122,8 @@ struct Section {
122
122
children : Vec < DocumentSymbol > ,
123
123
}
124
124
125
+ struct CollectContext ;
126
+
125
127
pub ( crate ) fn document_symbols (
126
128
state : & WorldState ,
127
129
params : & DocumentSymbolParams ,
@@ -136,7 +138,8 @@ pub(crate) fn document_symbols(
136
138
let mut result = Vec :: new ( ) ;
137
139
138
140
// Extract and process all symbols from the AST
139
- if let Err ( err) = collect_symbols ( & root_node, contents, 0 , & mut result) {
141
+ let mut ctx = CollectContext ;
142
+ if let Err ( err) = collect_symbols ( & mut ctx, & root_node, contents, 0 , & mut result) {
140
143
log:: error!( "Failed to collect symbols: {err:?}" ) ;
141
144
return Ok ( Vec :: new ( ) ) ;
142
145
}
@@ -146,33 +149,34 @@ pub(crate) fn document_symbols(
146
149
147
150
/// Collect all document symbols from a node recursively
148
151
fn collect_symbols (
152
+ ctx : & mut CollectContext ,
149
153
node : & Node ,
150
154
contents : & Rope ,
151
155
current_level : usize ,
152
156
symbols : & mut Vec < DocumentSymbol > ,
153
157
) -> anyhow:: Result < ( ) > {
154
158
match node. node_type ( ) {
155
159
NodeType :: Program | NodeType :: BracedExpression => {
156
- collect_sections ( node, contents, current_level, symbols) ?;
160
+ collect_sections ( ctx , node, contents, current_level, symbols) ?;
157
161
} ,
158
162
159
163
NodeType :: Call => {
160
- collect_call ( node, contents, symbols) ?;
164
+ collect_call ( ctx , node, contents, symbols) ?;
161
165
} ,
162
166
163
167
NodeType :: BinaryOperator ( BinaryOperatorType :: LeftAssignment ) |
164
168
NodeType :: BinaryOperator ( BinaryOperatorType :: EqualsAssignment ) => {
165
- collect_assignment ( node, contents, symbols) ?;
169
+ collect_assignment ( ctx , node, contents, symbols) ?;
166
170
} ,
167
171
168
172
// For all other node types, no symbols need to be added
169
173
_ => { } ,
170
174
}
171
-
172
175
Ok ( ( ) )
173
176
}
174
177
175
178
fn collect_sections (
179
+ ctx : & mut CollectContext ,
176
180
node : & Node ,
177
181
contents : & Rope ,
178
182
current_level : usize ,
@@ -224,11 +228,11 @@ fn collect_sections(
224
228
225
229
if active_sections. is_empty ( ) {
226
230
// If no active section, extend current vector of symbols
227
- collect_symbols ( & child, contents, current_level, symbols) ?;
231
+ collect_symbols ( ctx , & child, contents, current_level, symbols) ?;
228
232
} else {
229
233
// Otherwise create new store of symbols for the current section
230
234
let mut child_symbols = Vec :: new ( ) ;
231
- collect_symbols ( & child, contents, current_level, & mut child_symbols) ?;
235
+ collect_symbols ( ctx , & child, contents, current_level, & mut child_symbols) ?;
232
236
233
237
// Nest them inside last section
234
238
if !child_symbols. is_empty ( ) {
@@ -258,6 +262,7 @@ fn collect_sections(
258
262
}
259
263
260
264
fn collect_call (
265
+ ctx : & mut CollectContext ,
261
266
node : & Node ,
262
267
contents : & Rope ,
263
268
symbols : & mut Vec < DocumentSymbol > ,
@@ -268,19 +273,19 @@ fn collect_call(
268
273
269
274
if callee. is_identifier ( ) {
270
275
let fun_symbol = contents. node_slice ( & callee) ?. to_string ( ) ;
271
-
272
276
match fun_symbol. as_str ( ) {
273
- "test_that" => return collect_call_test_that ( node, contents, symbols) ,
277
+ "test_that" => return collect_call_test_that ( ctx , node, contents, symbols) ,
274
278
_ => { } , // fallthrough
275
279
}
276
280
}
277
281
278
- collect_call_arguments ( node, contents, symbols) ?;
282
+ collect_call_arguments ( ctx , node, contents, symbols) ?;
279
283
280
284
Ok ( ( ) )
281
285
}
282
286
283
287
fn collect_call_arguments (
288
+ ctx : & mut CollectContext ,
284
289
node : & Node ,
285
290
contents : & Rope ,
286
291
symbols : & mut Vec < DocumentSymbol > ,
@@ -289,28 +294,24 @@ fn collect_call_arguments(
289
294
return Ok ( ( ) ) ;
290
295
} ;
291
296
292
- let mut cursor = node . walk ( ) ;
297
+ let mut cursor = arguments . walk ( ) ;
293
298
for arg in arguments. children ( & mut cursor) {
294
- if arg. kind ( ) != "argument" {
295
- continue ;
296
- }
297
-
298
299
let Some ( arg_value) = arg. child_by_field_name ( "value" ) else {
299
300
continue ;
300
301
} ;
301
302
302
303
// Recurse into arguments. They might be a braced list, another call
303
304
// that might contain functions, etc.
304
- collect_symbols ( & arg_value, contents, 0 , symbols) ?;
305
+ collect_symbols ( ctx , & arg_value, contents, 0 , symbols) ?;
305
306
306
307
if arg_value. kind ( ) == "function_definition" {
307
308
if let Some ( arg_fun) = arg. child_by_field_name ( "name" ) {
308
309
// If this is a named function, collect it as a method
309
- collect_method ( & arg_fun, & arg_value, contents, symbols) ?;
310
+ collect_method ( ctx , & arg_fun, & arg_value, contents, symbols) ?;
310
311
} else {
311
312
// Otherwise, just recurse into the function
312
313
let body = arg_value. child_by_field_name ( "body" ) . into_result ( ) ?;
313
- collect_symbols ( & body, contents, 0 , symbols) ?;
314
+ collect_symbols ( ctx , & body, contents, 0 , symbols) ?;
314
315
} ;
315
316
}
316
317
}
@@ -319,6 +320,7 @@ fn collect_call_arguments(
319
320
}
320
321
321
322
fn collect_method (
323
+ ctx : & mut CollectContext ,
322
324
arg_fun : & Node ,
323
325
arg_value : & Node ,
324
326
contents : & Rope ,
@@ -334,7 +336,7 @@ fn collect_method(
334
336
335
337
let body = arg_value. child_by_field_name ( "body" ) . into_result ( ) ?;
336
338
let mut children = vec ! [ ] ;
337
- collect_symbols ( & body, contents, 0 , & mut children) ?;
339
+ collect_symbols ( ctx , & body, contents, 0 , & mut children) ?;
338
340
339
341
let mut symbol = new_symbol_node (
340
342
arg_name_str,
@@ -355,6 +357,7 @@ fn collect_method(
355
357
356
358
// https://github.com/posit-dev/positron/issues/1428
357
359
fn collect_call_test_that (
360
+ ctx : & mut CollectContext ,
358
361
node : & Node ,
359
362
contents : & Rope ,
360
363
symbols : & mut Vec < DocumentSymbol > ,
@@ -381,7 +384,7 @@ fn collect_call_test_that(
381
384
let mut cursor = arguments. walk ( ) ;
382
385
for child in arguments. children_by_field_name ( "argument" , & mut cursor) {
383
386
if let Some ( value) = child. child_by_field_name ( "value" ) {
384
- collect_symbols ( & value, contents, 0 , & mut children) ?;
387
+ collect_symbols ( ctx , & value, contents, 0 , & mut children) ?;
385
388
}
386
389
}
387
390
@@ -398,6 +401,7 @@ fn collect_call_test_that(
398
401
}
399
402
400
403
fn collect_assignment (
404
+ ctx : & mut CollectContext ,
401
405
node : & Node ,
402
406
contents : & Rope ,
403
407
symbols : & mut Vec < DocumentSymbol > ,
@@ -418,7 +422,7 @@ fn collect_assignment(
418
422
// If a function, collect symbol as function
419
423
let function = lhs. is_identifier_or_string ( ) && rhs. is_function_definition ( ) ;
420
424
if function {
421
- return collect_assignment_with_function ( node, contents, symbols) ;
425
+ return collect_assignment_with_function ( ctx , node, contents, symbols) ;
422
426
}
423
427
424
428
// Otherwise, collect as generic object
@@ -429,7 +433,7 @@ fn collect_assignment(
429
433
430
434
// Now recurse into RHS
431
435
let mut children = Vec :: new ( ) ;
432
- collect_symbols ( & rhs, contents, 0 , & mut children) ?;
436
+ collect_symbols ( ctx , & rhs, contents, 0 , & mut children) ?;
433
437
434
438
let symbol = new_symbol_node ( name, SymbolKind :: VARIABLE , Range { start, end } , children) ;
435
439
symbols. push ( symbol) ;
@@ -438,6 +442,7 @@ fn collect_assignment(
438
442
}
439
443
440
444
fn collect_assignment_with_function (
445
+ ctx : & mut CollectContext ,
441
446
node : & Node ,
442
447
contents : & Rope ,
443
448
symbols : & mut Vec < DocumentSymbol > ,
@@ -469,7 +474,7 @@ fn collect_assignment_with_function(
469
474
470
475
// Process the function body to extract child symbols
471
476
let mut children = Vec :: new ( ) ;
472
- collect_symbols ( & body, contents, 0 , & mut children) ?;
477
+ collect_symbols ( ctx , & body, contents, 0 , & mut children) ?;
473
478
474
479
let mut symbol = new_symbol_node ( name, SymbolKind :: FUNCTION , range, children) ;
475
480
symbol. detail = Some ( detail) ;
@@ -536,7 +541,8 @@ mod tests {
536
541
let node = doc. ast . root_node ( ) ;
537
542
538
543
let mut symbols = Vec :: new ( ) ;
539
- collect_symbols ( & node, & doc. contents , 0 , & mut symbols) . unwrap ( ) ;
544
+ let mut ctx = CollectContext ;
545
+ collect_symbols ( & mut ctx, & node, & doc. contents , 0 , & mut symbols) . unwrap ( ) ;
540
546
symbols
541
547
}
542
548
0 commit comments