Add offset method to ruff_python_trivia::Cursor (#18371)
Some checks are pending
CI / cargo fuzz build (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (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 / 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

@ -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))
}