rust/src/rustdoc/path_pass.rs

109 lines
2.6 KiB
Rust
Raw Normal View History

//! Records the full path to items
2012-09-18 18:48:40 -05:00
use doc::ItemUtils;
2012-09-05 12:41:47 -05:00
use syntax::ast;
export mk_pass;
2012-09-18 18:48:40 -05:00
fn mk_pass() -> Pass {
2012-02-27 20:07:16 -06:00
{
name: ~"path",
2012-02-27 20:07:16 -06:00
f: run
}
}
2012-09-18 18:48:40 -05:00
type Ctxt = {
srv: astsrv::Srv,
mut path: ~[~str]
};
2012-08-01 15:35:33 -05:00
#[allow(non_implicitly_copyable_typarams)]
2012-09-18 18:48:40 -05:00
fn run(srv: astsrv::Srv, doc: doc::Doc) -> doc::Doc {
let ctxt = {
srv: srv,
mut path: ~[]
};
2012-09-18 18:48:40 -05:00
let fold = fold::Fold({
fold_item: fold_item,
fold_mod: fold_mod,
2012-09-04 15:29:32 -05:00
fold_nmod: fold_nmod,
.. *fold::default_any_fold(ctxt)
});
fold.fold_doc(fold, doc)
}
2012-09-18 18:48:40 -05:00
fn fold_item(fold: fold::Fold<Ctxt>, doc: doc::ItemDoc) -> doc::ItemDoc {
{
2012-09-04 15:29:32 -05:00
path: fold.ctxt.path,
.. doc
}
}
2012-08-01 15:35:33 -05:00
#[allow(non_implicitly_copyable_typarams)]
2012-09-18 18:48:40 -05:00
fn fold_mod(fold: fold::Fold<Ctxt>, doc: doc::ModDoc) -> doc::ModDoc {
let is_topmod = doc.id() == ast::crate_node_id;
if !is_topmod { fold.ctxt.path.push(doc.name()); }
let doc = fold::default_any_fold_mod(fold, doc);
2012-09-28 00:20:47 -05:00
if !is_topmod { fold.ctxt.path.pop(); }
2012-09-18 18:48:40 -05:00
doc::ModDoc_({
2012-09-04 15:29:32 -05:00
item: fold.fold_item(fold, doc.item),
.. *doc
})
}
2012-09-18 18:48:40 -05:00
fn fold_nmod(fold: fold::Fold<Ctxt>, doc: doc::NmodDoc) -> doc::NmodDoc {
fold.ctxt.path.push(doc.name());
let doc = fold::default_seq_fold_nmod(fold, doc);
2012-09-28 00:20:47 -05:00
fold.ctxt.path.pop();
{
2012-09-04 15:29:32 -05:00
item: fold.fold_item(fold, doc.item),
.. doc
}
}
#[test]
fn should_record_mod_paths() {
let source = ~"mod a { mod b { mod c { } } mod d { mod e { } } }";
2012-06-30 18:19:07 -05:00
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv, ~"");
let doc = run(srv, doc);
assert doc.cratemod().mods()[0].mods()[0].mods()[0].path()
== ~[~"a", ~"b"];
assert doc.cratemod().mods()[0].mods()[1].mods()[0].path()
== ~[~"a", ~"d"];
}
}
#[test]
fn should_record_fn_paths() {
let source = ~"mod a { fn b() { } }";
2012-06-30 18:19:07 -05:00
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv, ~"");
let doc = run(srv, doc);
assert doc.cratemod().mods()[0].fns()[0].path() == ~[~"a"];
}
}
#[test]
fn should_record_foreign_mod_paths() {
let source = ~"mod a { extern mod b { } }";
2012-06-30 18:19:07 -05:00
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv, ~"");
let doc = run(srv, doc);
assert doc.cratemod().mods()[0].nmods()[0].path() == ~[~"a"];
}
}
#[test]
fn should_record_foreign_fn_paths() {
let source = ~"extern mod a { fn b(); }";
2012-06-30 18:19:07 -05:00
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv, ~"");
let doc = run(srv, doc);
assert doc.cratemod().nmods()[0].fns[0].path() == ~[~"a"];
}
}