diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index f831ff314cc701..e9af873e00e563 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -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. diff --git a/crates/ruff_python_ast/src/str_prefix.rs b/crates/ruff_python_ast/src/str_prefix.rs index 978e95b2754c3a..37f8421711da8f 100644 --- a/crates/ruff_python_ast/src/str_prefix.rs +++ b/crates/ruff_python_ast/src/str_prefix.rs @@ -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 { @@ -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 { @@ -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(),