]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_db/src/lib.rs
Merge #4296
[rust.git] / crates / ra_ide_db / src / lib.rs
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
5 pub mod marks;
6 pub mod line_index;
7 pub mod line_index_utils;
8 pub mod symbol_index;
9 pub mod change;
10 pub mod defs;
11 pub mod search;
12 pub mod imports_locator;
13 pub mod source_change;
14 mod wasm_shims;
15
16 use std::sync::Arc;
17
18 use hir::db::{AstDatabase, DefDatabase};
19 use ra_db::{
20     salsa::{self, Database, Durability},
21     Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath,
22     SourceDatabase, SourceRootId, Upcast,
23 };
24 use rustc_hash::FxHashMap;
25
26 use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
27
28 #[salsa::database(
29     ra_db::SourceDatabaseStorage,
30     ra_db::SourceDatabaseExtStorage,
31     LineIndexDatabaseStorage,
32     symbol_index::SymbolsDatabaseStorage,
33     hir::db::InternDatabaseStorage,
34     hir::db::AstDatabaseStorage,
35     hir::db::DefDatabaseStorage,
36     hir::db::HirDatabaseStorage
37 )]
38 #[derive(Debug)]
39 pub struct RootDatabase {
40     runtime: salsa::Runtime<RootDatabase>,
41     pub(crate) debug_data: Arc<DebugData>,
42     pub last_gc: crate::wasm_shims::Instant,
43     pub last_gc_check: crate::wasm_shims::Instant,
44 }
45
46 impl Upcast<dyn AstDatabase> for RootDatabase {
47     fn upcast(&self) -> &(dyn AstDatabase + 'static) {
48         &*self
49     }
50 }
51
52 impl Upcast<dyn DefDatabase> for RootDatabase {
53     fn upcast(&self) -> &(dyn DefDatabase + 'static) {
54         &*self
55     }
56 }
57
58 impl FileLoader for RootDatabase {
59     fn file_text(&self, file_id: FileId) -> Arc<String> {
60         FileLoaderDelegate(self).file_text(file_id)
61     }
62     fn resolve_relative_path(
63         &self,
64         anchor: FileId,
65         relative_path: &RelativePath,
66     ) -> Option<FileId> {
67         FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path)
68     }
69     fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
70         FileLoaderDelegate(self).relevant_crates(file_id)
71     }
72     fn resolve_extern_path(
73         &self,
74         extern_id: ra_db::ExternSourceId,
75         relative_path: &RelativePath,
76     ) -> Option<FileId> {
77         FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
78     }
79 }
80
81 impl salsa::Database for RootDatabase {
82     fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
83         &self.runtime
84     }
85     fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> {
86         &mut self.runtime
87     }
88     fn on_propagated_panic(&self) -> ! {
89         Canceled::throw()
90     }
91     fn salsa_event(&self, event: impl Fn() -> salsa::Event<RootDatabase>) {
92         match event().kind {
93             salsa::EventKind::DidValidateMemoizedValue { .. }
94             | salsa::EventKind::WillExecute { .. } => {
95                 self.check_canceled();
96             }
97             _ => (),
98         }
99     }
100 }
101
102 impl Default for RootDatabase {
103     fn default() -> RootDatabase {
104         RootDatabase::new(None)
105     }
106 }
107
108 impl RootDatabase {
109     pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
110         let mut db = RootDatabase {
111             runtime: salsa::Runtime::default(),
112             last_gc: crate::wasm_shims::Instant::now(),
113             last_gc_check: crate::wasm_shims::Instant::now(),
114             debug_data: Default::default(),
115         };
116         db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
117         db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
118         db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
119         db.update_lru_capacity(lru_capacity);
120         db
121     }
122
123     pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
124         let lru_capacity = lru_capacity.unwrap_or(ra_db::DEFAULT_LRU_CAP);
125         self.query_mut(ra_db::ParseQuery).set_lru_capacity(lru_capacity);
126         self.query_mut(hir::db::ParseMacroQuery).set_lru_capacity(lru_capacity);
127         self.query_mut(hir::db::MacroExpandQuery).set_lru_capacity(lru_capacity);
128     }
129 }
130
131 impl salsa::ParallelDatabase for RootDatabase {
132     fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
133         salsa::Snapshot::new(RootDatabase {
134             runtime: self.runtime.snapshot(self),
135             last_gc: self.last_gc,
136             last_gc_check: self.last_gc_check,
137             debug_data: Arc::clone(&self.debug_data),
138         })
139     }
140 }
141
142 #[salsa::query_group(LineIndexDatabaseStorage)]
143 pub trait LineIndexDatabase: ra_db::SourceDatabase + CheckCanceled {
144     fn line_index(&self, file_id: FileId) -> Arc<LineIndex>;
145 }
146
147 fn line_index(db: &impl LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
148     let text = db.file_text(file_id);
149     Arc::new(LineIndex::new(&*text))
150 }
151
152 #[derive(Debug, Default, Clone)]
153 pub(crate) struct DebugData {
154     pub(crate) root_paths: FxHashMap<SourceRootId, String>,
155 }
156
157 impl DebugData {
158     pub(crate) fn merge(&mut self, other: DebugData) {
159         self.root_paths.extend(other.root_paths.into_iter());
160     }
161 }