Skip to content

Commit 6b5de7a

Browse files
committed
Auto merge of rust-lang#84754 - GuillaumeGomez:toggle-migration, r=jsha
Migrate trait and impl blocks' toggles into Part of rust-lang#83332 After this, I think only the "global" doc comment will be used as JS toggle. Once this PR is merged, I check what remains and remove them. There is one change that this PR brings: ![Screenshot from 2021-04-30 15-39-04](https://user-images.githubusercontent.com/3050060/116713412-0f9ce200-a9d5-11eb-979c-2e7a73d16706.png) ![Screenshot from 2021-04-30 15-39-07](https://user-images.githubusercontent.com/3050060/116713415-10357880-a9d5-11eb-9868-1ba9e5ebf65e.png) As you can see, I had to move the "undocumented" items below, they're not mixed with the others anymore. Unfortunately, I don't see a way to keep the current appearance without JS. As a a reminder, currently it looks like this: ![Screenshot from 2021-04-30 15-39-12](https://user-images.githubusercontent.com/3050060/116713547-31966480-a9d5-11eb-90bb-686042eefeec.png) ![Screenshot from 2021-04-30 15-39-15](https://user-images.githubusercontent.com/3050060/116713549-322efb00-a9d5-11eb-94a9-cfea073120db.png) r? `@jsha`
2 parents e10cbc3 + 0d52eb9 commit 6b5de7a

File tree

12 files changed

+186
-232
lines changed

12 files changed

+186
-232
lines changed

src/librustdoc/clean/types.rs

-4
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,6 @@ impl ItemKind {
678678
| KeywordItem(_) => [].iter(),
679679
}
680680
}
681-
682-
crate fn is_type_alias(&self) -> bool {
683-
matches!(self, ItemKind::TypedefItem(..) | ItemKind::AssocTypeItem(..))
684-
}
685681
}
686682

687683
#[derive(Clone, Debug)]

src/librustdoc/html/render/mod.rs

+89-98
Original file line numberDiff line numberDiff line change
@@ -508,23 +508,16 @@ fn document(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, parent: Option
508508
if let Some(ref name) = item.name {
509509
info!("Documenting {}", name);
510510
}
511-
document_item_info(w, cx, item, false, parent);
512-
document_full(w, item, cx, false);
511+
document_item_info(w, cx, item, parent);
512+
document_full(w, item, cx);
513513
}
514514

