diff options
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/lib.rs | 57 |
1 files changed, 26 insertions, 31 deletions
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 2e63cb46e..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,14 +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 | /// Note that we intentionally accept a `&str` and not a `&Path` here. This | ||
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. | ||
92 | fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>; | 97 | fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>; |
93 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>; | 98 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>; |
94 | |||
95 | fn resolve_extern_path( | ||
96 | &self, | ||
97 | extern_id: ExternSourceId, | ||
98 | relative_path: &RelativePath, | ||
99 | ) -> Option<FileId>; | ||
100 | } | 99 | } |
101 | 100 | ||
102 | /// Database which stores all significant input facts: source code and project | 101 | /// Database which stores all significant input facts: source code and project |
@@ -154,34 +153,30 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> { | |||
154 | fn file_text(&self, file_id: FileId) -> Arc<String> { | 153 | fn file_text(&self, file_id: FileId) -> Arc<String> { |
155 | SourceDatabaseExt::file_text(self.0, file_id) | 154 | SourceDatabaseExt::file_text(self.0, file_id) |
156 | } | 155 | } |
157 | /// Note that we intentionally accept a `&str` and not a `&Path` here. This | ||
158 | /// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such, | ||
159 | /// so the input is guaranteed to be utf-8 string. We might introduce | ||
160 | /// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we | ||
161 | /// get by with a `&str` for the time being. | ||
162 | fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { | 156 | fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> { |
163 | let rel_path = { | 157 | // FIXME: this *somehow* should be platform agnostic... |
164 | let mut rel_path = self.0.file_relative_path(anchor); | 158 | if std::path::Path::new(path).is_absolute() { |
165 | assert!(rel_path.pop()); | 159 | let krate = *self.relevant_crates(anchor).get(0)?; |
166 | rel_path.push(path); | 160 | let (extern_source_id, relative_file) = |
167 | rel_path.normalize() | 161 | self.0.crate_graph()[krate].extern_source.extern_path(path)?; |
168 | }; | 162 | |
169 | let source_root = self.0.file_source_root(anchor); | 163 | let source_root = self.0.source_root(SourceRootId(extern_source_id.0)); |
170 | let source_root = self.0.source_root(source_root); | 164 | source_root.file_by_relative_path(&relative_file) |
171 | source_root.file_by_relative_path(&rel_path) | 165 | } else { |
166 | let rel_path = { | ||
167 | let mut rel_path = self.0.file_relative_path(anchor); | ||
168 | assert!(rel_path.pop()); | ||
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 | } |