2012-12-03 18:48:01 -06: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 16:53:12 -05:00
|
|
|
//! Generic pass for performing an operation on all descriptions
|
2012-01-24 23:16:01 -06:00
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use astsrv;
|
2012-09-18 18:48:40 -05:00
|
|
|
use doc::ItemUtils;
|
2012-12-23 16:41:37 -06:00
|
|
|
use doc;
|
2012-12-05 17:06:54 -06:00
|
|
|
use fold::Fold;
|
2012-12-23 16:41:37 -06:00
|
|
|
use fold;
|
2013-01-08 21:37:25 -06:00
|
|
|
use pass::Pass;
|
2012-11-26 20:03:36 -06:00
|
|
|
use util::NominalOp;
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2013-02-25 15:23:16 -06:00
|
|
|
use core::cell::Cell;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
pub fn mk_pass(name: ~str, op: @fn(&str) -> ~str) -> Pass {
|
2013-06-04 05:03:58 -05:00
|
|
|
let op = Cell::new(op);
|
2013-01-08 16:00:45 -06:00
|
|
|
Pass {
|
2013-01-30 15:14:35 -06:00
|
|
|
name: copy name,
|
2013-01-31 19:12:29 -06:00
|
|
|
f: |srv: astsrv::Srv, doc: doc::Doc| -> doc::Doc {
|
2013-01-30 20:52:31 -06:00
|
|
|
run(srv, doc, op.take())
|
2012-02-27 20:07:16 -06:00
|
|
|
}
|
2012-01-24 23:16:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
type Op = @fn(&str) -> ~str;
|
2012-01-24 23:16:01 -06:00
|
|
|
|
2012-08-01 15:35:33 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-01-24 23:16:01 -06:00
|
|
|
fn run(
|
2012-09-18 18:48:40 -05:00
|
|
|
_srv: astsrv::Srv,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::Doc,
|
|
|
|
op: Op
|
2012-09-18 18:48:40 -05:00
|
|
|
) -> doc::Doc {
|
2012-11-26 20:03:36 -06:00
|
|
|
let op = NominalOp {
|
2013-02-15 02:37:08 -06:00
|
|
|
op: op
|
2012-11-26 20:03:36 -06:00
|
|
|
};
|
2012-12-05 17:06:54 -06:00
|
|
|
let fold = Fold {
|
2012-02-17 17:59:57 -06:00
|
|
|
fold_item: fold_item,
|
2012-01-27 00:27:13 -06:00
|
|
|
fold_enum: fold_enum,
|
2012-07-03 18:30:42 -05:00
|
|
|
fold_trait: fold_trait,
|
2012-09-04 15:29:32 -05:00
|
|
|
fold_impl: fold_impl,
|
2013-02-15 02:37:08 -06:00
|
|
|
.. fold::default_any_fold(op)
|
2012-12-05 17:06:54 -06:00
|
|
|
};
|
2012-11-29 19:51:16 -06:00
|
|
|
(fold.fold_doc)(&fold, doc)
|
2012-01-24 23:16:01 -06:00
|
|
|
}
|
|
|
|
|
2013-01-30 21:32:36 -06:00
|
|
|
fn maybe_apply_op(op: NominalOp<Op>, s: &Option<~str>) -> Option<~str> {
|
2013-01-30 20:52:31 -06:00
|
|
|
s.map(|s| (op.op)(*s) )
|
2012-01-24 23:16:01 -06:00
|
|
|
}
|
|
|
|
|
2012-11-26 20:03:36 -06:00
|
|
|
fn fold_item(
|
|
|
|
fold: &fold::Fold<NominalOp<Op>>,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::ItemDoc
|
2012-11-26 20:03:36 -06:00
|
|
|
) -> doc::ItemDoc {
|
2012-02-17 17:59:57 -06:00
|
|
|
let doc = fold::default_seq_fold_item(fold, doc);
|
2012-01-24 23:16:01 -06:00
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
doc::ItemDoc {
|
2013-01-30 20:52:31 -06:00
|
|
|
brief: maybe_apply_op(copy fold.ctxt, &doc.brief),
|
|
|
|
desc: maybe_apply_op(copy fold.ctxt, &doc.desc),
|
|
|
|
sections: apply_to_sections(copy fold.ctxt, copy doc.sections),
|
2012-09-04 15:29:32 -05:00
|
|
|
.. doc
|
2012-01-24 23:16:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 20:03:36 -06:00
|
|
|
fn apply_to_sections(
|
2013-02-01 20:05:10 -06:00
|
|
|
op: NominalOp<Op>,
|
2013-01-30 21:32:36 -06:00
|
|
|
sections: ~[doc::Section]
|
2012-11-26 20:03:36 -06:00
|
|
|
) -> ~[doc::Section] {
|
2013-02-16 16:54:34 -06:00
|
|
|
sections.map(|section| doc::Section {
|
2013-01-30 15:14:35 -06:00
|
|
|
header: (op.op)(copy section.header),
|
|
|
|
body: (op.op)(copy section.body)
|
2012-06-26 15:55:56 -05:00
|
|
|
})
|
2012-03-09 13:47:31 -06:00
|
|
|
}
|
|
|
|
|
2012-11-26 20:03:36 -06:00
|
|
|
fn fold_enum(
|
|
|
|
fold: &fold::Fold<NominalOp<Op>>,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::EnumDoc) -> doc::EnumDoc {
|
2012-02-17 17:59:57 -06:00
|
|
|
let doc = fold::default_seq_fold_enum(fold, doc);
|
2012-11-19 20:48:46 -06:00
|
|
|
let fold_copy = copy *fold;
|
2012-02-17 17:59:57 -06:00
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
doc::EnumDoc {
|
2013-02-16 16:54:34 -06:00
|
|
|
variants: do doc.variants.map |variant| {
|
2013-01-25 18:57:39 -06:00
|
|
|
doc::VariantDoc {
|
2013-01-30 20:52:31 -06:00
|
|
|
desc: maybe_apply_op(copy fold_copy.ctxt, &variant.desc),
|
2013-01-30 15:14:35 -06:00
|
|
|
.. copy *variant
|
2012-01-25 20:51:46 -06:00
|
|
|
}
|
2012-09-04 15:29:32 -05:00
|
|
|
},
|
|
|
|
.. doc
|
2012-01-25 20:51:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 20:03:36 -06:00
|
|
|
fn fold_trait(
|
|
|
|
fold: &fold::Fold<NominalOp<Op>>,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::TraitDoc
|
2012-11-26 20:03:36 -06:00
|
|
|
) -> doc::TraitDoc {
|
2012-07-03 18:30:42 -05:00
|
|
|
let doc = fold::default_seq_fold_trait(fold, doc);
|
2012-02-17 17:59:57 -06:00
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
doc::TraitDoc {
|
2013-01-30 20:52:31 -06:00
|
|
|
methods: apply_to_methods(copy fold.ctxt, copy doc.methods),
|
2012-09-04 15:29:32 -05:00
|
|
|
.. doc
|
2012-01-31 22:42:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 20:03:36 -06:00
|
|
|
fn apply_to_methods(
|
2013-01-30 21:32:36 -06:00
|
|
|
op: NominalOp<Op>,
|
|
|
|
docs: ~[doc::MethodDoc]
|
2012-11-26 20:03:36 -06:00
|
|
|
) -> ~[doc::MethodDoc] {
|
2013-01-10 12:59:58 -06:00
|
|
|
let op = copy op;
|
2013-01-31 19:12:29 -06:00
|
|
|
do docs.map |doc| {
|
2013-01-25 18:57:39 -06:00
|
|
|
doc::MethodDoc {
|
2013-01-30 20:52:31 -06:00
|
|
|
brief: maybe_apply_op(copy op, &doc.brief),
|
|
|
|
desc: maybe_apply_op(copy op, &doc.desc),
|
|
|
|
sections: apply_to_sections(copy op, copy doc.sections),
|
2013-01-30 15:14:35 -06:00
|
|
|
.. copy *doc
|
2012-01-30 21:36:58 -06:00
|
|
|
}
|
2012-01-31 22:42:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 20:03:36 -06:00
|
|
|
fn fold_impl(
|
|
|
|
fold: &fold::Fold<NominalOp<Op>>,
|
2013-01-30 21:32:36 -06:00
|
|
|
doc: doc::ImplDoc
|
2012-11-26 20:03:36 -06:00
|
|
|
) -> doc::ImplDoc {
|
2012-02-17 17:59:57 -06:00
|
|
|
let doc = fold::default_seq_fold_impl(fold, doc);
|
2012-01-30 21:36:58 -06:00
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
doc::ImplDoc {
|
2013-01-30 20:52:31 -06:00
|
|
|
methods: apply_to_methods(copy fold.ctxt, copy doc.methods),
|
2012-09-04 15:29:32 -05:00
|
|
|
.. doc
|
2012-02-02 00:41:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2013-05-22 21:59:22 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
use astsrv;
|
|
|
|
use attr_pass;
|
|
|
|
use desc_to_brief_pass;
|
|
|
|
use doc;
|
|
|
|
use extract;
|
|
|
|
use sectionalize_pass;
|
|
|
|
use text_pass::mk_pass;
|
2012-01-25 20:51:46 -06:00
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use core::str;
|
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
fn mk_doc(source: ~str) -> doc::Doc {
|
|
|
|
do astsrv::from_str(copy source) |srv| {
|
|
|
|
let doc = extract::from_srv(srv.clone(), ~"");
|
|
|
|
let doc = (attr_pass::mk_pass().f)(srv.clone(), doc);
|
|
|
|
let doc = (desc_to_brief_pass::mk_pass().f)(srv.clone(), doc);
|
|
|
|
let doc = (sectionalize_pass::mk_pass().f)(srv.clone(), doc);
|
2013-06-10 06:03:16 -05:00
|
|
|
(mk_pass(~"", |s| s.trim().to_owned() ).f)(srv.clone(), doc)
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-27 00:27:13 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_enum_brief() {
|
|
|
|
let doc = mk_doc(~"#[doc = \" a \"] enum a { b }");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().enums()[0].brief(), Some(~"a"));
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
2012-01-30 21:36:58 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_enum_desc() {
|
|
|
|
let doc = mk_doc(~"#[doc = \" a \"] enum a { b }");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().enums()[0].desc(), Some(~"a"));
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
2012-01-30 21:36:58 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_variant_desc() {
|
|
|
|
let doc = mk_doc(~"enum a { #[doc = \" a \"] b }");
|
|
|
|
assert!(doc.cratemod().enums()[0].variants[0].desc == Some(~"a"));
|
|
|
|
}
|
2012-01-30 21:36:58 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_trait_brief() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \" a \"] trait i { fn a(); }");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().traits()[0].brief(), Some(~"a"));
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
2012-01-30 21:36:58 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_trait_desc() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \" a \"] trait i { fn a(); }");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().traits()[0].desc(), Some(~"a"));
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
2012-01-31 22:42:06 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_trait_method_brief() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"trait i { #[doc = \" a \"] fn a(); }");
|
|
|
|
assert!(doc.cratemod().traits()[0].methods[0].brief == Some(~"a"));
|
|
|
|
}
|
2012-01-31 22:42:06 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_trait_method_desc() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"trait i { #[doc = \" a \"] fn a(); }");
|
|
|
|
assert!(doc.cratemod().traits()[0].methods[0].desc == Some(~"a"));
|
|
|
|
}
|
2012-01-31 22:42:06 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_impl_brief() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \" a \"] impl int { fn a() { } }");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().impls()[0].brief(), Some(~"a"));
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
2012-01-31 22:42:06 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_impl_desc() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \" a \"] impl int { fn a() { } }");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().impls()[0].desc(), Some(~"a"));
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
2012-02-02 00:41:41 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_impl_method_brief() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"impl int { #[doc = \" a \"] fn a() { } }");
|
|
|
|
assert!(doc.cratemod().impls()[0].methods[0].brief == Some(~"a"));
|
|
|
|
}
|
2012-02-02 00:41:41 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_impl_method_desc() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"impl int { #[doc = \" a \"] fn a() { } }");
|
|
|
|
assert!(doc.cratemod().impls()[0].methods[0].desc == Some(~"a"));
|
|
|
|
}
|
2012-03-09 13:47:31 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_type_brief() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \" a \"] type t = int;");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().types()[0].brief(), Some(~"a"));
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
2012-03-09 13:47:31 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_op_on_type_desc() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \" a \"] type t = int;");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(doc.cratemod().types()[0].desc(), Some(~"a"));
|
2013-04-15 10:09:55 -05:00
|
|
|
}
|
2012-03-09 13:47:31 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_on_item_section_headers() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \"\
|
|
|
|
# Header \n\
|
|
|
|
Body\"]\
|
|
|
|
fn a() { }");
|
|
|
|
assert!(doc.cratemod().fns()[0].sections()[0].header == ~"Header");
|
|
|
|
}
|
2012-03-09 13:47:31 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_on_item_section_bodies() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body \"]\
|
|
|
|
fn a() { }");
|
|
|
|
assert!(doc.cratemod().fns()[0].sections()[0].body == ~"Body");
|
|
|
|
}
|
2012-03-09 13:47:31 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_on_trait_method_section_headers() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"trait i {
|
|
|
|
#[doc = \"\
|
|
|
|
# Header \n\
|
|
|
|
Body\"]\
|
|
|
|
fn a(); }");
|
|
|
|
assert!(doc.cratemod().traits()[0].methods[0].sections[0].header
|
|
|
|
== ~"Header");
|
|
|
|
}
|
2012-03-09 13:47:31 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_on_trait_method_section_bodies() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"trait i {
|
|
|
|
#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body \"]\
|
|
|
|
fn a(); }");
|
|
|
|
assert!(doc.cratemod().traits()[0].methods[0].sections[0].body ==
|
|
|
|
~"Body");
|
|
|
|
}
|
2012-12-29 19:38:20 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_on_impl_method_section_headers() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"impl bool {
|
|
|
|
#[doc = \"\
|
|
|
|
# Header \n\
|
|
|
|
Body\"]\
|
|
|
|
fn a() { } }");
|
|
|
|
assert!(doc.cratemod().impls()[0].methods[0].sections[0].header
|
|
|
|
== ~"Header");
|
|
|
|
}
|
2012-12-29 19:38:20 -06:00
|
|
|
|
2013-04-15 10:09:55 -05:00
|
|
|
#[test]
|
|
|
|
fn should_execute_on_impl_method_section_bodies() {
|
|
|
|
let doc = mk_doc(
|
|
|
|
~"impl bool {
|
|
|
|
#[doc = \"\
|
|
|
|
# Header\n\
|
|
|
|
Body \"]\
|
|
|
|
fn a() { } }");
|
|
|
|
assert!(doc.cratemod().impls()[0].methods[0].sections[0].body ==
|
|
|
|
~"Body");
|
2012-01-31 20:32:37 -06:00
|
|
|
}
|
2012-05-24 16:49:39 -05:00
|
|
|
}
|