Skip to content

Commit 92531fc

Browse files
authored
Unrolled build for rust-lang#117869
Rollup merge of rust-lang#117869 - GuillaumeGomez:comment-highlighting-item-decl, r=notriddle [rustdoc] Add highlighting for comments in items declaration Fixes rust-lang#117555. So after the discussion in rust-lang#117643, the outcome was that having the comments in the item declaration at the same level (in term of color) as the rest of the code was actually a bit distracting and could be improved. The current highlighting color for comments is "lighter" than the rest and I think it fits perfectly to improve the current situation. With this, we now have different "levels" which makes it easier to read and filter out what we want when reading the items declaration. Here's a screenshot: ![image](https://github.com/rust-lang/rust/assets/3050060/dbd98029-e98b-4997-9a89-6b823eaac9a4) r? `@notriddle`
2 parents d12dc74 + 06695ea commit 92531fc

11 files changed

+137
-14
lines changed

src/librustdoc/html/render/print_item.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>(
15091509
matches!(*field.kind, clean::StrippedItem(box clean::StructFieldItem(..)))
15101510
})
15111511
{
1512-
return f.write_str("/* private fields */");
1512+
return f.write_str("<span class=\"comment\">/* private fields */</span>");
15131513
}
15141514

15151515
for (i, ty) in s.iter().enumerate() {
@@ -1666,7 +1666,7 @@ fn render_enum_fields(
16661666
}
16671667

16681668
if variants_stripped && !is_non_exhaustive {
1669-
w.write_str(" // some variants omitted\n");
1669+
w.write_str(" <span class=\"comment\">// some variants omitted</span>\n");
16701670
}
16711671
if toggle {
16721672
toggle_close(&mut w);
@@ -1811,15 +1811,21 @@ fn item_proc_macro(
18111811
let name = it.name.expect("proc-macros always have names");
18121812
match m.kind {
18131813
MacroKind::Bang => {
1814-
write!(buffer, "{name}!() {{ /* proc-macro */ }}").unwrap();
1814+
write!(buffer, "{name}!() {{ <span class=\"comment\">/* proc-macro */</span> }}")
1815+
.unwrap();
18151816
}
18161817
MacroKind::Attr => {
18171818
write!(buffer, "#[{name}]").unwrap();
18181819
}
18191820
MacroKind::Derive => {
18201821
write!(buffer, "#[derive({name})]").unwrap();
18211822
if !m.helpers.is_empty() {
1822-
buffer.write_str("\n{\n // Attributes available to this derive:\n").unwrap();
1823+
buffer
1824+
.write_str(
1825+
"\n{\n \
1826+
<span class=\"comment\">// Attributes available to this derive:</span>\n",
1827+
)
1828+
.unwrap();
18231829
for attr in &m.helpers {
18241830
writeln!(buffer, " #[{attr}]").unwrap();
18251831
}
@@ -2181,7 +2187,7 @@ fn render_union<'a, 'cx: 'a>(
21812187
}
21822188

21832189
if it.has_stripped_entries().unwrap() {
2184-
write!(f, " /* private fields */\n")?;
2190+
write!(f, " <span class=\"comment\">/* private fields */</span>\n")?;
21852191
}
21862192
if toggle {
21872193
toggle_close(&mut f);
@@ -2267,11 +2273,11 @@ fn render_struct_fields(
22672273

22682274
if has_visible_fields {
22692275
if has_stripped_entries {
2270-
write!(w, "\n{tab} /* private fields */");
2276+
write!(w, "\n{tab} <span class=\"comment\">/* private fields */</span>");
22712277
}
22722278
write!(w, "\n{tab}");
22732279
} else if has_stripped_entries {
2274-
write!(w, " /* private fields */ ");
2280+
write!(w, " <span class=\"comment\">/* private fields */</span> ");
22752281
}
22762282
if toggle {
22772283
toggle_close(&mut w);
@@ -2285,7 +2291,7 @@ fn render_struct_fields(
22852291
matches!(*field.kind, clean::StrippedItem(box clean::StructFieldItem(..)))
22862292
})
22872293
{
2288-
write!(w, "/* private fields */");
2294+
write!(w, "<span class=\"comment\">/* private fields */</span>");
22892295
} else {
22902296
for (i, field) in fields.iter().enumerate() {
22912297
if i > 0 {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// This test checks that comments in item declarations are highlighted.
2+
go-to: "file://" + |DOC_PATH| + "/test_docs/private/enum.Enum.html"
3+
show-text: true
4+
5+
define-function: (
6+
"check-item-decl-comment",
7+
(theme, url, comment_color),
8+
block {
9+
go-to: |url|
10+
set-local-storage: {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}
11+
reload:
12+
assert-css: (".item-decl .comment", {"color": |comment_color|}, ALL)
13+
}
14+
)
15+
16+
define-function: (
17+
"check-items-for-theme",
18+
(theme, comment_color),
19+
block {
20+
call-function: ("check-item-decl-comment", {
21+
"theme": |theme|,
22+
"url": "file://" + |DOC_PATH| + "/test_docs/private/enum.Enum.html",
23+
"comment_color": |comment_color|,
24+
})
25+
call-function: ("check-item-decl-comment", {
26+
"theme": |theme|,
27+
"url": "file://" + |DOC_PATH| + "/test_docs/private/struct.Struct.html",
28+
"comment_color": |comment_color|,
29+
})
30+
call-function: ("check-item-decl-comment", {
31+
"theme": |theme|,
32+
"url": "file://" + |DOC_PATH| + "/test_docs/private/struct.Tuple.html",
33+
"comment_color": |comment_color|,
34+
})
35+
call-function: ("check-item-decl-comment", {
36+
"theme": |theme|,
37+
"url": "file://" + |DOC_PATH| + "/test_docs/private/union.Union.html",
38+
"comment_color": |comment_color|,
39+
})
40+
call-function: ("check-item-decl-comment", {
41+
"theme": |theme|,
42+
"url": "file://" + |DOC_PATH| + "/proc_macro_test/macro.make_answer.html",
43+
"comment_color": |comment_color|,
44+
})
45+
call-function: ("check-item-decl-comment", {
46+
"theme": |theme|,
47+
"url": "file://" + |DOC_PATH| + "/proc_macro_test/derive.HelperAttr.html",
48+
"comment_color": |comment_color|,
49+
})
50+
}
51+
)
52+
53+
call-function: (
54+
"check-items-for-theme",
55+
{
56+
"theme": "ayu",
57+
"comment_color": "#788797",
58+
}
59+
)
60+
call-function: (
61+
"check-items-for-theme",
62+
{
63+
"theme": "dark",
64+
"comment_color": "#8d8d8b",
65+
}
66+
)
67+
call-function: (
68+
"check-items-for-theme",
69+
{
70+
"theme": "light",
71+
"comment_color": "#8e908c",
72+
}
73+
)

tests/rustdoc-gui/sidebar-source-code.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ assert: "//*[@class='dir-entry' and @open]/*[text()='sub_mod']"
7373
// Only "another_folder" should be "open" in "lib2".
7474
assert: "//*[@class='dir-entry' and not(@open)]/*[text()='another_mod']"
7575
// All other trees should be collapsed.
76-
assert-count: ("//*[@id='src-sidebar']/details[not(text()='lib2') and not(@open)]", 10)
76+
assert-count: ("//*[@id='src-sidebar']/details[not(text()='lib2') and not(@open)]", 11)
7777

7878
// We now switch to mobile mode.
7979
set-window-size: (600, 600)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 3
4+
5+
[[package]]
6+
name = "proc_macro_test"
7+
version = "0.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "proc_macro_test"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
path = "lib.rs"
8+
proc-macro = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use proc_macro::TokenStream;
2+
3+
#[proc_macro]
4+
pub fn make_answer(_item: TokenStream) -> TokenStream {
5+
"fn answer() -> u32 { 42 }".parse().unwrap()
6+
}
7+
8+
#[proc_macro_derive(HelperAttr, attributes(helper))]
9+
pub fn derive_helper_attr(_item: TokenStream) -> TokenStream {
10+
TokenStream::new()
11+
}

tests/rustdoc-gui/src/test_docs/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,21 @@ pub mod foreign_impl_order {
593593
fn f(&mut self, fg: [u8; 3]) {}
594594
}
595595
}
596+
597+
pub mod private {
598+
pub struct Tuple(u32, u8);
599+
pub struct Struct {
600+
a: u8,
601+
}
602+
603+
pub union Union {
604+
a: u8,
605+
b: u16,
606+
}
607+
608+
pub enum Enum {
609+
A,
610+
#[doc(hidden)]
611+
B,
612+
}
613+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<pre class="rust item-decl"><code>pub struct Simd&lt;T&gt;(/* private fields */)
1+
<pre class="rust item-decl"><code>pub struct Simd&lt;T&gt;(<span class="comment">/* private fields */</span>)
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>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<code>pub struct Alpha&lt;A&gt;(/* private fields */)
1+
<code>pub struct Alpha&lt;A&gt;(<span class="comment">/* private fields */</span>)
22
<span class="where">where
33
A: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<pre class="rust item-decl"><code>pub union Union&lt;'a, B&gt;<div class="where">where
22
B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>&gt; + ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a,</div>{
3-
/* private fields */
3+
<span class="comment">/* private fields */</span>
44
}</code></pre>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<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;<a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>&gt; + 'a&gt; {
2-
/* private fields */
2+
<span class="comment">/* private fields */</span>
33
}</code></pre>

0 commit comments

Comments
 (0)