10
10
11
11
use visitor:: FmtVisitor ;
12
12
use utils:: * ;
13
- use lists:: { write_list, ListFormatting , SeparatorTactic , ListTactic } ;
13
+ use lists:: { write_list, ListFormatting , SeparatorTactic , ListTactic , rewrite_tuple } ;
14
14
15
15
use syntax:: { ast, ptr} ;
16
16
use syntax:: codemap:: { Pos , Span } ;
@@ -21,7 +21,7 @@ use MIN_STRING;
21
21
22
22
impl < ' a > FmtVisitor < ' a > {
23
23
// TODO NEEDS TESTS
24
- fn rewrite_string_lit ( & mut self , s : & str , span : Span , width : usize , offset : usize ) -> String {
24
+ fn rewrite_string_lit ( & self , s : & str , span : Span , width : usize , offset : usize ) -> String {
25
25
// FIXME I bet this stomps unicode escapes in the source string
26
26
27
27
// Check if there is anything to fix: we always try to fixup multi-line
@@ -84,7 +84,7 @@ impl<'a> FmtVisitor<'a> {
84
84
result
85
85
}
86
86
87
- fn rewrite_call ( & mut self ,
87
+ fn rewrite_call ( & self ,
88
88
callee : & ast:: Expr ,
89
89
args : & [ ptr:: P < ast:: Expr > ] ,
90
90
width : usize ,
@@ -121,7 +121,7 @@ impl<'a> FmtVisitor<'a> {
121
121
format ! ( "{}({})" , callee_str, args_str)
122
122
}
123
123
124
- fn rewrite_paren ( & mut self , subexpr : & ast:: Expr , width : usize , offset : usize ) -> String {
124
+ fn rewrite_paren ( & self , subexpr : & ast:: Expr , width : usize , offset : usize ) -> String {
125
125
debug ! ( "rewrite_paren, width: {}, offset: {}" , width, offset) ;
126
126
// 1 is for opening paren, 2 is for opening+closing, we want to keep the closing
127
127
// paren on the same line as the subexpr
@@ -130,7 +130,7 @@ impl<'a> FmtVisitor<'a> {
130
130
format ! ( "({})" , subexpr_str)
131
131
}
132
132
133
- fn rewrite_struct_lit ( & mut self ,
133
+ fn rewrite_struct_lit ( & self ,
134
134
path : & ast:: Path ,
135
135
fields : & [ ast:: Field ] ,
136
136
base : Option < & ast:: Expr > ,
@@ -176,53 +176,20 @@ impl<'a> FmtVisitor<'a> {
176
176
// }
177
177
}
178
178
179
- fn rewrite_field ( & mut self , field : & ast:: Field , width : usize , offset : usize ) -> String {
179
+ fn rewrite_field ( & self , field : & ast:: Field , width : usize , offset : usize ) -> String {
180
180
let name = & token:: get_ident ( field. ident . node ) ;
181
181
let overhead = name. len ( ) + 2 ;
182
182
let expr = self . rewrite_expr ( & field. expr , width - overhead, offset + overhead) ;
183
183
format ! ( "{}: {}" , name, expr)
184
184
}
185
185
186
- fn rewrite_tuple_lit ( & mut self , items : & [ ptr:: P < ast:: Expr > ] , width : usize , offset : usize )
187
- -> String {
188
- // opening paren
189
- let indent = offset + 1 ;
190
- // In case of length 1, need a trailing comma
191
- if items. len ( ) == 1 {
192
- return format ! ( "({},)" , self . rewrite_expr( & * items[ 0 ] , width - 3 , indent) ) ;
193
- }
194
- // Only last line has width-1 as budget, other may take max_width
195
- let item_strs: Vec < _ > =
196
- items. iter ( )
197
- . enumerate ( )
198
- . map ( |( i, item) | self . rewrite_expr (
199
- item,
200
- // last line : given width (minus "("+")"), other lines : max_width
201
- // (minus "("+","))
202
- if i == items. len ( ) - 1 { width - 2 } else { config ! ( max_width) - indent - 2 } ,
203
- indent) )
204
- . collect ( ) ;
205
- let tactics = if item_strs. iter ( ) . any ( |s| s. contains ( '\n' ) ) {
206
- ListTactic :: Vertical
207
- } else {
208
- ListTactic :: HorizontalVertical
209
- } ;
210
- // FIXME handle comments
211
- let item_strs: Vec < _ > = item_strs. into_iter ( ) . map ( |s| ( s, String :: new ( ) ) ) . collect ( ) ;
212
- let fmt = ListFormatting {
213
- tactic : tactics,
214
- separator : "," ,
215
- trailing_separator : SeparatorTactic :: Never ,
216
- indent : indent,
217
- h_width : width - 2 ,
218
- v_width : width - 2 ,
219
- } ;
220
- let item_str = write_list ( & item_strs, & fmt) ;
221
- format ! ( "({})" , item_str)
186
+ fn rewrite_tuple_lit ( & self , items : & [ ptr:: P < ast:: Expr > ] , width : usize , offset : usize )
187
+ -> Option < String >
188
+ {
189
+ rewrite_tuple ( items, width, offset, & |exp, w, o| self . rewrite_expr ( exp, w, o) )
222
190
}
223
191
224
-
225
- pub fn rewrite_expr ( & mut self , expr : & ast:: Expr , width : usize , offset : usize ) -> String {
192
+ pub fn rewrite_expr ( & self , expr : & ast:: Expr , width : usize , offset : usize ) -> String {
226
193
match expr. node {
227
194
ast:: Expr_ :: ExprLit ( ref l) => {
228
195
match l. node {
@@ -248,7 +215,10 @@ impl<'a> FmtVisitor<'a> {
248
215
offset) ;
249
216
}
250
217
ast:: Expr_ :: ExprTup ( ref items) => {
251
- return self . rewrite_tuple_lit ( items, width, offset) ;
218
+ match self . rewrite_tuple_lit ( items, width, offset) {
219
+ Some ( s) => { return s; }
220
+ None => { }
221
+ }
252
222
}
253
223
_ => { }
254
224
}
0 commit comments