rust/src/librustdoc/page_pass.rs

208 lines
4.7 KiB
Rust
Raw Normal View History

// 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.
/*!
Divides the document tree into pages.
Each page corresponds is a logical section. There may be pages for
individual modules, pages for the crate, indexes, etc.
*/
use core::prelude::*;
use astsrv;
use config;
2012-09-18 18:48:40 -05:00
use doc::{ItemUtils, PageUtils};
use doc;
use fold::Fold;
use fold;
use pass::Pass;
use sort_pass;
use util::NominalOp;
use util;
use core::oldcomm;
use core::option;
use core::vec;
use syntax::ast;
2012-11-19 20:00:12 -06:00
pub fn mk_pass(output_style: config::OutputStyle) -> Pass {
{
name: ~"page",
2012-11-19 20:48:46 -06:00
f: fn~(srv: astsrv::Srv, +doc: doc::Doc) -> doc::Doc {
run(srv, doc, output_style)
}
}
}
pub fn run(
2012-09-18 18:48:40 -05:00
_srv: astsrv::Srv,
2012-11-19 20:48:46 -06:00
+doc: doc::Doc,
2012-09-18 18:48:40 -05:00
output_style: config::OutputStyle
) -> doc::Doc {
2012-09-18 18:48:40 -05:00
if output_style == config::DocPerCrate {
2012-08-01 19:30:05 -05:00
return doc;
}
let (result_port, page_chan) = do util::spawn_conversation
2012-08-08 15:38:26 -05:00
|page_port, result_chan| {
2012-12-13 16:18:47 -06:00
oldcomm::send(result_chan, make_doc_from_pages(page_port));
};
find_pages(doc, page_chan);
2012-12-13 16:18:47 -06:00
oldcomm::recv(result_port)
}
2012-12-13 16:18:47 -06:00
type PagePort = oldcomm::Port<Option<doc::Page>>;
type PageChan = oldcomm::Chan<Option<doc::Page>>;
type NominalPageChan = NominalOp<PageChan>;
2012-09-18 18:48:40 -05:00
fn make_doc_from_pages(page_port: PagePort) -> doc::Doc {
let mut pages = ~[];
loop {
2012-12-13 16:18:47 -06:00
let val = oldcomm::recv(page_port);
2012-09-21 21:37:57 -05:00
if val.is_some() {
2012-09-19 00:43:54 -05:00
pages += ~[option::unwrap(move val)];
} else {
break;
}
}
2012-09-18 18:48:40 -05:00
doc::Doc_({
pages: pages
})
}
2012-09-18 18:48:40 -05:00
fn find_pages(doc: doc::Doc, page_chan: PageChan) {
let fold = Fold {
fold_crate: fold_crate,
fold_mod: fold_mod,
2012-09-04 15:29:32 -05:00
fold_nmod: fold_nmod,
.. fold::default_any_fold(NominalOp { op: page_chan })
};
(fold.fold_doc)(&fold, doc);
2012-12-13 16:18:47 -06:00
oldcomm::send(page_chan, None);
}
fn fold_crate(
fold: &fold::Fold<NominalPageChan>,
2012-11-19 20:48:46 -06:00
+doc: doc::CrateDoc
2012-09-18 18:48:40 -05:00
) -> doc::CrateDoc {
let doc = fold::default_seq_fold_crate(fold, doc);
2012-09-18 18:48:40 -05:00
let page = doc::CratePage({
2012-09-04 15:29:32 -05:00
topmod: strip_mod(doc.topmod),
.. doc
});
2012-12-13 16:18:47 -06:00
oldcomm::send(fold.ctxt.op, Some(page));
doc
}
fn fold_mod(
fold: &fold::Fold<NominalPageChan>,
2012-11-19 20:48:46 -06:00
+doc: doc::ModDoc
2012-09-18 18:48:40 -05:00
) -> doc::ModDoc {
let doc = fold::default_any_fold_mod(fold, doc);
if doc.id() != ast::crate_node_id {
let doc = strip_mod(doc);
2012-09-18 18:48:40 -05:00
let page = doc::ItemPage(doc::ModTag(doc));
2012-12-13 16:18:47 -06:00
oldcomm::send(fold.ctxt.op, Some(page));
}
doc
}
2012-09-18 18:48:40 -05:00
fn strip_mod(doc: doc::ModDoc) -> doc::ModDoc {
doc::ModDoc_({
2012-06-30 18:19:07 -05:00
items: do vec::filter(doc.items) |item| {
2012-09-28 00:20:47 -05:00
match *item {
2012-09-18 18:48:40 -05:00
doc::ModTag(_) => false,
doc::NmodTag(_) => false,
2012-08-03 21:59:04 -05:00
_ => true
}
2012-09-04 15:29:32 -05:00
},
.. *doc
})
}
fn fold_nmod(
fold: &fold::Fold<NominalPageChan>,
2012-11-19 20:48:46 -06:00
+doc: doc::NmodDoc
2012-09-18 18:48:40 -05:00
) -> doc::NmodDoc {
let doc = fold::default_seq_fold_nmod(fold, doc);
2012-09-18 18:48:40 -05:00
let page = doc::ItemPage(doc::NmodTag(doc));
2012-12-13 16:18:47 -06:00
oldcomm::send(fold.ctxt.op, Some(page));
2012-08-01 19:30:05 -05:00
return doc;
}
#[test]
fn should_not_split_the_doc_into_pages_for_doc_per_crate() {
let doc = test::mk_doc_(
2012-09-18 18:48:40 -05:00
config::DocPerCrate,
~"mod a { } mod b { mod c { } }"
);
assert doc.pages.len() == 1u;
}
#[test]
fn should_make_a_page_for_every_mod() {
let doc = test::mk_doc(~"mod a { }");
assert doc.pages.mods()[0].name() == ~"a";
}
#[test]
fn should_remove_mods_from_containing_mods() {
let doc = test::mk_doc(~"mod a { }");
assert vec::is_empty(doc.cratemod().mods());
}
#[test]
fn should_make_a_page_for_every_foreign_mod() {
let doc = test::mk_doc(~"extern mod a { }");
assert doc.pages.nmods()[0].name() == ~"a";
}
#[test]
fn should_remove_foreign_mods_from_containing_mods() {
let doc = test::mk_doc(~"extern mod a { }");
assert vec::is_empty(doc.cratemod().nmods());
}
#[cfg(test)]
mod test {
use astsrv;
use config;
use doc;
use extract;
use page_pass::run;
pub fn mk_doc_(
2012-09-18 18:48:40 -05:00
output_style: config::OutputStyle,
source: ~str
2012-09-18 18:48:40 -05:00
) -> doc::Doc {
2012-06-30 18:19:07 -05:00
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv, ~"");
run(srv, doc, output_style)
}
}
pub fn mk_doc(source: ~str) -> doc::Doc {
2012-09-18 18:48:40 -05:00
mk_doc_(config::DocPerMod, source)
}
}