From abf2179c0b606730acadc61451739baecfa33a5d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 14 Oct 2019 16:20:55 +0300 Subject: Prepare SourceDatabase API for lazy file loading --- crates/ra_hir/src/from_source.rs | 10 ++++---- crates/ra_hir/src/ids.rs | 35 +------------------------- crates/ra_hir/src/mock.rs | 23 ++++++++++++++--- crates/ra_hir/src/nameres/tests/incremental.rs | 2 +- 4 files changed, 27 insertions(+), 43 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index a012f33f7..f80d8eb5f 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -189,14 +189,14 @@ impl Module { ModuleSource::SourceFile(_) => None, }; - let source_root_id = db.file_source_root(src.file_id.original_file(db)); - db.source_root_crates(source_root_id).iter().map(|&crate_id| Crate { crate_id }).find_map( - |krate| { + db.relevant_crates(src.file_id.original_file(db)) + .iter() + .map(|&crate_id| Crate { crate_id }) + .find_map(|krate| { let def_map = db.crate_def_map(krate); let module_id = def_map.find_module_by_source(src.file_id, decl_id)?; Some(Module { krate, module_id }) - }, - ) + }) } } diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 85b022744..499dcafea 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -85,11 +85,7 @@ impl HirFileId { // Note: // The final goal we would like to make all parse_macro success, // such that the following log will not call anyway. - log::warn!( - "fail on macro_parse: (reason: {}) {}", - err, - macro_call_id.debug_dump(db) - ); + log::warn!("fail on macro_parse: (reason: {})", err,); }) .ok()?; match macro_file.macro_file_kind { @@ -367,35 +363,6 @@ impl AstItemDef for TypeAliasId { } } -impl MacroCallId { - pub fn debug_dump(self, db: &impl AstDatabase) -> String { - let loc = self.loc(db); - let node = loc.ast_id.to_node(db); - let syntax_str = { - let mut res = String::new(); - node.syntax().text().for_each_chunk(|chunk| { - if !res.is_empty() { - res.push(' ') - } - res.push_str(chunk) - }); - res - }; - - // dump the file name - let file_id: HirFileId = self.loc(db).ast_id.file_id(); - let original = file_id.original_file(db); - let macro_rules = db.macro_def(loc.def); - - format!( - "macro call [file: {:?}] : {}\nhas rules: {}", - db.file_relative_path(original), - syntax_str, - macro_rules.is_some() - ) - } -} - /// This exists just for Chalk, because Chalk just has a single `StructId` where /// we have different kinds of ADTs, primitive types and special type /// constructors like tuples and function pointers. diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 827424983..0b278deb3 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -5,10 +5,10 @@ use std::{panic, sync::Arc}; use parking_lot::Mutex; use ra_cfg::CfgOptions; use ra_db::{ - salsa, CrateGraph, CrateId, Edition, FileId, FilePosition, SourceDatabase, SourceRoot, - SourceRootId, + salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, FilePosition, + SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, }; -use relative_path::RelativePathBuf; +use relative_path::{RelativePath, RelativePathBuf}; use rustc_hash::FxHashMap; use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; @@ -17,6 +17,7 @@ use crate::{db, debug::HirDebugHelper, diagnostics::DiagnosticSink}; pub const WORKSPACE: SourceRootId = SourceRootId(0); #[salsa::database( + ra_db::SourceDatabaseExtStorage, ra_db::SourceDatabaseStorage, db::InternDatabaseStorage, db::AstDatabaseStorage, @@ -34,6 +35,22 @@ pub struct MockDatabase { impl panic::RefUnwindSafe for MockDatabase {} +impl FileLoader for MockDatabase { + fn file_text(&self, file_id: FileId) -> Arc { + FileLoaderDelegate(self).file_text(file_id) + } + fn resolve_relative_path( + &self, + anchor: FileId, + relative_path: &RelativePath, + ) -> Option { + FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) + } + fn relevant_crates(&self, file_id: FileId) -> Arc> { + FileLoaderDelegate(self).relevant_crates(file_id) + } +} + impl HirDebugHelper for MockDatabase { fn crate_name(&self, krate: CrateId) -> Option { self.crate_names.get(&krate).cloned() diff --git a/crates/ra_hir/src/nameres/tests/incremental.rs b/crates/ra_hir/src/nameres/tests/incremental.rs index c41862a0b..af9c39760 100644 --- a/crates/ra_hir/src/nameres/tests/incremental.rs +++ b/crates/ra_hir/src/nameres/tests/incremental.rs @@ -2,7 +2,7 @@ use super::*; use std::sync::Arc; -use ra_db::SourceDatabase; +use ra_db::{SourceDatabase, SourceDatabaseExt}; fn check_def_map_is_not_recomputed(initial: &str, file_change: &str) { let (mut db, pos) = MockDatabase::with_position(initial); -- cgit v1.2.3