Skip to content

Commit b9827a4

Browse files
authored
Remove layout values from AnyStringPart (#13681)
1 parent 93eff7f commit b9827a4

File tree

5 files changed

+45
-62
lines changed

5 files changed

+45
-62
lines changed

crates/ruff_python_formatter/src/other/bytes_literal.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub struct FormatBytesLiteral;
99
impl FormatNodeRule<BytesLiteral> for FormatBytesLiteral {
1010
fn fmt_fields(&self, item: &BytesLiteral, f: &mut PyFormatter) -> FormatResult<()> {
1111
StringNormalizer::from_context(f.context())
12-
.with_preferred_quote_style(f.options().quote_style())
1312
.normalize(item.into())
1413
.fmt(f)
1514
}

crates/ruff_python_formatter/src/other/f_string.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
4040
self.quoting
4141
};
4242

43-
let normalizer = StringNormalizer::from_context(f.context())
44-
.with_quoting(quoting)
45-
.with_preferred_quote_style(f.options().quote_style());
43+
let normalizer = StringNormalizer::from_context(f.context()).with_quoting(quoting);
4644

4745
// If f-string formatting is disabled (not in preview), then we will
4846
// fall back to the previous behavior of normalizing the f-string.

crates/ruff_python_formatter/src/string/any.rs

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ use ruff_source_file::Locator;
1010
use ruff_text_size::{Ranged, TextRange};
1111

1212
use crate::expression::expr_f_string::f_string_quoting;
13-
use crate::other::f_string::FormatFString;
14-
use crate::other::string_literal::StringLiteralKind;
15-
use crate::prelude::*;
1613
use crate::string::Quoting;
1714

1815
/// Represents any kind of string expression. This could be either a string,
@@ -46,6 +43,10 @@ impl<'a> AnyString<'a> {
4643
}
4744
}
4845

46+
pub(crate) const fn is_fstring(self) -> bool {
47+
matches!(self, Self::FString(_))
48+
}
49+
4950
/// Returns the quoting to be used for this string.
5051
pub(super) fn quoting(self, locator: &Locator<'_>) -> Quoting {
5152
match self {
@@ -54,23 +55,21 @@ impl<'a> AnyString<'a> {
5455
}
5556
}
5657

57-
/// Returns a vector of all the [`AnyStringPart`] of this string.
58-
pub(super) fn parts(self, quoting: Quoting) -> AnyStringPartsIter<'a> {
58+
/// Returns an iterator over the [`AnyStringPart`]s of this string.
59+
pub(super) fn parts(self) -> AnyStringPartsIter<'a> {
5960
match self {
6061
Self::String(ExprStringLiteral { value, .. }) => {
6162
AnyStringPartsIter::String(value.iter())
6263
}
6364
Self::Bytes(ExprBytesLiteral { value, .. }) => AnyStringPartsIter::Bytes(value.iter()),
64-
Self::FString(ExprFString { value, .. }) => {
65-
AnyStringPartsIter::FString(value.iter(), quoting)
66-
}
65+
Self::FString(ExprFString { value, .. }) => AnyStringPartsIter::FString(value.iter()),
6766
}
6867
}
6968

