]> git.lizzy.rs Git - rust.git/blob - crates/ra_lsp_server/src/config.rs
67942aa4147cd1f6bd273d87fe369dfdf04e142b
[rust.git] / crates / ra_lsp_server / src / config.rs
1 //! Config used by the language server.
2 //!
3 //! We currently get this config from `initialize` LSP request, which is not the
4 //! best way to do it, but was the simplest thing we could implement.
5 //!
6 //! Of particular interest is the `feature_flags` hash map: while other fields
7 //! configure the server itself, feature flags are passed into analysis, and
8 //! tweak things like automatic insertion of `()` in completions.
9
10 use rustc_hash::FxHashMap;
11
12 use ra_project_model::CargoFeatures;
13 use serde::{Deserialize, Deserializer};
14
15 /// Client provided initialization options
16 #[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
17 #[serde(rename_all = "camelCase", default)]
18 pub struct ServerConfig {
19     /// Whether the client supports our custom highlighting publishing decorations.
20     /// This is different to the highlightingOn setting, which is whether the user
21     /// wants our custom highlighting to be used.
22     ///
23     /// Defaults to `false`
24     #[serde(deserialize_with = "nullable_bool_false")]
25     pub publish_decorations: bool,
26
27     pub exclude_globs: Vec<String>,
28     #[serde(deserialize_with = "nullable_bool_false")]
29     pub use_client_watching: bool,
30
31     pub lru_capacity: Option<usize>,
32
33     pub max_inlay_hint_length: Option<usize>,
34
35     /// For internal usage to make integrated tests faster.
36     #[serde(deserialize_with = "nullable_bool_true")]
37     pub with_sysroot: bool,
38
39     /// Fine grained feature flags to disable specific features.
40     pub feature_flags: FxHashMap<String, bool>,
41
42     /// Cargo feature configurations.
43     pub cargo_features: CargoFeatures,
44 }
45
46 impl Default for ServerConfig {
47     fn default() -> ServerConfig {
48         ServerConfig {
49             publish_decorations: false,
50             exclude_globs: Vec::new(),
51             use_client_watching: false,
52             lru_capacity: None,
53             max_inlay_hint_length: None,
54             with_sysroot: true,
55             feature_flags: FxHashMap::default(),
56             cargo_features: Default::default(),
57         }
58     }
59 }
60
61 /// Deserializes a null value to a bool false by default
62 fn nullable_bool_false<'de, D>(deserializer: D) -> Result<bool, D::Error>
63 where
64     D: Deserializer<'de>,
65 {
66     let opt = Option::deserialize(deserializer)?;
67     Ok(opt.unwrap_or(false))
68 }
69
70 /// Deserializes a null value to a bool true by default
71 fn nullable_bool_true<'de, D>(deserializer: D) -> Result<bool, D::Error>
72 where
73     D: Deserializer<'de>,
74 {
75     let opt = Option::deserialize(deserializer)?;
76     Ok(opt.unwrap_or(true))
77 }
78
79 #[cfg(test)]
80 mod test {
81     use super::*;
82
83     #[test]
84     fn deserialize_init_options_defaults() {
85         // check that null == default for both fields
86         let default = ServerConfig::default();
87         assert_eq!(default, serde_json::from_str(r#"{}"#).unwrap());
88         assert_eq!(
89             default,
90             serde_json::from_str(r#"{"publishDecorations":null, "lruCapacity":null}"#).unwrap()
91         );
92     }
93 }