1 use crate::clean::{self, DocFragment, Item};
2 use crate::core::DocContext;
4 use crate::fold::{DocFolder};
5 use crate::passes::Pass;
9 pub const COLLAPSE_DOCS: Pass = Pass {
10 name: "collapse-docs",
12 description: "concatenates all document attributes into one document attribute",
15 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
16 enum DocFragmentKind {
23 fn kind(&self) -> DocFragmentKind {
25 DocFragment::SugaredDoc(..) => DocFragmentKind::Sugared,
26 DocFragment::RawDoc(..) => DocFragmentKind::Raw,
27 DocFragment::Include(..) => DocFragmentKind::Include,
32 pub fn collapse_docs(krate: clean::Crate, _: &DocContext<'_>) -> clean::Crate {
33 Collapser.fold_crate(krate)
38 impl fold::DocFolder for Collapser {
39 fn fold_item(&mut self, mut i: Item) -> Option<Item> {
40 i.attrs.collapse_doc_comments();
41 self.fold_item_recur(i)
45 fn collapse(doc_strings: &mut Vec<DocFragment>) {
46 let mut docs = vec![];
47 let mut last_frag: Option<DocFragment> = None;
49 for frag in replace(doc_strings, vec![]) {
50 if let Some(mut curr_frag) = last_frag.take() {
51 let curr_kind = curr_frag.kind();
52 let new_kind = frag.kind();
54 if curr_kind == DocFragmentKind::Include || curr_kind != new_kind {
56 DocFragment::SugaredDoc(_, _, ref mut doc_string)
57 | DocFragment::RawDoc(_, _, ref mut doc_string) => {
58 // add a newline for extra padding between segments
59 doc_string.push('\n');
64 last_frag = Some(frag);
67 DocFragment::SugaredDoc(_, ref mut span, ref mut doc_string)
68 | DocFragment::RawDoc(_, ref mut span, ref mut doc_string) => {
69 doc_string.push('\n');
70 doc_string.push_str(frag.as_str());
71 *span = span.to(frag.span());
75 last_frag = Some(curr_frag);
78 last_frag = Some(frag);
82 if let Some(frag) = last_frag.take() {
88 impl clean::Attributes {
89 pub fn collapse_doc_comments(&mut self) {
90 collapse(&mut self.doc_strings);