-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add f-string ranges builder to support nested f-strings
- Loading branch information
1 parent
c488fc4
commit de2bd87
Showing
4 changed files
with
133 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use ruff_python_parser::Tok; | ||
use ruff_text_size::{TextRange, TextSize}; | ||
|
||
/// Stores the ranges of all f-strings in a file sorted by [`TextRange::start`]. | ||
/// There can be multiple overlapping ranges for nested f-strings. | ||
#[derive(Debug)] | ||
pub struct FStringRanges { | ||
raw: BTreeMap<TextSize, TextRange>, | ||
} | ||
|
||
impl FStringRanges { | ||
/// Return the [`TextRange`] of the innermost f-string at the given offset. | ||
pub fn innermost(&self, offset: TextSize) -> Option<TextRange> { | ||
self.raw | ||
.range(..=offset) | ||
.rev() | ||
.find(|(_, range)| range.contains(offset)) | ||
.map(|(_, range)| *range) | ||
} | ||
|
||
/// Return the [`TextRange`] of the outermost f-string at the given offset. | ||
pub fn outermost(&self, offset: TextSize) -> Option<TextRange> { | ||
self.raw | ||
.range(..=offset) | ||
.find(|(_, range)| range.contains(offset)) | ||
.map(|(_, range)| *range) | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) fn ranges(&self) -> impl Iterator<Item = TextRange> + '_ { | ||
self.raw.values().copied() | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub(crate) struct FStringRangesBuilder { | ||
start_locations: Vec<TextSize>, | ||
raw: BTreeMap<TextSize, TextRange>, | ||
} | ||
|
||
impl FStringRangesBuilder { | ||
pub(crate) fn visit_token(&mut self, token: &Tok, range: TextRange) { | ||
match token { | ||
Tok::FStringStart => { | ||
self.start_locations.push(range.start()); | ||
} | ||
Tok::FStringEnd => { | ||
if let Some(start) = self.start_locations.pop() { | ||
self.raw.insert(start, TextRange::new(start, range.end())); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
pub(crate) fn finish(self) -> FStringRanges { | ||
FStringRanges { raw: self.raw } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod comment_ranges; | ||
mod fstring_ranges; | ||
mod indexer; | ||
|
||
pub use comment_ranges::CommentRangesBuilder; | ||
|