Skip to content

Commit

Permalink
Add text_len() methods to more *Prefix enums in ruff_python_ast (
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Feb 19, 2025
1 parent 55ea094 commit f50849a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ pub trait StringFlags: Copy {
/// i.e., the length of the prefixes plus the length
/// of the quotes used to open the string.
fn opener_len(self) -> TextSize {
self.prefix().as_str().text_len() + self.quote_len()
self.prefix().text_len() + self.quote_len()
}

/// The total length of the string's closer.
Expand Down
22 changes: 22 additions & 0 deletions crates/ruff_python_ast/src/str_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ impl FStringPrefix {
}
}

pub const fn text_len(self) -> TextSize {
match self {
Self::Regular => TextSize::new(1),
Self::Raw { .. } => TextSize::new(2),
}
}

/// Return true if this prefix indicates a "raw f-string",
/// e.g. `rf"{bar}"` or `Rf"{bar}"`
pub const fn is_raw(self) -> bool {
Expand Down Expand Up @@ -105,6 +112,13 @@ impl ByteStringPrefix {
}
}

pub const fn text_len(self) -> TextSize {
match self {
Self::Regular => TextSize::new(1),
Self::Raw { .. } => TextSize::new(2),
}
}

/// Return true if this prefix indicates a "raw bytestring",
/// e.g. `rb"foo"` or `Rb"foo"`
pub const fn is_raw(self) -> bool {
Expand Down Expand Up @@ -150,6 +164,14 @@ impl AnyStringPrefix {
}
}

pub const fn text_len(self) -> TextSize {
match self {
Self::Regular(regular_prefix) => regular_prefix.text_len(),
Self::Bytes(bytestring_prefix) => bytestring_prefix.text_len(),
Self::Format(fstring_prefix) => fstring_prefix.text_len(),
}
}

pub const fn is_raw(self) -> bool {
match self {
Self::Regular(regular_prefix) => regular_prefix.is_raw(),
Expand Down

0 comments on commit f50849a

Please sign in to comment.