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 < T > {
8
+ pub key : & ' static str ,
9
+ pub set : fn ( & mut T , 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
+ ///
17
+ /// This array is for global settings. If the setting should only affect a given
18
+ /// document URI, add it to `DOCUMENT_SETTINGS` instead.
19
+ pub static GLOBAL_SETTINGS : & [ Setting < LspConfig > ] = & [
20
+ Setting {
21
+ key : "positron.r.diagnostics.enable" ,
22
+ set : |cfg, v| {
23
+ cfg. diagnostics . enable = v
24
+ . as_bool ( )
25
+ . unwrap_or_else ( || DiagnosticsConfig :: default ( ) . enable )
26
+ } ,
27
+ } ,
28
+ Setting {
29
+ key : "positron.r.symbols.includeAssignmentsInBlocks" ,
30
+ set : |cfg, v| {
31
+ cfg. symbols . include_assignments_in_blocks = v
32
+ . as_bool ( )
33
+ . unwrap_or_else ( || SymbolsConfig :: default ( ) . include_assignments_in_blocks )
34
+ } ,
35
+ } ,
36
+ ] ;
37
+
38
+ /// These document settings are updated on a URI basis. Each document has its
39
+ /// own value of the setting.
40
+ pub static DOCUMENT_SETTINGS : & [ Setting < DocumentConfig > ] = & [
41
+ Setting {
42
+ key : "editor.insertSpaces" ,
43
+ set : |cfg, v| {
44
+ let default_style = IndentationConfig :: default ( ) . indent_style ;
45
+ cfg. indent . indent_style = if v
46
+ . as_bool ( )
47
+ . unwrap_or_else ( || default_style == IndentStyle :: Space )
48
+ {
49
+ IndentStyle :: Space
50
+ } else {
51
+ IndentStyle :: Tab
52
+ }
53
+ } ,
54
+ } ,
55
+ Setting {
56
+ key : "editor.indentSize" ,
57
+ set : |cfg, v| {
58
+ cfg. indent . indent_size = v
59
+ . as_u64 ( )
60
+ . map ( |n| n as usize )
61
+ . unwrap_or_else ( || IndentationConfig :: default ( ) . indent_size )
62
+ } ,
63
+ } ,
64
+ Setting {
65
+ key : "editor.tabSize" ,
66
+ set : |cfg, v| {
67
+ cfg. indent . tab_width = v
68
+ . as_u64 ( )
69
+ . map ( |n| n as usize )
70
+ . unwrap_or_else ( || IndentationConfig :: default ( ) . tab_width )
71
+ } ,
72
+ } ,
73
+ ] ;
74
+
8
75
/// Configuration of the LSP
9
76
#[ derive( Clone , Default , Debug ) ]
10
77
pub ( crate ) struct LspConfig {
@@ -39,40 +106,12 @@ pub struct IndentationConfig {
39
106
pub tab_width : usize ,
40
107
}
41
108
42
- #[ derive( Serialize , Deserialize , Clone , Debug ) ]
109
+ #[ derive( PartialEq , Serialize , Deserialize , Clone , Debug ) ]
43
110
pub enum IndentStyle {
44
111
Tab ,
45
112
Space ,
46
113
}
47
114
48
- /// VS Code representation of a document configuration
49
- #[ derive( Serialize , Deserialize , FieldNamesAsArray , Clone , Debug ) ]
50
- pub ( crate ) struct VscDocumentConfig {
51
- // DEV NOTE: Update `section_from_key()` method after adding a field
52
- pub insert_spaces : bool ,
53
- pub indent_size : VscIndentSize ,
54
- pub tab_size : usize ,
55
- }
56
-
57
- #[ derive( Serialize , Deserialize , FieldNamesAsArray , Clone , Debug ) ]
58
- pub ( crate ) struct VscDiagnosticsConfig {
59
- // DEV NOTE: Update `section_from_key()` method after adding a field
60
- pub enable : bool ,
61
- }
62
-
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
-
69
- #[ derive( Serialize , Deserialize , Clone , Debug ) ]
70
- #[ serde( untagged) ]
71
- pub ( crate ) enum VscIndentSize {
72
- Alias ( String ) ,
73
- Size ( usize ) ,
74
- }
75
-
76
115
impl Default for SymbolsConfig {
77
116
fn default ( ) -> Self {
78
117
Self {
@@ -91,79 +130,6 @@ impl Default for IndentationConfig {
91
130
}
92
131
}
93
132
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
133
pub ( crate ) fn indent_style_from_lsp ( insert_spaces : bool ) -> IndentStyle {
168
134
if insert_spaces {
169
135
IndentStyle :: Space
0 commit comments