Skip to content

Commit 8829193

Browse files
authored
Unrolled build for rust-lang#127474
Rollup merge of rust-lang#127474 - tesuji:foldable-inline-derefs, r=t-rustdoc doc: Make block of inline Deref methods foldable After: ![image](https://github.com/rust-lang/rust/assets/15225902/3e8ab320-dbf7-436f-9be0-d0ef82664663) Before: ![image](https://github.com/rust-lang/rust/assets/15225902/f6f7635d-d4c3-437e-a2d9-147726287b05) Fix rust-lang#127470. Current status: - [x] Bug when hovering over title "Methods from ...": The anchor sign $ overlaps with `[-]`: rust-lang#127474 (comment) => Fixed by rust-lang#127474 (comment)
2 parents 9b82580 + 9855a38 commit 8829193

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

src/librustdoc/html/render/mod.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,7 @@ fn render_assoc_items_inner(
12501250
let Some(v) = cache.impls.get(&it) else { return };
12511251
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
12521252
if !non_trait.is_empty() {
1253+
let mut close_tags = <Vec<&str>>::with_capacity(1);
12531254
let mut tmp_buf = Buffer::html();
12541255
let (render_mode, id, class_html) = match what {
12551256
AssocItemRender::All => {
@@ -1260,6 +1261,8 @@ fn render_assoc_items_inner(
12601261
let id =
12611262
cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx))));
12621263
let derived_id = cx.derive_id(&id);
1264+
tmp_buf.write_str("<details class=\"toggle big-toggle\" open><summary>");
1265+
close_tags.push("</details>");
12631266
write_impl_section_heading(
12641267
&mut tmp_buf,
12651268
&format!(
@@ -1269,6 +1272,7 @@ fn render_assoc_items_inner(
12691272
),
12701273
&id,
12711274
);
1275+
tmp_buf.write_str("</summary>");
12721276
if let Some(def_id) = type_.def_id(cx.cache()) {
12731277
cx.deref_id_map.insert(def_id, id);
12741278
}
@@ -1302,6 +1306,9 @@ fn render_assoc_items_inner(
13021306
impls_buf.into_inner()
13031307
)
13041308
.unwrap();
1309+
for tag in close_tags.into_iter().rev() {
1310+
w.write_str(tag).unwrap();
1311+
}
13051312
}
13061313
}
13071314

@@ -1558,7 +1565,7 @@ fn render_impl(
15581565
let cache = &shared.cache;
15591566
let traits = &cache.traits;
15601567
let trait_ = i.trait_did().map(|did| &traits[&did]);
1561-
let mut close_tags = String::new();
1568+
let mut close_tags = <Vec<&str>>::with_capacity(2);
15621569

15631570
// For trait implementations, the `interesting` output contains all methods that have doc
15641571
// comments, and the `boring` output contains all methods that do not. The distinction is
@@ -1870,7 +1877,7 @@ fn render_impl(
18701877
if render_mode == RenderMode::Normal {
18711878
let toggled = !(impl_items.is_empty() && default_impl_items.is_empty());
18721879
if toggled {
1873-
close_tags.insert_str(0, "</details>");
1880+
close_tags.push("</details>");
18741881
write!(
18751882
w,
18761883
"<details class=\"toggle implementors-toggle\"{}>\
@@ -1916,14 +1923,16 @@ fn render_impl(
19161923
}
19171924
if !default_impl_items.is_empty() || !impl_items.is_empty() {
19181925
w.write_str("<div class=\"impl-items\">");
1919-
close_tags.insert_str(0, "</div>");
1926+
close_tags.push("</div>");
19201927
}
19211928
}
19221929
if !default_impl_items.is_empty() || !impl_items.is_empty() {
19231930
w.push_buffer(default_impl_items);
19241931
w.push_buffer(impl_items);
19251932
}
1926-
w.write_str(&close_tags);
1933+
for tag in close_tags.into_iter().rev() {
1934+
w.write_str(tag);
1935+
}
19271936
}
19281937

19291938
// Render the items that appear on the right side of methods, impls, and

src/librustdoc/html/static/css/rustdoc.css

+10
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,11 @@ details.toggle {
18521852
position: relative;
18531853
}
18541854

1855+
details.big-toggle {
1856+
/* This makes [-] on the same line as <summary>. */
1857+
contain: inline-size;
1858+
}
1859+
18551860
/* The hideme class is used on summary tags that contain a span with
18561861
placeholder text shown only when the toggle is closed. For instance,
18571862
"Expand description" or "Show methods". */
@@ -1942,6 +1947,11 @@ details.toggle > summary:not(.hideme)::before {
19421947
left: -24px;
19431948
}
19441949

1950+
details.big-toggle > summary:not(.hideme)::before {
1951+
left: -34px;
1952+
top: 9px;
1953+
}
1954+
19451955
/* When a "hideme" summary is open and the "Expand description" or "Show
19461956
methods" text is hidden, we want the [-] toggle that remains to not
19471957
affect the layout of the items to its right. To do that, we use

tests/rustdoc-gui/deref-block.goml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This test ensures that several clickable items actually have the pointer cursor.
2+
go-to: "file://" + |DOC_PATH| + "/lib2/struct.Derefer.html"
3+
4+
assert-text: (".big-toggle summary", "Methods from Deref<Target = str>§")
5+
// We ensure it doesn't go over `§`.
6+
assert-css: (".big-toggle summary::before", {
7+
"left": "-34px",
8+
"top": "9px",
9+
})
10+
// It should NOT have the same X or Y position as the other toggles.
11+
compare-elements-position-false: (
12+
".big-toggle summary::before",
13+
".method-toggle summary::before",
14+
["x", "y"],
15+
)
16+
17+
// We now check that if we're in mobile mode, it gets back to its original X position.
18+
set-window-size: (600, 600)
19+
assert-css: (".big-toggle summary::before", {
20+
"left": "-11px",
21+
"top": "9px",
22+
})
23+
// It should have the same X position as the other toggles.
24+
compare-elements-position: (".big-toggle summary::before", ".method-toggle summary::before", ["x"])
25+
// But still shouldn't have the same Y position.
26+
compare-elements-position-false: (
27+
".big-toggle summary::before",
28+
".method-toggle summary::before",
29+
["y"],
30+
)

tests/rustdoc-gui/src/lib2/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,13 @@ pub mod scroll_traits {
356356
fn this_is_a_method_with_a_long_name_returning_something() -> String;
357357
}
358358
}
359+
360+
pub struct Derefer(String);
361+
362+
impl std::ops::Deref for Derefer {
363+
type Target = str;
364+
365+
fn deref(&self) -> &Self::Target {
366+
&self.0
367+
}
368+
}

0 commit comments

Comments
 (0)