diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2021-04-28 21:19:52 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2021-05-02 14:17:07 +0200 |
| commit | dfde867fcd4d29213dbdaf0b948ed00fe82a43e5 (patch) | |
| tree | a7ed595c1cd3ce66670e0837d709da68f598f361 /src/librustdoc/html/render/mod.rs | |
| parent | bcd696d722c04a0f8c34d884aa4ed2322f55cdd8 (diff) | |
| download | rust-dfde867fcd4d29213dbdaf0b948ed00fe82a43e5.tar.gz rust-dfde867fcd4d29213dbdaf0b948ed00fe82a43e5.zip | |
Migrate trait and impl blocks' toggles into
Diffstat (limited to 'src/librustdoc/html/render/mod.rs')
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 165 |
1 files changed, 78 insertions, 87 deletions
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 7de72d81987..dcb8635ade6 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -508,23 +508,16 @@ fn document(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, parent: Option if let Some(ref name) = item.name { info!("Documenting {}", name); } - document_item_info(w, cx, item, false, parent); - document_full(w, item, cx, false); + document_item_info(w, cx, item, parent); + document_full(w, item, cx); } /// Render md_text as markdown. -fn render_markdown( - w: &mut Buffer, - cx: &Context<'_>, - md_text: &str, - links: Vec<RenderedLink>, - is_hidden: bool, -) { +fn render_markdown(w: &mut Buffer, cx: &Context<'_>, md_text: &str, links: Vec<RenderedLink>) { let mut ids = cx.id_map.borrow_mut(); write!( w, - "<div class=\"docblock{}\">{}</div>", - if is_hidden { " hidden" } else { "" }, + "<div class=\"docblock\">{}</div>", Markdown( md_text, &links, @@ -544,11 +537,10 @@ fn document_short( item: &clean::Item, cx: &Context<'_>, link: AssocItemLink<'_>, - is_hidden: bool, parent: &clean::Item, show_def_docs: bool, ) { - document_item_info(w, cx, item, is_hidden, Some(parent)); + document_item_info(w, cx, item, Some(parent)); if !show_def_docs { return; } @@ -565,19 +557,14 @@ fn document_short( } } - write!( - w, - "<div class='docblock{}'>{}</div>", - if is_hidden { " hidden" } else { "" }, - summary_html, - ); + write!(w, "<div class='docblock'>{}</div>", summary_html,); } } -fn document_full(w: &mut Buffer, item: &clean::Item, cx: &Context<'_>, is_hidden: bool) { +fn document_full(w: &mut Buffer, item: &clean::Item, cx: &Context<'_>) { if let Some(s) = cx.shared.maybe_collapsed_doc_value(item) { debug!("Doc block: =====\n{}\n=====", s); - render_markdown(w, cx, &s, item.links(cx), is_hidden); + render_markdown(w, cx, &s, item.links(cx)); } } @@ -590,16 +577,11 @@ fn document_item_info( w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, - is_hidden: bool, parent: Option<&clean::Item>, ) { let item_infos = short_item_info(item, cx, parent); if !item_infos.is_empty() { - if is_hidden { - w.write_str("<div class=\"item-info hidden\">"); - } else { - w.write_str("<div class=\"item-info\">"); - } + w.write_str("<div class=\"item-info\">"); for info in item_infos { w.write_str(&info); } @@ -1281,8 +1263,12 @@ fn render_impl( let trait_ = i.trait_did_full(cache).map(|did| &traits[&did]); let mut close_tags = String::new(); + // For trait implementations, the `interesting` output contains all methods that have doc + // comments, and the `boring` output contains all methods that do not. The distinction is + // used to allow hiding the boring methods. fn doc_impl_item( - w: &mut Buffer, + boring: &mut Buffer, + interesting: &mut Buffer, cx: &Context<'_>, item: &clean::Item, parent: &clean::Item, @@ -1305,15 +1291,46 @@ fn render_impl( } }; - let (is_hidden, extra_class) = - if (trait_.is_none() || item.doc_value().is_some() || item.kind.is_type_alias()) - && !is_default_item - { - (false, "") - } else { - (true, " hidden") - }; let in_trait_class = if trait_.is_some() { " trait-impl" } else { "" }; + + let mut doc_buffer = Buffer::empty_from(boring); + let mut info_buffer = Buffer::empty_from(boring); + let mut short_documented = true; + + if render_method_item { + if !is_default_item { + if let Some(t) = trait_ { + // The trait item may have been stripped so we might not + // find any documentation or stability for it. + if let Some(it) = t.items.iter().find(|i| i.name == item.name) { + // We need the stability of the item from the trait + // because impls can't have a stability. + if item.doc_value().is_some() { + document_item_info(&mut info_buffer, cx, it, Some(parent)); + document_full(&mut doc_buffer, item, cx); + short_documented = false; + } else { + // In case the item isn't documented, + // provide short documentation from the trait. + document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs); + } + } + } else { + document_item_info(&mut info_buffer, cx, item, Some(parent)); + if show_def_docs { + document_full(&mut doc_buffer, item, cx); + short_documented = false; + } + } + } else { + document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs); + } + } + let w = if short_documented && trait_.is_some() { interesting } else { boring }; + + if !doc_buffer.is_empty() { + w.write_str("<details class=\"rustdoc-toggle\" open><summary>"); + } match *item.kind { clean::MethodItem(..) | clean::TyMethodItem(_) => { // Only render when the method is not static or we allow static methods @@ -1326,11 +1343,7 @@ fn render_impl( }) }) .map(|item| format!("{}.{}", item.type_(), name)); - write!( - w, - "<h4 id=\"{}\" class=\"{}{}{}\">", - id, item_type, extra_class, in_trait_class, - ); + write!(w, "<h4 id=\"{}\" class=\"{}{}\">", id, item_type, in_trait_class,); w.write_str("<code>"); render_assoc_item( w, @@ -1355,11 +1368,7 @@ fn render_impl( clean::TypedefItem(ref tydef, _) => { let source_id = format!("{}.{}", ItemType::AssocType, name); let id = cx.derive_id(source_id.clone()); - write!( - w, - "<h4 id=\"{}\" class=\"{}{}{}\"><code>", - id, item_type, extra_class, in_trait_class - ); + write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, in_trait_class); assoc_type( w, item, @@ -1376,11 +1385,7 @@ fn render_impl( clean::AssocConstItem(ref ty, ref default) => { let source_id = format!("{}.{}", item_type, name); let id = cx.derive_id(source_id.clone()); - write!( - w, - "<h4 id=\"{}\" class=\"{}{}{}\"><code>", - id, item_type, extra_class, in_trait_class - ); + write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, in_trait_class); assoc_const( w, item, @@ -1405,11 +1410,7 @@ fn render_impl( clean::AssocTypeItem(ref bounds, ref default) => { let source_id = format!("{}.{}", item_type, name); let id = cx.derive_id(source_id.clone()); - write!( - w, - "<h4 id=\"{}\" class=\"{}{}{}\"><code>", - id, item_type, extra_class, in_trait_class - ); + write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, in_trait_class); assoc_type( w, item, @@ -1427,38 +1428,20 @@ fn render_impl( _ => panic!("can't make docs for trait item with name {:?}", item.name), } - if render_method_item { - if !is_default_item { - if let Some(t) = trait_ { - // The trait item may have been stripped so we might not - // find any documentation or stability for it. - if let Some(it) = t.items.iter().find(|i| i.name == item.name) { - // We need the stability of the item from the trait - // because impls can't have a stability. - if item.doc_value().is_some() { - document_item_info(w, cx, it, is_hidden, Some(parent)); - document_full(w, item, cx, is_hidden); - } else { - // In case the item isn't documented, - // provide short documentation from the trait. - document_short(w, it, cx, link, is_hidden, parent, show_def_docs); - } - } - } else { - document_item_info(w, cx, item, is_hidden, Some(parent)); - if show_def_docs { - document_full(w, item, cx, is_hidden); - } - } - } else { - document_short(w, item, cx, link, is_hidden, parent, show_def_docs); - } + w.push_buffer(info_buffer); + if !doc_buffer.is_empty() { + w.write_str("</summary>"); + w.push_buffer(doc_buffer); + w.push_str("</details>"); } } let mut impl_items = Buffer::empty_from(w); + let mut default_impl_items = Buffer::empty_from(w); + for trait_item in &i.inner_impl().items { doc_impl_item( + &mut default_impl_items, &mut impl_items, cx, trait_item, @@ -1475,6 +1458,7 @@ fn render_impl( fn render_default_items( w: &mut Buffer, + tmp_w: &mut Buffer, cx: &Context<'_>, t: &clean::Trait, i: &clean::Impl, @@ -1494,6 +1478,7 @@ fn render_impl( doc_impl_item( w, + tmp_w, cx, trait_item, parent, @@ -1515,6 +1500,7 @@ fn render_impl( if show_default_items { if let Some(t) = trait_ { render_default_items( + &mut default_impl_items, &mut impl_items, cx, &t.trait_, @@ -1527,7 +1513,7 @@ fn render_impl( ); } } - let details_str = if impl_items.is_empty() { + let details_str = if impl_items.is_empty() && default_impl_items.is_empty() { "" } else { "<details class=\"rustdoc-toggle implementors-toggle\" open><summary>" @@ -1554,7 +1540,7 @@ fn render_impl( "{}<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">", details_str, id, aliases ); - if !impl_items.is_empty() { + if !impl_items.is_empty() || !default_impl_items.is_empty() { close_tags.insert_str(0, "</details>"); } write!(w, "{}", i.inner_impl().print(use_absolute, cx)); @@ -1585,7 +1571,7 @@ fn render_impl( aliases, i.inner_impl().print(false, cx) ); - if !impl_items.is_empty() { + if !impl_items.is_empty() || !default_impl_items.is_empty() { close_tags.insert_str(0, "</details>"); } } @@ -1598,7 +1584,7 @@ fn render_impl( outer_const_version, ); write_srclink(cx, &i.impl_item, w); - if impl_items.is_empty() { + if impl_items.is_empty() && default_impl_items.is_empty() { w.write_str("</h3>"); } else { w.write_str("</h3></summary>"); @@ -1627,8 +1613,13 @@ fn render_impl( ); } } - if !impl_items.is_empty() { + if !impl_items.is_empty() || !default_impl_items.is_empty() { w.write_str("<div class=\"impl-items\">"); + w.push_buffer(default_impl_items); + if trait_.is_some() && !impl_items.is_empty() { + w.write_str("<details class=\"undocumented\"><summary></summary>"); + close_tags.insert_str(0, "</details>"); + } w.push_buffer(impl_items); close_tags.insert_str(0, "</div>"); } |