515515
/// Render md_text as markdown.
516-
fn render_markdown(
517-
w: &mut Buffer,
518-
cx: &Context<'_>,
519-
md_text: &str,
520-
links: Vec<RenderedLink>,
521-
is_hidden: bool,
522-
) {
516+
fn render_markdown(w: &mut Buffer, cx: &Context<'_>, md_text: &str, links: Vec<RenderedLink>) {
523517
let mut ids = cx.id_map.borrow_mut();
524518
write!(
525519
w,
526-
"<div class=\"docblock{}\">{}</div>",
527-
if is_hidden { " hidden" } else { "" },
520+
"<div class=\"docblock\">{}</div>",
528521
Markdown(
529522
md_text,
530523
&links,
@@ -544,11 +537,10 @@ fn document_short(
544537
item: &clean::Item,
545538
cx: &Context<'_>,
546539
link: AssocItemLink<'_>,
547-
is_hidden: bool,
548540
parent: &clean::Item,
549541
show_def_docs: bool,
550542
) {
551-
document_item_info(w, cx, item, is_hidden, Some(parent));
543+
document_item_info(w, cx, item, Some(parent));
552544
if !show_def_docs {
553545
return;
554546
}
@@ -565,19 +557,14 @@ fn document_short(
565557
}
566558
}
567559

568-
write!(
569-
w,
570-
"<div class='docblock{}'>{}</div>",
571-
if is_hidden { " hidden" } else { "" },
572-
summary_html,
573-
);
560+
write!(w, "<div class='docblock'>{}</div>", summary_html,);
574561
}
575562
}
576563

577-
fn document_full(w: &mut Buffer, item: &clean::Item, cx: &Context<'_>, is_hidden: bool) {
564+
fn document_full(w: &mut Buffer, item: &clean::Item, cx: &Context<'_>) {
578565
if let Some(s) = cx.shared.maybe_collapsed_doc_value(item) {
579566
debug!("Doc block: =====\n{}\n=====", s);
580-
render_markdown(w, cx, &s, item.links(cx), is_hidden);
567+
render_markdown(w, cx, &s, item.links(cx));
581568
}
582569
}
583570

@@ -590,16 +577,11 @@ fn document_item_info(
590577
w: &mut Buffer,
591578
cx: &Context<'_>,
592579
item: &clean::Item,
593-
is_hidden: bool,
594580
parent: Option<&clean::Item>,
595581
) {
596582
let item_infos = short_item_info(item, cx, parent);
597583
if !item_infos.is_empty() {
598-
if is_hidden {
599-
w.write_str("<div class=\"item-info hidden\">");
600-
} else {
601-
w.write_str("<div class=\"item-info\">");
602-
}
584+
w.write_str("<div class=\"item-info\">");
603585
for info in item_infos {
604586
w.write_str(&info);
605587
}
@@ -1282,8 +1264,12 @@ fn render_impl(
12821264
let trait_ = i.trait_did_full(cache).map(|did| &traits[&did]);
12831265
let mut close_tags = String::new();
12841266

1267+
// For trait implementations, the `interesting` output contains all methods that have doc
1268+
// comments, and the `boring` output contains all methods that do not. The distinction is
1269+
// used to allow hiding the boring methods.
12851270
fn doc_impl_item(
1286-
w: &mut Buffer,
1271+
boring: &mut Buffer,
1272+
interesting: &mut Buffer,
12871273
cx: &Context<'_>,
12881274
item: &clean::Item,
12891275
parent: &clean::Item,
@@ -1306,15 +1292,46 @@ fn render_impl(
13061292
}
13071293
};
13081294

1309-
let (is_hidden, extra_class) =
1310-
if (trait_.is_none() || item.doc_value().is_some() || item.kind.is_type_alias())
1311-
&& !is_default_item
1312-
{
1313-
(false, "")
1314-
} else {
1315-
(true, " hidden")
1316-
};
13171295
let in_trait_class = if trait_.is_some() { " trait-impl" } else { "" };
1296+
1297+
let mut doc_buffer = Buffer::empty_from(boring);
1298+
let mut info_buffer = Buffer::empty_from(boring);
1299+
let mut short_documented = true;
1300+
1301+
if render_method_item {
1302+
if !is_default_item {
1303+
if let Some(t) = trait_ {
1304+
// The trait item may have been stripped so we might not
1305+
// find any documentation or stability for it.
1306+
if let Some(it) = t.items.iter().find(|i| i.name == item.name) {
1307+
// We need the stability of the item from the trait
1308+
// because impls can't have a stability.
1309+
if item.doc_value().is_some() {
1310+
document_item_info(&mut info_buffer, cx, it, Some(parent));
1311+
document_full(&mut doc_buffer, item, cx);
1312+
short_documented = false;
1313+
} else {
1314+
// In case the item isn't documented,
1315+
// provide short documentation from the trait.
1316+
document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs);
1317+
}
1318+
}
1319+
} else {
1320+
document_item_info(&mut info_buffer, cx, item, Some(parent));
1321+
if show_def_docs {
1322+
document_full(&mut doc_buffer, item, cx);
1323+
short_documented = false;
1324+
}
1325+
}
1326+
} else {
1327+
document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs);
1328+
}
1329+
}
1330+
let w = if short_documented && trait_.is_some() { interesting } else { boring };
1331+
1332+
if !doc_buffer.is_empty() {
1333+
w.write_str("<details class=\"rustdoc-toggle\" open><summary>");
1334+
}
13181335
match *item.kind {
13191336
clean::MethodItem(..) | clean::TyMethodItem(_) => {
13201337
// Only render when the method is not static or we allow static methods
@@ -1327,11 +1344,7 @@ fn render_impl(
13271344
})
13281345
})
13291346
.map(|item| format!("{}.{}", item.type_(), name));
1330-
write!(
1331-
w,
1332-
"<h4 id=\"{}\" class=\"{}{}{}\">",
1333-
id, item_type, extra_class, in_trait_class,
1334-
);
1347+
write!(w, "<h4 id=\"{}\" class=\"{}{}\">", id, item_type, in_trait_class,);
13351348
w.write_str("<code>");
13361349
render_assoc_item(
13371350
w,
@@ -1356,11 +1369,7 @@ fn render_impl(
13561369
clean::TypedefItem(ref tydef, _) => {
13571370
let source_id = format!("{}.{}", ItemType::AssocType, name);
13581371
let id = cx.derive_id(source_id.clone());
1359-
write!(
1360-
w,
1361-
"<h4 id=\"{}\" class=\"{}{}{}\"><code>",
1362-
id, item_type, extra_class, in_trait_class
1363-
);
1372+
write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, in_trait_class);
13641373
assoc_type(
13651374
w,
13661375
item,
@@ -1377,11 +1386,7 @@ fn render_impl(
13771386
clean::AssocConstItem(ref ty, ref default) => {
13781387
let source_id = format!("{}.{}", item_type, name);
13791388
let id = cx.derive_id(source_id.clone());
1380-
write!(
1381-
w,
1382-
"<h4 id=\"{}\" class=\"{}{}{}\"><code>",
1383-
id, item_type, extra_class, in_trait_class
1384-
);
1389+
write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, in_trait_class);
13851390
assoc_const(
13861391
w,
13871392
item,
@@ -1406,11 +1411,7 @@ fn render_impl(
14061411
clean::AssocTypeItem(ref bounds, ref default) => {
14071412
let source_id = format!("{}.{}", item_type, name);
14081413
let id = cx.derive_id(source_id.clone());
1409-
write!(
1410-
w,
1411-
"<h4 id=\"{}\" class=\"{}{}{}\"><code>",
1412-
id, item_type, extra_class, in_trait_class
1413-
);
1414+
write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, in_trait_class);
14141415
assoc_type(
14151416
w,
14161417
item,
@@ -1428,38 +1429,20 @@ fn render_impl(
14281429
_ => panic!("can't make docs for trait item with name {:?}", item.name),
14291430
}
14301431

1431-
if render_method_item {
1432-
if !is_default_item {
1433-
if let Some(t) = trait_ {
1434-
// The trait item may have been stripped so we might not
1435-
// find any documentation or stability for it.
1436-
if let Some(it) = t.items.iter().find(|i| i.name == item.name) {
1437-
// We need the stability of the item from the trait
1438-
// because impls can't have a stability.
1439-
if item.doc_value().is_some() {
1440-
document_item_info(w, cx, it, is_hidden, Some(parent));
1441-
document_full(w, item, cx, is_hidden);
1442-
} else {
1443-
// In case the item isn't documented,
1444-
// provide short documentation from the trait.
1445-
document_short(w, it, cx, link, is_hidden, parent, show_def_docs);
1446-
}
1447-
}
1448-
} else {
1449-
document_item_info(w, cx, item, is_hidden, Some(parent));
1450-
if show_def_docs {
1451-
document_full(w, item, cx, is_hidden);
1452-
}
1453-
}
1454-
} else {
1455-
document_short(w, item, cx, link, is_hidden, parent, show_def_docs);
1456-
}
1432+
w.push_buffer(info_buffer);
1433+
if !doc_buffer.is_empty() {
1434+
w.write_str("</summary>");
1435+
w.push_buffer(doc_buffer);
1436+
w.push_str("</details>");
14571437
}
14581438
}
14591439

14601440
let mut impl_items = Buffer::empty_from(w);
1441+
let mut default_impl_items = Buffer::empty_from(w);
1442+
14611443
for trait_item in &i.inner_impl().items {
14621444
doc_impl_item(
1445+
&mut default_impl_items,
14631446
&mut impl_items,
14641447
cx,
14651448
trait_item,
@@ -1475,7 +1458,8 @@ fn render_impl(
14751458
}
14761459

14771460
fn render_default_items(
1478-
w: &mut Buffer,
1461+
boring: &mut Buffer,
1462+
interesting: &mut Buffer,
14791463
cx: &Context<'_>,
14801464
t: &clean::Trait,
14811465
i: &clean::Impl,
@@ -1495,7 +1479,8 @@ fn render_impl(
14951479
let assoc_link = AssocItemLink::GotoSource(did, &provided_methods);
14961480

14971481
doc_impl_item(
1498-
w,
1482+
boring,
1483+
interesting,
14991484
cx,
15001485
trait_item,
15011486
parent,
@@ -1517,6 +1502,7 @@ fn render_impl(
15171502
if show_default_items {
15181503
if let Some(t) = trait_ {
15191504
render_default_items(
1505+
&mut default_impl_items,
15201506
&mut impl_items,
15211507
cx,
15221508
&t.trait_,
@@ -1529,10 +1515,14 @@ fn render_impl(
15291515
);
15301516
}
15311517
}
1532-
let details_str = if impl_items.is_empty() {
1533-
""
1534-
} else {
1535-
"<details class=\"rustdoc-toggle implementors-toggle\" open><summary>"
1518+
let toggled = !impl_items.is_empty() || !default_impl_items.is_empty();
1519+
let open_details = |close_tags: &mut String| {
1520+
if toggled {
1521+
close_tags.insert_str(0, "</details>");
1522+
"<details class=\"rustdoc-toggle implementors-toggle\" open><summary>"
1523+
} else {
1524+
""
1525+
}
15361526
};
15371527
if render_mode == RenderMode::Normal {
15381528
let id = cx.derive_id(match i.inner_impl().trait_ {
@@ -1554,11 +1544,10 @@ fn render_impl(
15541544
write!(
15551545
w,
15561546
"{}<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">",
1557-
details_str, id, aliases
1547+
open_details(&mut close_tags),
1548+
id,
1549+
aliases
15581550
);
1559-
if !impl_items.is_empty() {
1560-
close_tags.insert_str(0, "</details>");
1561-
}
15621551
write!(w, "{}", i.inner_impl().print(use_absolute, cx));
15631552
if show_def_docs {
15641553
for it in &i.inner_impl().items {
@@ -1582,14 +1571,11 @@ fn render_impl(
15821571
write!(
15831572
w,
15841573
"{}<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">{}</code>",
1585-
details_str,
1574+
open_details(&mut close_tags),
15861575
id,
15871576
aliases,
15881577
i.inner_impl().print(false, cx)
15891578
);
1590-
if !impl_items.is_empty() {
1591-
close_tags.insert_str(0, "</details>");
1592-
}
15931579
}
15941580
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
15951581
render_stability_since_raw(
@@ -1600,7 +1586,7 @@ fn render_impl(
16001586
outer_const_version,
16011587
);
16021588
write_srclink(cx, &i.impl_item, w);
1603-
if impl_items.is_empty() {
1589+
if !toggled {
16041590
w.write_str("</h3>");
16051591
} else {
16061592
w.write_str("</h3></summary>");
@@ -1629,8 +1615,13 @@ fn render_impl(
16291615
);
16301616
}
16311617
}
1632-
if !impl_items.is_empty() {
1618+
if toggled {
16331619
w.write_str("<div class=\"impl-items\">");
1620+
w.push_buffer(default_impl_items);
1621+
if trait_.is_some() && !impl_items.is_empty() {
1622+
w.write_str("<details class=\"undocumented\"><summary></summary>");
1623+
close_tags.insert_str(0, "</details>");
1624+
}
16341625
w.push_buffer(impl_items);
16351626
close_tags.insert_str(0, "</div>");
16361627
}

0 commit comments

Comments
 (0)