aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/mock.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-25 12:34:10 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-25 12:34:10 +0000
commit021e691997efc35c983808ee3470a060a3ec3e96 (patch)
treea3f49352542d3c6803e318cd2f155cc4543062c5 /crates/ra_hir/src/mock.rs
parent04ce89313369a1606b057b3be1368d72b8a5cd63 (diff)
parent08c12e424d5d3fb4e11f081a07b9c265dc7a96b6 (diff)
Merge #639
639: Update salsa r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/mock.rs')
-rw-r--r--crates/ra_hir/src/mock.rs83
1 files changed, 15 insertions, 68 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index 1a83a5c87..36b174cd6 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -2,8 +2,7 @@ use std::{sync::Arc, panic};
2 2
3use parking_lot::Mutex; 3use parking_lot::Mutex;
4use ra_db::{ 4use ra_db::{
5 BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, 5 BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, FilesDatabase,
6 salsa::{self, Database},
7}; 6};
8use relative_path::RelativePathBuf; 7use relative_path::RelativePathBuf;
9use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; 8use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
@@ -12,6 +11,7 @@ use crate::{db, HirInterner};
12 11
13pub const WORKSPACE: SourceRootId = SourceRootId(0); 12pub const WORKSPACE: SourceRootId = SourceRootId(0);
14 13
14#[salsa::database(ra_db::FilesDatabase, ra_db::SyntaxDatabase, db::HirDatabase)]
15#[derive(Debug)] 15#[derive(Debug)]
16pub(crate) struct MockDatabase { 16pub(crate) struct MockDatabase {
17 events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, 17 events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>,
@@ -33,8 +33,7 @@ impl MockDatabase {
33 let mut db = MockDatabase::default(); 33 let mut db = MockDatabase::default();
34 let mut source_root = SourceRoot::default(); 34 let mut source_root = SourceRoot::default();
35 let file_id = db.add_file(WORKSPACE, &mut source_root, "/main.rs", text); 35 let file_id = db.add_file(WORKSPACE, &mut source_root, "/main.rs", text);
36 db.query_mut(ra_db::SourceRootQuery) 36 db.set_source_root(WORKSPACE, Arc::new(source_root.clone()));
37 .set(WORKSPACE, Arc::new(source_root.clone()));
38 (db, source_root, file_id) 37 (db, source_root, file_id)
39 } 38 }
40 39
@@ -44,11 +43,6 @@ impl MockDatabase {
44 (db, position) 43 (db, position)
45 } 44 }
46 45
47 pub(crate) fn set_crate_graph(&mut self, crate_graph: CrateGraph) {
48 self.query_mut(ra_db::CrateGraphQuery)
49 .set((), Arc::new(crate_graph));
50 }
51
52 fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) { 46 fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) {
53 let mut db = MockDatabase::default(); 47 let mut db = MockDatabase::default();
54 48
@@ -80,8 +74,7 @@ impl MockDatabase {
80 self.add_file(source_root_id, &mut source_root, &entry.meta, &entry.text); 74 self.add_file(source_root_id, &mut source_root, &entry.meta, &entry.text);
81 } 75 }
82 } 76 }
83 self.query_mut(ra_db::SourceRootQuery) 77 self.set_source_root(source_root_id, Arc::new(source_root.clone()));
84 .set(source_root_id, Arc::new(source_root.clone()));
85 (source_root, position) 78 (source_root, position)
86 } 79 }
87 80
@@ -99,17 +92,15 @@ impl MockDatabase {
99 let file_id = FileId(self.file_counter); 92 let file_id = FileId(self.file_counter);
100 self.file_counter += 1; 93 self.file_counter += 1;
101 let text = Arc::new(text.to_string()); 94 let text = Arc::new(text.to_string());
102 self.query_mut(ra_db::FileTextQuery).set(file_id, text); 95 self.set_file_text(file_id, text);
103 self.query_mut(ra_db::FileRelativePathQuery) 96 self.set_file_relative_path(file_id, path.clone());
104 .set(file_id, path.clone()); 97 self.set_file_source_root(file_id, source_root_id);
105 self.query_mut(ra_db::FileSourceRootQuery)
106 .set(file_id, source_root_id);
107 source_root.files.insert(path, file_id); 98 source_root.files.insert(path, file_id);
108 99
109 if is_crate_root { 100 if is_crate_root {
110 let mut crate_graph = CrateGraph::default(); 101 let mut crate_graph = CrateGraph::default();
111 crate_graph.add_crate_root(file_id); 102 crate_graph.add_crate_root(file_id);
112 self.set_crate_graph(crate_graph); 103 self.set_crate_graph(Arc::new(crate_graph));
113 } 104 }
114 file_id 105 file_id
115 } 106 }
@@ -148,12 +139,9 @@ impl Default for MockDatabase {
148 interner: Default::default(), 139 interner: Default::default(),
149 file_counter: 0, 140 file_counter: 0,
150 }; 141 };
151 db.query_mut(ra_db::CrateGraphQuery) 142 db.set_crate_graph(Default::default());
152 .set((), Default::default()); 143 db.set_local_roots(Default::default());
153 db.query_mut(ra_db::LocalRootsQuery) 144 db.set_library_roots(Default::default());
154 .set((), Default::default());
155 db.query_mut(ra_db::LibraryRootsQuery)
156 .set((), Default::default());
157 db 145 db
158 } 146 }
159} 147}
@@ -181,8 +169,7 @@ impl MockDatabase {
181 pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<MockDatabase>> { 169 pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<MockDatabase>> {
182 *self.events.lock() = Some(Vec::new()); 170 *self.events.lock() = Some(Vec::new());
183 f(); 171 f();
184 let events = self.events.lock().take().unwrap(); 172 self.events.lock().take().unwrap()
185 events
186 } 173 }
187 174
188 pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> { 175 pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
@@ -192,51 +179,11 @@ impl MockDatabase {
192 .filter_map(|e| match e.kind { 179 .filter_map(|e| match e.kind {
193 // This pretty horrible, but `Debug` is the only way to inspect 180 // This pretty horrible, but `Debug` is the only way to inspect
194 // QueryDescriptor at the moment. 181 // QueryDescriptor at the moment.
195 salsa::EventKind::WillExecute { descriptor } => Some(format!("{:?}", descriptor)), 182 salsa::EventKind::WillExecute { database_key } => {
183 Some(format!("{:?}", database_key))
184 }
196 _ => None, 185 _ => None,
197 }) 186 })
198 .collect() 187 .collect()
199 } 188 }
200} 189}
201
202salsa::database_storage! {
203 pub(crate) struct MockDatabaseStorage for MockDatabase {
204 impl ra_db::FilesDatabase {
205 fn file_text() for ra_db::FileTextQuery;
206 fn file_relative_path() for ra_db::FileRelativePathQuery;
207 fn file_source_root() for ra_db::FileSourceRootQuery;
208 fn source_root() for ra_db::SourceRootQuery;
209 fn source_root_crates() for ra_db::SourceRootCratesQuery;
210 fn local_roots() for ra_db::LocalRootsQuery;
211 fn library_roots() for ra_db::LibraryRootsQuery;
212 fn crate_graph() for ra_db::CrateGraphQuery;
213 }
214 impl ra_db::SyntaxDatabase {
215 fn source_file() for ra_db::SourceFileQuery;
216 }
217 impl db::HirDatabase {
218 fn hir_source_file() for db::HirSourceFileQuery;
219 fn expand_macro_invocation() for db::ExpandMacroInvocationQuery;
220 fn module_tree() for db::ModuleTreeQuery;
221 fn fn_scopes() for db::FnScopesQuery;
222 fn file_items() for db::FileItemsQuery;
223 fn file_item() for db::FileItemQuery;
224 fn lower_module() for db::LowerModuleQuery;
225 fn lower_module_module() for db::LowerModuleModuleQuery;
226 fn lower_module_source_map() for db::LowerModuleSourceMapQuery;
227 fn item_map() for db::ItemMapQuery;
228 fn submodules() for db::SubmodulesQuery;
229 fn infer() for db::InferQuery;
230 fn type_for_def() for db::TypeForDefQuery;
231 fn type_for_field() for db::TypeForFieldQuery;
232 fn struct_data() for db::StructDataQuery;
233 fn enum_data() for db::EnumDataQuery;
234 fn impls_in_module() for db::ImplsInModuleQuery;
235 fn impls_in_crate() for db::ImplsInCrateQuery;
236 fn body_hir() for db::BodyHirQuery;
237 fn body_syntax_mapping() for db::BodySyntaxMappingQuery;
238 fn fn_signature() for db::FnSignatureQuery;
239 fn generic_params() for db::GenericParamsQuery;
240 }
241 }
242}