diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2020-12-26 14:03:33 +0100 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2021-01-02 19:16:27 +0100 |
| commit | 4ba1928fa2aa73abeb541dad7d72bda94509d113 (patch) | |
| tree | c7b58d8fcacc6e4bcfba45cab0b4e1704a63869c /src | |
| parent | 0ab6c90699e5fc70ee0a9bfe369bc356f05127f9 (diff) | |
| download | rust-4ba1928fa2aa73abeb541dad7d72bda94509d113.tar.gz rust-4ba1928fa2aa73abeb541dad7d72bda94509d113.zip | |
Improve code for DocFragment rework
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustdoc/clean/types.rs | 15 | ||||
| -rw-r--r-- | src/librustdoc/formats/cache.rs | 7 | ||||
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 8 | ||||
| -rw-r--r-- | src/librustdoc/passes/calculate_doc_coverage.rs | 7 |
4 files changed, 18 insertions, 19 deletions
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 48d720fa77f..8d5ef1ed28c 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -486,6 +486,13 @@ crate enum DocFragmentKind { Include { filename: Symbol }, } +// The goal of this function is to apply the `DocFragment` transformations that are required when +// transforming into the final markdown. So the transformations in here are: +// +// * Applying the computed indent to each lines in each doc fragment (a `DocFragment` can contain +// multiple lines in case of `#[doc = ""]`). +// * Adding backlines between `DocFragment`s and adding an extra one if required (stored in the +// `need_backline` field). fn add_doc_fragment(out: &mut String, frag: &DocFragment) { let s = frag.doc.as_str(); let mut iter = s.lines().peekable(); @@ -792,11 +799,14 @@ impl Attributes { if out.is_empty() { None } else { Some(out) } } + /// Return the doc-comments on this item, grouped by the module they came from. + /// + /// The module can be different if this is a re-export with added documentation. crate fn collapsed_doc_value_by_module_level(&self) -> FxHashMap<Option<DefId>, String> { let mut ret = FxHashMap::default(); for new_frag in self.doc_strings.iter() { - let out = ret.entry(new_frag.parent_module).or_insert_with(|| String::new()); + let out = ret.entry(new_frag.parent_module).or_default(); add_doc_fragment(out, &new_frag); } ret @@ -805,8 +815,7 @@ impl Attributes { /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined /// with newlines. crate fn collapsed_doc_value(&self) -> Option<String> { - let s: String = self.doc_strings.iter().collect(); - if s.is_empty() { None } else { Some(s) } + if self.doc_strings.is_empty() { None } else { Some(self.doc_strings.iter().collect()) } } /// Gets links as a vector diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index df892d15b7b..c1f8b12cac4 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -314,10 +314,9 @@ impl DocFolder for Cache { ty: item.type_(), name: s.to_string(), path: path.join("::"), - desc: item.doc_value().map_or_else( - || String::new(), - |x| short_markdown_summary(&x.as_str()), - ), + desc: item + .doc_value() + .map_or_else(String::new, |x| short_markdown_summary(&x.as_str())), parent, parent_idx: None, search_type: get_index_search_type(&item), diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 4339fa077e5..1c713df9276 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -198,11 +198,7 @@ impl SharedContext<'_> { /// Based on whether the `collapse-docs` pass was run, return either the `doc_value` or the /// `collapsed_doc_value` of the given item. crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<String> { - if self.collapsed { - item.collapsed_doc_value().map(|s| s.to_string()) - } else { - item.doc_value().map(|s| s.to_string()) - } + if self.collapsed { item.collapsed_doc_value() } else { item.doc_value() } } } @@ -2196,7 +2192,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl let stab = myitem.stability_class(cx.tcx()); let add = if stab.is_some() { " " } else { "" }; - let doc_value = myitem.doc_value().unwrap_or_else(String::new); + let doc_value = myitem.doc_value().unwrap_or_default(); write!( w, "<tr class=\"{stab}{add}module-item\">\ diff --git a/src/librustdoc/passes/calculate_doc_coverage.rs b/src/librustdoc/passes/calculate_doc_coverage.rs index c8c72545669..05a3a15adac 100644 --- a/src/librustdoc/passes/calculate_doc_coverage.rs +++ b/src/librustdoc/passes/calculate_doc_coverage.rs @@ -235,12 +235,7 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> { let mut tests = Tests { found_tests: 0 }; find_testable_code( - &i.attrs - .doc_strings - .iter() - .map(|d| d.doc.to_string()) - .collect::<Vec<_>>() - .join("\n"), + &i.attrs.collapsed_doc_value().unwrap_or_default(), &mut tests, ErrorCodes::No, false, |
