1- use super :: flow_map_implicit_entry:: format_implicit_entry_body ;
1+ use super :: flow_map_implicit_entry:: FormatImplicitEntryBody ;
22use crate :: prelude:: * ;
33use biome_formatter:: trivia:: format_dangling_comments;
4- use biome_formatter:: write;
4+ use biome_formatter:: { format_args , write} ;
55use biome_rowan:: AstNode ;
66use biome_yaml_syntax:: {
77 AnyYamlFlowNode , AnyYamlMappingImplicitKey , YamlFlowMapExplicitEntry ,
@@ -31,20 +31,43 @@ impl FormatNodeRule<YamlFlowMapExplicitEntry> for FormatYamlFlowMapExplicitEntry
3131
3232 // The explicit form is required when the key is a flow collection
3333 // that may break across multiple lines, or when a comment sits
34- // between the key and the `:`. Everywhere else Prettier prints the
35- // entry in the implicit `key: value` form
34+ // between the key and the `:`:
35+ //
36+ // ```yaml
37+ // {
38+ // ? [a, b]
39+ // : v,
40+ // ? key
41+ // # comment
42+ // : v,
43+ // }
44+ // ```
45+ //
46+ // Otherwise print the entry in the implicit `key: value` form:
47+ //
48+ // ```yaml
49+ // { ? key : v }
50+ // ```
51+ //
52+ // becomes
53+ //
54+ // ```yaml
55+ // { key: v }
56+ // ```
3657 let keep_explicit = colon_token. is_some ( )
3758 && ( has_dangling_comments
3859 || ( value. is_some ( ) && key. as_ref ( ) . is_some_and ( |key| key. is_flow_collection ( ) ) ) ) ;
3960
4061 if let ( Some ( colon_token) , true ) = ( & colon_token, keep_explicit) {
41- format_explicit_pair (
42- Some ( & question_mark_token) ,
43- & key,
44- colon_token,
45- & value,
46- has_dangling_comments. then ( || node. syntax ( ) ) ,
62+ write ! (
4763 f,
64+ [ FormatExplicitPair {
65+ question_mark_token: & question_mark_token,
66+ key: & key,
67+ colon_token,
68+ value: & value,
69+ dangling_comments_of: has_dangling_comments. then( || node. syntax( ) ) ,
70+ } ]
4871 )
4972 } else if !in_flow_mapping {
5073 // Inside a flow sequence, `? key` is a compact single-pair
@@ -61,24 +84,32 @@ impl FormatNodeRule<YamlFlowMapExplicitEntry> for FormatYamlFlowMapExplicitEntry
6184 f,
6285 [ align(
6386 " " ,
64- & format_with( |f| {
65- format_implicit_entry_body( & key, & colon_token, & value, false , f)
66- } )
87+ & FormatImplicitEntryBody {
88+ key: & key,
89+ colon_token: & colon_token,
90+ value: & value,
91+ in_flow_mapping: false
92+ }
6793 ) ]
6894 )
6995 } ) ) ]
7096 )
7197 } else if key. is_none ( ) && colon_token. is_none ( ) && value. is_none ( ) {
7298 // A lone `?` denotes an entry with an empty key and an empty
73- // value, which Prettier prints as a lone `:`
99+ // value, which we normalize to `:`.
74100 write ! ( f, [ format_replaced( & question_mark_token, & text( ":" , None ) ) ] )
75101 } else {
76102 write ! (
77103 f,
78- [ group( & format_with( |f| {
79- write!( f, [ format_removed( & question_mark_token) ] ) ?;
80- format_implicit_entry_body( & key, & colon_token, & value, true , f)
81- } ) ) ]
104+ [ group( & format_args![
105+ format_removed( & question_mark_token) ,
106+ FormatImplicitEntryBody {
107+ key: & key,
108+ colon_token: & colon_token,
109+ value: & value,
110+ in_flow_mapping: true
111+ }
112+ ] ) ]
82113 )
83114 }
84115 }
@@ -88,7 +119,7 @@ impl FormatNodeRule<YamlFlowMapExplicitEntry> for FormatYamlFlowMapExplicitEntry
88119 _: & YamlFlowMapExplicitEntry ,
89120 _: & mut YamlFormatter ,
90121 ) -> FormatResult < ( ) > {
91- // Printed in `fmt_fields ` between the key and the `:`
122+ // Printed in `FormatExplicitPair ` between the key and the `:`
92123 Ok ( ( ) )
93124 }
94125}
@@ -100,31 +131,28 @@ impl FormatNodeRule<YamlFlowMapExplicitEntry> for FormatYamlFlowMapExplicitEntry
100131/// : value
101132/// ```
102133///
103- /// When `question_mark_token` is absent (an implicit entry whose collection
104- /// key breaks across multiple lines, where only the explicit form is valid),
105- /// a `?` is synthesized, exactly like Prettier does.
106- pub ( crate ) fn format_explicit_pair (
107- question_mark_token : Option < & YamlSyntaxToken > ,
108- key : & Option < AnyYamlMappingImplicitKey > ,
109- colon_token : & YamlSyntaxToken ,
110- value : & Option < AnyYamlFlowNode > ,
111- dangling_comments_of : Option < & YamlSyntaxNode > ,
112- f : & mut YamlFormatter ,
113- ) -> FormatResult < ( ) > {
114- match question_mark_token {
115- Some ( question_mark_token) => write ! ( f, [ question_mark_token. format( ) ] ) ?,
116- None => write ! ( f, [ text( "?" , None ) ] ) ?,
117- }
118- if let Some ( key) = key {
119- write ! ( f, [ space( ) , align( " " , & key. format( ) ) ] ) ?;
120- }
121- write ! ( f, [ hard_line_break( ) ] ) ?;
122- if let Some ( node) = dangling_comments_of {
123- write ! ( f, [ format_dangling_comments( node) , hard_line_break( ) ] ) ?;
124- }
125- write ! ( f, [ colon_token. format( ) ] ) ?;
126- if let Some ( value) = value {
127- write ! ( f, [ space( ) , align( " " , & value. format( ) ) ] ) ?;
134+ struct FormatExplicitPair < ' a > {
135+ question_mark_token : & ' a YamlSyntaxToken ,
136+ key : & ' a Option < AnyYamlMappingImplicitKey > ,
137+ colon_token : & ' a YamlSyntaxToken ,
138+ value : & ' a Option < AnyYamlFlowNode > ,
139+ dangling_comments_of : Option < & ' a YamlSyntaxNode > ,
140+ }
141+
142+ impl Format < YamlFormatContext > for FormatExplicitPair < ' _ > {
143+ fn fmt ( & self , f : & mut YamlFormatter ) -> FormatResult < ( ) > {
144+ write ! ( f, [ self . question_mark_token. format( ) ] ) ?;
145+ if let Some ( key) = self . key {
146+ write ! ( f, [ space( ) , align( " " , & key. format( ) ) ] ) ?;
147+ }
148+ write ! ( f, [ hard_line_break( ) ] ) ?;
149+ if let Some ( node) = self . dangling_comments_of {
150+ write ! ( f, [ format_dangling_comments( node) , hard_line_break( ) ] ) ?;
151+ }
152+ write ! ( f, [ self . colon_token. format( ) ] ) ?;
153+ if let Some ( value) = self . value {
154+ write ! ( f, [ space( ) , align( " " , & value. format( ) ) ] ) ?;
155+ }
156+ Ok ( ( ) )
128157 }
129- Ok ( ( ) )
130158}
0 commit comments