Refactor and rename skip_trailing_trivia (#6312)

Based on feedback here:
https://github.com/astral-sh/ruff/pull/6274#discussion_r1282747964.
This commit is contained in:
Charlie Marsh 2023-08-04 09:30:53 -04:00 committed by GitHub
parent 38a96c88c1
commit 1e3fe67ca5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 42 deletions

View file

@ -70,24 +70,18 @@ pub fn lines_after(offset: TextSize, code: &str) -> u32 {
newlines
}
/// Returns the position after skipping any trailing trivia up to, but not including the newline character.
pub fn skip_trailing_trivia(offset: TextSize, code: &str) -> TextSize {
let tokenizer = SimpleTokenizer::starts_at(offset, code);
for token in tokenizer {
match token.kind() {
SimpleTokenKind::Whitespace
| SimpleTokenKind::Comment
| SimpleTokenKind::Continuation => {
// No op
}
_ => {
return token.start();
}
}
}
offset
/// Counts the empty lines after `offset`, ignoring any trailing trivia on the same line as
/// `offset`.
#[allow(clippy::cast_possible_truncation)]
pub fn lines_after_ignoring_trivia(offset: TextSize, code: &str) -> u32 {
// SAFETY: We don't support files greater than 4GB, so casting to u32 is safe.
SimpleTokenizer::starts_at(offset, code)
.skip_while(|token| token.kind != SimpleTokenKind::Newline && token.kind.is_trivia())
.take_while(|token| {
token.kind == SimpleTokenKind::Newline || token.kind == SimpleTokenKind::Whitespace
})
.filter(|token| token.kind == SimpleTokenKind::Newline)
.count() as u32
}
fn is_identifier_start(c: char) -> bool {