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