@@ -237,10 +237,16 @@ fn bopen(s: ps) {
237
237
}
238
238
239
239
fn bclose_(s: ps, span: codemap::span, indented: uint) {
240
+ bclose_maybe_open(s, span, indented, true);
241
+ }
242
+ fn bclose_maybe_open (s: ps, span: codemap::span, indented: uint,
243
+ close_box: bool) {
240
244
maybe_print_comment(s, span.hi);
241
245
break_offset_if_not_bol(s, 1u, -(indented as int));
242
246
word(s.s, ~" } ") ;
243
- end ( s) ; // close the outer-box
247
+ if close_box {
248
+ end ( s) ; // close the outer-box
249
+ }
244
250
}
245
251
fn bclose ( s : ps , span : codemap:: span ) { bclose_ ( s, span, indent_unit) ; }
246
252
@@ -827,20 +833,27 @@ fn print_block(s: ps, blk: ast::blk) {
827
833
print_possibly_embedded_block(s, blk, block_normal, indent_unit);
828
834
}
829
835
836
+ fn print_block_unclosed(s: ps, blk: ast::blk) {
837
+ print_possibly_embedded_block_(s, blk, block_normal, indent_unit, ~[],
838
+ false);
839
+ }
840
+
830
841
fn print_block_with_attrs(s: ps, blk: ast::blk, attrs: ~[ast::attribute]) {
831
- print_possibly_embedded_block_(s, blk, block_normal, indent_unit, attrs);
842
+ print_possibly_embedded_block_(s, blk, block_normal, indent_unit, attrs,
843
+ true);
832
844
}
833
845
834
846
enum embed_type { block_block_fn, block_normal, }
835
847
836
848
fn print_possibly_embedded_block(s: ps, blk: ast::blk, embedded: embed_type,
837
849
indented: uint) {
838
850
print_possibly_embedded_block_(
839
- s, blk, embedded, indented, ~[]);
851
+ s, blk, embedded, indented, ~[], true );
840
852
}
841
853
842
854
fn print_possibly_embedded_block_(s: ps, blk: ast::blk, embedded: embed_type,
843
- indented: uint, attrs: ~[ast::attribute]) {
855
+ indented: uint, attrs: ~[ast::attribute],
856
+ close_box: bool) {
844
857
match blk.node.rules {
845
858
ast::unchecked_blk => word(s.s, ~" unchecked") ,
846
859
ast:: unsafe_blk => word( s. s , ~"unsafe ") ,
@@ -868,7 +881,7 @@ fn print_possibly_embedded_block_(s: ps, blk: ast::blk, embedded: embed_type,
868
881
}
869
882
_ => ( )
870
883
}
871
- bclose_ ( s, blk. span , indented) ;
884
+ bclose_maybe_open ( s, blk. span , indented, close_box ) ;
872
885
s. ann . post ( ann_node) ;
873
886
}
874
887
@@ -1060,9 +1073,9 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
1060
1073
let blk = if has_block {
1061
1074
let blk_arg = vec:: pop ( base_args) ;
1062
1075
match blk_arg. node {
1063
- ast:: expr_loop_body( _) => word_nbsp ( s, ~"for "),
1064
- ast::expr_do_body(_) => word_nbsp (s, ~" do") ,
1065
- _ => ( )
1076
+ ast:: expr_loop_body( _) => { head ( s, ~"for "); }
1077
+ ast::expr_do_body(_) => { head (s, ~" do") ; }
1078
+ _ => { }
1066
1079
}
1067
1080
some( blk_arg)
1068
1081
} else { none } ;
@@ -1074,7 +1087,19 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
1074
1087
}
1075
1088
if has_block {
1076
1089
nbsp ( s) ;
1077
- print_expr ( s, option:: get ( blk) ) ;
1090
+ match blk. get ( ) . node {
1091
+ // need to handle closures specifically
1092
+ ast:: expr_do_body( e) | ast:: expr_loop_body( e) => {
1093
+ end ( s) ; // we close our head box; closure
1094
+ // will create it's own.
1095
+ print_expr ( s, e) ;
1096
+ end ( s) ; // close outer box, as closures don't
1097
+ }
1098
+ _ => {
1099
+ // not sure if this can happen.
1100
+ print_expr ( s, blk. get ( ) ) ;
1101
+ }
1102
+ }
1078
1103
}
1079
1104
}
1080
1105
ast:: expr_binary ( op, lhs, rhs) => {
@@ -1174,12 +1199,31 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
1174
1199
print_block(s, body);
1175
1200
}
1176
1201
ast::expr_fn_block(decl, body, cap_clause) => {
1202
+ // in do/for blocks we don't want to show an empty
1203
+ // argument list, but at this point we don't know which
1204
+ // we are inside.
1205
+ //
1206
+ // if !decl.inputs.is_empty() {
1177
1207
print_fn_block_args(s, decl, *cap_clause);
1178
- // The parser always adds an extra implicit block around lambdas
1208
+ space(s.s);
1209
+ // }
1179
1210
assert body.node.stmts.is_empty();
1180
1211
assert body.node.expr.is_some();
1181
- space(s.s);
1182
- print_expr(s, body.node.expr.get());
1212
+ // we extract the block, so as not to create another set of boxes
1213
+ match body.node.expr.get().node {
1214
+ ast::expr_block(blk) => {
1215
+ print_block_unclosed(s, blk);
1216
+ }
1217
+ _ => {
1218
+ // this is a bare expression
1219
+ print_expr(s, body.node.expr.get());
1220
+ end(s); // need to close a box
1221
+ }
1222
+ }
1223
+ // a box will be closed by print_expr, but we didn't want an overall
1224
+ // wrapper so we closed the corresponding opening. so create an
1225
+ // empty box to satisfy the close.
1226
+ ibox(s, 0);
1183
1227
}
1184
1228
ast::expr_loop_body(body) => {
1185
1229
print_expr(s, body);
0 commit comments