mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-30 15:17:59 +00:00
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:
parent
38a96c88c1
commit
1e3fe67ca5
5 changed files with 32 additions and 42 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue