diff --git a/crates/ruff_python_trivia/src/cursor.rs b/crates/ruff_python_trivia/src/cursor.rs index 8ab9c37d41..a2c7e17f2b 100644 --- a/crates/ruff_python_trivia/src/cursor.rs +++ b/crates/ruff_python_trivia/src/cursor.rs @@ -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() diff --git a/crates/ty_test/src/assertion.rs b/crates/ty_test/src/assertion.rs index 2e9bfd338c..8df0b1caf2 100644 --- a/crates/ty_test/src/assertion.rs +++ b/crates/ty_test/src/assertion.rs @@ -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(), }); } } diff --git a/crates/ty_test/src/parser.rs b/crates/ty_test/src/parser.rs index 805ce0d409..b1cc448beb 100644 --- a/crates/ty_test/src/parser.rs +++ b/crates/ty_test/src/parser.rs @@ -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)) }