From 05bf5b764ab032c7173810dadc3111f6041492a7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 13 Nov 2023 11:59:52 +0100 Subject: [PATCH 1/3] Add highlighting for comments in items declaration --- src/librustdoc/html/render/print_item.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index e4309c782f6a3..131b1d608e683 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -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("/* private fields */"); } for (i, ty) in s.iter().enumerate() { @@ -1666,7 +1666,7 @@ fn render_enum_fields( } if variants_stripped && !is_non_exhaustive { - w.write_str(" // some variants omitted\n"); + w.write_str(" // some variants omitted\n"); } if toggle { toggle_close(&mut w); @@ -1811,7 +1811,8 @@ 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}!() {{ /* proc-macro */ }}") + .unwrap(); } MacroKind::Attr => { write!(buffer, "#[{name}]").unwrap(); @@ -1819,7 +1820,12 @@ fn item_proc_macro( 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 \ + // Attributes available to this derive:\n", + ) + .unwrap(); for attr in &m.helpers { writeln!(buffer, " #[{attr}]").unwrap(); } @@ -2181,7 +2187,7 @@ fn render_union<'a, 'cx: 'a>( } if it.has_stripped_entries().unwrap() { - write!(f, " /* private fields */\n")?; + write!(f, " /* private fields */\n")?; } if toggle { toggle_close(&mut f); @@ -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} /* private fields */"); } write!(w, "\n{tab}"); } else if has_stripped_entries { - write!(w, " /* private fields */ "); + write!(w, " /* private fields */ "); } if toggle { toggle_close(&mut w); @@ -2285,7 +2291,7 @@ fn render_struct_fields( matches!(*field.kind, clean::StrippedItem(box clean::StructFieldItem(..))) }) { - write!(w, "/* private fields */"); + write!(w, "/* private fields */"); } else { for (i, field) in fields.iter().enumerate() { if i > 0 { From 768a614380813a66c5b97ca4a02b721ea7916868 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 13 Nov 2023 12:00:17 +0100 Subject: [PATCH 2/3] Add GUI tests for comments highlighting in items declaration --- .../item-decl-comment-highlighting.goml | 73 +++++++++++++++++++ tests/rustdoc-gui/sidebar-source-code.goml | 2 +- .../src/proc_macro_test/Cargo.lock | 7 ++ .../src/proc_macro_test/Cargo.toml | 8 ++ tests/rustdoc-gui/src/proc_macro_test/lib.rs | 11 +++ tests/rustdoc-gui/src/test_docs/lib.rs | 18 +++++ 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tests/rustdoc-gui/item-decl-comment-highlighting.goml create mode 100644 tests/rustdoc-gui/src/proc_macro_test/Cargo.lock create mode 100644 tests/rustdoc-gui/src/proc_macro_test/Cargo.toml create mode 100644 tests/rustdoc-gui/src/proc_macro_test/lib.rs diff --git a/tests/rustdoc-gui/item-decl-comment-highlighting.goml b/tests/rustdoc-gui/item-decl-comment-highlighting.goml new file mode 100644 index 0000000000000..60772693d6c5c --- /dev/null +++ b/tests/rustdoc-gui/item-decl-comment-highlighting.goml @@ -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", + } +) diff --git a/tests/rustdoc-gui/sidebar-source-code.goml b/tests/rustdoc-gui/sidebar-source-code.goml index 92b9045b734d7..0d72e670cf435 100644 --- a/tests/rustdoc-gui/sidebar-source-code.goml +++ b/tests/rustdoc-gui/sidebar-source-code.goml @@ -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) diff --git a/tests/rustdoc-gui/src/proc_macro_test/Cargo.lock b/tests/rustdoc-gui/src/proc_macro_test/Cargo.lock new file mode 100644 index 0000000000000..eae9d75367f75 --- /dev/null +++ b/tests/rustdoc-gui/src/proc_macro_test/Cargo.lock @@ -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" diff --git a/tests/rustdoc-gui/src/proc_macro_test/Cargo.toml b/tests/rustdoc-gui/src/proc_macro_test/Cargo.toml new file mode 100644 index 0000000000000..768ced65184e8 --- /dev/null +++ b/tests/rustdoc-gui/src/proc_macro_test/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "proc_macro_test" +version = "0.1.0" +edition = "2021" + +[lib] +path = "lib.rs" +proc-macro = true diff --git a/tests/rustdoc-gui/src/proc_macro_test/lib.rs b/tests/rustdoc-gui/src/proc_macro_test/lib.rs new file mode 100644 index 0000000000000..8a6c62df87c4b --- /dev/null +++ b/tests/rustdoc-gui/src/proc_macro_test/lib.rs @@ -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() +} diff --git a/tests/rustdoc-gui/src/test_docs/lib.rs b/tests/rustdoc-gui/src/test_docs/lib.rs index c7d115bdb98c3..0bc777230bf1b 100644 --- a/tests/rustdoc-gui/src/test_docs/lib.rs +++ b/tests/rustdoc-gui/src/test_docs/lib.rs @@ -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, + } +} From 06695ea43614f7d8234f720d0f480cf5926ffffe Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 13 Nov 2023 13:57:05 +0100 Subject: [PATCH 3/3] Update snapshots of rustdoc tests to take into account the comment highlighting --- tests/rustdoc/where.SWhere_Simd_item-decl.html | 4 ++-- tests/rustdoc/where.alpha_trait_decl.html | 2 +- tests/rustdoc/whitespace-after-where-clause.union.html | 2 +- tests/rustdoc/whitespace-after-where-clause.union2.html | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/rustdoc/where.SWhere_Simd_item-decl.html b/tests/rustdoc/where.SWhere_Simd_item-decl.html index 46708b9e4e925..1987b1d59f516 100644 --- a/tests/rustdoc/where.SWhere_Simd_item-decl.html +++ b/tests/rustdoc/where.SWhere_Simd_item-decl.html @@ -1,3 +1,3 @@ -
pub struct Simd<T>(/* private fields */)
+
pub struct Simd<T>(/* private fields */)
 where
-    T: MyTrait;
+ T: MyTrait;
\ No newline at end of file diff --git a/tests/rustdoc/where.alpha_trait_decl.html b/tests/rustdoc/where.alpha_trait_decl.html index 0c0b2d1ceca9d..2c010ca7c2d94 100644 --- a/tests/rustdoc/where.alpha_trait_decl.html +++ b/tests/rustdoc/where.alpha_trait_decl.html @@ -1,3 +1,3 @@ -pub struct Alpha<A>(/* private fields */) +pub struct Alpha<A>(/* private fields */) where A: MyTrait; \ No newline at end of file diff --git a/tests/rustdoc/whitespace-after-where-clause.union.html b/tests/rustdoc/whitespace-after-where-clause.union.html index 7e0d5f8717a79..e63374760d998 100644 --- a/tests/rustdoc/whitespace-after-where-clause.union.html +++ b/tests/rustdoc/whitespace-after-where-clause.union.html @@ -1,4 +1,4 @@
pub union Union<'a, B>
where B: ToOwned<()> + ?Sized + 'a,
{ - /* private fields */ + /* private fields */ }
\ No newline at end of file diff --git a/tests/rustdoc/whitespace-after-where-clause.union2.html b/tests/rustdoc/whitespace-after-where-clause.union2.html index 177a161b83a62..da984343daadc 100644 --- a/tests/rustdoc/whitespace-after-where-clause.union2.html +++ b/tests/rustdoc/whitespace-after-where-clause.union2.html @@ -1,3 +1,3 @@
pub union Union2<'a, B: ?Sized + ToOwned<()> + 'a> {
-    /* private fields */
+    /* private fields */
 }
\ No newline at end of file