Refactor to avoid traversing the line unecessarily

This commit is contained in:
Zanie 2023-07-14 13:25:10 -05:00
parent 8c81332205
commit 6050b5e4b7

View file

@ -96,26 +96,31 @@ where
if !self.start_of_line { if !self.start_of_line {
next = Some(Ok((soft_to_name(tok), *range))); next = Some(Ok((soft_to_name(tok), *range)));
} else { } else {
let mut nesting = 0; let mut is_type_alias = false;
let mut first = true; if let Some(Ok((tok, _))) = self.underlying.peek() {
let mut seen_name = false; if matches!(
let mut seen_equal = false; tok,
while let Some(Ok((tok, _))) = self.underlying.peek() {
match tok {
Tok::Newline => break,
Tok::Name { .. } | Tok::Name { .. } |
// We treat a soft keyword token following a type token as a // We treat a soft keyword token following a type token as a
// name to support cases like `type type = int` or `type match = int` // name to support cases like `type type = int` or `type match = int`
Tok::Type | Tok::Match | Tok::Case Tok::Type | Tok::Match | Tok::Case
if first => seen_name = true, ) {
Tok::Equal if nesting == 0 && seen_name => seen_equal = true, let mut nesting = 0;
Tok::Lpar | Tok::Lsqb | Tok::Lbrace => nesting += 1, while let Some(Ok((tok, _))) = self.underlying.peek() {
Tok::Rpar | Tok::Rsqb | Tok::Rbrace => nesting -= 1, match tok {
_ => {} Tok::Newline => break,
Tok::Equal if nesting == 0 => {
is_type_alias = true;
break;
}
Tok::Lpar | Tok::Lsqb | Tok::Lbrace => nesting += 1,
Tok::Rpar | Tok::Rsqb | Tok::Rbrace => nesting -= 1,
_ => {}
}
}
} }
first = false;
} }
if !(seen_name && seen_equal) { if !is_type_alias {
next = Some(Ok((soft_to_name(tok), *range))); next = Some(Ok((soft_to_name(tok), *range)));
} }
} }