Skip to content

Commit

Permalink
Unrolled build for rust-lang#117869
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
rust-timer authored Dec 3, 2023
2 parents d12dc74 + 06695ea commit 92531fc
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 14 deletions.
22 changes: 14 additions & 8 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>(
matches!(*field.kind, clean::StrippedItem(box clean::StructFieldItem(..)))
})
{
return f.write_str("/* private fields */");
return f.write_str("<span class=\"comment\">/* private fields */</span>");
}

for (i, ty) in s.iter().enumerate() {
Expand Down Expand Up @@ -1666,7 +1666,7 @@ fn render_enum_fields(
}

if variants_stripped && !is_non_exhaustive {
w.write_str(" // some variants omitted\n");
w.write_str(" <span class=\"comment\">// some variants omitted</span>\n");
}
if toggle {
toggle_close(&mut w);
Expand Down Expand Up @@ -1811,15 +1811,21 @@ fn item_proc_macro(
let name = it.name.expect("proc-macros always have names");
match m.kind {
MacroKind::Bang => {
write!(buffer, "{name}!() {{ /* proc-macro */ }}").unwrap();
write!(buffer, "{name}!() {{ <span class=\"comment\">/* proc-macro */</span> }}")
.unwrap();
}
MacroKind::Attr => {
write!(buffer, "#[{name}]").unwrap();
}
MacroKind::Derive => {
write!(buffer, "#[derive({name})]").unwrap();
if !m.helpers.is_empty() {
buffer.write_str("\n{\n // Attributes available to this derive:\n").unwrap();
buffer
.write_str(
"\n{\n \
<span class=\"comment\">// Attributes available to this derive:</span>\n",
)
.unwrap();
for attr in &m.helpers {
writeln!(buffer, " #[{attr}]").unwrap();
}
Expand Down Expand Up @@ -2181,7 +2187,7 @@ fn render_union<'a, 'cx: 'a>(
}

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

if has_visible_fields {
if has_stripped_entries {
write!(w, "\n{tab} /* private fields */");
write!(w, "\n{tab} <span class=\"comment\">/* private fields */</span>");
}
write!(w, "\n{tab}");
} else if has_stripped_entries {
write!(w, " /* private fields */ ");
write!(w, " <span class=\"comment\">/* private fields */</span> ");
}
if toggle {
toggle_close(&mut w);
Expand All @@ -2285,7 +2291,7 @@ fn render_struct_fields(
matches!(*field.kind, clean::StrippedItem(box clean::StructFieldItem(..)))
})
{
write!(w, "/* private fields */");
write!(w, "<span class=\"comment\">/* private fields */</span>");
} else {
for (i, field) in fields.iter().enumerate() {
if i > 0 {
Expand Down
73 changes: 73 additions & 0 deletions tests/rustdoc-gui/item-decl-comment-highlighting.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// This test checks that comments in item declarations are highlighted.
go-to: "file://" + |DOC_PATH| + "/test_docs/private/enum.Enum.html"
show-text: true

define-function: (
"check-item-decl-comment",
(theme, url, comment_color),
block {
go-to: |url|
set-local-storage: {"rustdoc-theme": |theme|, "rustdoc-use-system-theme": "false"}
reload:
assert-css: (".item-decl .comment", {"color": |comment_color|}, ALL)
}
)

define-function: (
"check-items-for-theme",
(theme, comment_color),
block {
call-function: ("check-item-decl-comment", {
"theme": |theme|,
"url": "file://" + |DOC_PATH| + "/test_docs/private/enum.Enum.html",
"comment_color": |comment_color|,
})
call-function: ("check-item-decl-comment", {
"theme": |theme|,
"url": "file://" + |DOC_PATH| + "/test_docs/private/struct.Struct.html",
"comment_color": |comment_color|,
})
call-function: ("check-item-decl-comment", {
"theme": |theme|,
"url": "file://" + |DOC_PATH| + "/test_docs/private/struct.Tuple.html",
"comment_color": |comment_color|,
})
call-function: ("check-item-decl-comment", {
"theme": |theme|,
"url": "file://" + |DOC_PATH| + "/test_docs/private/union.Union.html",
"comment_color": |comment_color|,
})
call-function: ("check-item-decl-comment", {
"theme": |theme|,
"url": "file://" + |DOC_PATH| + "/proc_macro_test/macro.make_answer.html",
"comment_color": |comment_color|,
})
call-function: ("check-item-decl-comment", {
"theme": |theme|,
"url": "file://" + |DOC_PATH| + "/proc_macro_test/derive.HelperAttr.html",
"comment_color": |comment_color|,
})
}
)

call-function: (
"check-items-for-theme",
{
"theme": "ayu",
"comment_color": "#788797",
}
)
call-function: (
"check-items-for-theme",
{
"theme": "dark",
"comment_color": "#8d8d8b",
}
)
call-function: (
"check-items-for-theme",
{
"theme": "light",
"comment_color": "#8e908c",
}
)
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/sidebar-source-code.goml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ assert: "//*[@class='dir-entry' and @open]/*[text()='sub_mod']"
// Only "another_folder" should be "open" in "lib2".
assert: "//*[@class='dir-entry' and not(@open)]/*[text()='another_mod']"
// All other trees should be collapsed.
assert-count: ("//*[@id='src-sidebar']/details[not(text()='lib2') and not(@open)]", 10)
assert-count: ("//*[@id='src-sidebar']/details[not(text()='lib2') and not(@open)]", 11)

// We now switch to mobile mode.
set-window-size: (600, 600)
Expand Down
7 changes: 7 additions & 0 deletions tests/rustdoc-gui/src/proc_macro_test/Cargo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "proc_macro_test"
version = "0.1.0"
8 changes: 8 additions & 0 deletions tests/rustdoc-gui/src/proc_macro_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "proc_macro_test"
version = "0.1.0"
edition = "2021"

[lib]
path = "lib.rs"
proc-macro = true
11 changes: 11 additions & 0 deletions tests/rustdoc-gui/src/proc_macro_test/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use proc_macro::TokenStream;

#[proc_macro]
pub fn make_answer(_item: TokenStream) -> TokenStream {
"fn answer() -> u32 { 42 }".parse().unwrap()
}

#[proc_macro_derive(HelperAttr, attributes(helper))]
pub fn derive_helper_attr(_item: TokenStream) -> TokenStream {
TokenStream::new()
}
18 changes: 18 additions & 0 deletions tests/rustdoc-gui/src/test_docs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,21 @@ pub mod foreign_impl_order {
fn f(&mut self, fg: [u8; 3]) {}
}
}

pub mod private {
pub struct Tuple(u32, u8);
pub struct Struct {
a: u8,
}

pub union Union {
a: u8,
b: u16,
}

pub enum Enum {
A,
#[doc(hidden)]
B,
}
}
4 changes: 2 additions & 2 deletions tests/rustdoc/where.SWhere_Simd_item-decl.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<pre class="rust item-decl"><code>pub struct Simd&lt;T&gt;(/* private fields */)
<pre class="rust item-decl"><code>pub struct Simd&lt;T&gt;(<span class="comment">/* private fields */</span>)
<span class="where">where
T: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code></pre>
T: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code></pre>
2 changes: 1 addition & 1 deletion tests/rustdoc/where.alpha_trait_decl.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<code>pub struct Alpha&lt;A&gt;(/* private fields */)
<code>pub struct Alpha&lt;A&gt;(<span class="comment">/* private fields */</span>)
<span class="where">where
A: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code>
2 changes: 1 addition & 1 deletion tests/rustdoc/whitespace-after-where-clause.union.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<pre class="rust item-decl"><code>pub union Union&lt;'a, B&gt;<div class="where">where
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>{
/* private fields */
<span class="comment">/* private fields */</span>
}</code></pre>
2 changes: 1 addition & 1 deletion tests/rustdoc/whitespace-after-where-clause.union2.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<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; {
/* private fields */
<span class="comment">/* private fields */</span>
}</code></pre>

0 comments on commit 92531fc

Please sign in to comment.