Skip to content

Commit f50849a

Browse files
authored
Add text_len() methods to more *Prefix enums in ruff_python_ast (#16254)
1 parent 55ea094 commit f50849a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

crates/ruff_python_ast/src/nodes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ pub trait StringFlags: Copy {
10291029
/// i.e., the length of the prefixes plus the length
10301030
/// of the quotes used to open the string.
10311031
fn opener_len(self) -> TextSize {
1032-
self.prefix().as_str().text_len() + self.quote_len()
1032+
self.prefix().text_len() + self.quote_len()
10331033
}
10341034

10351035
/// The total length of the string's closer.

crates/ruff_python_ast/src/str_prefix.rs

+22
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ impl FStringPrefix {
7171
}
7272
}
7373

74+
pub const fn text_len(self) -> TextSize {
75+
match self {
76+
Self::Regular => TextSize::new(1),
77+
Self::Raw { .. } => TextSize::new(2),
78+
}
79+
}
80+
7481
/// Return true if this prefix indicates a "raw f-string",
7582
/// e.g. `rf"{bar}"` or `Rf"{bar}"`
7683
pub const fn is_raw(self) -> bool {
@@ -105,6 +112,13 @@ impl ByteStringPrefix {
105112
}
106113
}
107114

115+
pub const fn text_len(self) -> TextSize {
116+
match self {
117+
Self::Regular => TextSize::new(1),
118+
Self::Raw { .. } => TextSize::new(2),
119+
}
120+
}
121+
108122
/// Return true if this prefix indicates a "raw bytestring",
109123
/// e.g. `rb"foo"` or `Rb"foo"`
110124
pub const fn is_raw(self) -> bool {
@@ -150,6 +164,14 @@ impl AnyStringPrefix {
150164
}
151165
}
152166

167+
pub const fn text_len(self) -> TextSize {
168+
match self {
169+
Self::Regular(regular_prefix) => regular_prefix.text_len(),
170+
Self::Bytes(bytestring_prefix) => bytestring_prefix.text_len(),
171+
Self::Format(fstring_prefix) => fstring_prefix.text_len(),
172+
}
173+
}
174+
153175
pub const fn is_raw(self) -> bool {
154176
match self {
155177
Self::Regular(regular_prefix) => regular_prefix.is_raw(),

0 commit comments

Comments
 (0)