Minimize FileLoader interface

This commit is contained in:
Aleksey Kladov 2020-06-05 16:45:20 +02:00
parent 9c52f527a1
commit bbb40d7463
6 changed files with 35 additions and 79 deletions

View File

@ -89,14 +89,13 @@ pub const DEFAULT_LRU_CAP: usize = 128;
pub trait FileLoader {
/// Text of the file.
fn file_text(&self, file_id: FileId) -> Arc<String>;
/// 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>;
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId>;
}
/// Database which stores all significant input facts: source code and project
@ -154,34 +153,30 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
fn file_text(&self, file_id: FileId) -> Arc<String> {
SourceDatabaseExt::file_text(self.0, file_id)
}
/// 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(&rel_path)
// FIXME: this *somehow* should be platform agnostic...
if std::path::Path::new(path).is_absolute() {
let krate = *self.relevant_crates(anchor).get(0)?;
let (extern_source_id, relative_file) =
self.0.crate_graph()[krate].extern_source.extern_path(path)?;
let source_root = self.0.source_root(SourceRootId(extern_source_id.0));
source_root.file_by_relative_path(&relative_file)
} else {
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(&rel_path)
}
}
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
let source_root = self.0.file_source_root(file_id);
self.0.source_root_crates(source_root)
}
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
let source_root = self.0.source_root(SourceRootId(extern_id.0));
source_root.file_by_relative_path(&relative_path)
}
}

View File

@ -6,9 +6,7 @@ use std::{
};
use hir_expand::db::AstDatabase;
use ra_db::{
salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath, Upcast,
};
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, Upcast};
use crate::db::DefDatabase;
@ -64,14 +62,6 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
fn resolve_extern_path(
&self,
extern_id: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
}
impl TestDB {

View File

@ -295,19 +295,13 @@ fn concat_expand(
fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> {
let call_site = call_id.as_file().original_file(db);
// Handle trivial case
if let Some(res) = db.resolve_path(call_site, path) {
// Prevent include itself
return if res == call_site { None } else { Some(res) };
let res = db.resolve_path(call_site, path)?;
// Prevent include itself
if res == call_site {
None
} else {
Some(res)
}
// Extern paths ?
let krate = *db.relevant_crates(call_site).get(0)?;
let (extern_source_id, relative_file) =
db.crate_graph()[krate].extern_source.extern_path(path)?;
db.resolve_extern_path(extern_source_id, &relative_file)
}
fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {

View File

@ -5,7 +5,7 @@ use std::{
sync::{Arc, Mutex},
};
use ra_db::{salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath};
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate};
#[salsa::database(
ra_db::SourceDatabaseExtStorage,
@ -47,11 +47,4 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
fn resolve_extern_path(
&self,
anchor: ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(anchor, relative_path)
}
}

View File

@ -7,9 +7,7 @@ use std::{
use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId};
use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink};
use ra_db::{
salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath, SourceDatabase, Upcast,
};
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast};
use stdx::format_to;
use crate::{db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator};
@ -78,13 +76,6 @@ impl FileLoader for TestDB {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
fn resolve_extern_path(
&self,
extern_id: ra_db::ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
}
impl TestDB {

View File

@ -16,8 +16,8 @@ use std::sync::Arc;
use hir::db::{AstDatabase, DefDatabase};
use ra_db::{
salsa::{self, Database, Durability},
Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath,
SourceDatabase, SourceRootId, Upcast,
Canceled, CheckCanceled, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase,
SourceRootId, Upcast,
};
use rustc_hash::FxHashMap;
@ -63,13 +63,6 @@ impl FileLoader for RootDatabase {
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
fn resolve_extern_path(
&self,
extern_id: ra_db::ExternSourceId,
relative_path: &RelativePath,
) -> Option<FileId> {
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
}
}
impl salsa::Database for RootDatabase {