aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-12-20 09:16:07 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-12-20 09:16:07 +0000
commit9a16cf2bf646a4a881f3f5acbf5582dacc4337bf (patch)
treeff90eb390a8f5af47b6d0c6ec4c49b4431b82d1c /crates/ra_db
parentdbb62b5baa36783bbca06f09c77794fce3ceff0c (diff)
parenta084412f0698d5f54d586f707930e141c88b0673 (diff)
Merge #292
292: Vfs r=matklad a=matklad closes #243 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_db')
-rw-r--r--crates/ra_db/src/file_resolver.rs76
-rw-r--r--crates/ra_db/src/input.rs27
-rw-r--r--crates/ra_db/src/lib.rs7
-rw-r--r--crates/ra_db/src/mock.rs28
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 @@
1use std::{
2 sync::Arc,
3 hash::{Hash, Hasher},
4 fmt,
5};
6
7use relative_path::RelativePath;
8
9use crate::input::FileId;
10
11pub 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)]
20pub struct FileResolverImp {
21 inner: Arc<FileResolver>,
22}
23
24impl PartialEq for FileResolverImp {
25 fn eq(&self, other: &FileResolverImp) -> bool {
26 self.inner() == other.inner()
27 }
28}
29
30impl Eq for FileResolverImp {}
31
32impl Hash for FileResolverImp {
33 fn hash<H: Hasher>(&self, hasher: &mut H) {
34 self.inner().hash(hasher);
35 }
36}
37
38impl 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
56impl 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 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use rustc_hash::{FxHashSet, FxHashMap}; 3use rustc_hash::{FxHashMap};
4use relative_path::RelativePathBuf;
4use ra_syntax::SmolStr; 5use ra_syntax::SmolStr;
5use salsa; 6use salsa;
6 7
7use crate::file_resolver::FileResolverImp; 8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub 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)]
10pub struct FileId(pub u32); 12pub 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)]
108pub struct SourceRootId(pub u32);
109
110#[derive(Default, Clone, Debug, PartialEq, Eq)] 118#[derive(Default, Clone, Debug, PartialEq, Eq)]
111pub struct SourceRoot { 119pub struct SourceRoot {
112 pub file_resolver: FileResolverImp, 120 pub files: FxHashMap<RelativePathBuf, FileId>,
113 pub files: FxHashSet<FileId>,
114} 121}
115
116pub 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.
2mod syntax_ptr; 2mod syntax_ptr;
3mod file_resolver;
4mod input; 3mod input;
5mod loc2id; 4mod loc2id;
6pub mod mock; 5pub mod mock;
@@ -24,10 +23,10 @@ impl std::error::Error for Canceled {}
24 23
25pub use crate::{ 24pub 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 @@
1use std::sync::Arc;
2
3use rustc_hash::FxHashSet; 1use rustc_hash::FxHashSet;
4use relative_path::{RelativePath, RelativePathBuf}; 2use relative_path::{RelativePath, RelativePathBuf};
5 3
6use crate::{FileId, FileResolver, SourceRoot, FileResolverImp}; 4use crate::{FileId};
7 5
8#[derive(Default, Debug, Clone)] 6#[derive(Default, Debug, Clone)]
9pub struct FileMap(Vec<(FileId, RelativePathBuf)>); 7pub 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
47impl 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}