diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-14 14:26:42 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-14 14:26:42 +0100 |
commit | 8e3864fd580fb93e8ce92e4b4e6ac51050e54047 (patch) | |
tree | 7cc9565b1e81ec069297167e935d062b7dc4bc06 /crates/ra_db/src | |
parent | e182825170e346abb84240b84458b49b73783dca (diff) | |
parent | abf2179c0b606730acadc61451739baecfa33a5d (diff) |
Merge #2008
2008: Prepare SourceDatabase API for lazy file loading r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/input.rs | 2 | ||||
-rw-r--r-- | crates/ra_db/src/lib.rs | 94 |
2 files changed, 61 insertions, 35 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index cae51b02c..eafa95921 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -57,7 +57,7 @@ impl SourceRoot { | |||
57 | pub fn walk(&self) -> impl Iterator<Item = FileId> + '_ { | 57 | pub fn walk(&self) -> impl Iterator<Item = FileId> + '_ { |
58 | self.files.values().copied() | 58 | self.files.values().copied() |
59 | } | 59 | } |
60 | pub(crate) fn file_by_relative_path(&self, path: &RelativePath) -> Option<FileId> { | 60 | pub fn file_by_relative_path(&self, path: &RelativePath) -> Option<FileId> { |
61 | self.files.get(path).copied() | 61 | self.files.get(path).copied() |
62 | } | 62 | } |
63 | } | 63 | } |
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 4d3a9c036..fc5d6d396 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs | |||
@@ -64,21 +64,39 @@ pub struct FileRange { | |||
64 | 64 | ||
65 | pub const DEFAULT_LRU_CAP: usize = 128; | 65 | pub const DEFAULT_LRU_CAP: usize = 128; |
66 | 66 | ||
67 | /// Database which stores all significant input facts: source code and project | 67 | pub trait FileLoader { |
68 | /// model. Everything else in rust-analyzer is derived from these queries. | ||
69 | #[salsa::query_group(SourceDatabaseStorage)] | ||
70 | pub trait SourceDatabase: CheckCanceled + std::fmt::Debug { | ||
71 | /// Text of the file. | 68 | /// Text of the file. |
72 | #[salsa::input] | ||
73 | fn file_text(&self, file_id: FileId) -> Arc<String>; | 69 | fn file_text(&self, file_id: FileId) -> Arc<String>; |
74 | |||
75 | #[salsa::transparent] | ||
76 | fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath) | 70 | fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath) |
77 | -> Option<FileId>; | 71 | -> Option<FileId>; |
72 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>; | ||
73 | } | ||
78 | 74 | ||
75 | /// Database which stores all significant input facts: source code and project | ||
76 | /// model. Everything else in rust-analyzer is derived from these queries. | ||
77 | #[salsa::query_group(SourceDatabaseStorage)] | ||
78 | pub trait SourceDatabase: CheckCanceled + FileLoader + std::fmt::Debug { | ||
79 | // Parses the file into the syntax tree. | 79 | // Parses the file into the syntax tree. |
80 | #[salsa::invoke(parse_query)] | 80 | #[salsa::invoke(parse_query)] |
81 | fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>; | 81 | fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>; |
82 | |||
83 | /// The crate graph. | ||
84 | #[salsa::input] | ||
85 | fn crate_graph(&self) -> Arc<CrateGraph>; | ||
86 | } | ||
87 | |||
88 | fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> { | ||
89 | let _p = profile("parse_query"); | ||
90 | let text = db.file_text(file_id); | ||
91 | SourceFile::parse(&*text) | ||
92 | } | ||
93 | |||
94 | /// We don't want to give HIR knowledge of source roots, hence we extract these | ||
95 | /// methods into a separate DB. | ||
96 | #[salsa::query_group(SourceDatabaseExtStorage)] | ||
97 | pub trait SourceDatabaseExt: SourceDatabase { | ||
98 | #[salsa::input] | ||
99 | fn file_text(&self, file_id: FileId) -> Arc<String>; | ||
82 | /// Path to a file, relative to the root of its source root. | 100 | /// Path to a file, relative to the root of its source root. |
83 | #[salsa::input] | 101 | #[salsa::input] |
84 | fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf; | 102 | fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf; |
@@ -88,40 +106,48 @@ pub trait SourceDatabase: CheckCanceled + std::fmt::Debug { | |||
88 | /// Contents of the source root. | 106 | /// Contents of the source root. |
89 | #[salsa::input] | 107 | #[salsa::input] |
90 | fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>; | 108 | fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>; |
91 | fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>; | ||
92 | /// The crate graph. | ||
93 | #[salsa::input] | ||
94 | fn crate_graph(&self) -> Arc<CrateGraph>; | ||
95 | } | ||
96 | 109 | ||
97 | fn resolve_relative_path( | 110 | fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>; |
98 | db: &impl SourceDatabase, | ||
99 | anchor: FileId, | ||
100 | relative_path: &RelativePath, | ||
101 | ) -> Option<FileId> { | ||
102 | let path = { | ||
103 | let mut path = db.file_relative_path(anchor); | ||
104 | // Workaround for relative path API: turn `lib.rs` into ``. | ||
105 | if !path.pop() { | ||
106 | path = RelativePathBuf::default(); | ||
107 | } | ||
108 | path.push(relative_path); | ||
109 | path.normalize() | ||
110 | }; | ||
111 | let source_root = db.file_source_root(anchor); | ||
112 | let source_root = db.source_root(source_root); | ||
113 | source_root.file_by_relative_path(&path) | ||
114 | } | 111 | } |
115 | 112 | ||
116 | fn source_root_crates(db: &impl SourceDatabase, id: SourceRootId) -> Arc<Vec<CrateId>> { | 113 | fn source_root_crates( |
114 | db: &(impl SourceDatabaseExt + SourceDatabase), | ||
115 | id: SourceRootId, | ||
116 | ) -> Arc<Vec<CrateId>> { | ||
117 | let root = db.source_root(id); | 117 | let root = db.source_root(id); |
118 | let graph = db.crate_graph(); | 118 | let graph = db.crate_graph(); |
119 | let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>(); | 119 | let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>(); |
120 | Arc::new(res) | 120 | Arc::new(res) |
121 | } | 121 | } |
122 | 122 | ||
123 | fn parse_query(db: &impl SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> { | 123 | /// Silly workaround for cyclic deps between the traits |
124 | let _p = profile("parse_query"); | 124 | pub struct FileLoaderDelegate<T>(pub T); |
125 | let text = db.file_text(file_id); | 125 | |
126 | SourceFile::parse(&*text) | 126 | impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> { |
127 | fn file_text(&self, file_id: FileId) -> Arc<String> { | ||
128 | SourceDatabaseExt::file_text(self.0, file_id) | ||
129 | } | ||
130 | fn resolve_relative_path( | ||
131 | &self, | ||
132 | anchor: FileId, | ||
133 | relative_path: &RelativePath, | ||
134 | ) -> Option<FileId> { | ||
135 | let path = { | ||
136 | let mut path = self.0.file_relative_path(anchor); | ||
137 | // Workaround for relative path API: turn `lib.rs` into ``. | ||
138 | if !path.pop() { | ||
139 | path = RelativePathBuf::default(); | ||
140 | } | ||
141 | path.push(relative_path); | ||
142 | path.normalize() | ||
143 | }; | ||
144 | let source_root = self.0.file_source_root(anchor); | ||
145 | let source_root = self.0.source_root(source_root); | ||
146 | source_root.file_by_relative_path(&path) | ||
147 | } | ||
148 | |||
149 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { | ||
150 | let source_root = self.0.file_source_root(file_id); | ||
151 | self.0.source_root_crates(source_root) | ||
152 | } | ||
127 | } | 153 | } |