Skip to content

Commit 3593482

Browse files
committed
Collect document symbols in RHS of assigned objects
1 parent ae5755f commit 3593482

File tree

3 files changed

+289
-21
lines changed

3 files changed

+289
-21
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
source: crates/ark/src/lsp/symbols.rs
3+
expression: "test_symbol(\"\n# section ----\nclass <- r6::r6class(\n 'class',\n public = list(\n initialize = function() 'initialize',\n foo = function() 'foo'\n ),\n private = list(\n bar = function() 'bar'\n )\n)\n\")"
4+
---
5+
[
6+
DocumentSymbol {
7+
name: "section",
8+
detail: None,
9+
kind: String,
10+
tags: None,
11+
deprecated: None,
12+
range: Range {
13+
start: Position {
14+
line: 1,
15+
character: 0,
16+
},
17+
end: Position {
18+
line: 11,
19+
character: 1,
20+
},
21+
},
22+
selection_range: Range {
23+
start: Position {
24+
line: 1,
25+
character: 0,
26+
},
27+
end: Position {
28+
line: 11,
29+
character: 1,
30+
},
31+
},
32+
children: Some(
33+
[
34+
DocumentSymbol {
35+
name: "class",
36+
detail: None,
37+
kind: Variable,
38+
tags: None,
39+
deprecated: None,
40+
range: Range {
41+
start: Position {
42+
line: 2,
43+
character: 0,
44+
},
45+
end: Position {
46+
line: 2,
47+
character: 5,
48+
},
49+
},
50+
selection_range: Range {
51+
start: Position {
52+
line: 2,
53+
character: 0,
54+
},
55+
end: Position {
56+
line: 2,
57+
character: 5,
58+
},
59+
},
60+
children: Some(
61+
[
62+
DocumentSymbol {
63+
name: "initialize",
64+
detail: Some(
65+
"function()",
66+
),
67+
kind: Method,
68+
tags: None,
69+
deprecated: None,
70+
range: Range {
71+
start: Position {
72+
line: 5,
73+
character: 17,
74+
},
75+
end: Position {
76+
line: 5,
77+
character: 40,
78+
},
79+
},
80+
selection_range: Range {
81+
start: Position {
82+
line: 5,
83+
character: 17,
84+
},
85+
end: Position {
86+
line: 5,
87+
character: 40,
88+
},
89+
},
90+
children: Some(
91+
[],
92+
),
93+
},
94+
DocumentSymbol {
95+
name: "foo",
96+
detail: Some(
97+
"function()",
98+
),
99+
kind: Method,
100+
tags: None,
101+
deprecated: None,
102+
range: Range {
103+
start: Position {
104+
line: 6,
105+
character: 10,
106+
},
107+
end: Position {
108+
line: 6,
109+
character: 26,
110+
},
111+
},
112+
selection_range: Range {
113+
start: Position {
114+
line: 6,
115+
character: 10,
116+
},
117+
end: Position {
118+
line: 6,
119+
character: 26,
120+
},
121+
},
122+
children: Some(
123+
[],
124+
),
125+
},
126+
DocumentSymbol {
127+
name: "bar",
128+
detail: Some(
129+
"function()",
130+
),
131+
kind: Method,
132+
tags: None,
133+
deprecated: None,
134+
range: Range {
135+
start: Position {
136+
line: 9,
137+
character: 10,
138+
},
139+
end: Position {
140+
line: 9,
141+
character: 26,
142+
},
143+
},
144+
selection_range: Range {
145+
start: Position {
146+
line: 9,
147+
character: 10,
148+
},
149+
end: Position {
150+
line: 9,
151+
character: 26,
152+
},
153+
},
154+
children: Some(
155+
[],
156+
),
157+
},
158+
],
159+
),
160+
},
161+
],
162+
),
163+
},
164+
]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
source: crates/ark/src/lsp/symbols.rs
3+
expression: "test_symbol(\"\nfoo <- {\n bar <- function() {}\n}\n\")"
4+
---
5+
[
6+
DocumentSymbol {
7+
name: "foo",
8+
detail: None,
9+
kind: Variable,
10+
tags: None,
11+
deprecated: None,
12+
range: Range {
13+
start: Position {
14+
line: 1,
15+
character: 0,
16+
},
17+
end: Position {
18+
line: 1,
19+
character: 3,
20+
},
21+
},
22+
selection_range: Range {
23+
start: Position {
24+
line: 1,
25+
character: 0,
26+
},
27+
end: Position {
28+
line: 1,
29+
character: 3,
30+
},
31+
},
32+
children: Some(
33+
[
34+
DocumentSymbol {
35+
name: "bar",
36+
detail: Some(
37+
"function()",
38+
),
39+
kind: Function,
40+
tags: None,
41+
deprecated: None,
42+
range: Range {
43+
start: Position {
44+
line: 2,
45+
character: 4,
46+
},
47+
end: Position {
48+
line: 2,
49+
character: 24,
50+
},
51+
},
52+
selection_range: Range {
53+
start: Position {
54+
line: 2,
55+
character: 4,
56+
},
57+
end: Position {
58+
line: 2,
59+
character: 24,
60+
},
61+
},
62+
children: Some(
63+
[],
64+
),
65+
},
66+
],
67+
),
68+
},
69+
]

