Skip to content

Commit 0407a50

Browse files
committed
Rust: extract CrateModule
1 parent 6df7fa1 commit 0407a50

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

rust/extractor/src/main.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use itertools::Itertools;
77
use log::{info, warn};
88
use ra_ap_base_db::{CrateId, SourceDatabase};
99
use ra_ap_cfg::CfgAtom;
10-
use ra_ap_hir::Semantics;
10+
use ra_ap_hir::db::DefDatabase;
11+
use ra_ap_hir::{DefMap, Semantics};
12+
use ra_ap_hir_def::LocalModuleId;
1113
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
1214
use ra_ap_ide_db::RootDatabase;
1315
use ra_ap_project_model::{CargoConfig, ProjectManifest};
@@ -20,6 +22,7 @@ use std::{
2022
collections::HashMap,
2123
path::{Path, PathBuf},
2224
};
25+
use trap::TrapFile;
2326

2427
mod archive;
2528
mod config;
@@ -221,8 +224,40 @@ impl<'a> Extractor<'a> {
221224
.map(|(module, hash)| trap.label(format!("crate:{module}:{hash}").into()))
222225
.collect(),
223226
};
224-
trap.emit(element);
227+
let parent = trap.emit(element);
228+
229+
go(
230+
db,
231+
db.crate_def_map(krate_id).as_ref(),
232+
parent.into(),
233+
"crate",
234+
DefMap::ROOT,
235+
&mut trap,
236+
);
225237
trap.commit();
238+
239+
fn go(
240+
db: &dyn DefDatabase,
241+
map: &DefMap,
242+
parent: trap::Label<generated::ModuleContainer>,
243+
name: &str,
244+
module: LocalModuleId,
245+
trap: &mut TrapFile,
246+
) {
247+
let module = &map.modules[module];
248+
let label = trap.emit(generated::CrateModule {
249+
id: trap::TrapId::Star,
250+
name: name.to_owned(),
251+
parent,
252+
});
253+
for (name, child) in module
254+
.children
255+
.iter()
256+
.sorted_by(|a, b| Ord::cmp(&a.0, &b.0))
257+
{
258+
go(db, map, label.into(), name.as_str(), *child, trap);
259+
}
260+
}
226261
}
227262
}
228263
}

0 commit comments

Comments
 (0)