Apply suggestions from code review

Co-Authored-By: bjorn3 <bjorn3@users.noreply.github.com>
This commit is contained in:
Leander Tentrup 2020-04-22 10:08:46 +02:00 committed by Leander Tentrup
parent ac798e1f7c
commit b2829a5216
2 changed files with 32 additions and 34 deletions

View file

@ -194,7 +194,7 @@ pub(crate) fn highlight(
let token = sema.descend_into_macros(token.clone()); let token = sema.descend_into_macros(token.clone());
let parent = token.parent(); let parent = token.parent();
// Check if macro takes a format string and remeber it for highlighting later. // Check if macro takes a format string and remember it for highlighting later.
// The macros that accept a format string expand to a compiler builtin macros // The macros that accept a format string expand to a compiler builtin macros
// `format_args` and `format_args_nl`. // `format_args` and `format_args_nl`.
if let Some(fmt_macro_call) = parent.parent().and_then(ast::MacroCall::cast) { if let Some(fmt_macro_call) = parent.parent().and_then(ast::MacroCall::cast) {
@ -233,8 +233,7 @@ pub(crate) fn highlight(
} }
} }
let is_format_string = let is_format_string = format_string.as_ref() == Some(&element_to_highlight);
format_string.as_ref().map(|fs| fs == &element_to_highlight).unwrap_or_default();
if let Some((highlight, binding_hash)) = if let Some((highlight, binding_hash)) =
highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight.clone()) highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight.clone())
@ -245,7 +244,7 @@ pub(crate) fn highlight(
{ {
stack.push(); stack.push();
if is_format_string { if is_format_string {
string.lex_format_specifier(&mut |piece_range, kind| { string.lex_format_specifier(|piece_range, kind| {
let highlight = match kind { let highlight = match kind {
FormatSpecifier::Open FormatSpecifier::Open
| FormatSpecifier::Close | FormatSpecifier::Close

View file

@ -192,7 +192,7 @@ pub enum FormatSpecifier {
} }
pub trait HasFormatSpecifier: AstToken { pub trait HasFormatSpecifier: AstToken {
fn lex_format_specifier<F>(&self, callback: &mut F) fn lex_format_specifier<F>(&self, mut callback: F)
where where
F: FnMut(TextRange, FormatSpecifier), F: FnMut(TextRange, FormatSpecifier),
{ {
@ -217,21 +217,15 @@ pub trait HasFormatSpecifier: AstToken {
FormatSpecifier::Open, FormatSpecifier::Open,
); );
let next_char = if let Some(c) = chars.clone().next() {
c
} else {
break;
};
// check for integer/identifier // check for integer/identifier
match next_char { match chars.clone().next().unwrap_or_default() {
'0'..='9' => { '0'..='9' => {
// integer // integer
read_integer(&mut chars, initial_len, callback); read_integer(&mut chars, initial_len, &mut callback);
} }
'a'..='z' | 'A'..='Z' | '_' => { 'a'..='z' | 'A'..='Z' | '_' => {
// identifier // identifier
read_identifier(&mut chars, initial_len, callback); read_identifier(&mut chars, initial_len, &mut callback);
} }
_ => {} _ => {}
} }
@ -241,7 +235,7 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::Colon, FormatSpecifier::Colon,
callback, &mut callback,
); );
// check for fill/align // check for fill/align
@ -255,13 +249,13 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::Fill, FormatSpecifier::Fill,
callback, &mut callback,
); );
skip_char_and_emit( skip_char_and_emit(
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::Align, FormatSpecifier::Align,
callback, &mut callback,
); );
} }
_ => match first { _ => match first {
@ -270,7 +264,7 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::Align, FormatSpecifier::Align,
callback, &mut callback,
); );
} }
_ => {} _ => {}
@ -284,7 +278,7 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::Sign, FormatSpecifier::Sign,
callback, &mut callback,
); );
} }
_ => {} _ => {}
@ -296,7 +290,7 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::NumberSign, FormatSpecifier::NumberSign,
callback, &mut callback,
); );
} }
@ -310,25 +304,25 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::Zero, FormatSpecifier::Zero,
callback, &mut callback,
); );
} }
// width // width
match chars.clone().next().unwrap_or_default() { match chars.clone().next().unwrap_or_default() {
'0'..='9' => { '0'..='9' => {
read_integer(&mut chars, initial_len, callback); read_integer(&mut chars, initial_len, &mut callback);
if chars.clone().next() == Some('$') { if chars.clone().next() == Some('$') {
skip_char_and_emit( skip_char_and_emit(
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::DollarSign, FormatSpecifier::DollarSign,
callback, &mut callback,
); );
} }
} }
'a'..='z' | 'A'..='Z' | '_' => { 'a'..='z' | 'A'..='Z' | '_' => {
read_identifier(&mut chars, initial_len, callback); read_identifier(&mut chars, initial_len, &mut callback);
if chars.clone().next() != Some('$') { if chars.clone().next() != Some('$') {
continue; continue;
} }
@ -336,7 +330,7 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::DollarSign, FormatSpecifier::DollarSign,
callback, &mut callback,
); );
} }
_ => {} _ => {}
@ -348,7 +342,7 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::Dot, FormatSpecifier::Dot,
callback, &mut callback,
); );
match chars.clone().next().unwrap_or_default() { match chars.clone().next().unwrap_or_default() {
@ -357,22 +351,22 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::Asterisk, FormatSpecifier::Asterisk,
callback, &mut callback,
); );
} }
'0'..='9' => { '0'..='9' => {
read_integer(&mut chars, initial_len, callback); read_integer(&mut chars, initial_len, &mut callback);
if chars.clone().next() == Some('$') { if chars.clone().next() == Some('$') {
skip_char_and_emit( skip_char_and_emit(
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::DollarSign, FormatSpecifier::DollarSign,
callback, &mut callback,
); );
} }
} }
'a'..='z' | 'A'..='Z' | '_' => { 'a'..='z' | 'A'..='Z' | '_' => {
read_identifier(&mut chars, initial_len, callback); read_identifier(&mut chars, initial_len, &mut callback);
if chars.clone().next() != Some('$') { if chars.clone().next() != Some('$') {
continue; continue;
} }
@ -380,7 +374,7 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::DollarSign, FormatSpecifier::DollarSign,
callback, &mut callback,
); );
} }
_ => { _ => {
@ -396,11 +390,11 @@ pub trait HasFormatSpecifier: AstToken {
&mut chars, &mut chars,
initial_len, initial_len,
FormatSpecifier::QuestionMark, FormatSpecifier::QuestionMark,
callback, &mut callback,
); );
} }
'a'..='z' | 'A'..='Z' | '_' => { 'a'..='z' | 'A'..='Z' | '_' => {
read_identifier(&mut chars, initial_len, callback); read_identifier(&mut chars, initial_len, &mut callback);
} }
_ => {} _ => {}
} }
@ -416,7 +410,12 @@ pub trait HasFormatSpecifier: AstToken {
// Escaped format end specifier, `}}` // Escaped format end specifier, `}}`
continue; continue;
} }
skip_char_and_emit(&mut chars, initial_len, FormatSpecifier::Close, callback); skip_char_and_emit(
&mut chars,
initial_len,
FormatSpecifier::Close,
&mut callback,
);
} }
_ => { _ => {
while let Some(next_char) = chars.clone().next() { while let Some(next_char) = chars.clone().next() {