7069
pub(crate) fn is_multiline(self, source: &str) -> bool {
7170
match self {
7271
AnyString::String(_) | AnyString::Bytes(_) => {
73-
self.parts(Quoting::default())
72+
self.parts()
7473
.next()
7574
.is_some_and(|part| part.flags().is_triple_quoted())
7675
&& memchr2(b'\n', b'\r', source[self.range()].as_bytes()).is_some()
@@ -139,36 +138,20 @@ impl<'a> From<&'a ExprFString> for AnyString<'a> {
139138
pub(super) enum AnyStringPartsIter<'a> {
140139
String(std::slice::Iter<'a, StringLiteral>),
141140
Bytes(std::slice::Iter<'a, ast::BytesLiteral>),
142-
FString(std::slice::Iter<'a, ast::FStringPart>, Quoting),
141+
FString(std::slice::Iter<'a, ast::FStringPart>),
143142
}
144143

145144
impl<'a> Iterator for AnyStringPartsIter<'a> {
146145
type Item = AnyStringPart<'a>;
147146

148147
fn next(&mut self) -> Option<Self::Item> {
149148
let part = match self {
150-
Self::String(inner) => {
151-
let part = inner.next()?;
152-
AnyStringPart::String {
153-
part,
154-
layout: StringLiteralKind::String,
155-
}
156-
}
149+
Self::String(inner) => AnyStringPart::String(inner.next()?),
157150
Self::Bytes(inner) => AnyStringPart::Bytes(inner.next()?),
158-
Self::FString(inner, quoting) => {
159-
let part = inner.next()?;
160-
match part {
161-
ast::FStringPart::Literal(string_literal) => AnyStringPart::String {
162-
part: string_literal,
163-
#[allow(deprecated)]
164-
layout: StringLiteralKind::InImplicitlyConcatenatedFString(*quoting),
165-
},
166-
ast::FStringPart::FString(f_string) => AnyStringPart::FString {
167-
part: f_string,
168-
quoting: *quoting,
169-
},
170-
}
171-
}
151+
Self::FString(inner) => match inner.next()? {
152+
ast::FStringPart::Literal(string_literal) => AnyStringPart::String(string_literal),
153+
ast::FStringPart::FString(f_string) => AnyStringPart::FString(f_string),
154+
},
172155
};
173156

174157
Some(part)
@@ -183,53 +166,37 @@ impl FusedIterator for AnyStringPartsIter<'_> {}
183166
/// This is constructed from the [`AnyString::parts`] method on [`AnyString`].
184167
#[derive(Clone, Debug)]
185168
pub(super) enum AnyStringPart<'a> {
186-
String {
187-
part: &'a ast::StringLiteral,
188-
layout: StringLiteralKind,
189-
},
169+
String(&'a ast::StringLiteral),
190170
Bytes(&'a ast::BytesLiteral),
191-
FString {
192-
part: &'a ast::FString,
193-
quoting: Quoting,
194-
},
171+
FString(&'a ast::FString),
195172
}
196173

197174
impl AnyStringPart<'_> {
198175
fn flags(&self) -> AnyStringFlags {
199176
match self {
200-
Self::String { part, .. } => part.flags.into(),
177+
Self::String(part) => part.flags.into(),
201178
Self::Bytes(bytes_literal) => bytes_literal.flags.into(),
202-
Self::FString { part, .. } => part.flags.into(),
179+
Self::FString(part) => part.flags.into(),
203180
}
204181
}
205182
}
206183

207184
impl<'a> From<&AnyStringPart<'a>> for AnyNodeRef<'a> {
208185
fn from(value: &AnyStringPart<'a>) -> Self {
209186
match value {
210-
AnyStringPart::String { part, .. } => AnyNodeRef::StringLiteral(part),
187+
AnyStringPart::String(part) => AnyNodeRef::StringLiteral(part),
211188
AnyStringPart::Bytes(part) => AnyNodeRef::BytesLiteral(part),
212-
AnyStringPart::FString { part, .. } => AnyNodeRef::FString(part),
189+
AnyStringPart::FString(part) => AnyNodeRef::FString(part),
213190
}
214191
}
215192
}
216193

217194
impl Ranged for AnyStringPart<'_> {
218195
fn range(&self) -> TextRange {
219196
match self {
220-
Self::String { part, .. } => part.range(),
197+
Self::String(part) => part.range(),
221198
Self::Bytes(part) => part.range(),
222-
Self::FString { part, .. } => part.range(),
223-
}
224-
}
225-
}
226-
227-
impl Format<PyFormatContext<'_>> for AnyStringPart<'_> {
228-
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
229-
match self {
230-
AnyStringPart::String { part, layout } => part.format().with_options(*layout).fmt(f),
231-
AnyStringPart::Bytes(bytes_literal) => bytes_literal.format().fmt(f),
232-
AnyStringPart::FString { part, quoting } => FormatFString::new(part, *quoting).fmt(f),
199+
Self::FString(part) => part.range(),
233200
}
234201
}
235202
}

crates/ruff_python_formatter/src/string/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use ruff_text_size::{Ranged, TextRange};
1111

1212
use crate::comments::{leading_comments, trailing_comments};
1313
use crate::expression::parentheses::in_parentheses_only_soft_line_break_or_space;
14+
use crate::other::f_string::FormatFString;
15+
use crate::other::string_literal::StringLiteralKind;
1416
use crate::prelude::*;
17+
use crate::string::any::AnyStringPart;
1518
use crate::QuoteStyle;
1619

1720
mod any;
@@ -46,12 +49,28 @@ impl Format<PyFormatContext<'_>> for FormatImplicitConcatenatedString<'_> {
4649

4750
let mut joiner = f.join_with(in_parentheses_only_soft_line_break_or_space());
4851

49-
for part in self.string.parts(quoting) {
52+
for part in self.string.parts() {
5053
let part_comments = comments.leading_dangling_trailing(&part);
54+
55+
let format_part = format_with(|f: &mut PyFormatter| match part {
56+
AnyStringPart::String(part) => {
57+
let kind = if self.string.is_fstring() {
58+
#[allow(deprecated)]
59+
StringLiteralKind::InImplicitlyConcatenatedFString(quoting)
60+
} else {
61+
StringLiteralKind::String
62+
};
63+
64+
part.format().with_options(kind).fmt(f)
65+
}
66+
AnyStringPart::Bytes(bytes_literal) => bytes_literal.format().fmt(f),
67+
AnyStringPart::FString(part) => FormatFString::new(part, quoting).fmt(f),
68+
});
69+
5170
joiner.entry(&format_args![
5271
line_suffix_boundary(),
5372
leading_comments(part_comments.leading),
54-
part,
73+
format_part,
5574
trailing_comments(part_comments.trailing)
5675
]);
5776
}

crates/ruff_python_formatter/src/string/normalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'a, 'src> StringNormalizer<'a, 'src> {
2222
pub(crate) fn from_context(context: &'a PyFormatContext<'src>) -> Self {
2323
Self {
2424
quoting: Quoting::default(),
25-
preferred_quote_style: QuoteStyle::default(),
25+
preferred_quote_style: context.options().quote_style(),
2626
context,
2727
}
2828
}

0 commit comments

Comments
 (0)