Skip to content

Commit 10997e9

Browse files
authored
Unrolled build for #148171
Rollup merge of #148171 - GuillaumeGomez:line-number-highlight, r=yotamofek,lolbinarycat Simplify code to generate line numbers in highlight Follow-up of #146992. cc `````@yotamofek````` r? `````@lolbinarycat`````
2 parents d85276b + 5fabd2d commit 10997e9

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ struct TokenHandler<'a, 'tcx, F: Write> {
343343
/// We need to keep the `Class` for each element because it could contain a `Span` which is
344344
/// used to generate links.
345345
href_context: Option<HrefContext<'a, 'tcx>>,
346-
write_line_number: fn(u32) -> String,
346+
line_number_kind: LineNumberKind,
347347
line: u32,
348348
max_lines: u32,
349349
}
@@ -355,10 +355,10 @@ impl<F: Write> std::fmt::Debug for TokenHandler<'_, '_, F> {
355355
}
356356

357357
impl<'a, F: Write> TokenHandler<'a, '_, F> {
358-
fn handle_backline(&mut self) -> Option<String> {
358+
fn handle_backline(&mut self) -> Option<impl Display + use<F>> {
359359
self.line += 1;
360360
if self.line < self.max_lines {
361-
return Some((self.write_line_number)(self.line));
361+
return Some(self.line_number_kind.render(self.line));
362362
}
363363
None
364364
}
@@ -376,8 +376,7 @@ impl<'a, F: Write> TokenHandler<'a, '_, F> {
376376
if text == "\n"
377377
&& let Some(backline) = self.handle_backline()
378378
{
379-
self.out.write_str(&text).unwrap();
380-
self.out.write_str(&backline).unwrap();
379+
write!(self.out, "{text}{backline}").unwrap();
381380
} else {
382381
self.push_token_without_backline_check(class, text, true);
383382
}
@@ -437,20 +436,29 @@ impl<F: Write> Drop for TokenHandler<'_, '_, F> {
437436
}
438437
}
439438

440-
fn scraped_line_number(line: u32) -> String {
441-
// https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
442-
// Do not show "1 2 3 4 5 ..." in web search results.
443-
format!("<span data-nosnippet>{line}</span>")
444-
}
445-
446-
fn line_number(line: u32) -> String {
447-
// https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
448-
// Do not show "1 2 3 4 5 ..." in web search results.
449-
format!("<a href=#{line} id={line} data-nosnippet>{line}</a>")
439+
/// Represents the type of line number to be generated as HTML.
440+
#[derive(Clone, Copy)]
441+
enum LineNumberKind {
442+
/// Used for scraped code examples.
443+
Scraped,
444+
/// Used for source code pages.
445+
Normal,
446+
/// Code examples in documentation don't have line number generated by rustdoc.
447+
Empty,
450448
}
451449

452-
fn empty_line_number(_: u32) -> String {
453-
String::new()
450+
impl LineNumberKind {
451+
fn render(self, line: u32) -> impl Display {
452+
fmt::from_fn(move |f| {
453+
match self {
454+
// https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
455+
// Do not show "1 2 3 4 5 ..." in web search results.
456+
Self::Scraped => write!(f, "<span data-nosnippet>{line}</span>"),
457+
Self::Normal => write!(f, "<a href=#{line} id={line} data-nosnippet>{line}</a>"),
458+
Self::Empty => Ok(()),
459+
}
460+
})
461+
}
454462
}
455463

456464
fn get_next_expansion(
@@ -534,15 +542,15 @@ pub(super) fn write_code(
534542
let mut token_handler = TokenHandler {
535543
out,
536544
href_context,
537-
write_line_number: match line_info {
545+
line_number_kind: match line_info {
538546
Some(line_info) => {
539547
if line_info.is_scraped_example {
540-
scraped_line_number
548+
LineNumberKind::Scraped
541549
} else {
542-
line_number
550+
LineNumberKind::Normal
543551
}
544552
}
545-
None => empty_line_number,
553+
None => LineNumberKind::Empty,
546554
},
547555
line: 0,
548556
max_lines: u32::MAX,
@@ -552,8 +560,12 @@ pub(super) fn write_code(
552560
if let Some(line_info) = line_info {
553561
token_handler.line = line_info.start_line - 1;
554562
token_handler.max_lines = line_info.max_lines;
555-
if let Some(text) = token_handler.handle_backline() {
556-
token_handler.push_token_without_backline_check(None, Cow::Owned(text), false);
563+
if let Some(backline) = token_handler.handle_backline() {
564+
token_handler.push_token_without_backline_check(
565+
None,
566+
Cow::Owned(backline.to_string()),
567+
false,
568+
);
557569
}
558570
}
559571

0 commit comments

Comments
 (0)