rust/src/rustdoc/prune_hidden_pass.rs

64 lines
1.3 KiB
Rust
Raw Normal View History

//! Prunes things with the #[doc(hidden)] attribute
2012-03-07 14:49:52 -08:00
import doc::item_utils;
import std::map::hashmap;
2012-03-07 14:49:52 -08:00
export mk_pass;
fn mk_pass() -> pass {
{
name: ~"prune_hidden",
2012-03-07 14:49:52 -08:00
f: run
}
}
fn run(srv: astsrv::srv, doc: doc::doc) -> doc::doc {
let fold = fold::fold({
fold_mod: fold_mod
with *fold::default_any_fold(srv)
});
fold.fold_doc(fold, doc)
}
fn fold_mod(
fold: fold::fold<astsrv::srv>,
doc: doc::moddoc
) -> doc::moddoc {
let doc = fold::default_any_fold_mod(fold, doc);
doc::moddoc_({
2012-06-30 16:19:07 -07:00
items: vec::filter(doc.items, |itemtag| {
2012-03-07 14:49:52 -08:00
!is_hidden(fold.ctxt, itemtag.item())
})
with *doc
})
2012-03-07 14:49:52 -08:00
}
fn is_hidden(srv: astsrv::srv, doc: doc::itemdoc) -> bool {
import syntax::ast_map;
2012-03-07 14:49:52 -08:00
let id = doc.id;
2012-06-30 16:19:07 -07:00
do astsrv::exec(srv) |ctxt| {
2012-03-07 14:49:52 -08:00
let attrs = alt ctxt.ast_map.get(id) {
ast_map::node_item(item, _) { item.attrs }
_ { ~[] }
2012-03-07 14:49:52 -08:00
};
attr_parser::parse_hidden(attrs)
}
}
#[test]
fn should_prune_hidden_items() {
let doc = test::mk_doc(~"#[doc(hidden)] mod a { }");
2012-03-07 14:49:52 -08:00
assert vec::is_empty(doc.cratemod().mods())
}
#[cfg(test)]
mod test {
fn mk_doc(source: ~str) -> doc::doc {
2012-06-30 16:19:07 -07:00
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv, ~"");
2012-03-07 14:49:52 -08:00
run(srv, doc)
}
}
}