aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db/src/lib.rs')
-rw-r--r--crates/ide_db/src/lib.rs140
1 files changed, 140 insertions, 0 deletions
diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs
new file mode 100644
index 000000000..70ada02f3
--- /dev/null
+++ b/crates/ide_db/src/lib.rs
@@ -0,0 +1,140 @@
1//! This crate defines the core datastructure representing IDE state -- `RootDatabase`.
2//!
3//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
4
5pub mod label;
6pub mod line_index;
7pub mod symbol_index;
8pub mod change;
9pub mod defs;
10pub mod search;
11pub mod imports_locator;
12pub mod source_change;
13mod wasm_shims;
14
15use std::{fmt, sync::Arc};
16
17use base_db::{
18 salsa::{self, Durability},
19 Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
20 Upcast,
21};
22use hir::db::{AstDatabase, DefDatabase, HirDatabase};
23use rustc_hash::FxHashSet;
24
25use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
26
27#[salsa::database(
28 base_db::SourceDatabaseStorage,
29 base_db::SourceDatabaseExtStorage,
30 LineIndexDatabaseStorage,
31 symbol_index::SymbolsDatabaseStorage,
32 hir::db::InternDatabaseStorage,
33 hir::db::AstDatabaseStorage,
34 hir::db::DefDatabaseStorage,
35 hir::db::HirDatabaseStorage
36)]
37pub struct RootDatabase {
38 storage: salsa::Storage<RootDatabase>,
39 pub last_gc: crate::wasm_shims::Instant,
40 pub last_gc_check: crate::wasm_shims::Instant,
41}
42
43impl fmt::Debug for RootDatabase {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 f.debug_struct("RootDatabase").finish()
46 }
47}
48
49impl Upcast<dyn AstDatabase> for RootDatabase {
50 fn upcast(&self) -> &(dyn AstDatabase + 'static) {
51 &*self
52 }
53}
54
55impl Upcast<dyn DefDatabase> for RootDatabase {
56 fn upcast(&self) -> &(dyn DefDatabase + 'static) {
57 &*self
58 }
59}
60
61impl Upcast<dyn HirDatabase> for RootDatabase {
62 fn upcast(&self) -> &(dyn HirDatabase + 'static) {
63 &*self
64 }
65}
66
67impl FileLoader for RootDatabase {
68 fn file_text(&self, file_id: FileId) -> Arc<String> {
69 FileLoaderDelegate(self).file_text(file_id)
70 }
71 fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
72 FileLoaderDelegate(self).resolve_path(anchor, path)
73 }
74 fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
75 FileLoaderDelegate(self).relevant_crates(file_id)
76 }
77}
78
79impl salsa::Database for RootDatabase {
80 fn on_propagated_panic(&self) -> ! {
81 Canceled::throw()
82 }
83 fn salsa_event(&self, event: salsa::Event) {
84 match event.kind {
85 salsa::EventKind::DidValidateMemoizedValue { .. }
86 | salsa::EventKind::WillExecute { .. } => {
87 self.check_canceled();
88 }
89 _ => (),
90 }
91 }
92}
93
94impl Default for RootDatabase {
95 fn default() -> RootDatabase {
96 RootDatabase::new(None)
97 }
98}
99
100impl RootDatabase {
101 pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
102 let mut db = RootDatabase {
103 storage: salsa::Storage::default(),
104 last_gc: crate::wasm_shims::Instant::now(),
105 last_gc_check: crate::wasm_shims::Instant::now(),
106 };
107 db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
108 db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
109 db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
110 db.update_lru_capacity(lru_capacity);
111 db
112 }
113
114 pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
115 let lru_capacity = lru_capacity.unwrap_or(base_db::DEFAULT_LRU_CAP);
116 base_db::ParseQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
117 hir::db::ParseMacroQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
118 hir::db::MacroExpandQuery.in_db_mut(self).set_lru_capacity(lru_capacity);
119 }
120}
121
122impl salsa::ParallelDatabase for RootDatabase {
123 fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
124 salsa::Snapshot::new(RootDatabase {
125 storage: self.storage.snapshot(),
126 last_gc: self.last_gc,
127 last_gc_check: self.last_gc_check,
128 })
129 }
130}
131
132#[salsa::query_group(LineIndexDatabaseStorage)]
133pub trait LineIndexDatabase: base_db::SourceDatabase + CheckCanceled {
134 fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
135}
136
137fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
138 let text = db.file_text(file_id);
139 Arc::new(LineIndex::new(&*text))
140}