]>
Commit | Line | Data |
---|---|---|
29967ef6 | 1 | use crate::clean::{self, DocFragment, DocFragmentKind, Item}; |
532ac7d7 | 2 | use crate::core::DocContext; |
9fa01778 | 3 | use crate::fold; |
60c5eb7d | 4 | use crate::fold::DocFolder; |
9fa01778 XL |
5 | use crate::passes::Pass; |
6 | ||
416331ca | 7 | use std::mem::take; |
ff7c6d11 | 8 | |
fc512014 | 9 | crate const COLLAPSE_DOCS: Pass = Pass { |
532ac7d7 | 10 | name: "collapse-docs", |
60c5eb7d | 11 | run: collapse_docs, |
532ac7d7 XL |
12 | description: "concatenates all document attributes into one document attribute", |
13 | }; | |
b7449926 | 14 | |
fc512014 | 15 | crate fn collapse_docs(krate: clean::Crate, _: &DocContext<'_>) -> clean::Crate { |
416331ca XL |
16 | let mut krate = Collapser.fold_crate(krate); |
17 | krate.collapsed = true; | |
18 | krate | |
9e0c209e SL |
19 | } |
20 | ||
21 | struct Collapser; | |
22 | ||
23 | impl fold::DocFolder for Collapser { | |
24 | fn fold_item(&mut self, mut i: Item) -> Option<Item> { | |
476ff2be | 25 | i.attrs.collapse_doc_comments(); |
fc512014 | 26 | Some(self.fold_item_recur(i)) |
9e0c209e SL |
27 | } |
28 | } | |
476ff2be | 29 | |
ff7c6d11 XL |
30 | fn collapse(doc_strings: &mut Vec<DocFragment>) { |
31 | let mut docs = vec![]; | |
32 | let mut last_frag: Option<DocFragment> = None; | |
33 | ||
416331ca | 34 | for frag in take(doc_strings) { |
ff7c6d11 | 35 | if let Some(mut curr_frag) = last_frag.take() { |
29967ef6 XL |
36 | let curr_kind = &curr_frag.kind; |
37 | let new_kind = &frag.kind; | |
ff7c6d11 | 38 | |
29967ef6 XL |
39 | if matches!(*curr_kind, DocFragmentKind::Include { .. }) |
40 | || curr_kind != new_kind | |
41 | || curr_frag.parent_module != frag.parent_module | |
42 | { | |
43 | if *curr_kind == DocFragmentKind::SugaredDoc | |
44 | || *curr_kind == DocFragmentKind::RawDoc | |
45 | { | |
46 | // add a newline for extra padding between segments | |
47 | curr_frag.doc.push('\n'); | |
ff7c6d11 XL |
48 | } |
49 | docs.push(curr_frag); | |
50 | last_frag = Some(frag); | |
51 | } else { | |
29967ef6 XL |
52 | curr_frag.doc.push('\n'); |
53 | curr_frag.doc.push_str(&frag.doc); | |
54 | curr_frag.span = curr_frag.span.to(frag.span); | |
ff7c6d11 XL |
55 | last_frag = Some(curr_frag); |
56 | } | |
476ff2be | 57 | } else { |
ff7c6d11 | 58 | last_frag = Some(frag); |
476ff2be SL |
59 | } |
60 | } | |
ff7c6d11 XL |
61 | |
62 | if let Some(frag) = last_frag.take() { | |
63 | docs.push(frag); | |
64 | } | |
65 | *doc_strings = docs; | |
66 | } | |
67 | ||
68 | impl clean::Attributes { | |
fc512014 | 69 | crate fn collapse_doc_comments(&mut self) { |
ff7c6d11 XL |
70 | collapse(&mut self.doc_strings); |
71 | } | |
476ff2be | 72 | } |