From 0b9f2a82154a9a8df9691242a8be8a0a0bdbcd9e Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 25 Jan 2012 17:22:19 -0800 Subject: [PATCH] rustdoc: Extract variant signatures --- src/rustdoc/fold.rs | 3 ++- src/rustdoc/tystr_pass.rs | 46 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/rustdoc/fold.rs b/src/rustdoc/fold.rs index 943a317721a..cb3258660a1 100644 --- a/src/rustdoc/fold.rs +++ b/src/rustdoc/fold.rs @@ -97,7 +97,8 @@ fn default_seq_fold_mod( ~{ mods: fold.fold_modlist(fold, doc.mods), fns: fold.fold_fnlist(fold, doc.fns), - consts: fold.fold_constlist(fold, doc.consts) + consts: fold.fold_constlist(fold, doc.consts), + enums: fold.fold_enumlist(fold, doc.enums) with *doc } } diff --git a/src/rustdoc/tystr_pass.rs b/src/rustdoc/tystr_pass.rs index ba73e7d39c7..1595ec61ef5 100644 --- a/src/rustdoc/tystr_pass.rs +++ b/src/rustdoc/tystr_pass.rs @@ -17,7 +17,8 @@ fn run( ) -> doc::cratedoc { let fold = fold::fold({ fold_fn: fold_fn, - fold_const: fold_const + fold_const: fold_const, + fold_enum: fold_enum with *fold::default_seq_fold(srv) }); fold.fold_crate(fold, doc) @@ -179,4 +180,45 @@ fn should_add_const_types() { let doc = extract::from_srv(srv, ""); let doc = run(srv, doc); assert doc.topmod.consts[0].ty == some("bool"); -} \ No newline at end of file +} + +fn fold_enum( + fold: fold::fold, + doc: doc::enumdoc +) -> doc::enumdoc { + let srv = fold.ctxt; + + ~{ + variants: vec::map(doc.variants) {|variant| + let sig = astsrv::exec(srv) {|ctxt| + alt ctxt.map.get(doc.id) { + ast_map::node_item(@{ + node: ast::item_enum(ast_variants, _), _ + }) { + let ast_variant = option::get( + vec::find(ast_variants) {|v| + v.node.name == variant.name + }); + + pprust::variant_to_str(ast_variant) + } + } + }; + + ~{ + sig: some(sig) + with *variant + } + } + with *doc + } +} + +#[test] +fn should_add_variant_sigs() { + let source = "enum a { b(int) }"; + let srv = astsrv::mk_srv_from_str(source); + let doc = extract::from_srv(srv, ""); + let doc = run(srv, doc); + assert doc.topmod.enums[0].variants[0].sig == some("b(int)"); +}