* Rename LightSpan::empty into LightSpan::dummy

* Add Classifier::new_light_span to wrap LightSpan::new_in_file constructor
This commit is contained in:
Guillaume Gomez 2021-07-13 15:28:43 +02:00
parent fd69fa8670
commit 0799528db7
2 changed files with 19 additions and 23 deletions

View File

@ -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<T: Display>(
"self" | "Self" => write!(
&mut path,
"<span class=\"{}\">{}</span>",
Class::Self_(LightSpan::empty()).as_html(),
Class::Self_(LightSpan::dummy()).as_html(),
t
),
"crate" | "super" => {

View File

@ -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 }
}