diff options
Diffstat (limited to 'crates/ra_db')
-rw-r--r-- | crates/ra_db/src/file_resolver.rs | 76 | ||||
-rw-r--r-- | crates/ra_db/src/input.rs | 27 | ||||
-rw-r--r-- | crates/ra_db/src/lib.rs | 7 | ||||
-rw-r--r-- | crates/ra_db/src/mock.rs | 28 |
4 files changed, 20 insertions, 118 deletions
diff --git a/crates/ra_db/src/file_resolver.rs b/crates/ra_db/src/file_resolver.rs deleted file mode 100644 index f849ac752..000000000 --- a/crates/ra_db/src/file_resolver.rs +++ /dev/null | |||
@@ -1,76 +0,0 @@ | |||
1 | use std::{ | ||
2 | sync::Arc, | ||
3 | hash::{Hash, Hasher}, | ||
4 | fmt, | ||
5 | }; | ||
6 | |||
7 | use relative_path::RelativePath; | ||
8 | |||
9 | use crate::input::FileId; | ||
10 | |||
11 | pub trait FileResolver: fmt::Debug + Send + Sync + 'static { | ||
12 | fn file_stem(&self, file_id: FileId) -> String; | ||
13 | fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>; | ||
14 | fn debug_path(&self, _1file_id: FileId) -> Option<std::path::PathBuf> { | ||
15 | None | ||
16 | } | ||
17 | } | ||
18 | |||
19 | #[derive(Clone, Debug)] | ||
20 | pub struct FileResolverImp { | ||
21 | inner: Arc<FileResolver>, | ||
22 | } | ||
23 | |||
24 | impl PartialEq for FileResolverImp { | ||
25 | fn eq(&self, other: &FileResolverImp) -> bool { | ||
26 | self.inner() == other.inner() | ||
27 | } | ||
28 | } | ||
29 | |||
30 | impl Eq for FileResolverImp {} | ||
31 | |||
32 | impl Hash for FileResolverImp { | ||
33 | fn hash<H: Hasher>(&self, hasher: &mut H) { | ||
34 | self.inner().hash(hasher); | ||
35 | } | ||
36 | } | ||
37 | |||
38 | impl FileResolverImp { | ||
39 | pub fn new(inner: Arc<FileResolver>) -> FileResolverImp { | ||
40 | FileResolverImp { inner } | ||
41 | } | ||
42 | pub fn file_stem(&self, file_id: FileId) -> String { | ||
43 | self.inner.file_stem(file_id) | ||
44 | } | ||
45 | pub fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId> { | ||
46 | self.inner.resolve(file_id, path) | ||
47 | } | ||
48 | pub fn debug_path(&self, file_id: FileId) -> Option<std::path::PathBuf> { | ||
49 | self.inner.debug_path(file_id) | ||
50 | } | ||
51 | fn inner(&self) -> *const FileResolver { | ||
52 | &*self.inner | ||
53 | } | ||
54 | } | ||
55 | |||
56 | impl Default for FileResolverImp { | ||
57 | fn default() -> FileResolverImp { | ||
58 | #[derive(Debug)] | ||
59 | struct DummyResolver; | ||
60 | impl FileResolver for DummyResolver { | ||
61 | fn file_stem(&self, _file_: FileId) -> String { | ||
62 | panic!("file resolver not set") | ||
63 | } | ||
64 | fn resolve( | ||
65 | &self, | ||
66 | _file_id: FileId, | ||
67 | _path: &::relative_path::RelativePath, | ||
68 | ) -> Option<FileId> { | ||
69 | panic!("file resolver not set") | ||
70 | } | ||
71 | } | ||
72 | FileResolverImp { | ||
73 | inner: Arc::new(DummyResolver), | ||
74 | } | ||
75 | } | ||
76 | } | ||
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index ac144b991..cccf37cc2 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -1,10 +1,12 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use rustc_hash::{FxHashSet, FxHashMap}; | 3 | use rustc_hash::{FxHashMap}; |
4 | use relative_path::RelativePathBuf; | ||
4 | use ra_syntax::SmolStr; | 5 | use ra_syntax::SmolStr; |
5 | use salsa; | 6 | use salsa; |
6 | 7 | ||
7 | use crate::file_resolver::FileResolverImp; | 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
9 | pub struct SourceRootId(pub u32); | ||
8 | 10 | ||
9 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
10 | pub struct FileId(pub u32); | 12 | pub struct FileId(pub u32); |
@@ -85,6 +87,11 @@ salsa::query_group! { | |||
85 | type FileTextQuery; | 87 | type FileTextQuery; |
86 | storage input; | 88 | storage input; |
87 | } | 89 | } |
90 | /// Path to a file, relative to the root of its source root. | ||
91 | fn file_relative_path(file_id: FileId) -> RelativePathBuf { | ||
92 | type FileRelativePathQuery; | ||
93 | storage input; | ||
94 | } | ||
88 | fn file_source_root(file_id: FileId) -> SourceRootId { | 95 | fn file_source_root(file_id: FileId) -> SourceRootId { |
89 | type FileSourceRootQuery; | 96 | type FileSourceRootQuery; |
90 | storage input; | 97 | storage input; |
@@ -93,8 +100,12 @@ salsa::query_group! { | |||
93 | type SourceRootQuery; | 100 | type SourceRootQuery; |
94 | storage input; | 101 | storage input; |
95 | } | 102 | } |
96 | fn libraries() -> Arc<Vec<SourceRootId>> { | 103 | fn local_roots() -> Arc<Vec<SourceRootId>> { |
97 | type LibrariesQuery; | 104 | type LocalRootsQuery; |
105 | storage input; | ||
106 | } | ||
107 | fn library_roots() -> Arc<Vec<SourceRootId>> { | ||
108 | type LibraryRootsQuery; | ||
98 | storage input; | 109 | storage input; |
99 | } | 110 | } |
100 | fn crate_graph() -> Arc<CrateGraph> { | 111 | fn crate_graph() -> Arc<CrateGraph> { |
@@ -104,13 +115,7 @@ salsa::query_group! { | |||
104 | } | 115 | } |
105 | } | 116 | } |
106 | 117 | ||
107 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
108 | pub struct SourceRootId(pub u32); | ||
109 | |||
110 | #[derive(Default, Clone, Debug, PartialEq, Eq)] | 118 | #[derive(Default, Clone, Debug, PartialEq, Eq)] |
111 | pub struct SourceRoot { | 119 | pub struct SourceRoot { |
112 | pub file_resolver: FileResolverImp, | 120 | pub files: FxHashMap<RelativePathBuf, FileId>, |
113 | pub files: FxHashSet<FileId>, | ||
114 | } | 121 | } |
115 | |||
116 | pub const WORKSPACE: SourceRootId = SourceRootId(0); | ||
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 53805aada..65fa3cbfa 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | //! ra_db defines basic database traits. Concrete DB is defined by ra_analysis. | 1 | //! ra_db defines basic database traits. Concrete DB is defined by ra_analysis. |
2 | mod syntax_ptr; | 2 | mod syntax_ptr; |
3 | mod file_resolver; | ||
4 | mod input; | 3 | mod input; |
5 | mod loc2id; | 4 | mod loc2id; |
6 | pub mod mock; | 5 | pub mod mock; |
@@ -24,10 +23,10 @@ impl std::error::Error for Canceled {} | |||
24 | 23 | ||
25 | pub use crate::{ | 24 | pub use crate::{ |
26 | syntax_ptr::LocalSyntaxPtr, | 25 | syntax_ptr::LocalSyntaxPtr, |
27 | file_resolver::{FileResolver, FileResolverImp}, | ||
28 | input::{ | 26 | input::{ |
29 | FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, WORKSPACE, | 27 | FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, |
30 | FileTextQuery, FileSourceRootQuery, SourceRootQuery, LibrariesQuery, CrateGraphQuery, | 28 | FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery, |
29 | FileRelativePathQuery | ||
31 | }, | 30 | }, |
32 | loc2id::{LocationIntener, NumericId}, | 31 | loc2id::{LocationIntener, NumericId}, |
33 | }; | 32 | }; |
diff --git a/crates/ra_db/src/mock.rs b/crates/ra_db/src/mock.rs index 2f7551597..5e185062b 100644 --- a/crates/ra_db/src/mock.rs +++ b/crates/ra_db/src/mock.rs | |||
@@ -1,9 +1,7 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use rustc_hash::FxHashSet; | 1 | use rustc_hash::FxHashSet; |
4 | use relative_path::{RelativePath, RelativePathBuf}; | 2 | use relative_path::{RelativePath, RelativePathBuf}; |
5 | 3 | ||
6 | use crate::{FileId, FileResolver, SourceRoot, FileResolverImp}; | 4 | use crate::{FileId}; |
7 | 5 | ||
8 | #[derive(Default, Debug, Clone)] | 6 | #[derive(Default, Debug, Clone)] |
9 | pub struct FileMap(Vec<(FileId, RelativePathBuf)>); | 7 | pub struct FileMap(Vec<(FileId, RelativePathBuf)>); |
@@ -15,15 +13,6 @@ impl FileMap { | |||
15 | file_id | 13 | file_id |
16 | } | 14 | } |
17 | 15 | ||
18 | pub fn into_source_root(self) -> SourceRoot { | ||
19 | let files = self.files(); | ||
20 | let file_resolver = FileResolverImp::new(Arc::new(self)); | ||
21 | SourceRoot { | ||
22 | file_resolver, | ||
23 | files, | ||
24 | } | ||
25 | } | ||
26 | |||
27 | pub fn files(&self) -> FxHashSet<FileId> { | 16 | pub fn files(&self) -> FxHashSet<FileId> { |
28 | self.iter().map(|(id, _)| id).collect() | 17 | self.iter().map(|(id, _)| id).collect() |
29 | } | 18 | } |
@@ -38,19 +27,4 @@ impl FileMap { | |||
38 | .iter() | 27 | .iter() |
39 | .map(|(id, path)| (*id, path.as_relative_path())) | 28 | .map(|(id, path)| (*id, path.as_relative_path())) |
40 | } | 29 | } |
41 | |||
42 | fn path(&self, id: FileId) -> &RelativePath { | ||
43 | self.iter().find(|&(it, _)| it == id).unwrap().1 | ||
44 | } | ||
45 | } | ||
46 | |||
47 | impl FileResolver for FileMap { | ||
48 | fn file_stem(&self, id: FileId) -> String { | ||
49 | self.path(id).file_stem().unwrap().to_string() | ||
50 | } | ||
51 | fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> { | ||
52 | let path = self.path(id).join(rel).normalize(); | ||
53 | let id = self.iter().find(|&(_, p)| path == p)?.0; | ||
54 | Some(id) | ||
55 | } | ||
56 | } | 30 | } |