diff options
author | Aleksey Kladov <[email protected]> | 2018-12-19 09:40:41 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-20 09:15:38 +0000 |
commit | 26dcc70129399cadfd985f86bbad8d5127f8a3a0 (patch) | |
tree | a36409971145c034825ef5a2ed030b1a23e4445d /crates/ra_hir/src | |
parent | 18aac1df45ca39d01d80a56e3afab7299de160f8 (diff) |
fix hir mock
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/mock.rs | 35 | ||||
-rw-r--r-- | crates/ra_hir/src/module/nameres/tests.rs | 7 |
2 files changed, 23 insertions, 19 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index b7193c4f3..54260101c 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs | |||
@@ -2,7 +2,7 @@ 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, WORKSPACE, CrateGraph, SourceRoot}; |
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 | ||
@@ -16,10 +16,10 @@ pub(crate) struct MockDatabase { | |||
16 | } | 16 | } |
17 | 17 | ||
18 | impl MockDatabase { | 18 | impl MockDatabase { |
19 | pub(crate) fn with_files(fixture: &str) -> (MockDatabase, FileMap) { | 19 | pub(crate) fn with_files(fixture: &str) -> (MockDatabase, SourceRoot) { |
20 | let (db, file_map, position) = MockDatabase::from_fixture(fixture); | 20 | let (db, source_root, position) = MockDatabase::from_fixture(fixture); |
21 | assert!(position.is_none()); | 21 | assert!(position.is_none()); |
22 | (db, file_map) | 22 | (db, source_root) |
23 | } | 23 | } |
24 | 24 | ||
25 | pub(crate) fn with_position(fixture: &str) -> (MockDatabase, FilePosition) { | 25 | pub(crate) fn with_position(fixture: &str) -> (MockDatabase, FilePosition) { |
@@ -33,48 +33,50 @@ impl MockDatabase { | |||
33 | .set((), Arc::new(crate_graph)); | 33 | .set((), Arc::new(crate_graph)); |
34 | } | 34 | } |
35 | 35 | ||
36 | fn from_fixture(fixture: &str) -> (MockDatabase, FileMap, Option<FilePosition>) { | 36 | fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) { |
37 | let mut db = MockDatabase::default(); | 37 | let mut db = MockDatabase::default(); |
38 | 38 | ||
39 | let mut position = None; | 39 | let mut position = None; |
40 | let mut file_map = FileMap::default(); | 40 | let mut source_root = SourceRoot::default(); |
41 | for entry in parse_fixture(fixture) { | 41 | for entry in parse_fixture(fixture) { |
42 | if entry.text.contains(CURSOR_MARKER) { | 42 | if entry.text.contains(CURSOR_MARKER) { |
43 | assert!( | 43 | assert!( |
44 | position.is_none(), | 44 | position.is_none(), |
45 | "only one marker (<|>) per fixture is allowed" | 45 | "only one marker (<|>) per fixture is allowed" |
46 | ); | 46 | ); |
47 | position = Some(db.add_file_with_position(&mut file_map, &entry.meta, &entry.text)); | 47 | position = |
48 | Some(db.add_file_with_position(&mut source_root, &entry.meta, &entry.text)); | ||
48 | } else { | 49 | } else { |
49 | db.add_file(&mut file_map, &entry.meta, &entry.text); | 50 | db.add_file(&mut source_root, &entry.meta, &entry.text); |
50 | } | 51 | } |
51 | } | 52 | } |
52 | let source_root = file_map.clone().into_source_root(); | ||
53 | db.query_mut(ra_db::SourceRootQuery) | 53 | db.query_mut(ra_db::SourceRootQuery) |
54 | .set(WORKSPACE, Arc::new(source_root)); | 54 | .set(WORKSPACE, Arc::new(source_root.clone())); |
55 | (db, file_map, position) | 55 | (db, source_root, position) |
56 | } | 56 | } |
57 | 57 | ||
58 | fn add_file(&mut self, file_map: &mut FileMap, path: &str, text: &str) -> FileId { | 58 | fn add_file(&mut self, source_root: &mut SourceRoot, path: &str, text: &str) -> FileId { |
59 | assert!(path.starts_with('/')); | 59 | assert!(path.starts_with('/')); |
60 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); | 60 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); |
61 | 61 | 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()); | 62 | let text = Arc::new(text.to_string()); |
64 | self.query_mut(ra_db::FileTextQuery).set(file_id, text); | 63 | self.query_mut(ra_db::FileTextQuery).set(file_id, text); |
64 | self.query_mut(ra_db::FileRelativePathQuery) | ||
65 | .set(file_id, path.clone()); | ||
65 | self.query_mut(ra_db::FileSourceRootQuery) | 66 | self.query_mut(ra_db::FileSourceRootQuery) |
66 | .set(file_id, WORKSPACE); | 67 | .set(file_id, WORKSPACE); |
68 | source_root.files.insert(path, file_id); | ||
67 | file_id | 69 | file_id |
68 | } | 70 | } |
69 | 71 | ||
70 | fn add_file_with_position( | 72 | fn add_file_with_position( |
71 | &mut self, | 73 | &mut self, |
72 | file_map: &mut FileMap, | 74 | source_root: &mut SourceRoot, |
73 | path: &str, | 75 | path: &str, |
74 | text: &str, | 76 | text: &str, |
75 | ) -> FilePosition { | 77 | ) -> FilePosition { |
76 | let (offset, text) = extract_offset(text); | 78 | let (offset, text) = extract_offset(text); |
77 | let file_id = self.add_file(file_map, path, &text); | 79 | let file_id = self.add_file(source_root, path, &text); |
78 | FilePosition { file_id, offset } | 80 | FilePosition { file_id, offset } |
79 | } | 81 | } |
80 | } | 82 | } |
@@ -158,6 +160,7 @@ salsa::database_storage! { | |||
158 | pub(crate) struct MockDatabaseStorage for MockDatabase { | 160 | pub(crate) struct MockDatabaseStorage for MockDatabase { |
159 | impl ra_db::FilesDatabase { | 161 | impl ra_db::FilesDatabase { |
160 | fn file_text() for ra_db::FileTextQuery; | 162 | fn file_text() for ra_db::FileTextQuery; |
163 | fn file_relative_path() for ra_db::FileRelativePathQuery; | ||
161 | fn file_source_root() for ra_db::FileSourceRootQuery; | 164 | fn file_source_root() for ra_db::FileSourceRootQuery; |
162 | fn source_root() for ra_db::SourceRootQuery; | 165 | fn source_root() for ra_db::SourceRootQuery; |
163 | fn libraries() for ra_db::LibrariesQuery; | 166 | fn libraries() for ra_db::LibrariesQuery; |
diff --git a/crates/ra_hir/src/module/nameres/tests.rs b/crates/ra_hir/src/module/nameres/tests.rs index 9ddc32dcd..9fa9146e3 100644 --- a/crates/ra_hir/src/module/nameres/tests.rs +++ b/crates/ra_hir/src/module/nameres/tests.rs | |||
@@ -3,6 +3,7 @@ use std::sync::Arc; | |||
3 | use salsa::Database; | 3 | use salsa::Database; |
4 | use ra_db::{FilesDatabase, CrateGraph}; | 4 | use ra_db::{FilesDatabase, CrateGraph}; |
5 | use ra_syntax::SmolStr; | 5 | use ra_syntax::SmolStr; |
6 | use relative_path::RelativePath; | ||
6 | 7 | ||
7 | use crate::{ | 8 | use crate::{ |
8 | self as hir, | 9 | self as hir, |
@@ -44,7 +45,7 @@ fn item_map_smoke_test() { | |||
44 | 45 | ||
45 | #[test] | 46 | #[test] |
46 | fn item_map_across_crates() { | 47 | fn item_map_across_crates() { |
47 | let (mut db, files) = MockDatabase::with_files( | 48 | let (mut db, sr) = MockDatabase::with_files( |
48 | " | 49 | " |
49 | //- /main.rs | 50 | //- /main.rs |
50 | use test_crate::Baz; | 51 | use test_crate::Baz; |
@@ -53,8 +54,8 @@ fn item_map_across_crates() { | |||
53 | pub struct Baz; | 54 | pub struct Baz; |
54 | ", | 55 | ", |
55 | ); | 56 | ); |
56 | let main_id = files.file_id("/main.rs"); | 57 | let main_id = sr.files[RelativePath::new("/main.rs")]; |
57 | let lib_id = files.file_id("/lib.rs"); | 58 | let lib_id = sr.files[RelativePath::new("/lib.rs")]; |
58 | 59 | ||
59 | let mut crate_graph = CrateGraph::default(); | 60 | let mut crate_graph = CrateGraph::default(); |
60 | let main_crate = crate_graph.add_crate_root(main_id); | 61 | let main_crate = crate_graph.add_crate_root(main_id); |