Skip to content

Commit f7210b3

Browse files
Rollup merge of #107615 - notriddle:notriddle/nbsp, r=GuillaumeGomez
Replace nbsp in all rustdoc code blocks Based on #106125 by `@dtolnay` — this PR fixes the line wrapping bug. Fixes #106098. This makes code copyable from rustdoc rendered documentation into a Rust source file.
2 parents 1594b58 + 784665d commit f7210b3

8 files changed

+19
-17
lines changed

src/librustdoc/html/format.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,15 @@ impl clean::GenericParamDef {
208208
if f.alternate() {
209209
write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
210210
} else {
211-
write!(f, ": {}", print_generic_bounds(bounds, cx))?;
211+
write!(f, ": {}", print_generic_bounds(bounds, cx))?;
212212
}
213213
}
214214

215215
if let Some(ref ty) = default {
216216
if f.alternate() {
217217
write!(f, " = {:#}", ty.print(cx))?;
218218
} else {
219-
write!(f, " = {}", ty.print(cx))?;
219+
write!(f, " = {}", ty.print(cx))?;
220220
}
221221
}
222222

@@ -226,14 +226,14 @@ impl clean::GenericParamDef {
226226
if f.alternate() {
227227
write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
228228
} else {
229-
write!(f, "const {}: {}", self.name, ty.print(cx))?;
229+
write!(f, "const {}: {}", self.name, ty.print(cx))?;
230230
}
231231

232232
if let Some(default) = default {
233233
if f.alternate() {
234234
write!(f, " = {:#}", default)?;
235235
} else {
236-
write!(f, " = {}", default)?;
236+
write!(f, " = {}", default)?;
237237
}
238238
}
239239

@@ -354,12 +354,12 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
354354
let mut br_with_padding = String::with_capacity(6 * indent + 28);
355355
br_with_padding.push_str("<br>");
356356
for _ in 0..indent + 4 {
357-
br_with_padding.push_str("&nbsp;");
357+
br_with_padding.push_str(" ");
358358
}
359359
let where_preds = where_preds.to_string().replace("<br>", &br_with_padding);
360360

361361
if ending == Ending::Newline {
362-
let mut clause = "&nbsp;".repeat(indent.saturating_sub(1));
362+
let mut clause = " ".repeat(indent.saturating_sub(1));
363363
write!(clause, "<span class=\"where fmt-newline\">where{where_preds},</span>")?;
364364
clause
365365
} else {
@@ -368,7 +368,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
368368
format!("<br><span class=\"where\">where{where_preds}</span>")
369369
} else {
370370
let mut clause = br_with_padding;
371-
clause.truncate(clause.len() - 4 * "&nbsp;".len());
371+
clause.truncate(clause.len() - 4);
372372
write!(clause, "<span class=\"where\">where{where_preds}</span>")?;
373373
clause
374374
}
@@ -1391,8 +1391,8 @@ impl clean::FnDecl {
13911391

13921392
let declaration_len = header_len + args_plain.len() + arrow_plain.len();
13931393
let output = if declaration_len > 80 {
1394-
let full_pad = format!("<br>{}", "&nbsp;".repeat(indent + 4));
1395-
let close_pad = format!("<br>{}", "&nbsp;".repeat(indent));
1394+
let full_pad = format!("<br>{}", " ".repeat(indent + 4));
1395+
let close_pad = format!("<br>{}", " ".repeat(indent));
13961396
format!(
13971397
"({pad}{args}{close}){arrow}",
13981398
pad = if self.inputs.values.is_empty() { "" } else { &full_pad },
@@ -1611,7 +1611,7 @@ impl clean::TypeBinding {
16111611
if f.alternate() {
16121612
write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
16131613
} else {
1614-
write!(f, ":&nbsp;{}", print_generic_bounds(bounds, cx))?;
1614+
write!(f, ": {}", print_generic_bounds(bounds, cx))?;
16151615
}
16161616
}
16171617
}

src/librustdoc/html/render/print_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
11571157
fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item]) {
11581158
for (i, ty) in s.iter().enumerate() {
11591159
if i > 0 {
1160-
w.write_str(",&nbsp;");
1160+
w.write_str(", ");
11611161
}
11621162
match *ty.kind {
11631163
clean::StrippedItem(box clean::StructFieldItem(_)) => w.write_str("_"),
@@ -1297,7 +1297,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
12971297
"<div class=\"sub-variant-field\">\
12981298
<span id=\"{id}\" class=\"small-section-header\">\
12991299
<a href=\"#{id}\" class=\"anchor field\">§</a>\
1300-
<code>{f}:&nbsp;{t}</code>\
1300+
<code>{f}: {t}</code>\
13011301
</span>",
13021302
id = id,
13031303
f = field.name.unwrap(),

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

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ h4.code-header {
184184
font-weight: 600;
185185
margin: 0;
186186
padding: 0;
187+
white-space: pre-wrap;
187188
}
188189

189190
#crate-search,
@@ -642,6 +643,7 @@ pre, .rustdoc.source .example-wrap {
642643
.fn .where,
643644
.where.fmt-newline {
644645
display: block;
646+
white-space: pre-wrap;
645647
font-size: 0.875rem;
646648
}
647649

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<script type="text/json" id="notable-traits-data">{"SomeStruct":"&lt;h3&gt;Notable traits for &lt;code&gt;&lt;a class=\"struct\" href=\"struct.SomeStruct.html\" title=\"struct doc_notable_trait::SomeStruct\"&gt;SomeStruct&lt;/a&gt;&lt;/code&gt;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=\"where fmt-newline\"&gt;impl &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt; for &lt;a class=\"struct\" href=\"struct.SomeStruct.html\" title=\"struct doc_notable_trait::SomeStruct\"&gt;SomeStruct&lt;/a&gt;&lt;/span&gt;","Wrapper&lt;Self&gt;":"&lt;h3&gt;Notable traits for &lt;code&gt;&lt;a class=\"struct\" href=\"struct.Wrapper.html\" title=\"struct doc_notable_trait::Wrapper\"&gt;Wrapper&lt;/a&gt;&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=\"where fmt-newline\"&gt;impl&amp;lt;T:&amp;nbsp;&lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt;&amp;gt; &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt; for &lt;a class=\"struct\" href=\"struct.Wrapper.html\" title=\"struct doc_notable_trait::Wrapper\"&gt;Wrapper&lt;/a&gt;&amp;lt;T&amp;gt;&lt;/span&gt;"}</script>
1+
<script type="text/json" id="notable-traits-data">{"SomeStruct":"&lt;h3&gt;Notable traits for &lt;code&gt;&lt;a class=\"struct\" href=\"struct.SomeStruct.html\" title=\"struct doc_notable_trait::SomeStruct\"&gt;SomeStruct&lt;/a&gt;&lt;/code&gt;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=\"where fmt-newline\"&gt;impl &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt; for &lt;a class=\"struct\" href=\"struct.SomeStruct.html\" title=\"struct doc_notable_trait::SomeStruct\"&gt;SomeStruct&lt;/a&gt;&lt;/span&gt;","Wrapper&lt;Self&gt;":"&lt;h3&gt;Notable traits for &lt;code&gt;&lt;a class=\"struct\" href=\"struct.Wrapper.html\" title=\"struct doc_notable_trait::Wrapper\"&gt;Wrapper&lt;/a&gt;&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=\"where fmt-newline\"&gt;impl&amp;lt;T: &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt;&amp;gt; &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt; for &lt;a class=\"struct\" href=\"struct.Wrapper.html\" title=\"struct doc_notable_trait::Wrapper\"&gt;Wrapper&lt;/a&gt;&amp;lt;T&amp;gt;&lt;/span&gt;"}</script>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<script type="text/json" id="notable-traits-data">{"Wrapper&lt;Self&gt;":"&lt;h3&gt;Notable traits for &lt;code&gt;&lt;a class=\"struct\" href=\"struct.Wrapper.html\" title=\"struct doc_notable_trait::Wrapper\"&gt;Wrapper&lt;/a&gt;&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=\"where fmt-newline\"&gt;impl&amp;lt;T:&amp;nbsp;&lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt;&amp;gt; &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt; for &lt;a class=\"struct\" href=\"struct.Wrapper.html\" title=\"struct doc_notable_trait::Wrapper\"&gt;Wrapper&lt;/a&gt;&amp;lt;T&amp;gt;&lt;/span&gt;"}</script>
1+
<script type="text/json" id="notable-traits-data">{"Wrapper&lt;Self&gt;":"&lt;h3&gt;Notable traits for &lt;code&gt;&lt;a class=\"struct\" href=\"struct.Wrapper.html\" title=\"struct doc_notable_trait::Wrapper\"&gt;Wrapper&lt;/a&gt;&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=\"where fmt-newline\"&gt;impl&amp;lt;T: &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt;&amp;gt; &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait::SomeTrait\"&gt;SomeTrait&lt;/a&gt; for &lt;a class=\"struct\" href=\"struct.Wrapper.html\" title=\"struct doc_notable_trait::Wrapper\"&gt;Wrapper&lt;/a&gt;&amp;lt;T&amp;gt;&lt;/span&gt;"}</script>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<pre class="rust item-decl"><code>pub enum Cow2&lt;'a, B:&#160;?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>&gt; + 'a&gt; {
1+
<pre class="rust item-decl"><code>pub enum Cow2&lt;'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>&gt; + 'a&gt; {
22
Borrowed(<a class="primitive" href="{{channel}}/std/primitive.reference.html">&amp;'a B</a>),
33
Whatever(<a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>),
44
}</code></pre>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<pre class="rust item-decl"><code>pub struct Struct2&lt;'a, B:&#160;?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>&gt; + 'a&gt; {
1+
<pre class="rust item-decl"><code>pub struct Struct2&lt;'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>&gt; + 'a&gt; {
22
pub a: <a class="primitive" href="{{channel}}/std/primitive.reference.html">&amp;'a B</a>,
33
pub b: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>,
44
}</code></pre>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<pre class="rust item-decl"><code>pub union Union2&lt;'a, B:&#160;?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>&gt; + 'a&gt; {
1+
<pre class="rust item-decl"><code>pub union Union2&lt;'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>&gt; + 'a&gt; {
22
/* private fields */
33
}</code></pre>

0 commit comments

Comments
 (0)