From 4b9e5d5dd9e4e477fdff9051056fd2be4bf4b538 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Jan 2020 16:29:31 +0100 Subject: Publicize debug printing of CrateDefMap --- crates/ra_hir_def/src/nameres.rs | 40 +++++++++++++++++++++++++++++++ crates/ra_hir_def/src/nameres/tests.rs | 43 ++-------------------------------- 2 files changed, 42 insertions(+), 41 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 27c12e46c..852304dd0 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -229,6 +229,46 @@ impl CrateDefMap { self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path, shadow); (res.resolved_def, res.segment_index) } + + // FIXME: this can use some more human-readable format (ideally, an IR + // even), as this should be a great debugging aid. + pub fn dump(&self) -> String { + let mut buf = String::new(); + go(&mut buf, self, "\ncrate", self.root); + return buf.trim().to_string(); + + fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) { + *buf += path; + *buf += "\n"; + + let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect(); + entries.sort_by_key(|(name, _)| name.clone()); + + for (name, def) in entries { + *buf += &format!("{}:", name); + + if def.types.is_some() { + *buf += " t"; + } + if def.values.is_some() { + *buf += " v"; + } + if def.macros.is_some() { + *buf += " m"; + } + if def.is_none() { + *buf += " _"; + } + + *buf += "\n"; + } + + for (name, child) in map.modules[module].children.iter() { + let path = path.to_string() + &format!("::{}", name); + go(buf, map, &path, *child); + } + } + } } impl ModuleData { diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index 78bcdc850..82f0f835c 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs @@ -10,11 +10,10 @@ use insta::assert_snapshot; use ra_db::{fixture::WithFixture, SourceDatabase}; use test_utils::covers; -use crate::{db::DefDatabase, nameres::*, test_db::TestDB, LocalModuleId}; +use crate::{db::DefDatabase, nameres::*, test_db::TestDB}; fn def_map(fixture: &str) -> String { - let dm = compute_crate_def_map(fixture); - render_crate_def_map(&dm) + compute_crate_def_map(fixture).dump() } fn compute_crate_def_map(fixture: &str) -> Arc { @@ -23,44 +22,6 @@ fn compute_crate_def_map(fixture: &str) -> Arc { db.crate_def_map(krate) } -fn render_crate_def_map(map: &CrateDefMap) -> String { - let mut buf = String::new(); - go(&mut buf, map, "\ncrate", map.root); - return buf.trim().to_string(); - - fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) { - *buf += path; - *buf += "\n"; - - let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect(); - entries.sort_by_key(|(name, _)| name.clone()); - - for (name, def) in entries { - *buf += &format!("{}:", name); - - if def.types.is_some() { - *buf += " t"; - } - if def.values.is_some() { - *buf += " v"; - } - if def.macros.is_some() { - *buf += " m"; - } - if def.is_none() { - *buf += " _"; - } - - *buf += "\n"; - } - - for (name, child) in map.modules[module].children.iter() { - let path = path.to_string() + &format!("::{}", name); - go(buf, map, &path, *child); - } - } -} - #[test] fn crate_def_map_smoke_test() { let map = def_map( -- cgit v1.2.3