refactor: Simplify quote selection logic (#13536)

This commit is contained in:
Micha Reiser 2024-09-27 14:40:28 +02:00 committed by GitHub
parent 253f5f269a
commit f3e464ea4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 51 deletions

View file

@ -8,11 +8,9 @@ pub struct FormatBytesLiteral;
impl FormatNodeRule<BytesLiteral> for FormatBytesLiteral {
fn fmt_fields(&self, item: &BytesLiteral, f: &mut PyFormatter) -> FormatResult<()> {
let locator = f.context().locator();
StringNormalizer::from_context(f.context())
.with_preferred_quote_style(f.options().quote_style())
.normalize(item.into(), &locator)
.normalize(item.into())
.fmt(f)
}
}

View file

@ -36,7 +36,7 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
// If f-string formatting is disabled (not in preview), then we will
// fall back to the previous behavior of normalizing the f-string.
if !is_f_string_formatting_enabled(f.context()) {
let result = normalizer.normalize(self.value.into(), &locator).fmt(f);
let result = normalizer.normalize(self.value.into()).fmt(f);
let comments = f.context().comments();
self.value.elements.iter().for_each(|value| {
comments.mark_verbatim_node_comments_formatted(value.into());
@ -56,9 +56,7 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
return result;
}
let string_kind = normalizer
.choose_quotes(self.value.into(), &locator)
.flags();
let string_kind = normalizer.choose_quotes(self.value.into()).flags();
let context = FStringContext::new(
string_kind,

View file

@ -46,8 +46,6 @@ impl StringLiteralKind {
impl Format<PyFormatContext<'_>> for FormatStringLiteral<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let locator = f.context().locator();
let quote_style = f.options().quote_style();
let quote_style = if self.layout.is_docstring() && !quote_style.is_preserve() {
// Per PEP 8 and PEP 257, always prefer double quotes for docstrings,
@ -60,7 +58,7 @@ impl Format<PyFormatContext<'_>> for FormatStringLiteral<'_> {
let normalized = StringNormalizer::from_context(f.context())
.with_quoting(self.layout.quoting())
.with_preferred_quote_style(quote_style)
.normalize(self.value.into(), &locator);
.normalize(self.value.into());
if self.layout.is_docstring() {
docstring::format(&normalized, f)