diff options
-rw-r--r-- | crates/ra_lsp_server/src/init.rs | 100 |
1 files changed, 61 insertions, 39 deletions
diff --git a/crates/ra_lsp_server/src/init.rs b/crates/ra_lsp_server/src/init.rs index 0b7a47a0b..1b77e0312 100644 --- a/crates/ra_lsp_server/src/init.rs +++ b/crates/ra_lsp_server/src/init.rs | |||
@@ -1,39 +1,61 @@ | |||
1 | use serde::{Deserialize, Deserializer}; | 1 | use serde::{Deserialize, Deserializer}; |
2 | 2 | ||
3 | /// Client provided initialization options | 3 | /// Client provided initialization options |
4 | #[derive(Deserialize, Clone, Copy, Debug)] | 4 | #[derive(Deserialize, Clone, Copy, Debug, PartialEq, Eq)] |
5 | #[serde(rename_all = "camelCase")] | 5 | #[serde(rename_all = "camelCase", default)] |
6 | pub struct InitializationOptions { | 6 | pub struct InitializationOptions { |
7 | /// Whether the client supports our custom highlighting publishing decorations. | 7 | /// Whether the client supports our custom highlighting publishing decorations. |
8 | /// This is different to the highlightingOn setting, which is whether the user | 8 | /// This is different to the highlightingOn setting, which is whether the user |
9 | /// wants our custom highlighting to be used. | 9 | /// wants our custom highlighting to be used. |
10 | /// | 10 | /// |
11 | /// Defaults to `true` | 11 | /// Defaults to `false` |
12 | #[serde(default = "bool_true", deserialize_with = "nullable_bool_true")] | 12 | #[serde(deserialize_with = "nullable_bool_false")] |
13 | pub publish_decorations: bool, | 13 | pub publish_decorations: bool, |
14 | 14 | ||
15 | /// Whether or not the workspace loaded notification should be sent | 15 | /// Whether or not the workspace loaded notification should be sent |
16 | /// | 16 | /// |
17 | /// Defaults to `true` | 17 | /// Defaults to `true` |
18 | #[serde(default = "bool_true", deserialize_with = "nullable_bool_true")] | 18 | #[serde(deserialize_with = "nullable_bool_true")] |
19 | pub show_workspace_loaded: bool, | 19 | pub show_workspace_loaded: bool, |
20 | } | 20 | } |
21 | 21 | ||
22 | impl Default for InitializationOptions { | 22 | impl Default for InitializationOptions { |
23 | fn default() -> InitializationOptions { | 23 | fn default() -> InitializationOptions { |
24 | InitializationOptions { publish_decorations: true, show_workspace_loaded: true } | 24 | InitializationOptions { publish_decorations: false, show_workspace_loaded: true } |
25 | } | 25 | } |
26 | } | 26 | } |
27 | 27 | ||
28 | fn bool_true() -> bool { | 28 | /// Deserializes a null value to a bool false by default |
29 | true | 29 | fn nullable_bool_false<'de, D>(deserializer: D) -> Result<bool, D::Error> |
30 | } | 30 | where |
31 | 31 | D: Deserializer<'de>, | |
32 | /// Deserializes a null value to a bool true by default | 32 | { |
33 | fn nullable_bool_true<'de, D>(deserializer: D) -> Result<bool, D::Error> | 33 | let opt = Option::deserialize(deserializer)?; |
34 | where | 34 | Ok(opt.unwrap_or(false)) |
35 | D: Deserializer<'de>, | 35 | } |
36 | { | 36 | |
37 | let opt = Option::deserialize(deserializer)?; | 37 | /// Deserializes a null value to a bool true by default |
38 | Ok(opt.unwrap_or(true)) | 38 | fn nullable_bool_true<'de, D>(deserializer: D) -> Result<bool, D::Error> |
39 | } | 39 | where |
40 | D: Deserializer<'de>, | ||
41 | { | ||
42 | let opt = Option::deserialize(deserializer)?; | ||
43 | Ok(opt.unwrap_or(true)) | ||
44 | } | ||
45 | |||
46 | #[cfg(test)] | ||
47 | mod test { | ||
48 | use super::*; | ||
49 | |||
50 | #[test] | ||
51 | fn deserialize_init_options_defaults() { | ||
52 | // check that null == default for both fields | ||
53 | let default = InitializationOptions::default(); | ||
54 | assert_eq!(default, serde_json::from_str(r#"{}"#).unwrap()); | ||
55 | assert_eq!( | ||
56 | default, | ||
57 | serde_json::from_str(r#"{"publishDecorations":null, "showWorkspaceLoaded":null}"#) | ||
58 | .unwrap() | ||
59 | ); | ||
60 | } | ||
61 | } | ||