diff options
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/lib.rs | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index fd4280de2..91e0ee619 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs | |||
@@ -11,8 +11,8 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextSize}; | |||
11 | pub use crate::{ | 11 | pub use crate::{ |
12 | cancellation::Canceled, | 12 | cancellation::Canceled, |
13 | input::{ | 13 | input::{ |
14 | CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, ExternSourceId, | 14 | CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, |
15 | FileId, ProcMacroId, SourceRoot, SourceRootId, | 15 | ExternSourceId, FileId, ProcMacroId, SourceRoot, SourceRootId, |
16 | }, | 16 | }, |
17 | }; | 17 | }; |
18 | pub use relative_path::{RelativePath, RelativePathBuf}; | 18 | pub use relative_path::{RelativePath, RelativePathBuf}; |
@@ -89,15 +89,13 @@ pub const DEFAULT_LRU_CAP: usize = 128; | |||
89 | pub trait FileLoader { | 89 | pub trait FileLoader { |
90 | /// Text of the file. | 90 | /// Text of the file. |
91 | fn file_text(&self, file_id: FileId) -> Arc<String>; | 91 | fn file_text(&self, file_id: FileId) -> Arc<String>; |
92 | fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath) | 92 | /// Note that we intentionally accept a `&str` and not a `&Path` here. This |
93 | -> Option<FileId>; | 93 | /// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such, |
94 | /// so the input is guaranteed to be utf-8 string. We might introduce | ||
95 | /// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we | ||
96 | /// get by with a `&str` for the time being. | ||
97 | fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>; | ||
94 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>; | 98 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>; |
95 | |||
96 | fn resolve_extern_path( | ||
97 | &self, | ||
98 | extern_id: ExternSourceId, | ||
99 | relative_path: &RelativePath, | ||
100 | ) -> Option<FileId>; | ||
101 | } | 99 | } |
102 | 100 | ||
103 | /// Database which stores all significant input facts: source code and project | 101 | /// Database which stores all significant input facts: source code and project |
@@ -155,33 +153,30 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> { | |||
155 | fn file_text(&self, file_id: FileId) -> Arc<String> { | 153 | fn file_text(&self, file_id: FileId) -> Arc<String> { |
156 | SourceDatabaseExt::file_text(self.0, file_id) | 154 | SourceDatabaseExt::file_text(self.0, file_id) |
157 | } | 155 | } |
158 | fn resolve_relative_path( | 156 | fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { |
159 | &self, | 157 | // FIXME: this *somehow* should be platform agnostic... |
160 | anchor: FileId, | 158 | if std::path::Path::new(path).is_absolute() { |
161 | relative_path: &RelativePath, | 159 | let krate = *self.relevant_crates(anchor).get(0)?; |
162 | ) -> Option<FileId> { | 160 | let (extern_source_id, relative_file) = |
163 | let path = { | 161 | self.0.crate_graph()[krate].extern_source.extern_path(path)?; |
164 | let mut path = self.0.file_relative_path(anchor); | 162 | |
165 | assert!(path.pop()); | 163 | let source_root = self.0.source_root(SourceRootId(extern_source_id.0)); |
166 | path.push(relative_path); | 164 | source_root.file_by_relative_path(&relative_file) |
167 | path.normalize() | 165 | } else { |
168 | }; | 166 | let rel_path = { |
169 | let source_root = self.0.file_source_root(anchor); | 167 | let mut rel_path = self.0.file_relative_path(anchor); |
170 | let source_root = self.0.source_root(source_root); | 168 | assert!(rel_path.pop()); |
171 | source_root.file_by_relative_path(&path) | 169 | rel_path.push(path); |
170 | rel_path.normalize() | ||
171 | }; | ||
172 | let source_root = self.0.file_source_root(anchor); | ||
173 | let source_root = self.0.source_root(source_root); | ||
174 | source_root.file_by_relative_path(&rel_path) | ||
175 | } | ||
172 | } | 176 | } |
173 | 177 | ||
174 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { | 178 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { |
175 | let source_root = self.0.file_source_root(file_id); | 179 | let source_root = self.0.file_source_root(file_id); |
176 | self.0.source_root_crates(source_root) | 180 | self.0.source_root_crates(source_root) |
177 | } | 181 | } |
178 | |||
179 | fn resolve_extern_path( | ||
180 | &self, | ||
181 | extern_id: ExternSourceId, | ||
182 | relative_path: &RelativePath, | ||
183 | ) -> Option<FileId> { | ||
184 | let source_root = self.0.source_root(SourceRootId(extern_id.0)); | ||
185 | source_root.file_by_relative_path(&relative_path) | ||
186 | } | ||
187 | } | 182 | } |