@@ -17,7 +17,7 @@ use syntax::{ast, ptr};
17
17
use syntax:: codemap:: { BytePos , CodeMap , Span } ;
18
18
use syntax:: parse:: classify;
19
19
20
- use { Indent , Shape , Spanned } ;
20
+ use spanned :: Spanned ;
21
21
use chains:: rewrite_chain;
22
22
use codemap:: { LineRangeUtils , SpanUtils } ;
23
23
use comment:: { combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
@@ -30,12 +30,13 @@ use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_format
30
30
use macros:: { rewrite_macro, MacroArg , MacroPosition } ;
31
31
use patterns:: { can_be_overflowed_pat, TuplePatField } ;
32
32
use rewrite:: { Rewrite , RewriteContext } ;
33
+ use shape:: { Indent , Shape } ;
33
34
use string:: { rewrite_string, StringFormat } ;
34
35
use types:: { can_be_overflowed_type, rewrite_path, PathContext } ;
35
36
use utils:: { colon_spaces, contains_skip, extra_offset, first_line_width, inner_attributes,
36
37
last_line_extendable, last_line_width, left_most_sub_expr, mk_sp, outer_attributes,
37
38
paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
38
- trimmed_last_line_width, wrap_str } ;
39
+ trimmed_last_line_width} ;
39
40
use vertical:: rewrite_with_alignment;
40
41
use visitor:: FmtVisitor ;
41
42
@@ -75,11 +76,7 @@ pub fn format_expr(
75
76
ast:: LitKind :: Str ( _, ast:: StrStyle :: Cooked ) => {
76
77
rewrite_string_lit ( context, l. span , shape)
77
78
}
78
- _ => wrap_str (
79
- context. snippet ( expr. span ) ,
80
- context. config . max_width ( ) ,
81
- shape,
82
- ) ,
79
+ _ => Some ( context. snippet ( expr. span ) ) ,
83
80
} ,
84
81
ast:: ExprKind :: Call ( ref callee, ref args) => {
85
82
let inner_span = mk_sp ( callee. span . hi ( ) , expr. span . hi ( ) ) ;
@@ -152,11 +149,7 @@ pub fn format_expr(
152
149
Some ( ident) => format ! ( " {}" , ident. node) ,
153
150
None => String :: new ( ) ,
154
151
} ;
155
- wrap_str (
156
- format ! ( "continue{}" , id_str) ,
157
- context. config . max_width ( ) ,
158
- shape,
159
- )
152
+ Some ( format ! ( "continue{}" , id_str) )
160
153
}
161
154
ast:: ExprKind :: Break ( ref opt_ident, ref opt_expr) => {
162
155
let id_str = match * opt_ident {
@@ -167,17 +160,13 @@ pub fn format_expr(
167
160
if let Some ( ref expr) = * opt_expr {
168
161
rewrite_unary_prefix ( context, & format ! ( "break{} " , id_str) , & * * expr, shape)
169
162
} else {
170
- wrap_str (
171
- format ! ( "break{}" , id_str) ,
172
- context. config . max_width ( ) ,
173
- shape,
174
- )
163
+ Some ( format ! ( "break{}" , id_str) )
175
164
}
176
165
}
177
166
ast:: ExprKind :: Yield ( ref opt_expr) => if let Some ( ref expr) = * opt_expr {
178
167
rewrite_unary_prefix ( context, "yield " , & * * expr, shape)
179
168
} else {
180
- wrap_str ( "yield" . to_string ( ) , context . config . max_width ( ) , shape )
169
+ Some ( "yield" . to_string ( ) )
181
170
} ,
182
171
ast:: ExprKind :: Closure ( capture, ref fn_decl, ref body, _) => {
183
172
rewrite_closure ( capture, fn_decl, body, expr. span , context, shape)
@@ -189,17 +178,10 @@ pub fn format_expr(
189
178
ast:: ExprKind :: Mac ( ref mac) => {
190
179
// Failure to rewrite a marco should not imply failure to
191
180
// rewrite the expression.
192
- rewrite_macro ( mac, None , context, shape, MacroPosition :: Expression ) . or_else ( || {
193
- wrap_str (
194
- context. snippet ( expr. span ) ,
195
- context. config . max_width ( ) ,
196
- shape,
197
- )
198
- } )
199
- }
200
- ast:: ExprKind :: Ret ( None ) => {
201
- wrap_str ( "return" . to_owned ( ) , context. config . max_width ( ) , shape)
181
+ rewrite_macro ( mac, None , context, shape, MacroPosition :: Expression )
182
+ . or_else ( || Some ( context. snippet ( expr. span ) ) )
202
183
}
184
+ ast:: ExprKind :: Ret ( None ) => Some ( "return" . to_owned ( ) ) ,
203
185
ast:: ExprKind :: Ret ( Some ( ref expr) ) => {
204
186
rewrite_unary_prefix ( context, "return " , & * * expr, shape)
205
187
}
@@ -301,16 +283,14 @@ pub fn format_expr(
301
283
} ;
302
284
rewrite_unary_suffix ( context, & sp_delim, & * lhs, shape)
303
285
}
304
- ( None , None ) => wrap_str ( delim. into ( ) , context . config . max_width ( ) , shape ) ,
286
+ ( None , None ) => Some ( delim. into ( ) ) ,
305
287
}
306
288
}
307
289
// We do not format these expressions yet, but they should still
308
290
// satisfy our width restrictions.
309
- ast:: ExprKind :: InPlace ( ..) | ast:: ExprKind :: InlineAsm ( ..) => wrap_str (
310
- context. snippet ( expr. span ) ,
311
- context. config . max_width ( ) ,
312
- shape,
313
- ) ,
291
+ ast:: ExprKind :: InPlace ( ..) | ast:: ExprKind :: InlineAsm ( ..) => {
292
+ Some ( context. snippet ( expr. span ) )
293
+ }
314
294
ast:: ExprKind :: Catch ( ref block) => {
315
295
if let rw @ Some ( _) = rewrite_single_line_block ( context, "do catch " , block, shape) {
316
296
rw
@@ -382,7 +362,11 @@ where
382
362
. map ( |first_line| first_line. ends_with ( '{' ) )
383
363
. unwrap_or ( false ) ;
384
364
if !rhs_result. contains ( '\n' ) || allow_same_line {
385
- return Some ( format ! ( "{}{}{}{}" , lhs_result, infix, rhs_result, suffix) ) ;
365
+ let one_line_width = last_line_width ( & lhs_result) + infix. len ( )
366
+ + first_line_width ( & rhs_result) + suffix. len ( ) ;
367
+ if one_line_width <= shape. width {
368
+ return Some ( format ! ( "{}{}{}{}" , lhs_result, infix, rhs_result, suffix) ) ;
369
+ }
386
370
}
387
371
}
388
372
@@ -2231,12 +2215,23 @@ where
2231
2215
_ if args. len ( ) >= 1 => {
2232
2216
item_vec[ args. len ( ) - 1 ] . item = args. last ( )
2233
2217
. and_then ( |last_arg| last_arg. rewrite ( context, shape) ) ;
2234
- tactic = definitive_tactic (
2235
- & * item_vec,
2236
- ListTactic :: LimitedHorizontalVertical ( args_max_width) ,
2237
- Separator :: Comma ,
2238
- one_line_width,
2239
- ) ;
2218
+ // Use horizontal layout for a function with a single argument as long as
2219
+ // everything fits in a single line.
2220
+ if args. len ( ) == 1
2221
+ && args_max_width != 0 // Vertical layout is forced.
2222
+ && !item_vec[ 0 ] . has_comment ( )
2223
+ && !item_vec[ 0 ] . inner_as_ref ( ) . contains ( '\n' )
2224
+ && :: lists:: total_item_width ( & item_vec[ 0 ] ) <= one_line_width
2225
+ {
2226
+ tactic = DefinitiveListTactic :: Horizontal ;
2227
+ } else {
2228
+ tactic = definitive_tactic (
2229
+ & * item_vec,
2230
+ ListTactic :: LimitedHorizontalVertical ( args_max_width) ,
2231
+ Separator :: Comma ,
2232
+ one_line_width,
2233
+ ) ;
2234
+ }
2240
2235
}
2241
2236
_ => ( ) ,
2242
2237
}
@@ -2664,11 +2659,7 @@ pub fn rewrite_field(
2664
2659
prefix_max_width : usize ,
2665
2660
) -> Option < String > {
2666
2661
if contains_skip ( & field. attrs ) {
2667
- return wrap_str (
2668
- context. snippet ( field. span ( ) ) ,
2669
- context. config . max_width ( ) ,
2670
- shape,
2671
- ) ;
2662
+ return Some ( context. snippet ( field. span ( ) ) ) ;
2672
2663
}
2673
2664
let name = & field. ident . node . to_string ( ) ;
2674
2665
if field. is_shorthand {
0 commit comments