Skip to content

Commit

Permalink
Move detection into Indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 18, 2023
1 parent 01fc3f4 commit 2d1b3a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
28 changes: 4 additions & 24 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,30 +143,10 @@ impl<'a> Checker<'a> {
return None;
}

let Some(expr) = context.expr() else {
return None;
};

// Find the f-string containing the current expression.
let start = expr.start();
let string_ranges = indexer.f_string_ranges();
let Ok(string_range_index) = string_ranges.binary_search_by(|range| {
if start < range.start() {
std::cmp::Ordering::Greater
} else if range.contains(start) {
std::cmp::Ordering::Equal
} else {
std::cmp::Ordering::Less
}
}) else {
return None;
};
let string_range = string_ranges[string_range_index];

// Find the quote character used to start the f-string.
let Some(trailing_quote) = trailing_quote(locator.slice(string_range)) else {
return None;
};
// Find the quote character used to start the containing f-string.
let expr = context.expr()?;
let string_range = indexer.f_string_range(expr.start())?;
let trailing_quote = trailing_quote(locator.slice(string_range))?;

// Invert the quote character, if it's a single quote.
match *trailing_quote {
Expand Down
39 changes: 25 additions & 14 deletions crates/ruff_python_ast/src/source_code/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ impl Indexer {
line_start = range.end();
}
Tok::String {
triple_quoted,
kind,
triple_quoted: true,
..
} => {
if *triple_quoted {
triple_quoted_string_ranges.push(*range);
}
if matches!(kind, StringKind::FString | StringKind::RawFString) {
f_string_ranges.push(*range);
}
triple_quoted_string_ranges.push(*range);
}
Tok::String {
kind: StringKind::FString | StringKind::RawFString,
..
} => {
f_string_ranges.push(*range);
}
_ => {}
}
Expand Down Expand Up @@ -111,16 +111,27 @@ impl Indexer {
&self.triple_quoted_string_ranges
}

/// Return a slice of all ranges that include an f- string. The ranges are sorted by
/// [`TextRange::start`] in increasing order. No two ranges are overlapping.
pub fn f_string_ranges(&self) -> &[TextRange] {
&self.f_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 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| {
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.f_string_ranges[string_range_index])
}
}

#[cfg(test)]
Expand Down

0 comments on commit 2d1b3a7

Please sign in to comment.