Skip to content

Commit 1115f69

Browse files
committed
Deduplicate item sections
1 parent fa400ac commit 1115f69

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,19 +2534,11 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
25342534
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
25352535
let mut sidebar = String::new();
25362536

2537-
// Re-exports are handled a bit differently because they can be extern crates or imports.
2538-
if items.iter().any(|it| {
2539-
it.name.is_some()
2540-
&& (it.type_() == ItemType::ExternCrate
2541-
|| (it.type_() == ItemType::Import && !it.is_stripped()))
2542-
}) {
2543-
let sec = item_ty_to_section(ItemType::Import);
2544-
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
2545-
}
2546-
2537+
let mut already_emitted_sections = FxHashSet::default();
25472538
// ordering taken from item_module, reorder, where it prioritized elements in a certain order
25482539
// to print its headings
25492540
for &myty in &[
2541+
ItemType::Import,
25502542
ItemType::Primitive,
25512543
ItemType::Module,
25522544
ItemType::Macro,
@@ -2570,6 +2562,9 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
25702562
] {
25712563
if items.iter().any(|it| !it.is_stripped() && it.type_() == myty && it.name.is_some()) {
25722564
let sec = item_ty_to_section(myty);
2565+
if !already_emitted_sections.insert(sec) {
2566+
continue;
2567+
}
25732568
sidebar.push_str(&format!("<li><a href=\"#{}\">{}</a></li>", sec.id(), sec.name()));
25742569
}
25752570
}

src/librustdoc/html/render/print_item.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,32 +271,28 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
271271
});
272272

273273
debug!("{:?}", indices);
274-
let mut curty = None;
274+
let mut last_section = None;
275275

276276
for &idx in &indices {
277277
let myitem = &items[idx];
278278
if myitem.is_stripped() {
279279
continue;
280280
}
281281

282-
let myty = Some(myitem.type_());
283-
if curty == Some(ItemType::ExternCrate) && myty == Some(ItemType::Import) {
284-
// Put `extern crate` and `use` re-exports in the same section.
285-
curty = myty;
286-
} else if myty != curty {
287-
if curty.is_some() {
282+
let my_section = item_ty_to_section(myitem.type_());
283+
if Some(my_section) != last_section {
284+
if last_section.is_some() {
288285
w.write_str(ITEM_TABLE_CLOSE);
289286
}
290-
curty = myty;
291-
let sec = item_ty_to_section(myty.unwrap());
287+
last_section = Some(my_section);
292288
write!(
293289
w,
294290
"<h2 id=\"{id}\" class=\"small-section-header\">\
295291
<a href=\"#{id}\">{name}</a>\
296292
</h2>\n{}",
297293
ITEM_TABLE_OPEN,
298-
id = cx.derive_id(sec.id().to_owned()),
299-
name = sec.name(),
294+
id = cx.derive_id(my_section.id().to_owned()),
295+
name = my_section.name(),
300296
);
301297
}
302298

@@ -408,7 +404,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
408404
}
409405
}
410406

411-
if curty.is_some() {
407+
if last_section.is_some() {
412408
w.write_str(ITEM_TABLE_CLOSE);
413409
}
414410
}

0 commit comments

Comments
 (0)