diff options
author | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
commit | c26c911ec1e6c2ad1dcb7d155a6a1d528839ad1a (patch) | |
tree | 7cff36c38234be0afb65273146d8247083a5cfeb /crates/ide_db/src/lib.rs | |
parent | 3c018bf84de5c693b5ee1c6bec0fed3b201c2060 (diff) | |
parent | f1f73649a686dc6e6449afc35e0fa6fed00e225d (diff) |
Merge branch 'master' into add-disable-diagnostics
Diffstat (limited to 'crates/ide_db/src/lib.rs')
-rw-r--r-- | crates/ide_db/src/lib.rs | 139 |
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 | |||
5 | pub mod line_index; | ||
6 | pub mod symbol_index; | ||
7 | pub mod change; | ||
8 | pub mod defs; | ||
9 | pub mod search; | ||
10 | pub mod imports_locator; | ||
11 | pub mod source_change; | ||
12 | mod wasm_shims; | ||
13 | |||
14 | use std::{fmt, sync::Arc}; | ||
15 | |||
16 | use base_db::{ | ||
17 | salsa::{self, Durability}, | ||
18 | Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, | ||
19 | Upcast, | ||
20 | }; | ||
21 | use hir::db::{AstDatabase, DefDatabase, HirDatabase}; | ||
22 | use rustc_hash::FxHashSet; | ||
23 | |||
24 | use 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 | )] | ||
36 | pub 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 | |||
42 | impl fmt::Debug for RootDatabase { | ||
43 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
44 | f.debug_struct("RootDatabase").finish() | ||
45 | } | ||
46 | } | ||
47 | |||
48 | impl Upcast<dyn AstDatabase> for RootDatabase { | ||
49 | fn upcast(&self) -> &(dyn AstDatabase + 'static) { | ||
50 | &*self | ||
51 | } | ||
52 | } | ||
53 | |||
54 | impl Upcast<dyn DefDatabase> for RootDatabase { | ||
55 | fn upcast(&self) -> &(dyn DefDatabase + 'static) { | ||
56 | &*self | ||
57 | } | ||
58 | } | ||
59 | |||
60 | impl Upcast<dyn HirDatabase> for RootDatabase { | ||
61 | fn upcast(&self) -> &(dyn HirDatabase + 'static) { | ||
62 | &*self | ||
63 | } | ||
64 | } | ||
65 | |||
66 | impl 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 | |||
78 | impl 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 | |||
93 | impl Default for RootDatabase { | ||
94 | fn default() -> RootDatabase { | ||
95 | RootDatabase::new(None) | ||
96 | } | ||
97 | } | ||
98 | |||
99 | impl 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 | |||
121 | impl 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)] | ||
132 | pub trait LineIndexDatabase: base_db::SourceDatabase + CheckCanceled { | ||
133 | fn line_index(&self, file_id: FileId) -> Arc<LineIndex>; | ||
134 | } | ||
135 | |||
136 | fn 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 | } | ||