1
1
use ruff_formatter:: write;
2
2
use ruff_python_ast:: FString ;
3
+ use ruff_python_parser:: StringKind ;
3
4
use ruff_source_file:: Locator ;
4
- use ruff_text_size:: Ranged ;
5
5
6
6
use crate :: prelude:: * ;
7
7
use crate :: preview:: is_f_string_formatting_enabled;
8
- use crate :: string:: { Quoting , StringNormalizer , StringPart , StringPrefix , StringQuotes } ;
8
+ use crate :: string:: { Quoting , StringNormalizer , StringQuotes } ;
9
9
10
10
use super :: f_string_element:: FormatFStringElement ;
11
11
@@ -30,16 +30,14 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
30
30
fn fmt ( & self , f : & mut PyFormatter ) -> FormatResult < ( ) > {
31
31
let locator = f. context ( ) . locator ( ) ;
32
32
33
- let string = StringPart :: from_source ( self . value . range ( ) , & locator) ;
34
-
35
33
let normalizer = StringNormalizer :: from_context ( f. context ( ) )
36
34
. with_quoting ( self . quoting )
37
35
. with_preferred_quote_style ( f. options ( ) . quote_style ( ) ) ;
38
36
39
37
// If f-string formatting is disabled (not in preview), then we will
40
38
// fall back to the previous behavior of normalizing the f-string.
41
39
if !is_f_string_formatting_enabled ( f. context ( ) ) {
42
- let result = normalizer. normalize ( & string , & locator) . fmt ( f) ;
40
+ let result = normalizer. normalize ( self . value , & locator) . fmt ( f) ;
43
41
let comments = f. context ( ) . comments ( ) ;
44
42
self . value . elements . iter ( ) . for_each ( |value| {
45
43
comments. mark_verbatim_node_comments_formatted ( value. into ( ) ) ;
@@ -59,16 +57,16 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
59
57
return result;
60
58
}
61
59
62
- let quote_selection = normalizer. choose_quotes ( & string , & locator) ;
60
+ let string_kind = normalizer. choose_quotes ( self . value , & locator) . kind ( ) ;
63
61
64
62
let context = FStringContext :: new (
65
- string. prefix ( ) ,
66
- quote_selection. quotes ( ) ,
63
+ string_kind,
67
64
FStringLayout :: from_f_string ( self . value , & locator) ,
68
65
) ;
69
66
70
67
// Starting prefix and quote
71
- write ! ( f, [ string. prefix( ) , quote_selection. quotes( ) ] ) ?;
68
+ let quotes = StringQuotes :: from ( string_kind) ;
69
+ write ! ( f, [ string_kind. prefix( ) , quotes] ) ?;
72
70
73
71
f. join ( )
74
72
. entries (
@@ -80,32 +78,23 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
80
78
. finish ( ) ?;
81
79
82
80
// Ending quote
83
- quote_selection . quotes ( ) . fmt ( f)
81
+ quotes. fmt ( f)
84
82
}
85
83
}
86
84
87
85
#[ derive( Clone , Copy , Debug ) ]
88
86
pub ( crate ) struct FStringContext {
89
- prefix : StringPrefix ,
90
- quotes : StringQuotes ,
87
+ kind : StringKind ,
91
88
layout : FStringLayout ,
92
89
}
93
90
94
91
impl FStringContext {
95
- const fn new ( prefix : StringPrefix , quotes : StringQuotes , layout : FStringLayout ) -> Self {
96
- Self {
97
- prefix,
98
- quotes,
99
- layout,
100
- }
101
- }
102
-
103
- pub ( crate ) const fn quotes ( self ) -> StringQuotes {
104
- self . quotes
92
+ const fn new ( kind : StringKind , layout : FStringLayout ) -> Self {
93
+ Self { kind, layout }
105
94
}
106
95
107
- pub ( crate ) const fn prefix ( self ) -> StringPrefix {
108
- self . prefix
96
+ pub ( crate ) fn kind ( self ) -> StringKind {
97
+ self . kind
109
98
}
110
99
111
100
pub ( crate ) const fn layout ( self ) -> FStringLayout {
0 commit comments