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