@@ -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 ) {
@@ -104,16 +91,19 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
104
91
self . visit_stmt ( & stmt)
105
92
}
106
93
107
- match b. expr {
108
- Some ( ref e) => {
109
- self . format_missing_with_indent ( e. span . lo ) ;
110
- self . visit_expr ( e) ;
94
+ if let Some ( ref e) = b. expr {
95
+ self . format_missing_with_indent ( e. span . lo ) ;
96
+ let rewrite = e. rewrite ( & self . get_context ( ) ,
97
+ self . config . max_width - self . block_indent . width ( ) ,
98
+ self . block_indent )
99
+ . unwrap_or_else ( || self . snippet ( e. span ) ) ;
111
100
112
- if semicolon_for_expr ( e) {
113
- self . buffer . push_str ( ";" ) ;
114
- }
101
+ self . buffer . push_str ( & rewrite) ;
102
+ self . last_pos = e. span . hi ;
103
+
104
+ if semicolon_for_expr ( e) {
105
+ self . buffer . push_str ( ";" ) ;
115
106
}
116
- None => { }
117
107
}
118
108
119
109
self . block_indent = self . block_indent . block_unindent ( self . config ) ;
@@ -131,7 +121,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
131
121
b : & ' v ast:: Block ,
132
122
s : Span ,
133
123
_: ast:: NodeId ) {
134
-
135
124
let indent = self . block_indent ;
136
125
let rewrite = match fk {
137
126
visit:: FnKind :: ItemFn ( ident, ref generics, unsafety, constness, abi, vis) => {
@@ -190,6 +179,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
190
179
ast:: Item_ :: ItemUse ( ref vp) => {
191
180
self . format_import ( item. vis , vp, item. span ) ;
192
181
}
182
+ // TODO(#78): format traits and impl definitions.
193
183
ast:: Item_ :: ItemImpl ( ..) |
194
184
ast:: Item_ :: ItemTrait ( ..) => {
195
185
self . block_indent = self . block_indent . block_indent ( self . config ) ;
@@ -225,7 +215,36 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
225
215
self . format_missing_with_indent ( item. span . lo ) ;
226
216
self . format_foreign_mod ( foreign_mod, item. span ) ;
227
217
}
228
- _ => {
218
+ ast:: Item_ :: ItemStatic ( ref ty, mutability, ref expr) => {
219
+ self . format_missing_with_indent ( item. span . lo ) ;
220
+ let rewrite = rewrite_static ( "static" ,
221
+ item. ident ,
222
+ ty,
223
+ mutability,
224
+ expr,
225
+ & self . get_context ( ) ) ;
226
+ if let Some ( s) = rewrite {
227
+ self . buffer . push_str ( & s) ;
228
+ self . last_pos = item. span . hi ;
229
+ }
230
+ }
231
+ ast:: Item_ :: ItemConst ( ref ty, ref expr) => {
232
+ self . format_missing_with_indent ( item. span . lo ) ;
233
+ let rewrite = rewrite_static ( "const" ,
234
+ item. ident ,
235
+ ty,
236
+ ast:: Mutability :: MutImmutable ,
237
+ expr,
238
+ & self . get_context ( ) ) ;
239
+ if let Some ( s) = rewrite {
240
+ self . buffer . push_str ( & s) ;
241
+ self . last_pos = item. span . hi ;
242
+ }
243
+ }
244
+ // TODO(#486): format type aliases.
245
+ ast:: Item_ :: ItemDefaultImpl ( ..) |
246
+ ast:: Item_ :: ItemFn ( ..) |
247
+ ast:: Item_ :: ItemTy ( ..) => {
229
248
visit:: walk_item ( self , item) ;
230
249
}
231
250
}
@@ -247,7 +266,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
247
266
self . last_pos = ti. span . hi ;
248
267
}
249
268
}
250
- // TODO(#78): format trait types.
251
269
252
270
visit:: walk_trait_item ( self , ti)
253
271
}
0 commit comments