From 0799528db785e49ad13044aac193b8be2153e202 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 13 Jul 2021 15:28:43 +0200 Subject: [PATCH] * Rename LightSpan::empty into LightSpan::dummy * Add Classifier::new_light_span to wrap LightSpan::new_in_file constructor --- src/librustdoc/html/highlight.rs | 40 ++++++++++++-------------- src/librustdoc/html/render/span_map.rs | 2 +- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 47c870645ac..4555d98d2bf 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -235,6 +235,12 @@ fn new(src: &str, edition: Edition, file_span_lo: u32) -> Classifier<'_> { } } + /// Convenient wrapper around [`LightSpan::new_in_file`] to prevent passing the `file_span_lo` + /// argument every time. + fn new_light_span(&self, lo: u32, hi: u32) -> LightSpan { + LightSpan::new_in_file(self.file_span_lo, lo, hi) + } + /// Concatenate colons and idents as one when possible. fn get_full_ident_path(&mut self) -> Vec<(TokenKind, usize, usize)> { let start = self.byte_pos as usize; @@ -313,14 +319,12 @@ fn highlight(mut self, sink: &mut dyn FnMut(Highlight<'a>)) { .unwrap_or(false) { let tokens = self.get_full_ident_path(); - // We need this variable because `tokens` is consumed in the loop. - let skip = !tokens.is_empty(); - for (token, start, end) in tokens { - let text = &self.src[start..end]; - self.advance(token, text, sink, start as u32); + for (token, start, end) in &tokens { + let text = &self.src[*start..*end]; + self.advance(*token, text, sink, *start as u32); self.byte_pos += text.len() as u32; } - if skip { + if !tokens.is_empty() { continue; } } @@ -483,24 +487,16 @@ fn advance( self.in_macro_nonterminal = false; Class::MacroNonTerminal } - "self" | "Self" => Class::Self_(LightSpan::new_in_file( - self.file_span_lo, - before, - before + text.len() as u32, - )), - _ => Class::Ident(LightSpan::new_in_file( - self.file_span_lo, - before, - before + text.len() as u32, - )), + "self" | "Self" => { + Class::Self_(self.new_light_span(before, before + text.len() as u32)) + } + _ => Class::Ident(self.new_light_span(before, before + text.len() as u32)), }, Some(c) => c, }, - TokenKind::RawIdent | TokenKind::UnknownPrefix => Class::Ident(LightSpan::new_in_file( - self.file_span_lo, - before, - before + text.len() as u32, - )), + TokenKind::RawIdent | TokenKind::UnknownPrefix => { + Class::Ident(self.new_light_span(before, before + text.len() as u32)) + } TokenKind::Lifetime { .. } => Class::Lifetime, }; // Anything that didn't return above is the simple case where we the @@ -564,7 +560,7 @@ fn string( "self" | "Self" => write!( &mut path, "{}", - Class::Self_(LightSpan::empty()).as_html(), + Class::Self_(LightSpan::dummy()).as_html(), t ), "crate" | "super" => { diff --git a/src/librustdoc/html/render/span_map.rs b/src/librustdoc/html/render/span_map.rs index b60f5f433f7..390ac88faf0 100644 --- a/src/librustdoc/html/render/span_map.rs +++ b/src/librustdoc/html/render/span_map.rs @@ -51,7 +51,7 @@ impl LightSpan { Self { lo: lo + file_span_lo, hi: hi + file_span_lo } } - crate fn empty() -> Self { + crate fn dummy() -> Self { Self { lo: 0, hi: 0 } }