@@ -20,6 +20,7 @@ use config::Config;
20
20
use rewrite:: { Rewrite , RewriteContext } ;
21
21
use comment:: rewrite_comment;
22
22
use macros:: rewrite_macro;
23
+ use items:: rewrite_static;
23
24
24
25
pub struct FmtVisitor < ' a > {
25
26
pub codemap : & ' a CodeMap ,
@@ -31,22 +32,8 @@ pub struct FmtVisitor<'a> {
31
32
}
32
33
33
34
impl < ' a , ' v > visit:: Visitor < ' v > for FmtVisitor < ' a > {
34
- // FIXME: We'd rather not format expressions here, as we have little
35
- // context. How are we still reaching this?
36
- fn visit_expr ( & mut self , ex : & ' v ast:: Expr ) {
37
- debug ! ( "visit_expr: {:?} {:?}" ,
38
- self . codemap. lookup_char_pos( ex. span. lo) ,
39
- self . codemap. lookup_char_pos( ex. span. hi) ) ;
40
- self . format_missing ( ex. span . lo ) ;
41
-
42
- let rewrite = ex. rewrite ( & self . get_context ( ) ,
43
- self . config . max_width - self . block_indent . width ( ) ,
44
- self . block_indent ) ;
45
-
46
- if let Some ( new_str) = rewrite {
47
- self . buffer . push_str ( & new_str) ;
48
- self . last_pos = ex. span . hi ;
49
- }
35
+ fn visit_expr ( & mut self , _: & ' v ast:: Expr ) {
36
+ unreachable ! ( )
50
37
}
51
38
52
39
fn visit_stmt ( & mut self , stmt : & ' v ast:: Stmt ) {
@@ -58,7 +45,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
58
45
}
59
46
}
60
47
ast:: Stmt_ :: StmtExpr ( ref ex, _) | ast:: Stmt_ :: StmtSemi ( ref ex, _) => {
61
- self . format_missing_with_indent ( stmt. span . lo ) ;
62
48
let suffix = if semicolon_for_stmt ( stmt) {
63
49
";"
64
50
} else {
@@ -67,13 +53,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
67
53
let rewrite = ex. rewrite ( & self . get_context ( ) ,
68
54
self . config . max_width - self . block_indent . width ( ) -
69
55
suffix. len ( ) ,
70
- self . block_indent ) ;
71
-
72
- if let Some ( new_str) = rewrite {
73
- self . buffer . push_str ( & new_str) ;
74
- self . buffer . push_str ( suffix) ;
75
- self . last_pos = stmt. span . hi ;
76
- }
56
+ self . block_indent )
57
+ . map ( |s| s + suffix) ;
58
+ self . push_rewrite ( stmt. span , rewrite) ;
77
59
}
78
60
ast:: Stmt_ :: StmtMac ( ref _mac, _macro_style) => {
79
61
self . format_missing_with_indent ( stmt. span . lo ) ;
@@ -104,16 +86,19 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
104
86
self . visit_stmt ( & stmt)
105
87
}
106
88
107
- match b. expr {
108
- Some ( ref e) => {
109
- self . format_missing_with_indent ( e. span . lo ) ;
110
- self . visit_expr ( e) ;
89
+ if let Some ( ref e) = b. expr {
90
+ self . format_missing_with_indent ( e. span . lo ) ;
91
+ let rewrite = e. rewrite ( & self . get_context ( ) ,
92
+ self . config . max_width - self . block_indent . width ( ) ,
93
+ self . block_indent )
94
+ . unwrap_or_else ( || self . snippet ( e. span ) ) ;
111
95
112
- if semicolon_for_expr ( e) {
113
- self . buffer . push_str ( ";" ) ;
114
- }
96
+ self . buffer . push_str ( & rewrite) ;
97
+ self . last_pos = e. span . hi ;
98
+
99
+ if semicolon_for_expr ( e) {
100
+ self . buffer . push_str ( ";" ) ;
115
101
}
116
- None => { }
117
102
}
118
103
119
104
self . block_indent = self . block_indent . block_unindent ( self . config ) ;
@@ -131,7 +116,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
131
116
b : & ' v ast:: Block ,
132
117
s : Span ,
133
118
_: ast:: NodeId ) {
134
-
135
119
let indent = self . block_indent ;
136
120
let rewrite = match fk {
137
121
visit:: FnKind :: ItemFn ( ident, ref generics, unsafety, constness, abi, vis) => {
@@ -190,6 +174,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
190
174
ast:: Item_ :: ItemUse ( ref vp) => {
191
175
self . format_import ( item. vis , vp, item. span ) ;
192
176
}
177
+ // FIXME(#78): format traits and impl definitions.
193
178
ast:: Item_ :: ItemImpl ( ..) |
194
179
ast:: Item_ :: ItemTrait ( ..) => {
195
180
self . block_indent = self . block_indent . block_indent ( self . config ) ;
@@ -203,8 +188,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
203
188
self . last_pos = item. span . hi ;
204
189
}
205
190
ast:: Item_ :: ItemStruct ( ref def, ref generics) => {
206
- self . format_missing_with_indent ( item. span . lo ) ;
207
- 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) ;
208
200
}
209
201
ast:: Item_ :: ItemEnum ( ref def, ref generics) => {
210
202
self . format_missing_with_indent ( item. span . lo ) ;
@@ -225,7 +217,28 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
225
217
self . format_missing_with_indent ( item. span . lo ) ;
226
218
self . format_foreign_mod ( foreign_mod, item. span ) ;
227
219
}
228
- _ => {
220
+ ast:: Item_ :: ItemStatic ( ref ty, mutability, ref expr) => {
221
+ let rewrite = rewrite_static ( "static" ,
222
+ item. ident ,
223
+ ty,
224
+ mutability,
225
+ expr,
226
+ & self . get_context ( ) ) ;
227
+ self . push_rewrite ( item. span , rewrite) ;
228
+ }
229
+ ast:: Item_ :: ItemConst ( ref ty, ref expr) => {
230
+ let rewrite = rewrite_static ( "const" ,
231
+ item. ident ,
232
+ ty,
233
+ ast:: Mutability :: MutImmutable ,
234
+ expr,
235
+ & self . get_context ( ) ) ;
236
+ self . push_rewrite ( item. span , rewrite) ;
237
+ }
238
+ // FIXME(#486): format type aliases.
239
+ ast:: Item_ :: ItemDefaultImpl ( ..) |
240
+ ast:: Item_ :: ItemFn ( ..) |
241
+ ast:: Item_ :: ItemTy ( ..) => {
229
242
visit:: walk_item ( self , item) ;
230
243
}
231
244
}
@@ -237,17 +250,10 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
237
250
}
238
251
239
252
if let ast:: TraitItem_ :: MethodTraitItem ( ref sig, None ) = ti. node {
240
- self . format_missing_with_indent ( ti. span . lo ) ;
241
-
242
253
let indent = self . block_indent ;
243
- let new_fn = self . rewrite_required_fn ( indent, ti. ident , sig, ti. span ) ;
244
-
245
- if let Some ( fn_str) = new_fn {
246
- self . buffer . push_str ( & fn_str) ;
247
- self . last_pos = ti. span . hi ;
248
- }
254
+ let rewrite = self . rewrite_required_fn ( indent, ti. ident , sig, ti. span ) ;
255
+ self . push_rewrite ( ti. span , rewrite) ;
249
256
}
250
- // TODO(#78): format trait types.
251
257
252
258
visit:: walk_trait_item ( self , ti)
253
259
}
@@ -272,6 +278,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
272
278
}
273
279
274
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
+
275
290
pub fn from_codemap ( codemap : & ' a CodeMap , config : & ' a Config ) -> FmtVisitor < ' a > {
276
291
FmtVisitor {
277
292
codemap : codemap,
0 commit comments