Skip to content

Commit 0f795c1

Browse files
committed
Remove dangling current_level logic
1 parent 1dcb5b1 commit 0f795c1

File tree

1 file changed

+28
-45
lines changed

1 file changed

+28
-45
lines changed

crates/ark/src/lsp/symbols.rs

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub(crate) fn document_symbols(
173173
ctx.include_assignments_in_blocks = state.config.symbols.include_assignments_in_blocks;
174174

175175
// Extract and process all symbols from the AST
176-
if let Err(err) = collect_symbols(&mut ctx, &root_node, contents, 0, &mut result) {
176+
if let Err(err) = collect_symbols(&mut ctx, &root_node, contents, &mut result) {
177177
log::error!("Failed to collect symbols: {err:?}");
178178
return Ok(Vec::new());
179179
}
@@ -186,16 +186,15 @@ fn collect_symbols(
186186
ctx: &mut CollectContext,
187187
node: &Node,
188188
contents: &Rope,
189-
current_level: usize,
190189
symbols: &mut Vec<DocumentSymbol>,
191190
) -> anyhow::Result<()> {
192191
match node.node_type() {
193192
NodeType::Program => {
194-
collect_list_sections(ctx, node, contents, current_level, symbols)?;
193+
collect_list_sections(ctx, node, contents, symbols)?;
195194
},
196195

197196
NodeType::BracedExpression => {
198-
collect_list_sections(ctx, node, contents, current_level, symbols)?;
197+
collect_list_sections(ctx, node, contents, symbols)?;
199198
},
200199

201200
NodeType::IfStatement => {
@@ -205,7 +204,7 @@ fn collect_symbols(
205204
// } else {
206205
// x <- top_level_assignment
207206
// }
208-
collect_if_statement(ctx, node, contents, current_level, symbols)?;
207+
collect_if_statement(ctx, node, contents, symbols)?;
209208
},
210209

211210
NodeType::BinaryOperator(BinaryOperatorType::LeftAssignment) |
@@ -214,15 +213,15 @@ fn collect_symbols(
214213
},
215214

216215
NodeType::ForStatement => {
217-
collect_for_statement(ctx, node, contents, current_level, symbols)?;
216+
collect_for_statement(ctx, node, contents, symbols)?;
218217
},
219218

220219
NodeType::WhileStatement => {
221-
collect_while_statement(ctx, node, contents, current_level, symbols)?;
220+
collect_while_statement(ctx, node, contents, symbols)?;
222221
},
223222

224223
NodeType::RepeatStatement => {
225-
collect_repeat_statement(ctx, node, contents, current_level, symbols)?;
224+
collect_repeat_statement(ctx, node, contents, symbols)?;
226225
},
227226

228227
NodeType::Call => {
@@ -235,7 +234,7 @@ fn collect_symbols(
235234
NodeType::FunctionDefinition => {
236235
let old = ctx.top_level;
237236
ctx.top_level = false;
238-
collect_function(ctx, node, contents, current_level, symbols)?;
237+
collect_function(ctx, node, contents, symbols)?;
239238
ctx.top_level = old;
240239
},
241240

@@ -250,17 +249,16 @@ fn collect_if_statement(
250249
ctx: &mut CollectContext,
251250
node: &Node,
252251
contents: &Rope,
253-
current_level: usize,
254252
symbols: &mut Vec<DocumentSymbol>,
255253
) -> anyhow::Result<()> {
256254
if let Some(condition) = node.child_by_field_name("condition") {
257-
collect_symbols(ctx, &condition, contents, current_level, symbols)?;
255+
collect_symbols(ctx, &condition, contents, symbols)?;
258256
}
259257
if let Some(consequent) = node.child_by_field_name("consequence") {
260-
collect_symbols(ctx, &consequent, contents, current_level, symbols)?;
258+
collect_symbols(ctx, &consequent, contents, symbols)?;
261259
}
262260
if let Some(alternative) = node.child_by_field_name("alternative") {
263-
collect_symbols(ctx, &alternative, contents, current_level, symbols)?;
261+
collect_symbols(ctx, &alternative, contents, symbols)?;
264262
}
265263

266264
Ok(())
@@ -270,17 +268,16 @@ fn collect_for_statement(
270268
ctx: &mut CollectContext,
271269
node: &Node,
272270
contents: &Rope,
273-
current_level: usize,
274271
symbols: &mut Vec<DocumentSymbol>,
275272
) -> anyhow::Result<()> {
276273
if let Some(variable) = node.child_by_field_name("variable") {
277-
collect_symbols(ctx, &variable, contents, current_level, symbols)?;
274+
collect_symbols(ctx, &variable, contents, symbols)?;
278275
}
279276
if let Some(iterator) = node.child_by_field_name("iterator") {
280-
collect_symbols(ctx, &iterator, contents, current_level, symbols)?;
277+
collect_symbols(ctx, &iterator, contents, symbols)?;
281278
}
282279
if let Some(body) = node.child_by_field_name("body") {
283-
collect_symbols(ctx, &body, contents, current_level, symbols)?;
280+
collect_symbols(ctx, &body, contents, symbols)?;
284281
}
285282

286283
Ok(())
@@ -290,14 +287,13 @@ fn collect_while_statement(
290287
ctx: &mut CollectContext,
291288
node: &Node,
292289
contents: &Rope,
293-
current_level: usize,
294290
symbols: &mut Vec<DocumentSymbol>,
295291
) -> anyhow::Result<()> {
296292
if let Some(condition) = node.child_by_field_name("condition") {
297-
collect_symbols(ctx, &condition, contents, current_level, symbols)?;
293+
collect_symbols(ctx, &condition, contents, symbols)?;
298294
}
299295
if let Some(body) = node.child_by_field_name("body") {
300-
collect_symbols(ctx, &body, contents, current_level, symbols)?;
296+
collect_symbols(ctx, &body, contents, symbols)?;
301297
}
302298

303299
Ok(())
@@ -307,11 +303,10 @@ fn collect_repeat_statement(
307303
ctx: &mut CollectContext,
308304
node: &Node,
309305
contents: &Rope,
310-
current_level: usize,
311306
symbols: &mut Vec<DocumentSymbol>,
312307
) -> anyhow::Result<()> {
313308
if let Some(body) = node.child_by_field_name("body") {
314-
collect_symbols(ctx, &body, contents, current_level, symbols)?;
309+
collect_symbols(ctx, &body, contents, symbols)?;
315310
}
316311

317312
Ok(())
@@ -321,14 +316,13 @@ fn collect_function(
321316
ctx: &mut CollectContext,
322317
node: &Node,
323318
contents: &Rope,
324-
current_level: usize,
325319
symbols: &mut Vec<DocumentSymbol>,
326320
) -> anyhow::Result<()> {
327321
if let Some(parameters) = node.child_by_field_name("parameters") {
328322
collect_function_parameters(ctx, &parameters, contents, symbols)?;
329323
}
330324
if let Some(body) = node.child_by_field_name("body") {
331-
collect_symbols(ctx, &body, contents, current_level, symbols)?;
325+
collect_symbols(ctx, &body, contents, symbols)?;
332326
}
333327

334328
Ok(())
@@ -383,7 +377,6 @@ fn collect_sections<F>(
383377
ctx: &mut CollectContext,
384378
node: &Node,
385379
contents: &Rope,
386-
current_level: usize,
387380
symbols: &mut Vec<DocumentSymbol>,
388381
mut handle_child: F,
389382
) -> anyhow::Result<()>
@@ -404,11 +397,8 @@ where
404397

405398
// If we have a section comment, add it to our stack and close any sections if needed
406399
if let Some((level, title)) = parse_comment_as_section(&comment_text) {
407-
let absolute_level = current_level + level;
408-
409400
// Close any sections with equal or higher level
410-
while !active_sections.is_empty() &&
411-
active_sections.last().unwrap().level >= absolute_level
401+
while !active_sections.is_empty() && active_sections.last().unwrap().level >= level
412402
{
413403
// Set end position for the section being closed
414404
if let Some(section) = active_sections.last_mut() {
@@ -420,7 +410,7 @@ where
420410

421411
let section = Section {
422412
title,
423-
level: absolute_level,
413+
level,
424414
start_position: child.start_position(),
425415
end_position: None,
426416
children: Vec::new(),
@@ -473,18 +463,14 @@ fn collect_list_sections(
473463
ctx: &mut CollectContext,
474464
node: &Node,
475465
contents: &Rope,
476-
current_level: usize,
477466
symbols: &mut Vec<DocumentSymbol>,
478467
) -> anyhow::Result<()> {
479468
collect_sections(
480469
ctx,
481470
node,
482471
contents,
483-
current_level,
484472
symbols,
485-
|ctx, child, contents, symbols| {
486-
collect_symbols(ctx, child, contents, current_level, symbols)
487-
},
473+
|ctx, child, contents, symbols| collect_symbols(ctx, child, contents, symbols),
488474
)
489475
}
490476

@@ -525,7 +511,6 @@ fn collect_call_arguments(
525511
ctx,
526512
&arguments,
527513
contents,
528-
0,
529514
symbols,
530515
|ctx, child, contents, symbols| {
531516
let Some(arg_value) = child.child_by_field_name("value") else {
@@ -541,7 +526,7 @@ fn collect_call_arguments(
541526
// else fallthrough
542527
}
543528

544-
collect_symbols(ctx, &arg_value, contents, 0, symbols)?;
529+
collect_symbols(ctx, &arg_value, contents, symbols)?;
545530

546531
Ok(())
547532
},
@@ -558,7 +543,6 @@ fn collect_function_parameters(
558543
ctx,
559544
&node,
560545
contents,
561-
0,
562546
symbols,
563547
|_ctx, _child, _contents, _symbols| {
564548
// Only collect sections
@@ -583,7 +567,7 @@ fn collect_method(
583567
let end = convert_point_to_position(contents, arg_value.end_position());
584568

585569
let mut children = vec![];
586-
collect_symbols(ctx, arg_value, contents, 0, &mut children)?;
570+
collect_symbols(ctx, arg_value, contents, &mut children)?;
587571

588572
let mut symbol = new_symbol_node(
589573
arg_name_str,
@@ -631,7 +615,7 @@ fn collect_call_test_that(
631615
let mut cursor = arguments.walk();
632616
for child in arguments.children_by_field_name("argument", &mut cursor) {
633617
if let Some(value) = child.child_by_field_name("value") {
634-
collect_symbols(ctx, &value, contents, 0, &mut children)?;
618+
collect_symbols(ctx, &value, contents, &mut children)?;
635619
}
636620
}
637621

@@ -683,13 +667,13 @@ fn collect_assignment(
683667

684668
// Now recurse into RHS
685669
let mut children = Vec::new();
686-
collect_symbols(ctx, &rhs, contents, 0, &mut children)?;
670+
collect_symbols(ctx, &rhs, contents, &mut children)?;
687671

688672
let symbol = new_symbol_node(name, SymbolKind::VARIABLE, Range { start, end }, children);
689673
symbols.push(symbol);
690674
} else {
691675
// Recurse into RHS
692-
collect_symbols(ctx, &rhs, contents, 0, symbols)?;
676+
collect_symbols(ctx, &rhs, contents, symbols)?;
693677
}
694678

695679
Ok(())
@@ -726,7 +710,7 @@ fn collect_assignment_with_function(
726710

727711
// Process the function body to extract child symbols
728712
let mut children = Vec::new();
729-
collect_symbols(ctx, &rhs, contents, 0, &mut children)?;
713+
collect_symbols(ctx, &rhs, contents, &mut children)?;
730714

731715
let mut symbol = new_symbol_node(name, SymbolKind::FUNCTION, range, children);
732716
symbol.detail = Some(detail);
@@ -801,7 +785,6 @@ mod tests {
801785
&mut CollectContext::new(),
802786
&node,
803787
&doc.contents,
804-
0,
805788
&mut symbols,
806789
)
807790
.unwrap();
@@ -1172,7 +1155,7 @@ outer <- 4
11721155
ctx.include_assignments_in_blocks = true;
11731156

11741157
let mut symbols = Vec::new();
1175-
collect_symbols(ctx, &node, &doc.contents, 0, &mut symbols).unwrap();
1158+
collect_symbols(ctx, &node, &doc.contents, &mut symbols).unwrap();
11761159

11771160
insta::assert_debug_snapshot!(symbols);
11781161
}

0 commit comments

Comments
 (0)