From 315a16fced68f63f1f396756c1f5c192cdac44ed Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 8 Jul 2024 03:21:53 +0000 Subject: [PATCH 1/4] doc: Fold inline methods from Deref --- src/librustdoc/html/render/mod.rs | 17 +++++++++++++---- src/librustdoc/html/static/css/rustdoc.css | 5 +++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 877a00e206d..acc21faa464 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1256,6 +1256,7 @@ fn render_assoc_items_inner( let Some(v) = cache.impls.get(&it) else { return }; let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none()); if !non_trait.is_empty() { + let mut close_tags = >::with_capacity(1); let mut tmp_buf = Buffer::html(); let (render_mode, id, class_html) = match what { AssocItemRender::All => { @@ -1266,6 +1267,8 @@ fn render_assoc_items_inner( let id = cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx)))); let derived_id = cx.derive_id(&id); + tmp_buf.write_str("
"); + close_tags.push("
"); write_impl_section_heading( &mut tmp_buf, &format!( @@ -1275,6 +1278,7 @@ fn render_assoc_items_inner( ), &id, ); + tmp_buf.write_str(""); if let Some(def_id) = type_.def_id(cx.cache()) { cx.deref_id_map.insert(def_id, id); } @@ -1308,6 +1312,9 @@ fn render_assoc_items_inner( impls_buf.into_inner() ) .unwrap(); + for tag in close_tags.into_iter().rev() { + w.write_str(tag).unwrap(); + } } } @@ -1565,7 +1572,7 @@ fn render_impl( let cache = &shared.cache; let traits = &cache.traits; let trait_ = i.trait_did().map(|did| &traits[&did]); - let mut close_tags = String::new(); + let mut close_tags = >::with_capacity(2); // 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 @@ -1853,7 +1860,7 @@ fn render_default_items( if render_mode == RenderMode::Normal { let toggled = !(impl_items.is_empty() && default_impl_items.is_empty()); if toggled { - close_tags.insert_str(0, ""); + close_tags.push(""); write!( w, "
\ @@ -1899,14 +1906,16 @@ fn render_default_items( } if !default_impl_items.is_empty() || !impl_items.is_empty() { w.write_str("
"); - close_tags.insert_str(0, "
"); + close_tags.push(""); } } if !default_impl_items.is_empty() || !impl_items.is_empty() { w.push_buffer(default_impl_items); w.push_buffer(impl_items); } - w.write_str(&close_tags); + for tag in close_tags.into_iter().rev() { + w.write_str(tag); + } } // Render the items that appear on the right side of methods, impls, and diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 4c0ba75d261..7362ea79025 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1733,6 +1733,11 @@ details.toggle { position: relative; } +details.implementors-toggle { + /* This makes [-] on the same line as . */ + contain: inline-size; +} + /* The hideme class is used on summary tags that contain a span with placeholder text shown only when the toggle is closed. For instance, "Expand description" or "Show methods". */ From fbc794fe1e812fb719063b90e8e915fff6781057 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 15 Jul 2024 17:43:46 +0200 Subject: [PATCH 2/4] Fix style --- src/librustdoc/html/static/css/rustdoc.css | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 7362ea79025..4a8c7d3e0f0 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1828,6 +1828,11 @@ details.toggle > summary:not(.hideme)::before { left: -24px; } +details.implementors-toggle > summary:not(.hideme)::before { + left: -34px; + top: 9px; +} + /* When a "hideme" summary is open and the "Expand description" or "Show methods" text is hidden, we want the [-] toggle that remains to not affect the layout of the items to its right. To do that, we use From a868b3fce74c11e2efc54ed1e9f13894a4bc6381 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 15 Jul 2024 18:10:26 +0200 Subject: [PATCH 3/4] Create new CSS class for "big toggles" --- src/librustdoc/html/render/mod.rs | 2 +- src/librustdoc/html/static/css/rustdoc.css | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index acc21faa464..547d8d82eae 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1267,7 +1267,7 @@ fn render_assoc_items_inner( let id = cx.derive_id(small_url_encode(format!("deref-methods-{:#}", type_.print(cx)))); let derived_id = cx.derive_id(&id); - tmp_buf.write_str("
"); + tmp_buf.write_str("
"); close_tags.push("
"); write_impl_section_heading( &mut tmp_buf, diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 4a8c7d3e0f0..b104fbf4e97 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1733,7 +1733,7 @@ details.toggle { position: relative; } -details.implementors-toggle { +details.big-toggle { /* This makes [-] on the same line as . */ contain: inline-size; } @@ -1828,7 +1828,7 @@ details.toggle > summary:not(.hideme)::before { left: -24px; } -details.implementors-toggle > summary:not(.hideme)::before { +details.big-toggle > summary:not(.hideme)::before { left: -34px; top: 9px; } From 9855a3844bbe96d03f18541bdee3e325c35c8a34 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 15 Jul 2024 18:10:50 +0200 Subject: [PATCH 4/4] Add tests for new toggle on deref blocks --- tests/rustdoc-gui/deref-block.goml | 30 ++++++++++++++++++++++++++++++ tests/rustdoc-gui/src/lib2/lib.rs | 10 ++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/rustdoc-gui/deref-block.goml diff --git a/tests/rustdoc-gui/deref-block.goml b/tests/rustdoc-gui/deref-block.goml new file mode 100644 index 00000000000..24f612f8a6f --- /dev/null +++ b/tests/rustdoc-gui/deref-block.goml @@ -0,0 +1,30 @@ +// This test ensures that several clickable items actually have the pointer cursor. +go-to: "file://" + |DOC_PATH| + "/lib2/struct.Derefer.html" + +assert-text: (".big-toggle summary", "Methods from Deref§") +// We ensure it doesn't go over `§`. +assert-css: (".big-toggle summary::before", { + "left": "-34px", + "top": "9px", +}) +// It should NOT have the same X or Y position as the other toggles. +compare-elements-position-false: ( + ".big-toggle summary::before", + ".method-toggle summary::before", + ["x", "y"], +) + +// We now check that if we're in mobile mode, it gets back to its original X position. +set-window-size: (600, 600) +assert-css: (".big-toggle summary::before", { + "left": "-11px", + "top": "9px", +}) +// It should have the same X position as the other toggles. +compare-elements-position: (".big-toggle summary::before", ".method-toggle summary::before", ["x"]) +// But still shouldn't have the same Y position. +compare-elements-position-false: ( + ".big-toggle summary::before", + ".method-toggle summary::before", + ["y"], +) diff --git a/tests/rustdoc-gui/src/lib2/lib.rs b/tests/rustdoc-gui/src/lib2/lib.rs index b467b044052..fafb8f3b59b 100644 --- a/tests/rustdoc-gui/src/lib2/lib.rs +++ b/tests/rustdoc-gui/src/lib2/lib.rs @@ -355,3 +355,13 @@ pub trait TraitWithLongItemsName { fn this_is_a_method_with_a_long_name_returning_something() -> String; } } + +pub struct Derefer(String); + +impl std::ops::Deref for Derefer { + type Target = str; + + fn deref(&self) -> &Self::Target { + &self.0 + } +}