Skip to content

Commit 3f70236

Browse files
bors[bot]matklad
andauthored
Merge #2927
2927: Publicize debug printing of CrateDefMap r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 2ddfc4e + 4b9e5d5 commit 3f70236

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

crates/ra_hir_def/src/nameres.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,46 @@ impl CrateDefMap {
229229
self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path, shadow);
230230
(res.resolved_def, res.segment_index)
231231
}
232+
233+
// FIXME: this can use some more human-readable format (ideally, an IR
234+
// even), as this should be a great debugging aid.
235+
pub fn dump(&self) -> String {
236+
let mut buf = String::new();
237+
go(&mut buf, self, "\ncrate", self.root);
238+
return buf.trim().to_string();
239+
240+
fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) {
241+
*buf += path;
242+
*buf += "\n";
243+
244+
let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect();
245+
entries.sort_by_key(|(name, _)| name.clone());
246+
247+
for (name, def) in entries {
248+
*buf += &format!("{}:", name);
249+
250+
if def.types.is_some() {
251+
*buf += " t";
252+
}
253+
if def.values.is_some() {
254+
*buf += " v";
255+
}
256+
if def.macros.is_some() {
257+
*buf += " m";
258+
}
259+
if def.is_none() {
260+
*buf += " _";
261+
}
262+
263+
*buf += "\n";
264+
}
265+
266+
for (name, child) in map.modules[module].children.iter() {
267+
let path = path.to_string() + &format!("::{}", name);
268+
go(buf, map, &path, *child);
269+
}
270+
}
271+
}
232272
}
233273

234274
impl ModuleData {

crates/ra_hir_def/src/nameres/tests.rs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ use insta::assert_snapshot;
1010
use ra_db::{fixture::WithFixture, SourceDatabase};
1111
use test_utils::covers;
1212

13-
use crate::{db::DefDatabase, nameres::*, test_db::TestDB, LocalModuleId};
13+
use crate::{db::DefDatabase, nameres::*, test_db::TestDB};
1414

1515
fn def_map(fixture: &str) -> String {
16-
let dm = compute_crate_def_map(fixture);
17-
render_crate_def_map(&dm)
16+
compute_crate_def_map(fixture).dump()
1817
}
1918

2019
fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
@@ -23,44 +22,6 @@ fn compute_crate_def_map(fixture: &str) -> Arc<CrateDefMap> {
2322
db.crate_def_map(krate)
2423
}
2524

26-
fn render_crate_def_map(map: &CrateDefMap) -> String {
27-
let mut buf = String::new();
28-
go(&mut buf, map, "\ncrate", map.root);
29-
return buf.trim().to_string();
30-
31-
fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) {
32-
*buf += path;
33-
*buf += "\n";
34-
35-
let mut entries: Vec<_> = map.modules[module].scope.resolutions().collect();
36-
entries.sort_by_key(|(name, _)| name.clone());
37-
38-
for (name, def) in entries {
39-
*buf += &format!("{}:", name);
40-
41-
if def.types.is_some() {
42-
*buf += " t";
43-
}
44-
if def.values.is_some() {
45-
*buf += " v";
46-
}
47-
if def.macros.is_some() {
48-
*buf += " m";
49-
}
50-
if def.is_none() {
51-
*buf += " _";
52-
}
53-
54-
*buf += "\n";
55-
}
56-
57-
for (name, child) in map.modules[module].children.iter() {
58-
let path = path.to_string() + &format!("::{}", name);
59-
go(buf, map, &path, *child);
60-
}
61-
}
62-
}
63-
6425
#[test]
6526
fn crate_def_map_smoke_test() {
6627
let map = def_map(

0 commit comments

Comments
 (0)