Skip to content

Commit 0704a05

Browse files
committed
Add CollectContect arguments
1 parent 0f3d5a8 commit 0704a05

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

crates/ark/src/lsp/symbols.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ struct Section {
122122
children: Vec<DocumentSymbol>,
123123
}
124124

125+
struct CollectContext;
126+
125127
pub(crate) fn document_symbols(
126128
state: &WorldState,
127129
params: &DocumentSymbolParams,
@@ -136,7 +138,8 @@ pub(crate) fn document_symbols(
136138
let mut result = Vec::new();
137139

138140
// 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) {
140143
log::error!("Failed to collect symbols: {err:?}");
141144
return Ok(Vec::new());
142145
}
@@ -146,33 +149,34 @@ pub(crate) fn document_symbols(
146149

147150
/// Collect all document symbols from a node recursively
148151
fn collect_symbols(
152+
ctx: &mut CollectContext,
149153
node: &Node,
150154
contents: &Rope,
151155
current_level: usize,
152156
symbols: &mut Vec<DocumentSymbol>,
153157
) -> anyhow::Result<()> {
154158
match node.node_type() {
155159
NodeType::Program | NodeType::BracedExpression => {
156-
collect_sections(node, contents, current_level, symbols)?;
160+
collect_sections(ctx, node, contents, current_level, symbols)?;
157161
},
158162

159163
NodeType::Call => {
160-
collect_call(node, contents, symbols)?;
164+
collect_call(ctx, node, contents, symbols)?;
161165
},
162166

163167
NodeType::BinaryOperator(BinaryOperatorType::LeftAssignment) |
164168
NodeType::BinaryOperator(BinaryOperatorType::EqualsAssignment) => {
165-
collect_assignment(node, contents, symbols)?;
169+
collect_assignment(ctx, node, contents, symbols)?;
166170
},
167171

168172
// For all other node types, no symbols need to be added
169173
_ => {},
170174
}
171-
172175
Ok(())
173176
}
174177

175178
fn collect_sections(
179+
ctx: &mut CollectContext,
176180
node: &Node,
177181
contents: &Rope,
178182
current_level: usize,
@@ -224,11 +228,11 @@ fn collect_sections(
224228

225229
if active_sections.is_empty() {
226230
// 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)?;
228232
} else {
229233
// Otherwise create new store of symbols for the current section
230234
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)?;
232236

233237
// Nest them inside last section
234238
if !child_symbols.is_empty() {
@@ -258,6 +262,7 @@ fn collect_sections(
258262
}
259263

260264
fn collect_call(
265+
ctx: &mut CollectContext,
261266
node: &Node,
262267
contents: &Rope,
263268
symbols: &mut Vec<DocumentSymbol>,
@@ -268,19 +273,19 @@ fn collect_call(
268273

269274
if callee.is_identifier() {
270275
let fun_symbol = contents.node_slice(&callee)?.to_string();
271-
272276
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),
274278
_ => {}, // fallthrough
275279
}
276280
}
277281

278-
collect_call_arguments(node, contents, symbols)?;
282+
collect_call_arguments(ctx, node, contents, symbols)?;
279283

280284
Ok(())
281285
}
282286

283287
fn collect_call_arguments(
288+
ctx: &mut CollectContext,
284289
node: &Node,
285290
contents: &Rope,
286291
symbols: &mut Vec<DocumentSymbol>,
@@ -299,17 +304,17 @@ fn collect_call_arguments(
299304
"function_definition" => {
300305
if let Some(arg_fun) = arg.child_by_field_name("name") {
301306
// 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)?;
303308
} else {
304309
// Otherwise, just recurse into the function
305310
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)?;
307312
};
308313
},
309314
_ => {
310315
// Recurse into arguments. They might be a braced list, another call
311316
// that might contain functions, etc.
312-
collect_symbols(&arg_value, contents, 0, symbols)?;
317+
collect_symbols(ctx, &arg_value, contents, 0, symbols)?;
313318
},
314319
}
315320
}
@@ -318,6 +323,7 @@ fn collect_call_arguments(
318323
}
319324

320325
fn collect_method(
326+
ctx: &mut CollectContext,
321327
arg_fun: &Node,
322328
arg_value: &Node,
323329
contents: &Rope,
@@ -333,7 +339,7 @@ fn collect_method(
333339

334340
let body = arg_value.child_by_field_name("body").into_result()?;
335341
let mut children = vec![];
336-
collect_symbols(&body, contents, 0, &mut children)?;
342+
collect_symbols(ctx, &body, contents, 0, &mut children)?;
337343

338344
let mut symbol = new_symbol_node(
339345
arg_name_str,
@@ -354,6 +360,7 @@ fn collect_method(
354360

355361
// https://github.com/posit-dev/positron/issues/1428
356362
fn collect_call_test_that(
363+
ctx: &mut CollectContext,
357364
node: &Node,
358365
contents: &Rope,
359366
symbols: &mut Vec<DocumentSymbol>,
@@ -380,7 +387,7 @@ fn collect_call_test_that(
380387
let mut cursor = arguments.walk();
381388
for child in arguments.children_by_field_name("argument", &mut cursor) {
382389
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)?;
384391
}
385392
}
386393

@@ -397,6 +404,7 @@ fn collect_call_test_that(
397404
}
398405

399406
fn collect_assignment(
407+
ctx: &mut CollectContext,
400408
node: &Node,
401409
contents: &Rope,
402410
symbols: &mut Vec<DocumentSymbol>,
@@ -417,7 +425,7 @@ fn collect_assignment(
417425
// If a function, collect symbol as function
418426
let function = lhs.is_identifier_or_string() && rhs.is_function_definition();
419427
if function {
420-
return collect_assignment_with_function(node, contents, symbols);
428+
return collect_assignment_with_function(ctx, node, contents, symbols);
421429
}
422430

423431
// Otherwise, collect as generic object
@@ -428,7 +436,7 @@ fn collect_assignment(
428436

429437
// Now recurse into RHS
430438
let mut children = Vec::new();
431-
collect_symbols(&rhs, contents, 0, &mut children)?;
439+
collect_symbols(ctx, &rhs, contents, 0, &mut children)?;
432440

433441
let symbol = new_symbol_node(name, SymbolKind::VARIABLE, Range { start, end }, children);
434442
symbols.push(symbol);
@@ -437,6 +445,7 @@ fn collect_assignment(
437445
}
438446

439447
fn collect_assignment_with_function(
448+
ctx: &mut CollectContext,
440449
node: &Node,
441450
contents: &Rope,
442451
symbols: &mut Vec<DocumentSymbol>,
@@ -468,7 +477,7 @@ fn collect_assignment_with_function(
468477

469478
// Process the function body to extract child symbols
470479
let mut children = Vec::new();
471-
collect_symbols(&body, contents, 0, &mut children)?;
480+
collect_symbols(ctx, &body, contents, 0, &mut children)?;
472481

473482
let mut symbol = new_symbol_node(name, SymbolKind::FUNCTION, range, children);
474483
symbol.detail = Some(detail);
@@ -535,7 +544,8 @@ mod tests {
535544
let node = doc.ast.root_node();
536545

537546
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();
539549
symbols
540550
}
541551

0 commit comments

Comments
 (0)