Skip to content

Commit 9318e7c

Browse files
committed
Add CollectContect arguments
1 parent 698f1a3 commit 9318e7c

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

crates/ark/src/lsp/symbols.rs

Lines changed: 30 additions & 24 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>,
@@ -289,28 +294,24 @@ fn collect_call_arguments(
289294
return Ok(());
290295
};
291296

292-
let mut cursor = node.walk();
297+
let mut cursor = arguments.walk();
293298
for arg in arguments.children(&mut cursor) {
294-
if arg.kind() != "argument" {
295-
continue;
296-
}
297-
298299
let Some(arg_value) = arg.child_by_field_name("value") else {
299300
continue;
300301
};
301302

302303
// Recurse into arguments. They might be a braced list, another call
303304
// that might contain functions, etc.
304-
collect_symbols(&arg_value, contents, 0, symbols)?;
305+
collect_symbols(ctx, &arg_value, contents, 0, symbols)?;
305306

306307
if arg_value.kind() == "function_definition" {
307308
if let Some(arg_fun) = arg.child_by_field_name("name") {
308309
// 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)?;
310311
} else {
311312
// Otherwise, just recurse into the function
312313
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)?;
314315
};
315316
}
316317
}
@@ -319,6 +320,7 @@ fn collect_call_arguments(
319320
}
320321

321322
fn collect_method(
323+
ctx: &mut CollectContext,
322324
arg_fun: &Node,
323325
arg_value: &Node,
324326
contents: &Rope,
@@ -334,7 +336,7 @@ fn collect_method(
334336

335337
let body = arg_value.child_by_field_name("body").into_result()?;
336338
let mut children = vec![];
337-
collect_symbols(&body, contents, 0, &mut children)?;
339+
collect_symbols(ctx, &body, contents, 0, &mut children)?;
338340

339341
let mut symbol = new_symbol_node(
340342
arg_name_str,
@@ -355,6 +357,7 @@ fn collect_method(
355357

356358
// https://github.com/posit-dev/positron/issues/1428
357359
fn collect_call_test_that(
360+
ctx: &mut CollectContext,
358361
node: &Node,
359362
contents: &Rope,
360363
symbols: &mut Vec<DocumentSymbol>,
@@ -381,7 +384,7 @@ fn collect_call_test_that(
381384
let mut cursor = arguments.walk();
382385
for child in arguments.children_by_field_name("argument", &mut cursor) {
383386
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)?;
385388
}
386389
}
387390

@@ -398,6 +401,7 @@ fn collect_call_test_that(
398401
}
399402

400403
fn collect_assignment(
404+
ctx: &mut CollectContext,
401405
node: &Node,
402406
contents: &Rope,
403407
symbols: &mut Vec<DocumentSymbol>,
@@ -418,7 +422,7 @@ fn collect_assignment(
418422
// If a function, collect symbol as function
419423
let function = lhs.is_identifier_or_string() && rhs.is_function_definition();
420424
if function {
421-
return collect_assignment_with_function(node, contents, symbols);
425+
return collect_assignment_with_function(ctx, node, contents, symbols);
422426
}
423427

424428
// Otherwise, collect as generic object
@@ -429,7 +433,7 @@ fn collect_assignment(
429433

430434
// Now recurse into RHS
431435
let mut children = Vec::new();
432-
collect_symbols(&rhs, contents, 0, &mut children)?;
436+
collect_symbols(ctx, &rhs, contents, 0, &mut children)?;
433437

434438
let symbol = new_symbol_node(name, SymbolKind::VARIABLE, Range { start, end }, children);
435439
symbols.push(symbol);
@@ -438,6 +442,7 @@ fn collect_assignment(
438442
}
439443

440444
fn collect_assignment_with_function(
445+
ctx: &mut CollectContext,
441446
node: &Node,
442447
contents: &Rope,
443448
symbols: &mut Vec<DocumentSymbol>,
@@ -469,7 +474,7 @@ fn collect_assignment_with_function(
469474

470475
// Process the function body to extract child symbols
471476
let mut children = Vec::new();
472-
collect_symbols(&body, contents, 0, &mut children)?;
477+
collect_symbols(ctx, &body, contents, 0, &mut children)?;
473478

474479
let mut symbol = new_symbol_node(name, SymbolKind::FUNCTION, range, children);
475480
symbol.detail = Some(detail);
@@ -536,7 +541,8 @@ mod tests {
536541
let node = doc.ast.root_node();
537542

538543
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();
540546
symbols
541547
}
542548

0 commit comments

Comments
 (0)