aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/Cargo.toml2
-rw-r--r--crates/ra_hir/src/mock.rs83
-rw-r--r--crates/ra_hir/src/nameres/tests.rs11
3 files changed, 22 insertions, 74 deletions
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml
index 86937ccd8..3d802ade4 100644
--- a/crates/ra_hir/Cargo.toml
+++ b/crates/ra_hir/Cargo.toml
@@ -12,6 +12,8 @@ rustc-hash = "1.0"
12parking_lot = "0.7.0" 12parking_lot = "0.7.0"
13ena = "0.11" 13ena = "0.11"
14join_to_string = "0.1.3" 14join_to_string = "0.1.3"
15salsa = "0.10.0-alpha3"
16
15ra_syntax = { path = "../ra_syntax" } 17ra_syntax = { path = "../ra_syntax" }
16ra_arena = { path = "../ra_arena" } 18ra_arena = { path = "../ra_arena" }
17ra_db = { path = "../ra_db" } 19ra_db = { path = "../ra_db" }
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}
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs
index c033bebe8..24936976c 100644
--- a/crates/ra_hir/src/nameres/tests.rs
+++ b/crates/ra_hir/src/nameres/tests.rs
@@ -1,6 +1,6 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_db::{CrateGraph, SourceRootId, salsa::Database}; 3use ra_db::{CrateGraph, SourceRootId, FilesDatabase};
4use relative_path::RelativePath; 4use relative_path::RelativePath;
5use test_utils::{assert_eq_text, covers}; 5use test_utils::{assert_eq_text, covers};
6 6
@@ -257,7 +257,7 @@ fn item_map_across_crates() {
257 .add_dep(main_crate, "test_crate".into(), lib_crate) 257 .add_dep(main_crate, "test_crate".into(), lib_crate)
258 .unwrap(); 258 .unwrap();
259 259
260 db.set_crate_graph(crate_graph); 260 db.set_crate_graph(Arc::new(crate_graph));
261 261
262 let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); 262 let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
263 let krate = module.krate(&db).unwrap(); 263 let krate = module.krate(&db).unwrap();
@@ -309,7 +309,7 @@ fn import_across_source_roots() {
309 .add_dep(main_crate, "test_crate".into(), lib_crate) 309 .add_dep(main_crate, "test_crate".into(), lib_crate)
310 .unwrap(); 310 .unwrap();
311 311
312 db.set_crate_graph(crate_graph); 312 db.set_crate_graph(Arc::new(crate_graph));
313 313
314 let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); 314 let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
315 let krate = module.krate(&db).unwrap(); 315 let krate = module.krate(&db).unwrap();
@@ -351,7 +351,7 @@ fn reexport_across_crates() {
351 .add_dep(main_crate, "test_crate".into(), lib_crate) 351 .add_dep(main_crate, "test_crate".into(), lib_crate)
352 .unwrap(); 352 .unwrap();
353 353
354 db.set_crate_graph(crate_graph); 354 db.set_crate_graph(Arc::new(crate_graph));
355 355
356 let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); 356 let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
357 let krate = module.krate(&db).unwrap(); 357 let krate = module.krate(&db).unwrap();
@@ -377,8 +377,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) {
377 }); 377 });
378 assert!(format!("{:?}", events).contains("item_map")) 378 assert!(format!("{:?}", events).contains("item_map"))
379 } 379 }
380 db.query_mut(ra_db::FileTextQuery) 380 db.set_file_text(pos.file_id, Arc::new(file_change.to_string()));
381 .set(pos.file_id, Arc::new(file_change.to_string()));
382 381
383 { 382 {
384 let events = db.log_executed(|| { 383 let events = db.log_executed(|| {