Skip to content

Fix impl block items indent #140248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,7 @@ fn render_impl(
.split_summary_and_content()
})
.unwrap_or((None, None));

write!(
w,
"{}",
Expand All @@ -2097,24 +2098,19 @@ fn render_impl(
use_absolute,
aliases,
before_dox.as_deref(),
trait_.is_none() && impl_.items.is_empty(),
)
)?;
if toggled {
w.write_str("</summary>")?;
}

if before_dox.is_some() {
if trait_.is_none() && impl_.items.is_empty() {
w.write_str(
"<div class=\"item-info\">\
<div class=\"stab empty-impl\">This impl block contains no items.</div>\
</div>",
)?;
}
if let Some(after_dox) = after_dox {
write!(w, "<div class=\"docblock\">{after_dox}</div>")?;
}
if before_dox.is_some()
&& let Some(after_dox) = after_dox
{
write!(w, "<div class=\"docblock\">{after_dox}</div>")?;
}

if !default_impl_items.is_empty() || !impl_items.is_empty() {
w.write_str("<div class=\"impl-items\">")?;
close_tags.push("</div>");
Expand Down Expand Up @@ -2182,6 +2178,7 @@ fn render_impl_summary(
// in documentation pages for trait with automatic implementations like "Send" and "Sync".
aliases: &[String],
doc: Option<&str>,
impl_is_empty: bool,
) -> impl fmt::Display {
fmt::from_fn(move |w| {
let inner_impl = i.inner_impl();
Expand Down Expand Up @@ -2237,6 +2234,13 @@ fn render_impl_summary(
}

if let Some(doc) = doc {
if impl_is_empty {
w.write_str(
"<div class=\"item-info\">\
<div class=\"stab empty-impl\">This impl block contains no items.</div>\
</div>",
)?;
}
write!(w, "<div class=\"docblock\">{doc}</div>")?;
}

Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -2319,7 +2319,10 @@ details.toggle > summary:not(.hideme)::before {
doc block while aligning it with the impl block items. */
.implementors-toggle > .docblock,
/* We indent trait items as well. */
#main-content > .methods > :not(.item-info) {
#main-content > .methods > :not(.item-info),
.impl > .item-info,
.impl > .docblock,
.impl + .docblock {
margin-left: var(--impl-items-indent);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/docblock-table-overflow.goml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ assert-property: (".top-doc .docblock table", {"scrollWidth": "1572"})
// Checking it works on other doc blocks as well...

// Logically, the ".docblock" and the "<p>" should have the same scroll width (if we exclude the margin).
assert-property: ("#implementations-list > details .docblock", {"scrollWidth": 816})
assert-property: ("#implementations-list > details .docblock", {"scrollWidth": 835})
assert-property: ("#implementations-list > details .docblock > p", {"scrollWidth": 835})
// However, since there is overflow in the <table>, its scroll width is bigger.
assert-property: ("#implementations-list > details .docblock table", {"scrollWidth": "1572"})
16 changes: 16 additions & 0 deletions tests/rustdoc-gui/impl-doc-indent.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Checks the impl block docs have the correct indent.
go-to: "file://" + |DOC_PATH| + "/test_docs/impls_indent/struct.Context.html"

// First we ensure that the impl items are indent (more on the right of the screen) than the
// impl itself.
store-position: ("#impl-Context", {"x": impl_x})
store-position: ("#impl-Context > .item-info", {"x": impl_item_x})
assert: |impl_x| < |impl_item_x|

// And we ensure that all impl items have the same indent.
assert-position: ("#impl-Context > .docblock", {"x": |impl_item_x|})
assert-position: ("#impl-Context + .docblock", {"x": |impl_item_x|})

// Same with the collapsible impl block.
assert-position: ("#impl-Context-1 > .docblock", {"x": |impl_item_x|})
assert-position: (".implementors-toggle > summary + .docblock", {"x": |impl_item_x|})
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/item-info-overflow.goml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ compare-elements-property: (
)
assert-property: (
"#impl-SimpleTrait-for-LongItemInfo2 .item-info",
{"scrollWidth": "916"},
{"scrollWidth": "935"},
)
// Just to be sure we're comparing the correct "item-info":
assert-text: (
Expand Down
26 changes: 26 additions & 0 deletions tests/rustdoc-gui/src/test_docs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,29 @@ pub mod SidebarSort {
impl Sort for Cell<u8> {}
impl<'a> Sort for &'a str {}
}

pub mod impls_indent {
pub struct Context;

/// Working with objects.
///
/// # Safety
///
/// Functions that take indices of locals do not check bounds on these indices;
/// the caller must ensure that the indices are less than the number of locals
/// in the current stack frame.
impl Context {
}

/// Working with objects.
///
/// # Safety
///
/// Functions that take indices of locals do not check bounds on these indices;
/// the caller must ensure that the indices are less than the number of locals
/// in the current stack frame.
impl Context {
/// bla
pub fn bar() {}
}
}
Loading