More direct signature for resolve_path

This commit is contained in:
Aleksey Kladov 2020-06-05 15:07:30 +02:00
parent e63c00f100
commit bba374bab2
7 changed files with 24 additions and 19 deletions

View File

@ -89,7 +89,7 @@ pub const DEFAULT_LRU_CAP: usize = 128;
pub trait FileLoader {
/// Text of the file.
fn file_text(&self, file_id: FileId) -> Arc<String>;
fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option<FileId>;
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
fn resolve_extern_path(
@ -154,16 +154,21 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
fn file_text(&self, file_id: FileId) -> Arc<String> {
SourceDatabaseExt::file_text(self.0, file_id)
}
fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option<FileId> {
let path = {
let mut path = self.0.file_relative_path(anchor);
assert!(path.pop());
path.push(relative_path);
path.normalize()
/// Note that we intentionally accept a `&str` and not a `&Path` here. This
/// method exists to handle `#[path = "/some/path.rs"] mod foo;` and such,
/// so the input is guaranteed to be utf-8 string. We might introduce
/// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we
/// get by with a `&str` for the time being.
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
let rel_path = {
let mut rel_path = self.0.file_relative_path(anchor);
assert!(rel_path.pop());
rel_path.push(path);
rel_path.normalize()
};
let source_root = self.0.file_source_root(anchor);
let source_root = self.0.source_root(source_root);
source_root.file_by_relative_path(&path)
source_root.file_by_relative_path(&rel_path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {

View File

@ -61,7 +61,7 @@ impl ModDir {
};
for candidate in candidate_files.iter() {
if let Some(file_id) = db.resolve_path(file_id, candidate) {
if let Some(file_id) = db.resolve_path(file_id, candidate.as_str()) {
let mut root_non_dir_owner = false;
let mut mod_path = RelativePathBuf::new();
if !(candidate.ends_with("mod.rs") || attr_path.is_some()) {

View File

@ -58,8 +58,8 @@ impl FileLoader for TestDB {
fn file_text(&self, file_id: FileId) -> Arc<String> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, relative_path)
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)

View File

@ -8,7 +8,7 @@ use crate::{
use crate::{quote, EagerMacroId, LazyMacroId, MacroCallId};
use either::Either;
use mbe::parse_to_token_tree;
use ra_db::{FileId, RelativePath};
use ra_db::FileId;
use ra_parser::FragmentKind;
macro_rules! register_builtin {
@ -297,7 +297,7 @@ fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Opti
let call_site = call_id.as_file().original_file(db);
// Handle trivial case
if let Some(res) = db.resolve_path(call_site, &RelativePath::new(&path)) {
if let Some(res) = db.resolve_path(call_site, path) {
// Prevent include itself
return if res == call_site { None } else { Some(res) };
}

View File

@ -41,8 +41,8 @@ impl FileLoader for TestDB {
fn file_text(&self, file_id: FileId) -> Arc<String> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, relative_path)
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)

View File

@ -72,8 +72,8 @@ impl FileLoader for TestDB {
fn file_text(&self, file_id: FileId) -> Arc<String> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, relative_path)
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)

View File

@ -57,8 +57,8 @@ impl FileLoader for RootDatabase {
fn file_text(&self, file_id: FileId) -> Arc<String> {
FileLoaderDelegate(self).file_text(file_id)
}
fn resolve_path(&self, anchor: FileId, relative_path: &RelativePath) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, relative_path)
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(anchor, path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)