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