Add offset method to ruff_python_trivia::Cursor (#18371)
Some checks are pending
CI / cargo fmt (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

This commit is contained in:
Alex Waygood 2025-05-29 16:08:15 +01:00 committed by GitHub
parent 33ed502edb
commit 7df79cfb70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 29 deletions

View file

@ -40,7 +40,7 @@ use ruff_db::parsed::parsed_module;
use ruff_db::source::{SourceText, line_index, source_text};
use ruff_python_trivia::{CommentRanges, Cursor};
use ruff_source_file::{LineIndex, OneIndexed};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_text_size::{Ranged, TextRange, TextSize};
use smallvec::SmallVec;
use std::ops::Deref;
use std::str::FromStr;
@ -360,11 +360,6 @@ impl<'a> ErrorAssertionParser<'a> {
}
}
/// Retrieves the current offset of the cursor within the source code.
fn offset(&self) -> TextSize {
self.comment_source.text_len() - self.cursor.text_len()
}
/// Consume characters in the assertion comment until we find a non-whitespace character
fn skip_whitespace(&mut self) {
self.cursor.eat_while(char::is_whitespace);
@ -387,9 +382,10 @@ impl<'a> ErrorAssertionParser<'a> {
if rule.is_some() {
return Err(ErrorAssertionParseError::ColumnNumberAfterRuleCode);
}
let offset = self.offset() - TextSize::new(1);
let offset = self.cursor.offset() - TextSize::new(1);
self.cursor.eat_while(|c| !c.is_whitespace());
let column_str = &self.comment_source[TextRange::new(offset, self.offset())];
let column_str =
&self.comment_source[TextRange::new(offset, self.cursor.offset())];
column = OneIndexed::from_str(column_str)
.map(Some)
.map_err(|e| ErrorAssertionParseError::BadColumnNumber(column_str, e))?;
@ -400,12 +396,14 @@ impl<'a> ErrorAssertionParser<'a> {
if rule.is_some() {
return Err(ErrorAssertionParseError::MultipleRuleCodes);
}
let offset = self.offset();
let offset = self.cursor.offset();
self.cursor.eat_while(|c| c != ']');
if self.cursor.is_eof() {
return Err(ErrorAssertionParseError::UnclosedRuleCode);
}
rule = Some(self.comment_source[TextRange::new(offset, self.offset())].trim());
rule = Some(
self.comment_source[TextRange::new(offset, self.cursor.offset())].trim(),
);
self.cursor.bump();
}
@ -413,8 +411,8 @@ impl<'a> ErrorAssertionParser<'a> {
'"' => {
let comment_source = self.comment_source.trim();
return if comment_source.ends_with('"') {
let rest =
&comment_source[self.offset().to_usize()..comment_source.len() - 1];
let rest = &comment_source
[self.cursor.offset().to_usize()..comment_source.len() - 1];
Ok(ErrorAssertion {
rule,
column,
@ -434,7 +432,7 @@ impl<'a> ErrorAssertionParser<'a> {
unexpected => {
return Err(ErrorAssertionParseError::UnexpectedCharacter {
character: unexpected,
offset: self.offset().to_usize(),
offset: self.cursor.offset().to_usize(),
});
}
}