mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 00:20:38 +00:00
Move triple-quoted string detection into Indexer
method (#4495)
This commit is contained in:
parent
0e4d174551
commit
d3b18345c5
3 changed files with 30 additions and 33 deletions
|
@ -1,11 +1,12 @@
|
|||
//! Struct used to index source code, to enable efficient lookup of tokens that
|
||||
//! are omitted from the AST (e.g., commented lines).
|
||||
|
||||
use crate::source_code::Locator;
|
||||
use ruff_text_size::{TextRange, TextSize};
|
||||
use rustpython_parser::lexer::LexResult;
|
||||
use rustpython_parser::{StringKind, Tok};
|
||||
|
||||
use crate::source_code::Locator;
|
||||
|
||||
pub struct Indexer {
|
||||
/// Stores the ranges of comments sorted by [`TextRange::start`] in increasing order. No two ranges are overlapping.
|
||||
comment_ranges: Vec<TextRange>,
|
||||
|
@ -105,18 +106,28 @@ impl Indexer {
|
|||
&self.continuation_lines
|
||||
}
|
||||
|
||||
/// Return a slice of all ranges that include a triple-quoted string. The ranges are sorted by
|
||||
/// [`TextRange::start`] in increasing order. No two ranges are overlapping.
|
||||
pub fn triple_quoted_string_ranges(&self) -> &[TextRange] {
|
||||
&self.triple_quoted_string_ranges
|
||||
}
|
||||
|
||||
/// Returns `true` if the given offset is part of a continuation line.
|
||||
pub fn is_continuation(&self, offset: TextSize, locator: &Locator) -> bool {
|
||||
let line_start = locator.line_start(offset);
|
||||
self.continuation_lines.binary_search(&line_start).is_ok()
|
||||
}
|
||||
|
||||
/// Return the [`TextRange`] of the triple-quoted-string containing a given offset.
|
||||
pub fn triple_quoted_string_range(&self, offset: TextSize) -> Option<TextRange> {
|
||||
let Ok(string_range_index) = self.triple_quoted_string_ranges.binary_search_by(|range| {
|
||||
if offset < range.start() {
|
||||
std::cmp::Ordering::Greater
|
||||
} else if range.contains(offset) {
|
||||
std::cmp::Ordering::Equal
|
||||
} else {
|
||||
std::cmp::Ordering::Less
|
||||
}
|
||||
}) else {
|
||||
return None;
|
||||
};
|
||||
Some(self.triple_quoted_string_ranges[string_range_index])
|
||||
}
|
||||
|
||||
/// Return the [`TextRange`] of the f-string containing a given offset.
|
||||
pub fn f_string_range(&self, offset: TextSize) -> Option<TextRange> {
|
||||
let Ok(string_range_index) = self.f_string_ranges.binary_search_by(|range| {
|
||||
|
@ -228,7 +239,7 @@ import os
|
|||
let contents = r#""this is a single-quoted string""#;
|
||||
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
|
||||
let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents));
|
||||
assert_eq!(indexer.triple_quoted_string_ranges(), []);
|
||||
assert_eq!(indexer.triple_quoted_string_ranges, []);
|
||||
|
||||
let contents = r#"
|
||||
"""
|
||||
|
@ -238,7 +249,7 @@ import os
|
|||
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
|
||||
let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents));
|
||||
assert_eq!(
|
||||
indexer.triple_quoted_string_ranges(),
|
||||
indexer.triple_quoted_string_ranges,
|
||||
[TextRange::new(TextSize::from(13), TextSize::from(71))]
|
||||
);
|
||||
|
||||
|
@ -250,7 +261,7 @@ import os
|
|||
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
|
||||
let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents));
|
||||
assert_eq!(
|
||||
indexer.triple_quoted_string_ranges(),
|
||||
indexer.triple_quoted_string_ranges,
|
||||
[TextRange::new(TextSize::from(13), TextSize::from(107))]
|
||||
);
|
||||
|
||||
|
@ -267,7 +278,7 @@ import os
|
|||
let lxr: Vec<LexResult> = lexer::lex(contents, Mode::Module).collect();
|
||||
let indexer = Indexer::from_tokens(lxr.as_slice(), &Locator::new(contents));
|
||||
assert_eq!(
|
||||
indexer.triple_quoted_string_ranges(),
|
||||
indexer.triple_quoted_string_ranges,
|
||||
&[
|
||||
TextRange::new(TextSize::from(13), TextSize::from(85)),
|
||||
TextRange::new(TextSize::from(98), TextSize::from(161))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue