Skip to content

Commit bdd1af1

Browse files
authored
Merge pull request #859 from posit-dev/feature/top-level-assign-symbols
Don't emit nested objects as document symbols
2 parents 0f3d5a8 + db257d2 commit bdd1af1

7 files changed

+455
-76
lines changed

crates/ark/src/lsp/config.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ use crate::lsp;
66
use crate::lsp::diagnostics::DiagnosticsConfig;
77

88
/// Configuration of the LSP
9-
#[derive(Clone, Debug)]
9+
#[derive(Clone, Default, Debug)]
1010
pub(crate) struct LspConfig {
1111
pub(crate) diagnostics: DiagnosticsConfig,
12+
pub(crate) symbols: SymbolsConfig,
13+
}
14+
15+
#[derive(Serialize, Deserialize, Clone, Debug)]
16+
pub struct SymbolsConfig {
17+
/// Whether to emit assignments in `{` bloks as document symbols.
18+
pub include_assignments_in_blocks: bool,
1219
}
1320

1421
/// Configuration of a document.
@@ -53,17 +60,23 @@ pub(crate) struct VscDiagnosticsConfig {
5360
pub enable: bool,
5461
}
5562

63+
#[derive(Serialize, Deserialize, FieldNamesAsArray, Clone, Debug)]
64+
pub(crate) struct VscSymbolsConfig {
65+
// DEV NOTE: Update `section_from_key()` method after adding a field
66+
pub include_assignments_in_blocks: bool,
67+
}
68+
5669
#[derive(Serialize, Deserialize, Clone, Debug)]
5770
#[serde(untagged)]
5871
pub(crate) enum VscIndentSize {
5972
Alias(String),
6073
Size(usize),
6174
}
6275

63-
impl Default for LspConfig {
76+
impl Default for SymbolsConfig {
6477
fn default() -> Self {
6578
Self {
66-
diagnostics: Default::default(),
79+
include_assignments_in_blocks: false,
6780
}
6881
}
6982
}
@@ -134,6 +147,23 @@ impl From<VscDiagnosticsConfig> for DiagnosticsConfig {
134147
}
135148
}
136149

150+
impl VscSymbolsConfig {
151+
pub(crate) fn section_from_key(key: &str) -> &str {
152+
match key {
153+
"include_assignments_in_blocks" => "positron.r.symbols.includeAssignmentsInBlocks",
154+
_ => "unknown", // To be caught via downstream errors
155+
}
156+
}
157+
}
158+
159+
impl From<VscSymbolsConfig> for SymbolsConfig {
160+
fn from(value: VscSymbolsConfig) -> Self {
161+
Self {
162+
include_assignments_in_blocks: value.include_assignments_in_blocks,
163+
}
164+
}
165+
}
166+
137167
pub(crate) fn indent_style_from_lsp(insert_spaces: bool) -> IndentStyle {
138168
if insert_spaces {
139169
IndentStyle::Space

crates/ark/src/lsp/handlers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use crate::lsp::completions::provide_completions;
4848
use crate::lsp::completions::resolve_completion;
4949
use crate::lsp::config::VscDiagnosticsConfig;
5050
use crate::lsp::config::VscDocumentConfig;
51+
use crate::lsp::config::VscSymbolsConfig;
5152
use crate::lsp::definitions::goto_definition;
5253
use crate::lsp::document_context::DocumentContext;
5354
use crate::lsp::encoding::convert_lsp_range_to_tree_sitter_range;
@@ -109,12 +110,17 @@ pub(crate) async fn handle_initialized(
109110
VscDocumentConfig::FIELD_NAMES_AS_ARRAY.to_vec(),
110111
VscDocumentConfig::section_from_key,
111112
);
113+
let mut config_symbols_regs: Vec<Registration> = collect_regs(
114+
VscSymbolsConfig::FIELD_NAMES_AS_ARRAY.to_vec(),
115+
VscSymbolsConfig::section_from_key,
116+
);
112117
let mut config_diagnostics_regs: Vec<Registration> = collect_regs(
113118
VscDiagnosticsConfig::FIELD_NAMES_AS_ARRAY.to_vec(),
114119
VscDiagnosticsConfig::section_from_key,
115120
);
116121

117122
regs.append(&mut config_document_regs);
123+
regs.append(&mut config_symbols_regs);
118124
regs.append(&mut config_diagnostics_regs);
119125
}
120126

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
source: crates/ark/src/lsp/symbols.rs
3+
expression: "test_symbol(\"foo <- function() { bar <- function() 1 }\")"
4+
---
5+
[
6+
DocumentSymbol {
7+
name: "foo",
8+
detail: Some(
9+
"function()",
10+
),
11+
kind: Function,
12+
tags: None,
13+
deprecated: None,
14+
range: Range {
15+
start: Position {
16+
line: 0,
17+
character: 0,
18+
},
19+
end: Position {
20+
line: 0,
21+
character: 41,
22+
},
23+
},
24+
selection_range: Range {
25+
start: Position {
26+
line: 0,
27+
character: 0,
28+
},
29+
end: Position {
30+
line: 0,
31+
character: 41,
32+
},
33+
},
34+
children: Some(
35+
[
36+
DocumentSymbol {
37+
name: "bar",
38+
detail: Some(
39+
"function()",
40+
),
41+
kind: Function,
42+
tags: None,
43+
deprecated: None,
44+
range: Range {
45+
start: Position {
46+
line: 0,
47+
character: 20,
48+
},
49+
end: Position {
50+
line: 0,
51+
character: 39,
52+
},
53+
},
54+
selection_range: Range {
55+
start: Position {
56+
line: 0,
57+
character: 20,
58+
},
59+
end: Position {
60+
line: 0,
61+
character: 39,
62+
},
63+
},
64+
children: Some(
65+
[],
66+
),
67+
},
68+
],
69+
),
70+
},
71+
]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
source: crates/ark/src/lsp/symbols.rs
3+
expression: "test_symbol(\"\nlocal({\n inner1 <- 1 # Not a symbol\n})\na <- function() {\n inner2 <- 2 # Not a symbol\n inner3 <- function() 3 # Symbol\n}\n\")"
4+
---
5+
[
6+
DocumentSymbol {
7+
name: "a",
8+
detail: Some(
9+
"function()",
10+
),
11+
kind: Function,
12+
tags: None,
13+
deprecated: None,
14+
range: Range {
15+
start: Position {
16+
line: 4,
17+
character: 0,
18+
},
19+
end: Position {
20+
line: 7,
21+
character: 1,
22+
},
23+
},
24+
selection_range: Range {
25+
start: Position {
26+
line: 4,
27+
character: 0,
28+
},
29+
end: Position {
30+
line: 7,
31+
character: 1,
32+
},
33+
},
34+
children: Some(
35+
[
36+
DocumentSymbol {
37+
name: "inner3",
38+
detail: Some(
39+
"function()",
40+
),
41+
kind: Function,
42+
tags: None,
43+
deprecated: None,
44+
range: Range {
45+
start: Position {
46+
line: 6,
47+
character: 2,
48+
},
49+
end: Position {
50+
line: 6,
51+
character: 24,
52+
},
53+
},
54+
selection_range: Range {
55+
start: Position {
56+
line: 6,
57+
character: 2,
58+
},
59+
end: Position {
60+
line: 6,
61+
character: 24,
62+
},
63+
},
64+
children: Some(
65+
[],
66+
),
67+
},
68+
],
69+
),
70+
},
71+
]
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
source: crates/ark/src/lsp/symbols.rs
3+
expression: symbols
4+
---
5+
[
6+
DocumentSymbol {
7+
name: "inner1",
8+
detail: None,
9+
kind: Variable,
10+
tags: None,
11+
deprecated: None,
12+
range: Range {
13+
start: Position {
14+
line: 2,
15+
character: 2,
16+
},
17+
end: Position {
18+
line: 2,
19+
character: 8,
20+
},
21+
},
22+
selection_range: Range {
23+
start: Position {
24+
line: 2,
25+
character: 2,
26+
},
27+
end: Position {
28+
line: 2,
29+
character: 8,
30+
},
31+
},
32+
children: Some(
33+
[],
34+
),
35+
},
36+
DocumentSymbol {
37+
name: "a",
38+
detail: Some(
39+
"function()",
40+
),
41+
kind: Function,
42+
tags: None,
43+
deprecated: None,
44+
range: Range {
45+
start: Position {
46+
line: 4,
47+
character: 0,
48+
},
49+
end: Position {
50+
line: 7,
51+
character: 1,
52+
},
53+
},
54+
selection_range: Range {
55+
start: Position {
56+
line: 4,
57+
character: 0,
58+
},
59+
end: Position {
60+
line: 7,
61+
character: 1,
62+
},
63+
},
64+
children: Some(
65+
[
66+
DocumentSymbol {
67+
name: "inner2",
68+
detail: None,
69+
kind: Variable,
70+
tags: None,
71+
deprecated: None,
72+
range: Range {
73+
start: Position {
74+
line: 5,
75+
character: 2,
76+
},
77+
end: Position {
78+
line: 5,
79+
character: 8,
80+
},
81+
},
82+
selection_range: Range {
83+
start: Position {
84+
line: 5,
85+
character: 2,
86+
},
87+
end: Position {
88+
line: 5,
89+
character: 8,
90+
},
91+
},
92+
children: Some(
93+
[],
94+
),
95+
},
96+
DocumentSymbol {
97+
name: "inner3",
98+
detail: Some(
99+
"function()",
100+
),
101+
kind: Function,
102+
tags: None,
103+
deprecated: None,
104+
range: Range {
105+
start: Position {
106+
line: 6,
107+
character: 2,
108+
},
109+
end: Position {
110+
line: 6,
111+
character: 24,
112+
},
113+
},
114+
selection_range: Range {
115+
start: Position {
116+
line: 6,
117+
character: 2,
118+
},
119+
end: Position {
120+
line: 6,
121+
character: 24,
122+
},
123+
},
124+
children: Some(
125+
[],
126+
),
127+
},
128+
],
129+
),
130+
},
131+
]

0 commit comments

Comments
 (0)