|
1 | 1 | use serde::Deserialize;
|
2 | 2 | use serde::Serialize;
|
3 |
| -use struct_field_names_as_array::FieldNamesAsArray; |
| 3 | +use serde_json::Value; |
4 | 4 |
|
5 |
| -use crate::lsp; |
6 | 5 | use crate::lsp::diagnostics::DiagnosticsConfig;
|
7 | 6 |
|
| 7 | +pub struct Setting { |
| 8 | + pub key: &'static str, |
| 9 | + pub set: fn(&mut LspConfig, Value), |
| 10 | +} |
| 11 | + |
| 12 | +// List of LSP settings for which clients can send `didChangeConfiguration` |
| 13 | +// notifications. We register our interest in watching over these settings in |
| 14 | +// our `initialized` handler. The `set` methods convert from a json `Value` to |
| 15 | +// the expected type, using a default value if the conversion fails. |
| 16 | +pub static SETTINGS: &[Setting] = &[ |
| 17 | + Setting { |
| 18 | + key: "editor.insertSpaces", |
| 19 | + set: |cfg, v| { |
| 20 | + let default_style = IndentationConfig::default().indent_style; |
| 21 | + cfg.document.indent.indent_style = if v |
| 22 | + .as_bool() |
| 23 | + .unwrap_or_else(|| default_style == IndentStyle::Space) |
| 24 | + { |
| 25 | + IndentStyle::Space |
| 26 | + } else { |
| 27 | + IndentStyle::Tab |
| 28 | + } |
| 29 | + }, |
| 30 | + }, |
| 31 | + Setting { |
| 32 | + key: "editor.indentSize", |
| 33 | + set: |cfg, v| { |
| 34 | + cfg.document.indent.indent_size = v |
| 35 | + .as_u64() |
| 36 | + .map(|n| n as usize) |
| 37 | + .unwrap_or_else(|| IndentationConfig::default().indent_size) |
| 38 | + }, |
| 39 | + }, |
| 40 | + Setting { |
| 41 | + key: "editor.tabSize", |
| 42 | + set: |cfg, v| { |
| 43 | + cfg.document.indent.tab_width = v |
| 44 | + .as_u64() |
| 45 | + .map(|n| n as usize) |
| 46 | + .unwrap_or_else(|| IndentationConfig::default().tab_width) |
| 47 | + }, |
| 48 | + }, |
| 49 | + Setting { |
| 50 | + key: "positron.r.diagnostics.enable", |
| 51 | + set: |cfg, v| { |
| 52 | + cfg.diagnostics.enable = v |
| 53 | + .as_bool() |
| 54 | + .unwrap_or_else(|| DiagnosticsConfig::default().enable) |
| 55 | + }, |
| 56 | + }, |
| 57 | + Setting { |
| 58 | + key: "positron.r.symbols.includeAssignmentsInBlocks", |
| 59 | + set: |cfg, v| { |
| 60 | + cfg.symbols.include_assignments_in_blocks = v |
| 61 | + .as_bool() |
| 62 | + .unwrap_or_else(|| SymbolsConfig::default().include_assignments_in_blocks) |
| 63 | + }, |
| 64 | + }, |
| 65 | +]; |
| 66 | + |
8 | 67 | /// Configuration of the LSP
|
9 | 68 | #[derive(Clone, Default, Debug)]
|
10 | 69 | pub(crate) struct LspConfig {
|
11 | 70 | pub(crate) diagnostics: DiagnosticsConfig,
|
12 | 71 | pub(crate) symbols: SymbolsConfig,
|
| 72 | + pub(crate) document: DocumentConfig, |
13 | 73 | }
|
14 | 74 |
|
15 | 75 | #[derive(Serialize, Deserialize, Clone, Debug)]
|
@@ -39,28 +99,28 @@ pub struct IndentationConfig {
|
39 | 99 | pub tab_width: usize,
|
40 | 100 | }
|
41 | 101 |
|
42 |
| -#[derive(Serialize, Deserialize, Clone, Debug)] |
| 102 | +#[derive(PartialEq, Serialize, Deserialize, Clone, Debug)] |
43 | 103 | pub enum IndentStyle {
|
44 | 104 | Tab,
|
45 | 105 | Space,
|
46 | 106 | }
|
47 | 107 |
|
48 | 108 | /// VS Code representation of a document configuration
|
49 |
| -#[derive(Serialize, Deserialize, FieldNamesAsArray, Clone, Debug)] |
| 109 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
50 | 110 | pub(crate) struct VscDocumentConfig {
|
51 | 111 | // DEV NOTE: Update `section_from_key()` method after adding a field
|
52 | 112 | pub insert_spaces: bool,
|
53 | 113 | pub indent_size: VscIndentSize,
|
54 | 114 | pub tab_size: usize,
|
55 | 115 | }
|
56 | 116 |
|
57 |
| -#[derive(Serialize, Deserialize, FieldNamesAsArray, Clone, Debug)] |
| 117 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
58 | 118 | pub(crate) struct VscDiagnosticsConfig {
|
59 | 119 | // DEV NOTE: Update `section_from_key()` method after adding a field
|
60 | 120 | pub enable: bool,
|
61 | 121 | }
|
62 | 122 |
|
63 |
| -#[derive(Serialize, Deserialize, FieldNamesAsArray, Clone, Debug)] |
| 123 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
64 | 124 | pub(crate) struct VscSymbolsConfig {
|
65 | 125 | // DEV NOTE: Update `section_from_key()` method after adding a field
|
66 | 126 | pub include_assignments_in_blocks: bool,
|
@@ -91,79 +151,6 @@ impl Default for IndentationConfig {
|
91 | 151 | }
|
92 | 152 | }
|
93 | 153 |
|
94 |
| -impl VscDocumentConfig { |
95 |
| - pub(crate) fn section_from_key(key: &str) -> &str { |
96 |
| - match key { |
97 |
| - "insert_spaces" => "editor.insertSpaces", |
98 |
| - "indent_size" => "editor.indentSize", |
99 |
| - "tab_size" => "editor.tabSize", |
100 |
| - _ => "unknown", // To be caught via downstream errors |
101 |
| - } |
102 |
| - } |
103 |
| -} |
104 |
| - |
105 |
| -/// Convert from VS Code representation of a document config to our own |
106 |
| -/// representation. Currently one-to-one. |
107 |
| -impl From<VscDocumentConfig> for DocumentConfig { |
108 |
| - fn from(x: VscDocumentConfig) -> Self { |
109 |
| - let indent_style = indent_style_from_lsp(x.insert_spaces); |
110 |
| - |
111 |
| - let indent_size = match x.indent_size { |
112 |
| - VscIndentSize::Size(size) => size, |
113 |
| - VscIndentSize::Alias(var) => { |
114 |
| - if var == "tabSize" { |
115 |
| - x.tab_size |
116 |
| - } else { |
117 |
| - lsp::log_warn!("Unknown indent alias {var}, using default"); |
118 |
| - 2 |
119 |
| - } |
120 |
| - }, |
121 |
| - }; |
122 |
| - |
123 |
| - Self { |
124 |
| - indent: IndentationConfig { |
125 |
| - indent_style, |
126 |
| - indent_size, |
127 |
| - tab_width: x.tab_size, |
128 |
| - }, |
129 |
| - } |
130 |
| - } |
131 |
| -} |
132 |
| - |
133 |
| -impl VscDiagnosticsConfig { |
134 |
| - pub(crate) fn section_from_key(key: &str) -> &str { |
135 |
| - match key { |
136 |
| - "enable" => "positron.r.diagnostics.enable", |
137 |
| - _ => "unknown", // To be caught via downstream errors |
138 |
| - } |
139 |
| - } |
140 |
| -} |
141 |
| - |
142 |
| -impl From<VscDiagnosticsConfig> for DiagnosticsConfig { |
143 |
| - fn from(value: VscDiagnosticsConfig) -> Self { |
144 |
| - Self { |
145 |
| - enable: value.enable, |
146 |
| - } |
147 |
| - } |
148 |
| -} |
149 |
| - |
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 |
| - |
167 | 154 | pub(crate) fn indent_style_from_lsp(insert_spaces: bool) -> IndentStyle {
|
168 | 155 | if insert_spaces {
|
169 | 156 | IndentStyle::Space
|
|
0 commit comments