aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-10 17:56:15 +0000
committerAleksey Kladov <[email protected]>2020-03-10 17:56:15 +0000
commit14094e44770559c13a1e8bdfcfb989d3bedd00d8 (patch)
tree812c257acee9610a5a4ae227cd9363a248698c52 /crates/ra_ide_db
parentbf582e77d6e5603149b355a5650cd4d15318f776 (diff)
Move FeatureFlags
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r--crates/ra_ide_db/src/feature_flags.rs77
-rw-r--r--crates/ra_ide_db/src/lib.rs10
2 files changed, 3 insertions, 84 deletions
diff --git a/crates/ra_ide_db/src/feature_flags.rs b/crates/ra_ide_db/src/feature_flags.rs
deleted file mode 100644
index dbb3f50a0..000000000
--- a/crates/ra_ide_db/src/feature_flags.rs
+++ /dev/null
@@ -1,77 +0,0 @@
1//! See docs for `FeatureFlags`.
2
3use rustc_hash::FxHashMap;
4
5// FIXME: looks like a much better design is to pass options to each call,
6// rather than to have a global ambient feature flags -- that way, the clients
7// can issue two successive calls with different options.
8
9/// Feature flags hold fine-grained toggles for all *user-visible* features of
10/// rust-analyzer.
11///
12/// The exists such that users are able to disable any annoying feature (and,
13/// with many users and many features, some features are bound to be annoying
14/// for some users)
15///
16/// Note that we purposefully use run-time checked strings, and not something
17/// checked at compile time, to keep things simple and flexible.
18///
19/// Also note that, at the moment, `FeatureFlags` also store features for
20/// `rust-analyzer`. This should be benign layering violation.
21#[derive(Debug)]
22pub struct FeatureFlags {
23 flags: FxHashMap<String, bool>,
24}
25
26impl FeatureFlags {
27 fn new(flags: &[(&str, bool)]) -> FeatureFlags {
28 let flags = flags
29 .iter()
30 .map(|&(name, value)| {
31 check_flag_name(name);
32 (name.to_string(), value)
33 })
34 .collect();
35 FeatureFlags { flags }
36 }
37
38 pub fn set(&mut self, flag: &str, value: bool) -> Result<(), ()> {
39 match self.flags.get_mut(flag) {
40 None => Err(()),
41 Some(slot) => {
42 *slot = value;
43 Ok(())
44 }
45 }
46 }
47
48 pub fn get(&self, flag: &str) -> bool {
49 match self.flags.get(flag) {
50 None => panic!("unknown flag: {:?}", flag),
51 Some(value) => *value,
52 }
53 }
54}
55
56impl Default for FeatureFlags {
57 fn default() -> FeatureFlags {
58 FeatureFlags::new(&[
59 ("lsp.diagnostics", true),
60 ("completion.insertion.add-call-parenthesis", true),
61 ("completion.insertion.add-argument-snippets", true),
62 ("completion.enable-postfix", true),
63 ("call-info.full", true),
64 ("notifications.workspace-loaded", true),
65 ("notifications.cargo-toml-not-found", true),
66 ])
67 }
68}
69
70fn check_flag_name(flag: &str) {
71 for c in flag.bytes() {
72 match c {
73 b'a'..=b'z' | b'-' | b'.' => (),
74 _ => panic!("flag name does not match conventions: {:?}", flag),
75 }
76 }
77}
diff --git a/crates/ra_ide_db/src/lib.rs b/crates/ra_ide_db/src/lib.rs
index a105c7556..6bcccc848 100644
--- a/crates/ra_ide_db/src/lib.rs
+++ b/crates/ra_ide_db/src/lib.rs
@@ -5,7 +5,6 @@
5pub mod marks; 5pub mod marks;
6pub mod line_index; 6pub mod line_index;
7pub mod line_index_utils; 7pub mod line_index_utils;
8pub mod feature_flags;
9pub mod symbol_index; 8pub mod symbol_index;
10pub mod change; 9pub mod change;
11pub mod defs; 10pub mod defs;
@@ -22,7 +21,7 @@ use ra_db::{
22}; 21};
23use rustc_hash::FxHashMap; 22use rustc_hash::FxHashMap;
24 23
25use crate::{feature_flags::FeatureFlags, line_index::LineIndex, symbol_index::SymbolsDatabase}; 24use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
26 25
27#[salsa::database( 26#[salsa::database(
28 ra_db::SourceDatabaseStorage, 27 ra_db::SourceDatabaseStorage,
@@ -37,7 +36,6 @@ use crate::{feature_flags::FeatureFlags, line_index::LineIndex, symbol_index::Sy
37#[derive(Debug)] 36#[derive(Debug)]
38pub struct RootDatabase { 37pub struct RootDatabase {
39 runtime: salsa::Runtime<RootDatabase>, 38 runtime: salsa::Runtime<RootDatabase>,
40 pub feature_flags: Arc<FeatureFlags>,
41 pub(crate) debug_data: Arc<DebugData>, 39 pub(crate) debug_data: Arc<DebugData>,
42 pub last_gc: crate::wasm_shims::Instant, 40 pub last_gc: crate::wasm_shims::Instant,
43 pub last_gc_check: crate::wasm_shims::Instant, 41 pub last_gc_check: crate::wasm_shims::Instant,
@@ -82,17 +80,16 @@ impl salsa::Database for RootDatabase {
82 80
83impl Default for RootDatabase { 81impl Default for RootDatabase {
84 fn default() -> RootDatabase { 82 fn default() -> RootDatabase {
85 RootDatabase::new(None, FeatureFlags::default()) 83 RootDatabase::new(None)
86 } 84 }
87} 85}
88 86
89impl RootDatabase { 87impl RootDatabase {
90 pub fn new(lru_capacity: Option<usize>, feature_flags: FeatureFlags) -> RootDatabase { 88 pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
91 let mut db = RootDatabase { 89 let mut db = RootDatabase {
92 runtime: salsa::Runtime::default(), 90 runtime: salsa::Runtime::default(),
93 last_gc: crate::wasm_shims::Instant::now(), 91 last_gc: crate::wasm_shims::Instant::now(),
94 last_gc_check: crate::wasm_shims::Instant::now(), 92 last_gc_check: crate::wasm_shims::Instant::now(),
95 feature_flags: Arc::new(feature_flags),
96 debug_data: Default::default(), 93 debug_data: Default::default(),
97 }; 94 };
98 db.set_crate_graph_with_durability(Default::default(), Durability::HIGH); 95 db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
@@ -112,7 +109,6 @@ impl salsa::ParallelDatabase for RootDatabase {
112 runtime: self.runtime.snapshot(self), 109 runtime: self.runtime.snapshot(self),
113 last_gc: self.last_gc, 110 last_gc: self.last_gc,
114 last_gc_check: self.last_gc_check, 111 last_gc_check: self.last_gc_check,
115 feature_flags: Arc::clone(&self.feature_flags),
116 debug_data: Arc::clone(&self.debug_data), 112 debug_data: Arc::clone(&self.debug_data),
117 }) 113 })
118 } 114 }