Skip to content

Commit ae68f90

Browse files
committed
Track indent levels when looking backwards through tokens
1 parent 154f6b1 commit ae68f90

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

crates/ruff_linter/src/suppression.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,19 @@ impl<'a> SuppressionsBuilder<'a> {
501501
indents.clear();
502502

503503
let (before, after) = tokens.split_at(suppression.range.start());
504+
let mut count = 0;
504505
let last_indent = before
505506
.iter()
506-
.rfind(|token| token.kind() == TokenKind::Indent)
507+
.filter(|token| matches!(token.kind(), TokenKind::Indent | TokenKind::Dedent))
508+
.rfind(|token| {
509+
// ignore matching dedents and indents above until we find
510+
count += match token.kind() {
511+
TokenKind::Dedent => 1,
512+
TokenKind::Indent => -1,
513+
_ => 0,
514+
};
515+
token.kind() == TokenKind::Indent && count < 0
516+
})
507517
.map(|token| self.source.slice(token))
508518
.unwrap_or_default();
509519

0 commit comments

Comments
 (0)