crates/ark/src/lsp/symbols.rs

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,18 @@ fn collect_call(
265265
let Some(callee) = node.child_by_field_name("function") else {
266266
return Ok(());
267267
};
268-
if !callee.is_identifier() {
269-
return Ok(());
270-
}
271268

272-
let fun_symbol = contents.node_slice(&callee)?.to_string();
269+
if callee.is_identifier() {
270+
let fun_symbol = contents.node_slice(&callee)?.to_string();
273271

274-
match fun_symbol.as_str() {
275-
"test_that" => collect_call_test_that(node, contents, symbols)?,
276-
_ => collect_call_arguments(node, contents, symbols)?,
272+
match fun_symbol.as_str() {
273+
"test_that" => return collect_call_test_that(node, contents, symbols),
274+
_ => {}, // fallthrough
275+
}
277276
}
278277

278+
collect_call_arguments(node, contents, symbols)?;
279+
279280
Ok(())
280281
}
281282

@@ -402,32 +403,36 @@ fn collect_assignment(
402403
contents: &Rope,
403404
symbols: &mut Vec<DocumentSymbol>,
404405
) -> anyhow::Result<()> {
405-
// Check for assignment
406-
matches!(
407-
node.node_type(),
408-
NodeType::BinaryOperator(BinaryOperatorType::LeftAssignment) |
409-
NodeType::BinaryOperator(BinaryOperatorType::EqualsAssignment)
410-
)
411-
.into_result()?;
406+
let (NodeType::BinaryOperator(BinaryOperatorType::LeftAssignment) |
407+
NodeType::BinaryOperator(BinaryOperatorType::EqualsAssignment)) = node.node_type()
408+
else {
409+
return Ok(());
410+
};
412411

413-
// check for lhs, rhs
414-
let lhs = node.child_by_field_name("lhs").into_result()?;
415-
let rhs = node.child_by_field_name("rhs").into_result()?;
412+
let (Some(lhs), Some(rhs)) = (
413+
node.child_by_field_name("lhs"),
414+
node.child_by_field_name("rhs"),
415+
) else {
416+
return Ok(());
417+
};
416418

417-
// check for identifier on lhs, function on rhs
419+
// If a function, collect symbol as function
418420
let function = lhs.is_identifier_or_string() && rhs.is_function_definition();
419-
420421
if function {
421422
return collect_assignment_with_function(node, contents, symbols);
422423
}
423424

424-
// otherwise, just index as generic object
425+
// Otherwise, collect as generic object
425426
let name = contents.node_slice(&lhs)?.to_string();
426427

427428
let start = convert_point_to_position(contents, lhs.start_position());
428429
let end = convert_point_to_position(contents, lhs.end_position());
429430

430-
let symbol = new_symbol(name, SymbolKind::VARIABLE, Range { start, end });
431+
// Now recurse into RHS
432+
let mut children = Vec::new();
433+
collect_symbols(&rhs, contents, 0, &mut children)?;
434+
435+
let symbol = new_symbol_node(name, SymbolKind::VARIABLE, Range { start, end }, children);
431436
symbols.push(symbol);
432437

433438
Ok(())
@@ -792,6 +797,36 @@ local({
792797
1
793798
}
794799
})
800+
"
801+
));
802+
}
803+
804+
#[test]
805+
fn test_symbol_rhs_braced_list() {
806+
insta::assert_debug_snapshot!(test_symbol(
807+
"
808+
foo <- {
809+
bar <- function() {}
810+
}
811+
"
812+
));
813+
}
814+
815+
#[test]
816+
fn test_symbol_rhs_methods() {
817+
insta::assert_debug_snapshot!(test_symbol(
818+
"
819+
# section ----
820+
class <- r6::r6class(
821+
'class',
822+
public = list(
823+
initialize = function() 'initialize',
824+
foo = function() 'foo'
825+
),
826+
private = list(
827+
bar = function() 'bar'
828+
)
829+
)
795830
"
796831
));
797832
}

0 commit comments

Comments
 (0)