Add methods to iter over f-string elements (#10309)
## Summary This PR adds methods on `FString` to iterate over the two different kind of elements it can have - literals and expressions. This is similar to the methods we have on `ExprFString`. --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
@@ -1248,6 +1248,22 @@ pub struct FString {
|
||||
pub flags: FStringFlags,
|
||||
}
|
||||
|
||||
impl FString {
|
||||
/// Returns an iterator over all the [`FStringLiteralElement`] nodes contained in this f-string.
|
||||
pub fn literals(&self) -> impl Iterator<Item = &FStringLiteralElement> {
|
||||
self.elements
|
||||
.iter()
|
||||
.filter_map(|element| element.as_literal())
|
||||
}
|
||||
|
||||
/// Returns an iterator over all the [`FStringExpressionElement`] nodes contained in this f-string.
|
||||
pub fn expressions(&self) -> impl Iterator<Item = &FStringExpressionElement> {
|
||||
self.elements
|
||||
.iter()
|
||||
.filter_map(|element| element.as_expression())
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for FString {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
|
||||
Reference in New Issue
Block a user