Skip to content

Commit 3feee9f

Browse files
authored
Rollup merge of #112927 - GuillaumeGomez:where-clause-indent, r=notriddle
Fix indentation for where clause in rustdoc pages Screenshot of the bug: ![image](https://github.com/rust-lang/rust/assets/3050060/090cfeaa-0edc-46c7-9ea0-e26ac865b2c2) I used this opportunity to clarify the code a bit because some weird things were going on. r? ````@notriddle````
2 parents 8168915 + b858a47 commit 3feee9f

9 files changed

+34
-8
lines changed

src/librustdoc/html/format.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,19 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
347347
}
348348
} else {
349349
let mut br_with_padding = String::with_capacity(6 * indent + 28);
350-
br_with_padding.push_str("\n");
350+
br_with_padding.push('\n');
351351

352-
let padding_amount =
353-
if ending == Ending::Newline { indent + 4 } else { indent + "fn where ".len() };
352+
let where_indent = 3;
353+
let padding_amount = if ending == Ending::Newline {
354+
indent + 4
355+
} else if indent == 0 {
356+
4
357+
} else {
358+
indent + where_indent + "where ".len()
359+
};
354360

355361
for _ in 0..padding_amount {
356-
br_with_padding.push_str(" ");
362+
br_with_padding.push(' ');
357363
}
358364
let where_preds = where_preds.to_string().replace('\n', &br_with_padding);
359365

@@ -370,7 +376,8 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
370376
let where_preds = where_preds.replacen(&br_with_padding, " ", 1);
371377

372378
let mut clause = br_with_padding;
373-
clause.truncate(clause.len() - "where ".len());
379+
// +1 is for `\n`.
380+
clause.truncate(indent + 1 + where_indent);
374381

375382
write!(clause, "<span class=\"where\">where{where_preds}</span>")?;
376383
clause

src/librustdoc/html/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,8 @@ fn assoc_method(
860860
w.reserve(header_len + "<a href=\"\" class=\"fn\">{".len() + "</a>".len());
861861
write!(
862862
w,
863-
"{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn <a{href} class=\"fn\">{name}</a>\
864-
{generics}{decl}{notable_traits}{where_clause}",
863+
"{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn \
864+
<a{href} class=\"fn\">{name}</a>{generics}{decl}{notable_traits}{where_clause}",
865865
indent = indent_str,
866866
vis = vis,
867867
constness = constness,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h3 class="code-header">impl&lt;D&gt; <a class="struct" href="struct.Delta.html" title="struct foo::Delta">Delta</a>&lt;D&gt;<span class="where fmt-newline">where
2+
D: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a>,</span></h3>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<pre class="rust item-decl"><code>pub struct Simd&lt;T&gt;(_)
22
<span class="where">where
3-
T: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code></pre>
3+
T: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code></pre>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<code>pub struct Alpha&lt;A&gt;(_)
2+
<span class="where">where
3+
A: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<code>pub trait Bravo&lt;B&gt;<span class="where fmt-newline">where
2+
B: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a>,</span>{
3+
// Required method
4+
fn <a href="#tymethod.get" class="fn">get</a>(&amp;self, B: B);
5+
}</code>
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<code>pub fn charlie&lt;C&gt;()<span class="where fmt-newline">where
2+
C: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a>,</span></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<code>pub type Golf&lt;T&gt;<span class="where fmt-newline">where
2+
T: <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</span> = <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, T)</a>;</code>

tests/rustdoc/where.rs

+5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@ use std::io::Lines;
55
pub trait MyTrait { fn dummy(&self) { } }
66

77
// @has foo/struct.Alpha.html '//pre' "pub struct Alpha<A>(_) where A: MyTrait"
8+
// @snapshot alpha_trait_decl - '//*[@class="rust item-decl"]/code'
89
pub struct Alpha<A>(A) where A: MyTrait;
910
// @has foo/trait.Bravo.html '//pre' "pub trait Bravo<B>where B: MyTrait"
11+
// @snapshot bravo_trait_decl - '//*[@class="rust item-decl"]/code'
1012
pub trait Bravo<B> where B: MyTrait { fn get(&self, B: B); }
1113
// @has foo/fn.charlie.html '//pre' "pub fn charlie<C>()where C: MyTrait"
14+
// @snapshot charlie_fn_decl - '//*[@class="rust item-decl"]/code'
1215
pub fn charlie<C>() where C: MyTrait {}
1316

1417
pub struct Delta<D>(D);
1518

1619
// @has foo/struct.Delta.html '//*[@class="impl"]//h3[@class="code-header"]' \
1720
// "impl<D> Delta<D>where D: MyTrait"
21+
// @snapshot SWhere_Echo_impl - '//*[@id="impl-Delta%3CD%3E"]/h3[@class="code-header"]'
1822
impl<D> Delta<D> where D: MyTrait {
1923
pub fn delta() {}
2024
}
@@ -65,4 +69,5 @@ impl<F> MyTrait for Foxtrot<F>where F: MyTrait {}
6569

6670
// @has foo/type.Golf.html '//pre[@class="rust item-decl"]' \
6771
// "type Golf<T>where T: Clone, = (T, T)"
72+
// @snapshot golf_type_alias_decl - '//*[@class="rust item-decl"]/code'
6873
pub type Golf<T> where T: Clone = (T, T);

0 commit comments

Comments
 (0)