From a5e0f037be17204c640e2941e576a9dca09efc90 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 18 Jan 2012 23:48:25 -0800 Subject: [PATCH] rustdoc: Add path_pass for recording full paths to mods --- src/rustdoc/path_pass.rs | 49 ++++++++++++++++++++++++++++++++++++++++ src/rustdoc/rustdoc.rc | 1 + src/rustdoc/rustdoc.rs | 1 + 3 files changed, 51 insertions(+) create mode 100644 src/rustdoc/path_pass.rs diff --git a/src/rustdoc/path_pass.rs b/src/rustdoc/path_pass.rs new file mode 100644 index 00000000000..9bcdde561c4 --- /dev/null +++ b/src/rustdoc/path_pass.rs @@ -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, + 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, 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"]; +} \ No newline at end of file diff --git a/src/rustdoc/rustdoc.rc b/src/rustdoc/rustdoc.rc index 7b76664f934..12a8733a9b2 100644 --- a/src/rustdoc/rustdoc.rc +++ b/src/rustdoc/rustdoc.rc @@ -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; diff --git a/src/rustdoc/rustdoc.rs b/src/rustdoc/rustdoc.rs index 71888880f2f..b1314ecb39b 100755 --- a/src/rustdoc/rustdoc.rs +++ b/src/rustdoc/rustdoc.rs @@ -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(),