@@ -5,18 +5,16 @@ use smallvec::SmallVec;
5
5
6
6
use ruff_formatter:: write;
7
7
use ruff_python_ast:: {
8
- BytesConstant , Constant , Expr , ExprAttribute , ExprBinOp , ExprCompare , ExprConstant ,
9
- ExprUnaryOp , StringConstant , UnaryOp ,
8
+ Constant , Expr , ExprAttribute , ExprBinOp , ExprCompare , ExprConstant , ExprUnaryOp , UnaryOp ,
10
9
} ;
11
10
12
11
use crate :: comments:: { leading_comments, trailing_comments, Comments , SourceComment } ;
13
- use crate :: expression:: expr_constant:: ExprConstantLayout ;
14
12
use crate :: expression:: parentheses:: {
15
13
in_parentheses_only_group, in_parentheses_only_soft_line_break,
16
14
in_parentheses_only_soft_line_break_or_space, is_expression_parenthesized,
17
15
write_in_parentheses_only_group_end_tag, write_in_parentheses_only_group_start_tag,
18
16
} ;
19
- use crate :: expression:: string:: StringLayout ;
17
+ use crate :: expression:: string:: { AnyString , FormatString , StringLayout } ;
20
18
use crate :: expression:: OperatorPrecedence ;
21
19
use crate :: prelude:: * ;
22
20
@@ -197,29 +195,12 @@ impl Format<PyFormatContext<'_>> for BinaryLike<'_> {
197
195
let mut string_operands = flat_binary
198
196
. operands ( )
199
197
. filter_map ( |( index, operand) | {
200
- if let Expr :: Constant (
201
- constant @ ExprConstant {
202
- value :
203
- Constant :: Str ( StringConstant {
204
- implicit_concatenated : true ,
205
- ..
206
- } )
207
- | Constant :: Bytes ( BytesConstant {
208
- implicit_concatenated : true ,
209
- ..
210
- } ) ,
211
- ..
212
- } ,
213
- ) = operand. expression ( )
214
- {
215
- if is_expression_parenthesized ( constant. into ( ) , source) {
216
- None
217
- } else {
218
- Some ( ( index, constant, operand) )
219
- }
220
- } else {
221
- None
222
- }
198
+ AnyString :: from_expression ( operand. expression ( ) )
199
+ . filter ( |string| {
200
+ string. is_implicit_concatenated ( )
201
+ && !is_expression_parenthesized ( string. into ( ) , source)
202
+ } )
203
+ . map ( |string| ( index, string, operand) )
223
204
} )
224
205
. peekable ( ) ;
225
206
@@ -296,11 +277,11 @@ impl Format<PyFormatContext<'_>> for BinaryLike<'_> {
296
277
f,
297
278
[
298
279
operand. leading_binary_comments( ) . map( leading_comments) ,
299
- string_constant
300
- . format ( )
301
- . with_options ( ExprConstantLayout :: String (
302
- StringLayout :: ImplicitConcatenatedStringInBinaryLike ,
303
- ) ) ,
280
+ leading_comments ( comments . leading ( & string_constant) ) ,
281
+ FormatString :: new ( & string_constant ) . with_layout (
282
+ StringLayout :: ImplicitConcatenatedStringInBinaryLike ,
283
+ ) ,
284
+ trailing_comments ( comments . trailing ( & string_constant ) ) ,
304
285
operand. trailing_binary_comments( ) . map( trailing_comments) ,
305
286
line_suffix_boundary( ) ,
306
287
]
@@ -311,12 +292,16 @@ impl Format<PyFormatContext<'_>> for BinaryLike<'_> {
311
292
// "a" "b" + c
312
293
// ^^^^^^^-- format the first operand of a binary expression
313
294
// ```
314
- string_constant
315
- . format ( )
316
- . with_options ( ExprConstantLayout :: String (
317
- StringLayout :: ImplicitConcatenatedStringInBinaryLike ,
318
- ) )
319
- . fmt ( f) ?;
295
+ write ! (
296
+ f,
297
+ [
298
+ leading_comments( comments. leading( & string_constant) ) ,
299
+ FormatString :: new( & string_constant) . with_layout(
300
+ StringLayout :: ImplicitConcatenatedStringInBinaryLike
301
+ ) ,
302
+ trailing_comments( comments. trailing( & string_constant) ) ,
303
+ ]
304
+ ) ?;
320
305
}
321
306
322
307
// Write the right operator and start the group for the right side (if any)
0 commit comments