2012-07-04 16:53:12 -05:00
|
|
|
//! Records the full path to items
|
2012-01-19 01:48:25 -06:00
|
|
|
|
2012-07-11 17:00:40 -05:00
|
|
|
import doc::item_utils;
|
2012-05-13 19:12:56 -05:00
|
|
|
import syntax::ast;
|
|
|
|
|
2012-01-19 01:48:25 -06:00
|
|
|
export mk_pass;
|
|
|
|
|
2012-02-27 20:07:16 -06:00
|
|
|
fn mk_pass() -> pass {
|
|
|
|
{
|
2012-07-14 00:57:48 -05:00
|
|
|
name: ~"path",
|
2012-02-27 20:07:16 -06:00
|
|
|
f: run
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 01:48:25 -06:00
|
|
|
|
|
|
|
type ctxt = {
|
|
|
|
srv: astsrv::srv,
|
2012-07-14 00:57:48 -05:00
|
|
|
mut path: ~[~str]
|
2012-01-19 01:48:25 -06:00
|
|
|
};
|
|
|
|
|
2012-08-01 15:35:33 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-03-02 20:33:25 -06:00
|
|
|
fn run(srv: astsrv::srv, doc: doc::doc) -> doc::doc {
|
2012-01-19 01:48:25 -06:00
|
|
|
let ctxt = {
|
|
|
|
srv: srv,
|
2012-06-29 18:26:56 -05:00
|
|
|
mut path: ~[]
|
2012-01-19 01:48:25 -06:00
|
|
|
};
|
|
|
|
let fold = fold::fold({
|
2012-02-17 19:48:37 -06:00
|
|
|
fold_item: fold_item,
|
2012-02-24 16:08:47 -06:00
|
|
|
fold_mod: fold_mod,
|
2012-09-04 15:29:32 -05:00
|
|
|
fold_nmod: fold_nmod,
|
|
|
|
.. *fold::default_any_fold(ctxt)
|
2012-01-19 01:48:25 -06:00
|
|
|
});
|
2012-03-02 20:33:25 -06:00
|
|
|
fold.fold_doc(fold, doc)
|
2012-01-19 01:48:25 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 19:48:37 -06: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-02-17 19:48:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:35:33 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-01-19 01:48:25 -06:00
|
|
|
fn fold_mod(fold: fold::fold<ctxt>, doc: doc::moddoc) -> doc::moddoc {
|
2012-05-13 19:12:56 -05:00
|
|
|
let is_topmod = doc.id() == ast::crate_node_id;
|
2012-01-19 01:48:25 -06:00
|
|
|
|
2012-02-17 16:46:30 -06:00
|
|
|
if !is_topmod { vec::push(fold.ctxt.path, doc.name()); }
|
2012-02-21 00:24:59 -06:00
|
|
|
let doc = fold::default_any_fold_mod(fold, doc);
|
2012-01-19 01:48:25 -06:00
|
|
|
if !is_topmod { vec::pop(fold.ctxt.path); }
|
2012-02-17 19:48:37 -06:00
|
|
|
|
2012-07-11 17:00:40 -05:00
|
|
|
doc::moddoc_({
|
2012-09-04 15:29:32 -05:00
|
|
|
item: fold.fold_item(fold, doc.item),
|
|
|
|
.. *doc
|
2012-07-11 17:00:40 -05:00
|
|
|
})
|
2012-01-19 01:48:25 -06:00
|
|
|
}
|
|
|
|
|
2012-02-24 16:08:47 -06:00
|
|
|
fn fold_nmod(fold: fold::fold<ctxt>, doc: doc::nmoddoc) -> doc::nmoddoc {
|
|
|
|
vec::push(fold.ctxt.path, doc.name());
|
|
|
|
let doc = fold::default_seq_fold_nmod(fold, doc);
|
|
|
|
vec::pop(fold.ctxt.path);
|
2012-02-24 17:22:57 -06:00
|
|
|
|
|
|
|
{
|
2012-09-04 15:29:32 -05:00
|
|
|
item: fold.fold_item(fold, doc.item),
|
|
|
|
.. doc
|
2012-02-24 17:22:57 -06:00
|
|
|
}
|
2012-02-24 16:08:47 -06:00
|
|
|
}
|
|
|
|
|
2012-01-19 01:48:25 -06:00
|
|
|
#[test]
|
|
|
|
fn should_record_mod_paths() {
|
2012-07-14 00:57:48 -05:00
|
|
|
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| {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-20 23:08:19 -06:00
|
|
|
let doc = run(srv, doc);
|
2012-03-02 20:33:25 -06:00
|
|
|
assert doc.cratemod().mods()[0].mods()[0].mods()[0].path()
|
2012-07-14 00:57:48 -05:00
|
|
|
== ~[~"a", ~"b"];
|
2012-03-02 20:33:25 -06:00
|
|
|
assert doc.cratemod().mods()[0].mods()[1].mods()[0].path()
|
2012-07-14 00:57:48 -05:00
|
|
|
== ~[~"a", ~"d"];
|
2012-02-20 23:08:19 -06:00
|
|
|
}
|
2012-02-17 19:48:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_record_fn_paths() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"mod a { fn b() { } }";
|
2012-06-30 18:19:07 -05:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-20 23:08:19 -06:00
|
|
|
let doc = run(srv, doc);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert doc.cratemod().mods()[0].fns()[0].path() == ~[~"a"];
|
2012-02-20 23:08:19 -06:00
|
|
|
}
|
2012-02-24 16:08:47 -06:00
|
|
|
}
|
|
|
|
|
2012-02-24 17:22:57 -06:00
|
|
|
#[test]
|
2012-06-26 18:18:37 -05:00
|
|
|
fn should_record_foreign_mod_paths() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"mod a { extern mod b { } }";
|
2012-06-30 18:19:07 -05:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-24 17:22:57 -06:00
|
|
|
let doc = run(srv, doc);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert doc.cratemod().mods()[0].nmods()[0].path() == ~[~"a"];
|
2012-02-24 17:22:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 16:08:47 -06:00
|
|
|
#[test]
|
2012-06-26 18:18:37 -05:00
|
|
|
fn should_record_foreign_fn_paths() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"extern mod a { fn b(); }";
|
2012-06-30 18:19:07 -05:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-24 16:08:47 -06:00
|
|
|
let doc = run(srv, doc);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert doc.cratemod().nmods()[0].fns[0].path() == ~[~"a"];
|
2012-02-24 16:08:47 -06:00
|
|
|
}
|
2012-06-04 16:31:25 -05:00
|
|
|
}
|