@@ -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 > ,
@@ -299,17 +304,17 @@ fn collect_call_arguments(
299
304
"function_definition" => {
300
305
if let Some ( arg_fun) = arg. child_by_field_name ( "name" ) {
301
306
// If this is a named function, collect it as a method
302
- collect_method ( & arg_fun, & arg_value, contents, symbols) ?;
307
+ collect_method ( ctx , & arg_fun, & arg_value, contents, symbols) ?;
303
308
} else {
304
309
// Otherwise, just recurse into the function
305
310
let body = arg_value. child_by_field_name ( "body" ) . into_result ( ) ?;
306
- collect_symbols ( & body, contents, 0 , symbols) ?;
311
+ collect_symbols ( ctx , & body, contents, 0 , symbols) ?;
307
312
} ;
308
313
} ,
309
314
_ => {
310
315
// Recurse into arguments. They might be a braced list, another call
311
316
// that might contain functions, etc.
312
- collect_symbols ( & arg_value, contents, 0 , symbols) ?;
317
+ collect_symbols ( ctx , & arg_value, contents, 0 , symbols) ?;
313
318
} ,
314
319
}
315
320
}
@@ -318,6 +323,7 @@ fn collect_call_arguments(
318
323
}
319
324
320
325
fn collect_method (
326
+ ctx : & mut CollectContext ,
321
327
arg_fun : & Node ,
322
328
arg_value : & Node ,
323
329
contents : & Rope ,
@@ -333,7 +339,7 @@ fn collect_method(
333
339
334
340
let body = arg_value. child_by_field_name ( "body" ) . into_result ( ) ?;
335
341
let mut children = vec ! [ ] ;
336
- collect_symbols ( & body, contents, 0 , & mut children) ?;
342
+ collect_symbols ( ctx , & body, contents, 0 , & mut children) ?;
337
343
338
344
let mut symbol = new_symbol_node (
339
345
arg_name_str,
@@ -354,6 +360,7 @@ fn collect_method(
354
360
355
361
// https://github.com/posit-dev/positron/issues/1428
356
362
fn collect_call_test_that (
363
+ ctx : & mut CollectContext ,
357
364
node : & Node ,
358
365
contents : & Rope ,
359
366
symbols : & mut Vec < DocumentSymbol > ,
@@ -380,7 +387,7 @@ fn collect_call_test_that(
380
387
let mut cursor = arguments. walk ( ) ;
381
388
for child in arguments. children_by_field_name ( "argument" , & mut cursor) {
382
389
if let Some ( value) = child. child_by_field_name ( "value" ) {
383
- collect_symbols ( & value, contents, 0 , & mut children) ?;
390
+ collect_symbols ( ctx , & value, contents, 0 , & mut children) ?;
384
391
}
385
392
}
386
393
@@ -397,6 +404,7 @@ fn collect_call_test_that(
397
404
}
398
405
399
406
fn collect_assignment (
407
+ ctx : & mut CollectContext ,
400
408
node : & Node ,
401
409
contents : & Rope ,
402
410
symbols : & mut Vec < DocumentSymbol > ,
@@ -417,7 +425,7 @@ fn collect_assignment(
417
425
// If a function, collect symbol as function
418
426
let function = lhs. is_identifier_or_string ( ) && rhs. is_function_definition ( ) ;
419
427
if function {
420
- return collect_assignment_with_function ( node, contents, symbols) ;
428
+ return collect_assignment_with_function ( ctx , node, contents, symbols) ;
421
429
}
422
430
423
431
// Otherwise, collect as generic object
@@ -428,7 +436,7 @@ fn collect_assignment(
428
436
429
437
// Now recurse into RHS
430
438
let mut children = Vec :: new ( ) ;
431
- collect_symbols ( & rhs, contents, 0 , & mut children) ?;
439
+ collect_symbols ( ctx , & rhs, contents, 0 , & mut children) ?;
432
440
433
441
let symbol = new_symbol_node ( name, SymbolKind :: VARIABLE , Range { start, end } , children) ;
434
442
symbols. push ( symbol) ;
@@ -437,6 +445,7 @@ fn collect_assignment(
437
445
}
438
446
439
447
fn collect_assignment_with_function (
448
+ ctx : & mut CollectContext ,
440
449
node : & Node ,
441
450
contents : & Rope ,
442
451
symbols : & mut Vec < DocumentSymbol > ,
@@ -468,7 +477,7 @@ fn collect_assignment_with_function(
468
477
469
478
// Process the function body to extract child symbols
470
479
let mut children = Vec :: new ( ) ;
471
- collect_symbols ( & body, contents, 0 , & mut children) ?;
480
+ collect_symbols ( ctx , & body, contents, 0 , & mut children) ?;
472
481
473
482
let mut symbol = new_symbol_node ( name, SymbolKind :: FUNCTION , range, children) ;
474
483
symbol. detail = Some ( detail) ;
@@ -535,7 +544,8 @@ mod tests {
535
544
let node = doc. ast . root_node ( ) ;
536
545
537
546
let mut symbols = Vec :: new ( ) ;
538
- collect_symbols ( & node, & doc. contents , 0 , & mut symbols) . unwrap ( ) ;
547
+ let mut ctx = CollectContext ;
548
+ collect_symbols ( & mut ctx, & node, & doc. contents , 0 , & mut symbols) . unwrap ( ) ;
539
549
symbols
540
550
}
541
551
0 commit comments