From 1711bca4a0c97b809bc4c5b2204bc0da564feb99 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sun, 25 Feb 2024 18:28:46 +0100 Subject: [PATCH] FString formatting: remove fstring handling in `normalize_string` (#10119) --- .../src/other/f_string_element.rs | 1 + .../src/string/normalize.rs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/crates/ruff_python_formatter/src/other/f_string_element.rs b/crates/ruff_python_formatter/src/other/f_string_element.rs index c581413705..7bbfcffcb8 100644 --- a/crates/ruff_python_formatter/src/other/f_string_element.rs +++ b/crates/ruff_python_formatter/src/other/f_string_element.rs @@ -62,6 +62,7 @@ impl Format> for FormatFStringLiteralElement<'_> { self.context.quotes(), self.context.prefix(), is_hex_codes_in_unicode_sequences_enabled(f.context()), + true, ); match &normalized { Cow::Borrowed(_) => source_text_slice(self.element.range()).fmt(f), diff --git a/crates/ruff_python_formatter/src/string/normalize.rs b/crates/ruff_python_formatter/src/string/normalize.rs index 0047c58981..ad416019f1 100644 --- a/crates/ruff_python_formatter/src/string/normalize.rs +++ b/crates/ruff_python_formatter/src/string/normalize.rs @@ -7,7 +7,7 @@ use ruff_text_size::{Ranged, TextRange}; use crate::context::FStringState; use crate::options::PythonVersion; use crate::prelude::*; -use crate::preview::is_hex_codes_in_unicode_sequences_enabled; +use crate::preview::{is_f_string_formatting_enabled, is_hex_codes_in_unicode_sequences_enabled}; use crate::string::{QuoteChar, Quoting, StringPart, StringPrefix, StringQuotes}; use crate::QuoteStyle; @@ -18,6 +18,7 @@ pub(crate) struct StringNormalizer { f_string_state: FStringState, target_version: PythonVersion, normalize_hex: bool, + format_fstring: bool, } impl StringNormalizer { @@ -29,6 +30,7 @@ impl StringNormalizer { f_string_state: context.f_string_state(), target_version: context.options().target_version(), normalize_hex: is_hex_codes_in_unicode_sequences_enabled(context), + format_fstring: is_f_string_formatting_enabled(context), } } @@ -156,7 +158,13 @@ impl StringNormalizer { let quotes = self.choose_quotes(string, locator); - let normalized = normalize_string(raw_content, quotes, string.prefix(), self.normalize_hex); + let normalized = normalize_string( + raw_content, + quotes, + string.prefix(), + self.normalize_hex, + self.format_fstring, + ); NormalizedString { prefix: string.prefix(), @@ -394,6 +402,7 @@ pub(crate) fn normalize_string( quotes: StringQuotes, prefix: StringPrefix, normalize_hex: bool, + format_fstring: bool, ) -> Cow { // The normalized string if `input` is not yet normalized. // `output` must remain empty if `input` is already normalized. @@ -409,7 +418,7 @@ pub(crate) fn normalize_string( let mut chars = input.char_indices().peekable(); let is_raw = prefix.is_raw_string(); - let is_fstring = prefix.is_fstring(); + let is_fstring = !format_fstring && prefix.is_fstring(); let mut formatted_value_nesting = 0u32; while let Some((index, c)) = chars.next() { @@ -648,6 +657,7 @@ mod tests { }, StringPrefix::BYTE, true, + true, ); assert_eq!(r"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", &normalized);