11use crate :: {
22 config:: items:: ItemBraceStyle ,
33 fmt:: { Format , FormattedCode , Formatter } ,
4- utils:: {
5- bracket:: { AngleBracket , CurlyBrace } ,
6- item_len:: ItemLen ,
7- } ,
4+ utils:: { bracket:: CurlyBrace , item_len:: ItemLen } ,
85 FormatterError ,
96} ;
10- use sway_parse:: ItemStruct ;
7+ use std:: fmt:: Write ;
8+ use sway_parse:: {
9+ token:: { Delimiter , PunctKind } ,
10+ ItemStruct ,
11+ } ;
1112use sway_types:: Spanned ;
1213
1314impl Format for ItemStruct {
@@ -26,12 +27,11 @@ impl Format for ItemStruct {
2627 let struct_lit_single_line = formatter. config . structures . struct_lit_single_line ;
2728
2829 // Get the width limit of a struct to be formatted into single line if struct_lit_single_line is true.
29- let config_whitespace = formatter. config . whitespace ;
3030 let width_heuristics = formatter
3131 . config
3232 . heuristics
3333 . heuristics_pref
34- . to_width_heuristics ( & config_whitespace ) ;
34+ . to_width_heuristics ( & formatter . config . whitespace ) ;
3535 let struct_lit_width = width_heuristics. struct_lit_width ;
3636
3737 let multiline = !struct_lit_single_line || self . get_formatted_len ( ) > struct_lit_width;
@@ -65,43 +65,29 @@ fn format_struct(
6565) -> Result < ( ) , FormatterError > {
6666 // If there is a visibility token add it to the formatted_code with a ` ` after it.
6767 if let Some ( visibility) = & item_struct. visibility {
68- formatted_code. push_str ( visibility. span ( ) . as_str ( ) ) ;
69- formatted_code. push ( ' ' ) ;
68+ write ! ( formatted_code, "{} " , visibility. span( ) . as_str( ) ) ?;
7069 }
7170 // Add struct token
72- formatted_code. push_str ( item_struct. struct_token . span ( ) . as_str ( ) ) ;
73- formatted_code. push ( ' ' ) ;
71+ write ! (
72+ formatted_code,
73+ "{} " ,
74+ item_struct. struct_token. span( ) . as_str( )
75+ ) ?;
7476
7577 // Add struct name
7678 formatted_code. push_str ( item_struct. name . as_str ( ) ) ;
7779
78- // Check if there is generic provided
80+ // Format `GenericParams`, if any
7981 if let Some ( generics) = & item_struct. generics {
80- // Push angle brace
81- ItemStruct :: open_angle_bracket ( formatted_code, formatter) ?;
82- // Get generics fields
83- let generics = generics. parameters . inner . value_separator_pairs . clone ( ) ;
84- for ( index, generic) in generics. iter ( ) . enumerate ( ) {
85- // Push ident
86- formatted_code. push_str ( generic. 0 . as_str ( ) ) ;
87- if index != generics. len ( ) - 1 {
88- // Push `, ` if this is not the last generic
89- formatted_code. push_str ( ", " ) ;
90- }
91- }
82+ generics. format ( formatted_code, formatter) ?;
9283 }
9384
9485 // Handle openning brace
9586 if multiline {
9687 ItemStruct :: open_curly_brace ( formatted_code, formatter) ?;
9788 formatted_code. push ( '\n' ) ;
9889 } else {
99- // Push a single whitespace before `{`
100- formatted_code. push ( ' ' ) ;
101- // Push open brace
102- formatted_code. push ( '{' ) ;
103- // Push a single whitespace after `{`
104- formatted_code. push ( ' ' ) ;
90+ write ! ( formatted_code, " {} " , Delimiter :: Brace . as_open_char( ) ) ?;
10591 }
10692
10793 let items = item_struct
@@ -140,17 +126,19 @@ fn format_struct(
140126 // TODO: Improve handling this
141127 formatted_code. push_str ( & ( 0 ..required_alignment) . map ( |_| ' ' ) . collect :: < String > ( ) ) ;
142128 }
143- // Add `:`
144- formatted_code. push_str ( type_field. colon_token . ident ( ) . as_str ( ) ) ;
145- formatted_code. push ( ' ' ) ;
129+ // Add `:` and ty
146130 // TODO: We are currently converting ty to string directly but we will probably need to format ty before adding.
147- // Add ty
148- formatted_code. push_str ( type_field. ty . span ( ) . as_str ( ) ) ;
131+ write ! (
132+ formatted_code,
133+ "{} {}" ,
134+ type_field. colon_token. ident( ) . as_str( ) ,
135+ type_field. ty. span( ) . as_str( )
136+ ) ?;
149137 // Add `, ` if this isn't the last field.
150138 if !multiline && item_index != items. len ( ) - 1 {
151- formatted_code . push_str ( " , ") ;
139+ write ! ( formatted_code , "{} " , PunctKind :: Comma . as_char ( ) ) ? ;
152140 } else if multiline {
153- formatted_code . push_str ( ", \n " ) ;
141+ writeln ! ( formatted_code , "{}" , PunctKind :: Comma . as_char ( ) ) ? ;
154142 }
155143 }
156144 if !multiline {
@@ -181,12 +169,12 @@ impl CurlyBrace for ItemStruct {
181169 match brace_style {
182170 ItemBraceStyle :: AlwaysNextLine => {
183171 // Add openning brace to the next line.
184- line . push_str ( "\n {" ) ;
172+ write ! ( line , "\n {}" , Delimiter :: Brace . as_open_char ( ) ) ? ;
185173 shape = shape. block_indent ( extra_width) ;
186174 }
187175 _ => {
188176 // Add opening brace to the same line
189- line . push_str ( " {" ) ;
177+ write ! ( line , " {}" , Delimiter :: Brace . as_open_char ( ) ) ? ;
190178 shape = shape. block_indent ( extra_width) ;
191179 }
192180 }
@@ -199,7 +187,7 @@ impl CurlyBrace for ItemStruct {
199187 line : & mut String ,
200188 formatter : & mut Formatter ,
201189 ) -> Result < ( ) , FormatterError > {
202- line. push ( '}' ) ;
190+ line. push ( Delimiter :: Brace . as_close_char ( ) ) ;
203191 // If shape is becoming left-most alligned or - indent just have the defualt shape
204192 formatter. shape = formatter
205193 . shape
@@ -208,21 +196,3 @@ impl CurlyBrace for ItemStruct {
208196 Ok ( ( ) )
209197 }
210198}
211-
212- impl AngleBracket for ItemStruct {
213- fn open_angle_bracket (
214- line : & mut String ,
215- _formatter : & mut Formatter ,
216- ) -> Result < ( ) , FormatterError > {
217- line. push ( '<' ) ;
218- Ok ( ( ) )
219- }
220-
221- fn close_angle_bracket (
222- line : & mut String ,
223- _formatter : & mut Formatter ,
224- ) -> Result < ( ) , FormatterError > {
225- line. push ( '>' ) ;
226- Ok ( ( ) )
227- }
228- }
0 commit comments