Improve code for DocFragment rework

This commit is contained in:
Guillaume Gomez 2020-12-26 14:03:33 +01:00
parent 0ab6c90699
commit 4ba1928fa2
4 changed files with 18 additions and 19 deletions

View File

@ -486,6 +486,13 @@ fn get_word_attr(mut self, word: Symbol) -> (Option<ast::NestedMetaItem>, bool)
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 @@ fn update_need_backline(doc_strings: &mut Vec<DocFragment>, frag: &DocFragment)
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 @@ fn update_need_backline(doc_strings: &mut Vec<DocFragment>, frag: &DocFragment)
/// 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

View File

@ -314,10 +314,9 @@ fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
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),

View File

@ -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 cmp(
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\">\

View File

@ -235,12 +235,7 @@ fn fold_item(&mut self, i: clean::Item) -> Option<clean::Item> {
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,