rustdoc: Add path_pass for recording full paths to mods
This commit is contained in:
parent
c54f53b9d9
commit
a5e0f037be
49
src/rustdoc/path_pass.rs
Normal file
49
src/rustdoc/path_pass.rs
Normal file
@ -0,0 +1,49 @@
|
||||
#[doc = "Records the full path to items"];
|
||||
|
||||
export mk_pass;
|
||||
|
||||
fn mk_pass() -> pass { run }
|
||||
|
||||
type ctxt = {
|
||||
srv: astsrv::srv,
|
||||
mutable path: [str]
|
||||
};
|
||||
|
||||
fn run(srv: astsrv::srv, doc: doc::cratedoc) -> doc::cratedoc {
|
||||
let ctxt = {
|
||||
srv: srv,
|
||||
mutable path: []
|
||||
};
|
||||
let fold = fold::fold({
|
||||
fold_mod: fn~(
|
||||
f: fold::fold<ctxt>,
|
||||
d: doc::moddoc
|
||||
) -> doc::moddoc {
|
||||
fold_mod(f, d)
|
||||
}
|
||||
with *fold::default_seq_fold(ctxt)
|
||||
});
|
||||
fold.fold_crate(fold, doc)
|
||||
}
|
||||
|
||||
fn fold_mod(fold: fold::fold<ctxt>, doc: doc::moddoc) -> doc::moddoc {
|
||||
let is_topmod = doc.id == rustc::syntax::ast::crate_node_id;
|
||||
|
||||
if !is_topmod { vec::push(fold.ctxt.path, doc.name); }
|
||||
let doc = fold::default_seq_fold_mod(fold, doc);
|
||||
if !is_topmod { vec::pop(fold.ctxt.path); }
|
||||
~{
|
||||
path: fold.ctxt.path
|
||||
with *doc
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_record_mod_paths() {
|
||||
let source = "mod a { mod b { mod c { } } mod d { mod e { } } }";
|
||||
let srv = astsrv::mk_srv_from_str(source);
|
||||
let doc = extract::from_srv(srv, "");
|
||||
let doc = run(srv, doc);
|
||||
assert doc.topmod.mods[0].mods[0].mods[0].path == ["a", "b"];
|
||||
assert doc.topmod.mods[0].mods[1].mods[0].path == ["a", "d"];
|
||||
}
|
@ -18,6 +18,7 @@ mod attr_parser;
|
||||
mod doc;
|
||||
mod gen;
|
||||
mod fold;
|
||||
mod path_pass;
|
||||
mod attr_pass;
|
||||
mod tystr_pass;
|
||||
mod prune_undoc_pass;
|
||||
|
@ -93,6 +93,7 @@ fn run(source_file: str) {
|
||||
let doc = extract::from_srv(srv, default_name);
|
||||
run_passes(srv, doc, [
|
||||
attr_pass::mk_pass(),
|
||||
path_pass::mk_pass(),
|
||||
// FIXME: This pass should be optional
|
||||
prune_undoc_pass::mk_pass(),
|
||||
tystr_pass::mk_pass(),
|
||||
|
Loading…
Reference in New Issue
Block a user