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_project_model/src/lib.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'crates/ra_project_model') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 9b2f534e7..4fa32dc34 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -9,7 +9,7 @@ use std::{ path::{Path, PathBuf}, }; -use ra_db::{CrateGraph, Edition, FileId}; +use ra_db::{CrateGraph, CrateId, Edition, FileId}; use rustc_hash::FxHashMap; use serde_json::from_reader; @@ -113,8 +113,12 @@ impl ProjectWorkspace { } } - pub fn to_crate_graph(&self, load: &mut dyn FnMut(&Path) -> Option) -> CrateGraph { + pub fn to_crate_graph( + &self, + load: &mut dyn FnMut(&Path) -> Option, + ) -> (CrateGraph, FxHashMap) { let mut crate_graph = CrateGraph::default(); + let mut names = FxHashMap::default(); match self { ProjectWorkspace::Json { project } => { let mut crates = FxHashMap::default(); @@ -151,10 +155,9 @@ impl ProjectWorkspace { let mut sysroot_crates = FxHashMap::default(); for krate in sysroot.crates() { if let Some(file_id) = load(krate.root(&sysroot)) { - sysroot_crates.insert( - krate, - crate_graph.add_crate_root(file_id, Edition::Edition2018), - ); + let crate_id = crate_graph.add_crate_root(file_id, Edition::Edition2018); + sysroot_crates.insert(krate, crate_id); + names.insert(crate_id, krate.name(&sysroot).to_string()); } } for from in sysroot.crates() { @@ -182,6 +185,7 @@ impl ProjectWorkspace { if let Some(file_id) = load(root) { let edition = pkg.edition(&cargo); let crate_id = crate_graph.add_crate_root(file_id, edition); + names.insert(crate_id, pkg.name(&cargo).to_string()); if tgt.kind(&cargo) == TargetKind::Lib { lib_tgt = Some(crate_id); pkg_to_lib_crate.insert(pkg, crate_id); @@ -212,7 +216,7 @@ impl ProjectWorkspace { } } - // Now add a dep ednge from all targets of upstream to the lib + // Now add a dep edge from all targets of upstream to the lib // target of downstream. for pkg in cargo.packages() { for dep in pkg.dependencies(&cargo) { @@ -233,7 +237,7 @@ impl ProjectWorkspace { } } } - crate_graph + (crate_graph, names) } pub fn workspace_root_for(&self, path: &Path) -> Option<&Path> { -- cgit v1.2.3