Skip to content

Commit 4aa11ae

Browse files
authored
Rollup merge of rust-lang#107770 - notriddle:notriddle/br2nl, r=GuillaumeGomez
rustdoc: use a newline instead of `<br>` to format code headers Since these elements now use `white-space: pre-wrap` since rust-lang#107615, it's fine to use newlines for formatting, which is smaller and a bit less complicated.
2 parents 7ae3ccf + 8307fd7 commit 4aa11ae

15 files changed

+61
-39
lines changed

src/librustdoc/html/format.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
289289
if f.alternate() {
290290
f.write_str(" ")?;
291291
} else {
292-
f.write_str("<br>")?;
292+
f.write_str("\n")?;
293293
}
294294

295295
match pred {
@@ -352,24 +352,24 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
352352
}
353353
} else {
354354
let mut br_with_padding = String::with_capacity(6 * indent + 28);
355-
br_with_padding.push_str("<br>");
355+
br_with_padding.push_str("\n");
356356

357357
let padding_amout =
358358
if ending == Ending::Newline { indent + 4 } else { indent + "fn where ".len() };
359359

360360
for _ in 0..padding_amout {
361361
br_with_padding.push_str(" ");
362362
}
363-
let where_preds = where_preds.to_string().replace("<br>", &br_with_padding);
363+
let where_preds = where_preds.to_string().replace("\n", &br_with_padding);
364364

365365
if ending == Ending::Newline {
366366
let mut clause = " ".repeat(indent.saturating_sub(1));
367367
write!(clause, "<span class=\"where fmt-newline\">where{where_preds},</span>")?;
368368
clause
369369
} else {
370-
// insert a <br> tag after a single space but before multiple spaces at the start
370+
// insert a newline after a single space but before multiple spaces at the start
371371
if indent == 0 {
372-
format!("<br><span class=\"where\">where{where_preds}</span>")
372+
format!("\n<span class=\"where\">where{where_preds}</span>")
373373
} else {
374374
// put the first one on the same line as the 'where' keyword
375375
let where_preds = where_preds.replacen(&br_with_padding, " ", 1);
@@ -1315,7 +1315,8 @@ impl clean::FnDecl {
13151315

13161316
/// * `header_len`: The length of the function header and name. In other words, the number of
13171317
/// characters in the function declaration up to but not including the parentheses.
1318-
/// <br>Used to determine line-wrapping.
1318+
/// This is expected to go into a `<pre>`/`code-header` block, so indentation and newlines
1319+
/// are preserved.
13191320
/// * `indent`: The number of spaces to indent each successive line with, if line-wrapping is
13201321
/// necessary.
13211322
pub(crate) fn full_print<'a, 'tcx: 'a>(
@@ -1363,7 +1364,7 @@ impl clean::FnDecl {
13631364
}
13641365
} else {
13651366
if i > 0 {
1366-
args.push_str("<br>");
1367+
args.push_str("\n");
13671368
}
13681369
if input.is_const {
13691370
args.push_str("const ");
@@ -1389,7 +1390,7 @@ impl clean::FnDecl {
13891390
let mut args = args.into_inner();
13901391

13911392
if self.c_variadic {
1392-
args.push_str(",<br> ...");
1393+
args.push_str(",\n ...");
13931394
args_plain.push_str(", ...");
13941395
}
13951396

@@ -1399,24 +1400,20 @@ impl clean::FnDecl {
13991400

14001401
let declaration_len = header_len + args_plain.len() + arrow_plain.len();
14011402
let output = if declaration_len > 80 {
1402-
let full_pad = format!("<br>{}", " ".repeat(indent + 4));
1403-
let close_pad = format!("<br>{}", " ".repeat(indent));
1403+
let full_pad = format!("\n{}", " ".repeat(indent + 4));
1404+
let close_pad = format!("\n{}", " ".repeat(indent));
14041405
format!(
14051406
"({pad}{args}{close}){arrow}",
14061407
pad = if self.inputs.values.is_empty() { "" } else { &full_pad },
1407-
args = args.replace("<br>", &full_pad),
1408+
args = args.replace("\n", &full_pad),
14081409
close = close_pad,
14091410
arrow = arrow
14101411
)
14111412
} else {
1412-
format!("({args}){arrow}", args = args.replace("<br>", " "), arrow = arrow)
1413+
format!("({args}){arrow}", args = args.replace("\n", " "), arrow = arrow)
14131414
};
14141415

1415-
if f.alternate() {
1416-
write!(f, "{}", output.replace("<br>", "\n"))
1417-
} else {
1418-
write!(f, "{}", output)
1419-
}
1416+
write!(f, "{}", output)
14201417
}
14211418
}
14221419

tests/rustdoc/async-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct AsyncFdReadyGuard<'a, T> { x: &'a T }
7777

7878
impl Foo {
7979
// @has async_fn/struct.Foo.html
80-
// @has - '//*[@class="method"]' 'pub async fn complicated_lifetimes( &self, context: &impl Bar) -> impl Iterator<Item = &usize>'
80+
// @has - '//*[@class="method"]' 'pub async fn complicated_lifetimes( &self, context: &impl Bar ) -> impl Iterator<Item = &usize>'
8181
pub async fn complicated_lifetimes(&self, context: &impl Bar) -> impl Iterator<Item = &usize> {}
8282
// taken from `tokio` as an example of a method that was particularly bad before
8383
// @has - '//*[@class="method"]' "pub async fn readable<T>(&self) -> Result<AsyncFdReadyGuard<'_, T>, ()>"

tests/rustdoc/const-generics/const-generics-docs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Trait<{1 + 2}> for u8 {}
3131
impl<const N: usize> Trait<N> for [u8; N] {}
3232

3333
// @has foo/struct.Foo.html '//pre[@class="rust item-decl"]' \
34-
// 'pub struct Foo<const N: usize>where u8: Trait<N>'
34+
// 'pub struct Foo<const N: usize> where u8: Trait<N>'
3535
pub struct Foo<const N: usize> where u8: Trait<N>;
3636
// @has foo/struct.Bar.html '//pre[@class="rust item-decl"]' 'pub struct Bar<T, const N: usize>(_)'
3737
pub struct Bar<T, const N: usize>([T; N]);
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
<code>pub trait Write {
22
// Required methods
3-
fn <a href="#tymethod.poll_write" class="fn">poll_write</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;buf: &amp;mut [<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>]<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
4-
<span class="item-spacer" /> fn <a href="#tymethod.poll_flush" class="fn">poll_flush</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
5-
<span class="item-spacer" /> fn <a href="#tymethod.poll_close" class="fn">poll_close</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
3+
fn <a href="#tymethod.poll_write" class="fn">poll_write</a>(
4+
self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,
5+
cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,
6+
buf: &amp;mut [<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>]
7+
) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
8+
<span class="item-spacer" /> fn <a href="#tymethod.poll_flush" class="fn">poll_flush</a>(
9+
self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,
10+
cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;
11+
) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
12+
<span class="item-spacer" /> fn <a href="#tymethod.poll_close" class="fn">poll_close</a>(
13+
self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,
14+
cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;
15+
) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt;;
616

717
// Provided method
8-
fn <a href="#method.poll_write_vectored" class="fn">poll_write_vectored</a>(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;bufs: &amp;[<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>]<br />&#160;&#160;&#160;&#160;) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt; { ... }
9-
}</code>
18+
fn <a href="#method.poll_write_vectored" class="fn">poll_write_vectored</a>(
19+
self: <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,
20+
cx: &amp;mut <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="struct" href="{{channel}}/alloc/string/struct.String.html" title="struct alloc::string::String">String</a>&gt;,
21+
bufs: &amp;[<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>]
22+
) -&gt; <a class="enum" href="{{channel}}/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;<a class="enum" href="{{channel}}/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>, <a class="struct" href="struct.Error.html" title="struct foo::Error">Error</a>&gt;&gt; { ... }
23+
}</code>

tests/rustdoc/generic-associated-types/gats.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// @has foo/trait.LendingIterator.html
44
pub trait LendingIterator {
5-
// @has - '//*[@id="associatedtype.Item"]//h4[@class="code-header"]' "type Item<'a>where Self: 'a"
5+
// @has - '//*[@id="associatedtype.Item"]//h4[@class="code-header"]' "type Item<'a> where Self: 'a"
66
type Item<'a> where Self: 'a;
77

88
// @has - '//*[@id="tymethod.next"]//h4[@class="code-header"]' \
@@ -23,7 +23,7 @@ impl LendingIterator for () {
2323
pub struct Infinite<T>(T);
2424

2525
// @has foo/trait.LendingIterator.html
26-
// @has - '//*[@id="associatedtype.Item-2"]//h4[@class="code-header"]' "type Item<'a>where Self: 'a = &'a T"
26+
// @has - '//*[@id="associatedtype.Item-2"]//h4[@class="code-header"]' "type Item<'a> where Self: 'a = &'a T"
2727
impl<T> LendingIterator for Infinite<T> {
2828
type Item<'a> where Self: 'a = &'a T;
2929

tests/rustdoc/inline_cross/impl_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use impl_trait_aux::func;
1111
// @has impl_trait/fn.func2.html
1212
// @has - '//pre[@class="rust item-decl"]' "func2<T>("
1313
// @has - '//pre[@class="rust item-decl"]' "_x: impl Deref<Target = Option<T>> + Iterator<Item = T>,"
14-
// @has - '//pre[@class="rust item-decl"]' "_y: impl Iterator<Item = u8>)"
14+
// @has - '//pre[@class="rust item-decl"]' "_y: impl Iterator<Item = u8> )"
1515
// @!has - '//pre[@class="rust item-decl"]' 'where'
1616
pub use impl_trait_aux::func2;
1717

tests/rustdoc/issue-34928.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
pub trait Bar {}
44

5-
// @has foo/struct.Foo.html '//pre' 'pub struct Foo<T>(pub T)where T: Bar;'
5+
// @has foo/struct.Foo.html '//pre' 'pub struct Foo<T>(pub T) where T: Bar;'
66
pub struct Foo<T>(pub T) where T: Bar;

tests/rustdoc/reexports-priv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub mod outer {
9898
pub use reexports::foo;
9999
// @has 'foo/outer/inner/fn.foo_crate.html' '//pre[@class="rust item-decl"]' 'pub(crate) fn foo_crate()'
100100
pub(crate) use reexports::foo_crate;
101-
// @has 'foo/outer/inner/fn.foo_super.html' '//pre[@class="rust item-decl"]' 'pub(in outer) fn foo_super()'
101+
// @has 'foo/outer/inner/fn.foo_super.html' '//pre[@class="rust item-decl"]' 'pub(in outer) fn foo_super( )'
102102
pub(super) use::reexports::foo_super;
103103
// @!has 'foo/outer/inner/fn.foo_self.html'
104104
pub(self) use reexports::foo_self;
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
<pre class="rust item-decl"><code>pub struct Simd&lt;T&gt;(_)<br /><span class="where">where<br />&#160;&#160;&#160;&#160;T: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code></pre>
1+
<pre class="rust item-decl"><code>pub struct Simd&lt;T&gt;(_)
2+
<span class="where">where
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
@@ -1,8 +1,13 @@
11
<pre class="rust item-decl"><code>pub trait TraitWhere {
2-
type <a href="#associatedtype.Item" class="associatedtype">Item</a>&lt;'a&gt;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="where">where Self: 'a</span>;
2+
type <a href="#associatedtype.Item" class="associatedtype">Item</a>&lt;'a&gt;
3+
<span class="where">where Self: 'a</span>;
34

45
// Provided methods
5-
fn <a href="#method.func" class="fn">func</a>(self)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="where">where Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
6-
<span class="item-spacer" /> fn <a href="#method.lines" class="fn">lines</a>(self) -&gt; <a class="struct" href="{{channel}}/std/io/struct.Lines.html" title="struct std::io::Lines">Lines</a>&lt;Self&gt;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="where">where Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
7-
<span class="item-spacer" /> fn <a href="#method.merge" class="fn">merge</a>&lt;T&gt;(self, a: T)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="where">where Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;T: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
6+
fn <a href="#method.func" class="fn">func</a>(self)
7+
<span class="where">where Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
8+
<span class="item-spacer" /> fn <a href="#method.lines" class="fn">lines</a>(self) -&gt; <a class="struct" href="{{channel}}/std/io/struct.Lines.html" title="struct std::io::Lines">Lines</a>&lt;Self&gt;
9+
<span class="where">where Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
10+
<span class="item-spacer" /> fn <a href="#method.merge" class="fn">merge</a>&lt;T&gt;(self, a: T)
11+
<span class="where">where Self: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
12+
T: <a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span> { ... }
813
}</code></pre>

tests/rustdoc/where.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::Lines;
44

55
pub trait MyTrait { fn dummy(&self) { } }
66

7-
// @has foo/struct.Alpha.html '//pre' "pub struct Alpha<A>(_)where A: MyTrait"
7+
// @has foo/struct.Alpha.html '//pre' "pub struct Alpha<A>(_) where A: MyTrait"
88
pub struct Alpha<A>(A) where A: MyTrait;
99
// @has foo/trait.Bravo.html '//pre' "pub trait Bravo<B>where B: MyTrait"
1010
pub trait Bravo<B> where B: MyTrait { fn get(&self, B: B); }

0 commit comments

Comments
 (0)