diff options
Diffstat (limited to 'crates/ra_hir/src/mock.rs')
-rw-r--r-- | crates/ra_hir/src/mock.rs | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index b7193c4f3..9423e6571 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs | |||
@@ -2,12 +2,14 @@ use std::sync::Arc; | |||
2 | 2 | ||
3 | use parking_lot::Mutex; | 3 | use parking_lot::Mutex; |
4 | use salsa::{self, Database}; | 4 | use salsa::{self, Database}; |
5 | use ra_db::{LocationIntener, BaseDatabase, FilePosition, mock::FileMap, FileId, WORKSPACE, CrateGraph}; | 5 | use ra_db::{LocationIntener, BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId}; |
6 | use relative_path::RelativePathBuf; | 6 | use relative_path::RelativePathBuf; |
7 | use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; | 7 | use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; |
8 | 8 | ||
9 | use crate::{db, DefId, DefLoc}; | 9 | use crate::{db, DefId, DefLoc}; |
10 | 10 | ||
11 | const WORKSPACE: SourceRootId = SourceRootId(0); | ||
12 | |||
11 | #[derive(Debug)] | 13 | #[derive(Debug)] |
12 | pub(crate) struct MockDatabase { | 14 | pub(crate) struct MockDatabase { |
13 | events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, | 15 | events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, |
@@ -16,10 +18,10 @@ pub(crate) struct MockDatabase { | |||
16 | } | 18 | } |
17 | 19 | ||
18 | impl MockDatabase { | 20 | impl MockDatabase { |
19 | pub(crate) fn with_files(fixture: &str) -> (MockDatabase, FileMap) { | 21 | pub(crate) fn with_files(fixture: &str) -> (MockDatabase, SourceRoot) { |
20 | let (db, file_map, position) = MockDatabase::from_fixture(fixture); | 22 | let (db, source_root, position) = MockDatabase::from_fixture(fixture); |
21 | assert!(position.is_none()); | 23 | assert!(position.is_none()); |
22 | (db, file_map) | 24 | (db, source_root) |
23 | } | 25 | } |
24 | 26 | ||
25 | pub(crate) fn with_position(fixture: &str) -> (MockDatabase, FilePosition) { | 27 | pub(crate) fn with_position(fixture: &str) -> (MockDatabase, FilePosition) { |
@@ -33,48 +35,50 @@ impl MockDatabase { | |||
33 | .set((), Arc::new(crate_graph)); | 35 | .set((), Arc::new(crate_graph)); |
34 | } | 36 | } |
35 | 37 | ||
36 | fn from_fixture(fixture: &str) -> (MockDatabase, FileMap, Option<FilePosition>) { | 38 | fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) { |
37 | let mut db = MockDatabase::default(); | 39 | let mut db = MockDatabase::default(); |
38 | 40 | ||
39 | let mut position = None; | 41 | let mut position = None; |
40 | let mut file_map = FileMap::default(); | 42 | let mut source_root = SourceRoot::default(); |
41 | for entry in parse_fixture(fixture) { | 43 | for entry in parse_fixture(fixture) { |
42 | if entry.text.contains(CURSOR_MARKER) { | 44 | if entry.text.contains(CURSOR_MARKER) { |
43 | assert!( | 45 | assert!( |
44 | position.is_none(), | 46 | position.is_none(), |
45 | "only one marker (<|>) per fixture is allowed" | 47 | "only one marker (<|>) per fixture is allowed" |
46 | ); | 48 | ); |
47 | position = Some(db.add_file_with_position(&mut file_map, &entry.meta, &entry.text)); | 49 | position = |
50 | Some(db.add_file_with_position(&mut source_root, &entry.meta, &entry.text)); | ||
48 | } else { | 51 | } else { |
49 | db.add_file(&mut file_map, &entry.meta, &entry.text); | 52 | db.add_file(&mut source_root, &entry.meta, &entry.text); |
50 | } | 53 | } |
51 | } | 54 | } |
52 | let source_root = file_map.clone().into_source_root(); | ||
53 | db.query_mut(ra_db::SourceRootQuery) | 55 | db.query_mut(ra_db::SourceRootQuery) |
54 | .set(WORKSPACE, Arc::new(source_root)); | 56 | .set(WORKSPACE, Arc::new(source_root.clone())); |
55 | (db, file_map, position) | 57 | (db, source_root, position) |
56 | } | 58 | } |
57 | 59 | ||
58 | fn add_file(&mut self, file_map: &mut FileMap, path: &str, text: &str) -> FileId { | 60 | fn add_file(&mut self, source_root: &mut SourceRoot, path: &str, text: &str) -> FileId { |
59 | assert!(path.starts_with('/')); | 61 | assert!(path.starts_with('/')); |
60 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); | 62 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); |
61 | 63 | let file_id = FileId(source_root.files.len() as u32); | |
62 | let file_id = file_map.add(path); | ||
63 | let text = Arc::new(text.to_string()); | 64 | let text = Arc::new(text.to_string()); |
64 | self.query_mut(ra_db::FileTextQuery).set(file_id, text); | 65 | self.query_mut(ra_db::FileTextQuery).set(file_id, text); |
66 | self.query_mut(ra_db::FileRelativePathQuery) | ||
67 | .set(file_id, path.clone()); | ||
65 | self.query_mut(ra_db::FileSourceRootQuery) | 68 | self.query_mut(ra_db::FileSourceRootQuery) |
66 | .set(file_id, WORKSPACE); | 69 | .set(file_id, WORKSPACE); |
70 | source_root.files.insert(path, file_id); | ||
67 | file_id | 71 | file_id |
68 | } | 72 | } |
69 | 73 | ||
70 | fn add_file_with_position( | 74 | fn add_file_with_position( |
71 | &mut self, | 75 | &mut self, |
72 | file_map: &mut FileMap, | 76 | source_root: &mut SourceRoot, |
73 | path: &str, | 77 | path: &str, |
74 | text: &str, | 78 | text: &str, |
75 | ) -> FilePosition { | 79 | ) -> FilePosition { |
76 | let (offset, text) = extract_offset(text); | 80 | let (offset, text) = extract_offset(text); |
77 | let file_id = self.add_file(file_map, path, &text); | 81 | let file_id = self.add_file(source_root, path, &text); |
78 | FilePosition { file_id, offset } | 82 | FilePosition { file_id, offset } |
79 | } | 83 | } |
80 | } | 84 | } |
@@ -104,11 +108,11 @@ impl Default for MockDatabase { | |||
104 | runtime: salsa::Runtime::default(), | 108 | runtime: salsa::Runtime::default(), |
105 | id_maps: Default::default(), | 109 | id_maps: Default::default(), |
106 | }; | 110 | }; |
107 | db.query_mut(ra_db::SourceRootQuery) | ||
108 | .set(ra_db::WORKSPACE, Default::default()); | ||
109 | db.query_mut(ra_db::CrateGraphQuery) | 111 | db.query_mut(ra_db::CrateGraphQuery) |
110 | .set((), Default::default()); | 112 | .set((), Default::default()); |
111 | db.query_mut(ra_db::LibrariesQuery) | 113 | db.query_mut(ra_db::LocalRootsQuery) |
114 | .set((), Default::default()); | ||
115 | db.query_mut(ra_db::LibraryRootsQuery) | ||
112 | .set((), Default::default()); | 116 | .set((), Default::default()); |
113 | db | 117 | db |
114 | } | 118 | } |
@@ -158,9 +162,11 @@ salsa::database_storage! { | |||
158 | pub(crate) struct MockDatabaseStorage for MockDatabase { | 162 | pub(crate) struct MockDatabaseStorage for MockDatabase { |
159 | impl ra_db::FilesDatabase { | 163 | impl ra_db::FilesDatabase { |
160 | fn file_text() for ra_db::FileTextQuery; | 164 | fn file_text() for ra_db::FileTextQuery; |
165 | fn file_relative_path() for ra_db::FileRelativePathQuery; | ||
161 | fn file_source_root() for ra_db::FileSourceRootQuery; | 166 | fn file_source_root() for ra_db::FileSourceRootQuery; |
162 | fn source_root() for ra_db::SourceRootQuery; | 167 | fn source_root() for ra_db::SourceRootQuery; |
163 | fn libraries() for ra_db::LibrariesQuery; | 168 | fn local_roots() for ra_db::LocalRootsQuery; |
169 | fn library_roots() for ra_db::LibraryRootsQuery; | ||
164 | fn crate_graph() for ra_db::CrateGraphQuery; | 170 | fn crate_graph() for ra_db::CrateGraphQuery; |
165 | } | 171 | } |
166 | impl ra_db::SyntaxDatabase { | 172 | impl ra_db::SyntaxDatabase { |