aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_db/src/lib.rs53
-rw-r--r--crates/ra_hir_def/src/test_db.rs12
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs18
-rw-r--r--crates/ra_hir_expand/src/test_db.rs9
-rw-r--r--crates/ra_hir_ty/src/test_db.rs11
-rw-r--r--crates/ra_ide_db/src/lib.rs11
6 files changed, 35 insertions, 79 deletions
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
index 2e63cb46e..7354b3832 100644
--- a/crates/ra_db/src/lib.rs
+++ b/crates/ra_db/src/lib.rs
@@ -89,14 +89,13 @@ pub const DEFAULT_LRU_CAP: usize = 128;
89pub trait FileLoader { 89pub 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}
diff --git a/crates/ra_hir_def/src/test_db.rs b/crates/ra_hir_def/src/test_db.rs
index e7a5182f0..bcfa66ac9 100644
--- a/crates/ra_hir_def/src/test_db.rs
+++ b/crates/ra_hir_def/src/test_db.rs
@@ -6,9 +6,7 @@ use std::{
6}; 6};
7 7
8use hir_expand::db::AstDatabase; 8use hir_expand::db::AstDatabase;
9use ra_db::{ 9use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, Upcast};
10 salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath, Upcast,
11};
12 10
13use crate::db::DefDatabase; 11use crate::db::DefDatabase;
14 12
@@ -64,14 +62,6 @@ impl FileLoader for TestDB {
64 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { 62 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
65 FileLoaderDelegate(self).relevant_crates(file_id) 63 FileLoaderDelegate(self).relevant_crates(file_id)
66 } 64 }
67
68 fn resolve_extern_path(
69 &self,
70 extern_id: ExternSourceId,
71 relative_path: &RelativePath,
72 ) -> Option<FileId> {
73 FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
74 }
75} 65}
76 66
77impl TestDB { 67impl TestDB {
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index eec5fb8eb..7579546d2 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -295,19 +295,13 @@ fn concat_expand(
295 295
296fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> { 296fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> {
297 let call_site = call_id.as_file().original_file(db); 297 let call_site = call_id.as_file().original_file(db);
298 298 let res = db.resolve_path(call_site, path)?;
299 // Handle trivial case 299 // Prevent include itself
300 if let Some(res) = db.resolve_path(call_site, path) { 300 if res == call_site {
301 // Prevent include itself 301 None
302 return if res == call_site { None } else { Some(res) }; 302 } else {
303 Some(res)
303 } 304 }
304
305 // Extern paths ?
306 let krate = *db.relevant_crates(call_site).get(0)?;
307 let (extern_source_id, relative_file) =
308 db.crate_graph()[krate].extern_source.extern_path(path)?;
309
310 db.resolve_extern_path(extern_source_id, &relative_file)
311} 305}
312 306
313fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> { 307fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {
diff --git a/crates/ra_hir_expand/src/test_db.rs b/crates/ra_hir_expand/src/test_db.rs
index 765a2f6d1..fdf225f55 100644
--- a/crates/ra_hir_expand/src/test_db.rs
+++ b/crates/ra_hir_expand/src/test_db.rs
@@ -5,7 +5,7 @@ use std::{
5 sync::{Arc, Mutex}, 5 sync::{Arc, Mutex},
6}; 6};
7 7
8use ra_db::{salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; 8use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
9 9
10#[salsa::database( 10#[salsa::database(
11 ra_db::SourceDatabaseExtStorage, 11 ra_db::SourceDatabaseExtStorage,
@@ -47,11 +47,4 @@ impl FileLoader for TestDB {
47 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { 47 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
48 FileLoaderDelegate(self).relevant_crates(file_id) 48 FileLoaderDelegate(self).relevant_crates(file_id)
49 } 49 }
50 fn resolve_extern_path(
51 &self,
52 anchor: ExternSourceId,
53 relative_path: &RelativePath,
54 ) -> Option<FileId> {
55 FileLoaderDelegate(self).resolve_extern_path(anchor, relative_path)
56 }
57} 50}
diff --git a/crates/ra_hir_ty/src/test_db.rs b/crates/ra_hir_ty/src/test_db.rs
index 21a3bdfd1..e484968a0 100644
--- a/crates/ra_hir_ty/src/test_db.rs
+++ b/crates/ra_hir_ty/src/test_db.rs
@@ -7,9 +7,7 @@ use std::{
7 7
8use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId}; 8use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId};
9use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink}; 9use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink};
10use ra_db::{ 10use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast};
11 salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, Upcast,
12};
13use stdx::format_to; 11use stdx::format_to;
14 12
15use crate::{db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator}; 13use crate::{db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator};
@@ -78,13 +76,6 @@ impl FileLoader for TestDB {
78 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { 76 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
79 FileLoaderDelegate(self).relevant_crates(file_id) 77 FileLoaderDelegate(self).relevant_crates(file_id)
80 } 78 }
81 fn resolve_extern_path(
82 &self,
83 extern_id: ra_db::ExternSourceId,
84 relative_path: &RelativePath,
85 ) -> Option<FileId> {
86 FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
87 }
88} 79}
89 80
90impl TestDB { 81impl TestDB {
diff --git a/crates/ra_ide_db/src/lib.rs b/crates/ra_ide_db/src/lib.rs
index 93d5891a0..727d743b5 100644
--- a/crates/ra_ide_db/src/lib.rs
+++ b/crates/ra_ide_db/src/lib.rs
@@ -16,8 +16,8 @@ use std::sync::Arc;
16use hir::db::{AstDatabase, DefDatabase}; 16use hir::db::{AstDatabase, DefDatabase};
17use ra_db::{ 17use ra_db::{
18 salsa::{self, Database, Durability}, 18 salsa::{self, Database, Durability},
19 Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, 19 Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
20 SourceDatabase, SourceRootId, Upcast, 20 SourceRootId, Upcast,
21}; 21};
22use rustc_hash::FxHashMap; 22use rustc_hash::FxHashMap;
23 23
@@ -63,13 +63,6 @@ impl FileLoader for RootDatabase {
63 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { 63 fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
64 FileLoaderDelegate(self).relevant_crates(file_id) 64 FileLoaderDelegate(self).relevant_crates(file_id)
65 } 65 }
66 fn resolve_extern_path(
67 &self,
68 extern_id: ra_db::ExternSourceId,
69 relative_path: &RelativePath,
70 ) -> Option<FileId> {
71 FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
72 }
73} 66}
74 67
75impl salsa::Database for RootDatabase { 68impl salsa::Database for RootDatabase {