@@ -45,7 +45,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
45
45
}
46
46
}
47
47
ast:: Stmt_ :: StmtExpr ( ref ex, _) | ast:: Stmt_ :: StmtSemi ( ref ex, _) => {
48
- self . format_missing_with_indent ( stmt. span . lo ) ;
49
48
let suffix = if semicolon_for_stmt ( stmt) {
50
49
";"
51
50
} else {
@@ -54,13 +53,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
54
53
let rewrite = ex. rewrite ( & self . get_context ( ) ,
55
54
self . config . max_width - self . block_indent . width ( ) -
56
55
suffix. len ( ) ,
57
- self . block_indent ) ;
58
-
59
- if let Some ( new_str) = rewrite {
60
- self . buffer . push_str ( & new_str) ;
61
- self . buffer . push_str ( suffix) ;
62
- self . last_pos = stmt. span . hi ;
63
- }
56
+ self . block_indent )
57
+ . map ( |s| s + suffix) ;
58
+ self . push_rewrite ( stmt. span , rewrite) ;
64
59
}
65
60
ast:: Stmt_ :: StmtMac ( ref _mac, _macro_style) => {
66
61
self . format_missing_with_indent ( stmt. span . lo ) ;
@@ -179,7 +174,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
179
174
ast:: Item_ :: ItemUse ( ref vp) => {
180
175
self . format_import ( item. vis , vp, item. span ) ;
181
176
}
182
- // TODO (#78): format traits and impl definitions.
177
+ // FIXME (#78): format traits and impl definitions.
183
178
ast:: Item_ :: ItemImpl ( ..) |
184
179
ast:: Item_ :: ItemTrait ( ..) => {
185
180
self . block_indent = self . block_indent . block_indent ( self . config ) ;
@@ -193,8 +188,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
193
188
self . last_pos = item. span . hi ;
194
189
}
195
190
ast:: Item_ :: ItemStruct ( ref def, ref generics) => {
196
- self . format_missing_with_indent ( item. span . lo ) ;
197
- self . visit_struct ( item. ident , item. vis , def, generics, item. span ) ;
191
+ let indent = self . block_indent ;
192
+ let rewrite = self . format_struct ( "struct " ,
193
+ item. ident ,
194
+ item. vis ,
195
+ def,
196
+ Some ( generics) ,
197
+ item. span ,
198
+ indent) ;
199
+ self . push_rewrite ( item. span , rewrite) ;
198
200
}
199
201
ast:: Item_ :: ItemEnum ( ref def, ref generics) => {
200
202
self . format_missing_with_indent ( item. span . lo ) ;
@@ -216,32 +218,24 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
216
218
self . format_foreign_mod ( foreign_mod, item. span ) ;
217
219
}
218
220
ast:: Item_ :: ItemStatic ( ref ty, mutability, ref expr) => {
219
- self . format_missing_with_indent ( item. span . lo ) ;
220
221
let rewrite = rewrite_static ( "static" ,
221
222
item. ident ,
222
223
ty,
223
224
mutability,
224
225
expr,
225
226
& self . get_context ( ) ) ;
226
- if let Some ( s) = rewrite {
227
- self . buffer . push_str ( & s) ;
228
- self . last_pos = item. span . hi ;
229
- }
227
+ self . push_rewrite ( item. span , rewrite) ;
230
228
}
231
229
ast:: Item_ :: ItemConst ( ref ty, ref expr) => {
232
- self . format_missing_with_indent ( item. span . lo ) ;
233
230
let rewrite = rewrite_static ( "const" ,
234
231
item. ident ,
235
232
ty,
236
233
ast:: Mutability :: MutImmutable ,
237
234
expr,
238
235
& self . get_context ( ) ) ;
239
- if let Some ( s) = rewrite {
240
- self . buffer . push_str ( & s) ;
241
- self . last_pos = item. span . hi ;
242
- }
236
+ self . push_rewrite ( item. span , rewrite) ;
243
237
}
244
- // TODO (#486): format type aliases.
238
+ // FIXME (#486): format type aliases.
245
239
ast:: Item_ :: ItemDefaultImpl ( ..) |
246
240
ast:: Item_ :: ItemFn ( ..) |
247
241
ast:: Item_ :: ItemTy ( ..) => {
@@ -256,15 +250,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
256
250
}
257
251
258
252
if let ast:: TraitItem_ :: MethodTraitItem ( ref sig, None ) = ti. node {
259
- self . format_missing_with_indent ( ti. span . lo ) ;
260
-
261
253
let indent = self . block_indent ;
262
- let new_fn = self . rewrite_required_fn ( indent, ti. ident , sig, ti. span ) ;
263
-
264
- if let Some ( fn_str) = new_fn {
265
- self . buffer . push_str ( & fn_str) ;
266
- self . last_pos = ti. span . hi ;
267
- }
254
+ let rewrite = self . rewrite_required_fn ( indent, ti. ident , sig, ti. span ) ;
255
+ self . push_rewrite ( ti. span , rewrite) ;
268
256
}
269
257
270
258
visit:: walk_trait_item ( self , ti)
@@ -290,6 +278,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
290
278
}
291
279
292
280
impl < ' a > FmtVisitor < ' a > {
281
+ fn push_rewrite ( & mut self , span : Span , rewrite : Option < String > ) {
282
+ self . format_missing_with_indent ( span. lo ) ;
283
+
284
+ if let Some ( res) = rewrite {
285
+ self . buffer . push_str ( & res) ;
286
+ self . last_pos = span. hi ;
287
+ }
288
+ }
289
+
293
290
pub fn from_codemap ( codemap : & ' a CodeMap , config : & ' a Config ) -> FmtVisitor < ' a > {
294
291
FmtVisitor {
295
292
codemap : codemap,
0 commit comments