diff options
Diffstat (limited to 'crates/ra_ide_api/src/feature_flags.rs')
-rw-r--r-- | crates/ra_ide_api/src/feature_flags.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/feature_flags.rs b/crates/ra_ide_api/src/feature_flags.rs new file mode 100644 index 000000000..9f82ac71c --- /dev/null +++ b/crates/ra_ide_api/src/feature_flags.rs | |||
@@ -0,0 +1,67 @@ | |||
1 | use rustc_hash::FxHashMap; | ||
2 | |||
3 | /// Feature flags hold fine-grained toggles for all *user-visible* features of | ||
4 | /// rust-analyzer. | ||
5 | /// | ||
6 | /// The exists such that users are able to disable any annoying feature (and, | ||
7 | /// with many users and many features, some features are bound to be annoying | ||
8 | /// for some users) | ||
9 | /// | ||
10 | /// Note that we purposefully use run-time checked strings, and not something | ||
11 | /// checked at compile time, to keep things simple and flexible. | ||
12 | /// | ||
13 | /// Also note that, at the moment, `FeatureFlags` also store features for | ||
14 | /// `ra_lsp_server`. This should be benign layering violation. | ||
15 | #[derive(Debug)] | ||
16 | pub struct FeatureFlags { | ||
17 | flags: FxHashMap<String, bool>, | ||
18 | } | ||
19 | |||
20 | impl FeatureFlags { | ||
21 | fn new(flags: &[(&str, bool)]) -> FeatureFlags { | ||
22 | let flags = flags | ||
23 | .iter() | ||
24 | .map(|&(name, value)| { | ||
25 | check_flag_name(name); | ||
26 | (name.to_string(), value) | ||
27 | }) | ||
28 | .collect(); | ||
29 | FeatureFlags { flags } | ||
30 | } | ||
31 | |||
32 | pub fn set(&mut self, flag: &str, value: bool) -> Result<(), ()> { | ||
33 | match self.flags.get_mut(flag) { | ||
34 | None => Err(()), | ||
35 | Some(slot) => { | ||
36 | *slot = value; | ||
37 | Ok(()) | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | |||
42 | pub fn get(&self, flag: &str) -> bool { | ||
43 | match self.flags.get(flag) { | ||
44 | None => panic!("unknown flag: {:?}", flag), | ||
45 | Some(value) => *value, | ||
46 | } | ||
47 | } | ||
48 | } | ||
49 | |||
50 | impl Default for FeatureFlags { | ||
51 | fn default() -> FeatureFlags { | ||
52 | FeatureFlags::new(&[ | ||
53 | ("lsp.diagnostics", true), | ||
54 | ("completion.insertion.add-call-parenthesis", true), | ||
55 | ("notifications.workspace-loaded", true), | ||
56 | ]) | ||
57 | } | ||
58 | } | ||
59 | |||
60 | fn check_flag_name(flag: &str) { | ||
61 | for c in flag.bytes() { | ||
62 | match c { | ||
63 | b'a'..=b'z' | b'-' | b'.' => (), | ||
64 | _ => panic!("flag name does not match conventions: {:?}", flag), | ||
65 | } | ||
66 | } | ||
67 | } | ||