Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions crates/ra_hir_def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 2 additions & 41 deletions crates/ra_hir_def/src/nameres/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CrateDefMap> {
Expand All @@ -23,44 +22,6 @@ fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
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(
Expand Down