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

@ -21,6 +21,11 @@ impl<'a> Cursor<'a> {
}
}
/// Retrieves the current offset of the cursor within the source code.
pub fn offset(&self) -> TextSize {
self.source_length - self.text_len()
}
/// Return the remaining input as a string slice.
pub fn chars(&self) -> Chars<'a> {
self.chars.clone()

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(),
});
}
}

View file

@ -409,7 +409,6 @@ struct Parser<'s> {
explicit_path: Option<&'s str>,
source: &'s str,
source_len: TextSize,
/// Stack of ancestor sections.
stack: SectionStack,
@ -438,7 +437,6 @@ impl<'s> Parser<'s> {
cursor: Cursor::new(source),
preceding_blank_lines: 0,
explicit_path: None,
source_len: source.text_len(),
stack: SectionStack::new(root_section_id),
current_section_files: FxHashMap::default(),
current_section_has_config: false,
@ -460,7 +458,7 @@ impl<'s> Parser<'s> {
}
}
fn skip_whitespace(&mut self) {
fn skip_non_newline_whitespace(&mut self) {
self.cursor.eat_while(|c| c.is_whitespace() && c != '\n');
}
@ -474,11 +472,11 @@ impl<'s> Parser<'s> {
}
fn consume_until(&mut self, mut end_predicate: impl FnMut(char) -> bool) -> Option<&'s str> {
let start = self.offset().to_usize();
let start = self.cursor.offset().to_usize();
while !self.cursor.is_eof() {
if end_predicate(self.cursor.first()) {
return Some(&self.source[start..self.offset().to_usize()]);
return Some(&self.source[start..self.cursor.offset().to_usize()]);
}
self.cursor.bump();
}
@ -537,7 +535,7 @@ impl<'s> Parser<'s> {
if self.cursor.eat_char2('`', '`') {
// We saw the triple-backtick beginning of a code block.
let backtick_offset_start = self.offset() - "```".text_len();
let backtick_offset_start = self.cursor.offset() - "```".text_len();
if self.preceding_blank_lines < 1 && self.explicit_path.is_none() {
bail!(
@ -545,14 +543,14 @@ impl<'s> Parser<'s> {
);
}
self.skip_whitespace();
self.skip_non_newline_whitespace();
// Parse the code block language specifier
let lang = self
.consume_until(|c| matches!(c, ' ' | '\n'))
.unwrap_or_default();
self.skip_whitespace();
self.skip_non_newline_whitespace();
if !self.cursor.eat_char('\n') {
bail!(
@ -570,7 +568,7 @@ impl<'s> Parser<'s> {
code = &code[..code.len() - '\n'.len_utf8()];
}
let backtick_offset_end = self.offset() - "```".text_len();
let backtick_offset_end = self.cursor.offset() - "```".text_len();
self.process_code_block(
lang,
@ -590,7 +588,7 @@ impl<'s> Parser<'s> {
if let Some(path) = self.consume_until(|c| matches!(c, '`' | '\n')) {
if self.cursor.eat_char('`') {
self.skip_whitespace();
self.skip_non_newline_whitespace();
if self.cursor.eat_char(':') {
self.explicit_path = Some(path);
}
@ -609,7 +607,7 @@ impl<'s> Parser<'s> {
self.explicit_path = None;
if c.is_whitespace() {
self.skip_whitespace();
self.skip_non_newline_whitespace();
if self.cursor.eat_char('`')
&& self.cursor.eat_char('`')
&& self.cursor.eat_char('`')
@ -821,11 +819,6 @@ impl<'s> Parser<'s> {
}
}
/// Retrieves the current offset of the cursor within the source code.
fn offset(&self) -> TextSize {
self.source_len - self.cursor.text_len()
}
fn line_index(&self, char_index: TextSize) -> u32 {
self.source.count_lines(TextRange::up_to(char_index))
}