2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
//! Records the full path to items
|
2012-01-18 23:48:25 -08:00
|
|
|
|
2012-09-18 16:48:40 -07:00
|
|
|
use doc::ItemUtils;
|
2012-12-05 15:06:54 -08:00
|
|
|
use fold::Fold;
|
2012-09-05 10:41:47 -07:00
|
|
|
use syntax::ast;
|
2012-05-13 17:12:56 -07:00
|
|
|
|
2012-11-19 18:00:12 -08:00
|
|
|
pub fn mk_pass() -> Pass {
|
2012-02-27 18:07:16 -08:00
|
|
|
{
|
2012-07-13 22:57:48 -07:00
|
|
|
name: ~"path",
|
2012-02-27 18:07:16 -08:00
|
|
|
f: run
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 23:48:25 -08:00
|
|
|
|
2012-11-26 18:03:36 -08:00
|
|
|
struct Ctxt {
|
2012-09-18 16:48:40 -07:00
|
|
|
srv: astsrv::Srv,
|
2012-07-13 22:57:48 -07:00
|
|
|
mut path: ~[~str]
|
2012-11-26 18:03:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Ctxt: Clone {
|
|
|
|
fn clone(&self) -> Ctxt { copy *self }
|
|
|
|
}
|
2012-01-18 23:48:25 -08:00
|
|
|
|
2012-08-01 13:35:33 -07:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-11-19 18:48:46 -08:00
|
|
|
fn run(srv: astsrv::Srv, +doc: doc::Doc) -> doc::Doc {
|
2012-11-26 18:03:36 -08:00
|
|
|
let ctxt = Ctxt {
|
2012-01-18 23:48:25 -08:00
|
|
|
srv: srv,
|
2012-06-29 16:26:56 -07:00
|
|
|
mut path: ~[]
|
2012-01-18 23:48:25 -08:00
|
|
|
};
|
2012-12-05 15:06:54 -08:00
|
|
|
let fold = Fold {
|
2012-02-17 17:48:37 -08:00
|
|
|
fold_item: fold_item,
|
2012-02-24 14:08:47 -08:00
|
|
|
fold_mod: fold_mod,
|
2012-09-04 13:29:32 -07:00
|
|
|
fold_nmod: fold_nmod,
|
2012-12-05 15:06:54 -08:00
|
|
|
.. fold::default_any_fold(move ctxt)
|
|
|
|
};
|
2012-11-29 17:51:16 -08:00
|
|
|
(fold.fold_doc)(&fold, doc)
|
2012-01-18 23:48:25 -08:00
|
|
|
}
|
|
|
|
|
2012-11-19 18:48:46 -08:00
|
|
|
fn fold_item(fold: &fold::Fold<Ctxt>, +doc: doc::ItemDoc) -> doc::ItemDoc {
|
2012-02-17 17:48:37 -08:00
|
|
|
{
|
2012-09-04 13:29:32 -07:00
|
|
|
path: fold.ctxt.path,
|
|
|
|
.. doc
|
2012-02-17 17:48:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 13:35:33 -07:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-11-19 18:48:46 -08:00
|
|
|
fn fold_mod(fold: &fold::Fold<Ctxt>, +doc: doc::ModDoc) -> doc::ModDoc {
|
2012-05-13 17:12:56 -07:00
|
|
|
let is_topmod = doc.id() == ast::crate_node_id;
|
2012-01-18 23:48:25 -08:00
|
|
|
|
2012-09-26 17:33:34 -07:00
|
|
|
if !is_topmod { fold.ctxt.path.push(doc.name()); }
|
2012-02-20 22:24:59 -08:00
|
|
|
let doc = fold::default_any_fold_mod(fold, doc);
|
2012-09-27 22:20:47 -07:00
|
|
|
if !is_topmod { fold.ctxt.path.pop(); }
|
2012-02-17 17:48:37 -08:00
|
|
|
|
2012-09-18 16:48:40 -07:00
|
|
|
doc::ModDoc_({
|
2012-11-29 17:51:16 -08:00
|
|
|
item: (fold.fold_item)(fold, doc.item),
|
2012-09-04 13:29:32 -07:00
|
|
|
.. *doc
|
2012-07-11 15:00:40 -07:00
|
|
|
})
|
2012-01-18 23:48:25 -08:00
|
|
|
}
|
|
|
|
|
2012-11-19 18:48:46 -08:00
|
|
|
fn fold_nmod(fold: &fold::Fold<Ctxt>, +doc: doc::NmodDoc) -> doc::NmodDoc {
|
2012-09-26 17:33:34 -07:00
|
|
|
fold.ctxt.path.push(doc.name());
|
2012-02-24 14:08:47 -08:00
|
|
|
let doc = fold::default_seq_fold_nmod(fold, doc);
|
2012-09-27 22:20:47 -07:00
|
|
|
fold.ctxt.path.pop();
|
2012-02-24 15:22:57 -08:00
|
|
|
|
|
|
|
{
|
2012-11-29 17:51:16 -08:00
|
|
|
item: (fold.fold_item)(fold, doc.item),
|
2012-09-04 13:29:32 -07:00
|
|
|
.. doc
|
2012-02-24 15:22:57 -08:00
|
|
|
}
|
2012-02-24 14:08:47 -08:00
|
|
|
}
|
|
|
|
|
2012-01-18 23:48:25 -08:00
|
|
|
#[test]
|
|
|
|
fn should_record_mod_paths() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let source = ~"mod a { mod b { mod c { } } mod d { mod e { } } }";
|
2012-06-30 16:19:07 -07:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-20 21:08:19 -08:00
|
|
|
let doc = run(srv, doc);
|
2012-03-02 18:33:25 -08:00
|
|
|
assert doc.cratemod().mods()[0].mods()[0].mods()[0].path()
|
2012-07-13 22:57:48 -07:00
|
|
|
== ~[~"a", ~"b"];
|
2012-03-02 18:33:25 -08:00
|
|
|
assert doc.cratemod().mods()[0].mods()[1].mods()[0].path()
|
2012-07-13 22:57:48 -07:00
|
|
|
== ~[~"a", ~"d"];
|
2012-02-20 21:08:19 -08:00
|
|
|
}
|
2012-02-17 17:48:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_record_fn_paths() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let source = ~"mod a { fn b() { } }";
|
2012-06-30 16:19:07 -07:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-20 21:08:19 -08:00
|
|
|
let doc = run(srv, doc);
|
2012-07-13 22:57:48 -07:00
|
|
|
assert doc.cratemod().mods()[0].fns()[0].path() == ~[~"a"];
|
2012-02-20 21:08:19 -08:00
|
|
|
}
|
2012-02-24 14:08:47 -08:00
|
|
|
}
|
|
|
|
|
2012-02-24 15:22:57 -08:00
|
|
|
#[test]
|
2012-06-26 16:18:37 -07:00
|
|
|
fn should_record_foreign_mod_paths() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let source = ~"mod a { extern mod b { } }";
|
2012-06-30 16:19:07 -07:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-24 15:22:57 -08:00
|
|
|
let doc = run(srv, doc);
|
2012-07-13 22:57:48 -07:00
|
|
|
assert doc.cratemod().mods()[0].nmods()[0].path() == ~[~"a"];
|
2012-02-24 15:22:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 14:08:47 -08:00
|
|
|
#[test]
|
2012-06-26 16:18:37 -07:00
|
|
|
fn should_record_foreign_fn_paths() {
|
2012-07-13 22:57:48 -07:00
|
|
|
let source = ~"extern mod a { fn b(); }";
|
2012-06-30 16:19:07 -07:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-13 22:57:48 -07:00
|
|
|
let doc = extract::from_srv(srv, ~"");
|
2012-02-24 14:08:47 -08:00
|
|
|
let doc = run(srv, doc);
|
2012-07-13 22:57:48 -07:00
|
|
|
assert doc.cratemod().nmods()[0].fns[0].path() == ~[~"a"];
|
2012-02-24 14:08:47 -08:00
|
|
|
}
|
2012-06-04 14:31:25 -07:00
|
|
|
}
|