diff options
Diffstat (limited to 'crates/ra_ide_db/src/lib.rs')
-rw-r--r-- | crates/ra_ide_db/src/lib.rs | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/crates/ra_ide_db/src/lib.rs b/crates/ra_ide_db/src/lib.rs new file mode 100644 index 000000000..d04c59a4a --- /dev/null +++ b/crates/ra_ide_db/src/lib.rs | |||
@@ -0,0 +1,136 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | pub mod line_index; | ||
4 | pub mod line_index_utils; | ||
5 | pub mod feature_flags; | ||
6 | pub mod symbol_index; | ||
7 | pub mod change; | ||
8 | mod wasm_shims; | ||
9 | |||
10 | use std::sync::Arc; | ||
11 | |||
12 | use ra_db::{ | ||
13 | salsa::{self, Database, Durability}, | ||
14 | Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, | ||
15 | SourceDatabase, SourceRootId, | ||
16 | }; | ||
17 | use rustc_hash::FxHashMap; | ||
18 | |||
19 | use crate::{feature_flags::FeatureFlags, line_index::LineIndex, symbol_index::SymbolsDatabase}; | ||
20 | |||
21 | #[salsa::database( | ||
22 | ra_db::SourceDatabaseStorage, | ||
23 | ra_db::SourceDatabaseExtStorage, | ||
24 | LineIndexDatabaseStorage, | ||
25 | symbol_index::SymbolsDatabaseStorage, | ||
26 | hir::db::InternDatabaseStorage, | ||
27 | hir::db::AstDatabaseStorage, | ||
28 | hir::db::DefDatabaseStorage, | ||
29 | hir::db::HirDatabaseStorage | ||
30 | )] | ||
31 | #[derive(Debug)] | ||
32 | pub struct RootDatabase { | ||
33 | runtime: salsa::Runtime<RootDatabase>, | ||
34 | pub feature_flags: Arc<FeatureFlags>, | ||
35 | pub(crate) debug_data: Arc<DebugData>, | ||
36 | pub last_gc: crate::wasm_shims::Instant, | ||
37 | pub last_gc_check: crate::wasm_shims::Instant, | ||
38 | } | ||
39 | |||
40 | impl FileLoader for RootDatabase { | ||
41 | fn file_text(&self, file_id: FileId) -> Arc<String> { | ||
42 | FileLoaderDelegate(self).file_text(file_id) | ||
43 | } | ||
44 | fn resolve_relative_path( | ||
45 | &self, | ||
46 | anchor: FileId, | ||
47 | relative_path: &RelativePath, | ||
48 | ) -> Option<FileId> { | ||
49 | FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) | ||
50 | } | ||
51 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { | ||
52 | FileLoaderDelegate(self).relevant_crates(file_id) | ||
53 | } | ||
54 | } | ||
55 | |||
56 | impl salsa::Database for RootDatabase { | ||
57 | fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> { | ||
58 | &self.runtime | ||
59 | } | ||
60 | fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> { | ||
61 | &mut self.runtime | ||
62 | } | ||
63 | fn on_propagated_panic(&self) -> ! { | ||
64 | Canceled::throw() | ||
65 | } | ||
66 | fn salsa_event(&self, event: impl Fn() -> salsa::Event<RootDatabase>) { | ||
67 | match event().kind { | ||
68 | salsa::EventKind::DidValidateMemoizedValue { .. } | ||
69 | | salsa::EventKind::WillExecute { .. } => { | ||
70 | self.check_canceled(); | ||
71 | } | ||
72 | _ => (), | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | impl Default for RootDatabase { | ||
78 | fn default() -> RootDatabase { | ||
79 | RootDatabase::new(None, FeatureFlags::default()) | ||
80 | } | ||
81 | } | ||
82 | |||
83 | impl RootDatabase { | ||
84 | pub fn new(lru_capacity: Option<usize>, feature_flags: FeatureFlags) -> RootDatabase { | ||
85 | let mut db = RootDatabase { | ||
86 | runtime: salsa::Runtime::default(), | ||
87 | last_gc: crate::wasm_shims::Instant::now(), | ||
88 | last_gc_check: crate::wasm_shims::Instant::now(), | ||
89 | feature_flags: Arc::new(feature_flags), | ||
90 | debug_data: Default::default(), | ||
91 | }; | ||
92 | db.set_crate_graph_with_durability(Default::default(), Durability::HIGH); | ||
93 | db.set_local_roots_with_durability(Default::default(), Durability::HIGH); | ||
94 | db.set_library_roots_with_durability(Default::default(), Durability::HIGH); | ||
95 | let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP); | ||
96 | db.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity); | ||
97 | db.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity); | ||
98 | db.query_mut(hir::db::MacroExpandQuery).set_lru_capacity(lru_capacity); | ||
99 | db | ||
100 | } | ||
101 | } | ||
102 | |||
103 | impl salsa::ParallelDatabase for RootDatabase { | ||
104 | fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { | ||
105 | salsa::Snapshot::new(RootDatabase { | ||
106 | runtime: self.runtime.snapshot(self), | ||
107 | last_gc: self.last_gc, | ||
108 | last_gc_check: self.last_gc_check, | ||
109 | feature_flags: Arc::clone(&self.feature_flags), | ||
110 | debug_data: Arc::clone(&self.debug_data), | ||
111 | }) | ||
112 | } | ||
113 | } | ||
114 | |||
115 | #[salsa::query_group(LineIndexDatabaseStorage)] | ||
116 | pub trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled { | ||
117 | fn line_index(&self, file_id: FileId) -> Arc<LineIndex>; | ||
118 | } | ||
119 | |||
120 | fn line_index(db: &impl LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> { | ||
121 | let text = db.file_text(file_id); | ||
122 | Arc::new(LineIndex::new(&*text)) | ||
123 | } | ||
124 | |||
125 | #[derive(Debug, Default, Clone)] | ||
126 | pub(crate) struct DebugData { | ||
127 | pub(crate) root_paths: FxHashMap<SourceRootId, String>, | ||
128 | pub(crate) crate_names: FxHashMap<CrateId, String>, | ||
129 | } | ||
130 | |||
131 | impl DebugData { | ||
132 | pub(crate) fn merge(&mut self, other: DebugData) { | ||
133 | self.root_paths.extend(other.root_paths.into_iter()); | ||
134 | self.crate_names.extend(other.crate_names.into_iter()); | ||
135 | } | ||
136 | } | ||