From 04a10f76106a5f80cfebb596bf2c762d6c6c8626 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 27 Oct 2025 14:34:57 +0100 Subject: [PATCH 1/2] Simplify code to generate line numbers in highlight --- src/librustdoc/html/highlight.rs | 56 +++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index c37736f137df9..1a59456a5322e 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -343,7 +343,7 @@ struct TokenHandler<'a, 'tcx, F: Write> { /// We need to keep the `Class` for each element because it could contain a `Span` which is /// used to generate links. href_context: Option>, - write_line_number: fn(u32) -> String, + write_line_number: LineNumberKind, line: u32, max_lines: u32, } @@ -355,10 +355,10 @@ impl std::fmt::Debug for TokenHandler<'_, '_, F> { } impl<'a, F: Write> TokenHandler<'a, '_, F> { - fn handle_backline(&mut self) -> Option { + fn handle_backline(&mut self) -> Option> { self.line += 1; if self.line < self.max_lines { - return Some((self.write_line_number)(self.line)); + return Some(self.write_line_number.render(self.line)); } None } @@ -376,8 +376,7 @@ impl<'a, F: Write> TokenHandler<'a, '_, F> { if text == "\n" && let Some(backline) = self.handle_backline() { - self.out.write_str(&text).unwrap(); - self.out.write_str(&backline).unwrap(); + write!(self.out, "{text}{backline}").unwrap(); } else { self.push_token_without_backline_check(class, text, true); } @@ -437,20 +436,29 @@ impl Drop for TokenHandler<'_, '_, F> { } } -fn scraped_line_number(line: u32) -> String { - // https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr - // Do not show "1 2 3 4 5 ..." in web search results. - format!("{line}") -} - -fn line_number(line: u32) -> String { - // https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr - // Do not show "1 2 3 4 5 ..." in web search results. - format!("{line}") +/// Represents line numbers to be generated as HTML. +#[derive(Clone, Copy)] +enum LineNumberKind { + /// Used for scraped code examples. + Scraped, + /// Used for source code pages. + Normal, + /// Code examples in documentation don't have line number generated by rustdoc. + Empty, } -fn empty_line_number(_: u32) -> String { - String::new() +impl LineNumberKind { + fn render(self, line: u32) -> impl Display { + fmt::from_fn(move |f| { + match self { + // https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr + // Do not show "1 2 3 4 5 ..." in web search results. + Self::Scraped => write!(f, "{line}"), + Self::Normal => write!(f, "{line}"), + Self::Empty => Ok(()), + } + }) + } } fn get_next_expansion( @@ -537,12 +545,12 @@ pub(super) fn write_code( write_line_number: match line_info { Some(line_info) => { if line_info.is_scraped_example { - scraped_line_number + LineNumberKind::Scraped } else { - line_number + LineNumberKind::Normal } } - None => empty_line_number, + None => LineNumberKind::Empty, }, line: 0, max_lines: u32::MAX, @@ -552,8 +560,12 @@ pub(super) fn write_code( if let Some(line_info) = line_info { token_handler.line = line_info.start_line - 1; token_handler.max_lines = line_info.max_lines; - if let Some(text) = token_handler.handle_backline() { - token_handler.push_token_without_backline_check(None, Cow::Owned(text), false); + if let Some(backline) = token_handler.handle_backline() { + token_handler.push_token_without_backline_check( + None, + Cow::Owned(backline.to_string()), + false, + ); } } From 5fabd2da1c0665a80eb6c5cc339208b200ffa35e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 31 Oct 2025 17:32:42 +0100 Subject: [PATCH 2/2] Improve code --- src/librustdoc/html/highlight.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 1a59456a5322e..0363418f91647 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -343,7 +343,7 @@ struct TokenHandler<'a, 'tcx, F: Write> { /// We need to keep the `Class` for each element because it could contain a `Span` which is /// used to generate links. href_context: Option>, - write_line_number: LineNumberKind, + line_number_kind: LineNumberKind, line: u32, max_lines: u32, } @@ -358,7 +358,7 @@ impl<'a, F: Write> TokenHandler<'a, '_, F> { fn handle_backline(&mut self) -> Option> { self.line += 1; if self.line < self.max_lines { - return Some(self.write_line_number.render(self.line)); + return Some(self.line_number_kind.render(self.line)); } None } @@ -436,7 +436,7 @@ impl Drop for TokenHandler<'_, '_, F> { } } -/// Represents line numbers to be generated as HTML. +/// Represents the type of line number to be generated as HTML. #[derive(Clone, Copy)] enum LineNumberKind { /// Used for scraped code examples. @@ -542,7 +542,7 @@ pub(super) fn write_code( let mut token_handler = TokenHandler { out, href_context, - write_line_number: match line_info { + line_number_kind: match line_info { Some(line_info) => { if line_info.is_scraped_example { LineNumberKind::Scraped