From ef2b84ddf119c950272c5f1eb321f3f9e90bedd4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 8 Sep 2019 09:48:45 +0300 Subject: introduce hir debugging infra This is to make debugging rust-analyzer easier. The idea is that `dbg!(krate.debug(db))` will print the actual, fuzzy crate name, instead of precise ID. Debug printing infra is a separate thing, to make sure that the actual hir doesn't have access to global information. Do not use `.debug` for `log::` logging: debugging executes queries, and might introduce unneded dependencies to the crate graph --- crates/ra_ide_api/src/change.rs | 15 +++++++++++++-- crates/ra_ide_api/src/db.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs index 89631935a..0d52f5ffb 100644 --- a/crates/ra_ide_api/src/change.rs +++ b/crates/ra_ide_api/src/change.rs @@ -2,7 +2,7 @@ use std::{fmt, sync::Arc, time}; use ra_db::{ salsa::{Database, Durability, SweepStrategy}, - CrateGraph, FileId, SourceDatabase, SourceRoot, SourceRootId, + CrateGraph, CrateId, FileId, SourceDatabase, SourceRoot, SourceRootId, }; use ra_prof::{memory_usage, profile, Bytes}; use ra_syntax::SourceFile; @@ -11,7 +11,7 @@ use relative_path::RelativePathBuf; use rustc_hash::FxHashMap; use crate::{ - db::RootDatabase, + db::{DebugData, RootDatabase}, status::syntax_tree_stats, symbol_index::{SymbolIndex, SymbolsDatabase}, }; @@ -23,6 +23,7 @@ pub struct AnalysisChange { files_changed: Vec<(FileId, Arc)>, libraries_added: Vec, crate_graph: Option, + debug_data: DebugData, } impl fmt::Debug for AnalysisChange { @@ -83,6 +84,14 @@ impl AnalysisChange { pub fn set_crate_graph(&mut self, graph: CrateGraph) { self.crate_graph = Some(graph); } + + pub fn set_debug_crate_name(&mut self, crate_id: CrateId, name: String) { + self.debug_data.crate_names.insert(crate_id, name); + } + + pub fn set_debug_root_path(&mut self, source_root_id: SourceRootId, path: String) { + self.debug_data.root_paths.insert(source_root_id, path); + } } #[derive(Debug)] @@ -200,6 +209,8 @@ impl RootDatabase { if let Some(crate_graph) = change.crate_graph { self.set_crate_graph_with_durability(Arc::new(crate_graph), Durability::HIGH) } + + Arc::make_mut(&mut self.debug_data).merge(change.debug_data) } fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs index f2e6b8f12..4c5159d61 100644 --- a/crates/ra_ide_api/src/db.rs +++ b/crates/ra_ide_api/src/db.rs @@ -2,8 +2,9 @@ use std::{sync::Arc, time}; use ra_db::{ salsa::{self, Database, Durability}, - Canceled, CheckCanceled, FileId, SourceDatabase, + Canceled, CheckCanceled, CrateId, FileId, SourceDatabase, SourceRootId, }; +use rustc_hash::FxHashMap; use crate::{ symbol_index::{self, SymbolsDatabase}, @@ -23,10 +24,23 @@ use crate::{ pub(crate) struct RootDatabase { runtime: salsa::Runtime, pub(crate) feature_flags: Arc, + pub(crate) debug_data: Arc, pub(crate) last_gc: time::Instant, pub(crate) last_gc_check: time::Instant, } +impl hir::debug::HirDebugHelper for RootDatabase { + fn crate_name(&self, krate: CrateId) -> Option { + self.debug_data.crate_names.get(&krate).cloned() + } + fn file_path(&self, file_id: FileId) -> Option { + let source_root_id = self.file_source_root(file_id); + let source_root_path = self.debug_data.root_paths.get(&source_root_id)?; + let file_path = self.file_relative_path(file_id); + Some(format!("{}/{}", source_root_path, file_path.display())) + } +} + impl salsa::Database for RootDatabase { fn salsa_runtime(&self) -> &salsa::Runtime { &self.runtime @@ -58,6 +72,7 @@ impl RootDatabase { last_gc: time::Instant::now(), last_gc_check: time::Instant::now(), feature_flags: Arc::new(feature_flags), + debug_data: Default::default(), }; db.set_crate_graph_with_durability(Default::default(), Durability::HIGH); db.set_local_roots_with_durability(Default::default(), Durability::HIGH); @@ -77,6 +92,7 @@ impl salsa::ParallelDatabase for RootDatabase { last_gc: self.last_gc, last_gc_check: self.last_gc_check, feature_flags: Arc::clone(&self.feature_flags), + debug_data: Arc::clone(&self.debug_data), }) } } @@ -90,3 +106,16 @@ fn line_index(db: &impl ra_db::SourceDatabase, file_id: FileId) -> Arc, + pub(crate) crate_names: FxHashMap, +} + +impl DebugData { + pub(crate) fn merge(&mut self, other: DebugData) { + self.root_paths.extend(other.root_paths.into_iter()); + self.crate_names.extend(other.crate_names.into_iter()); + } +} -- cgit v1.2.3