@@ -299,47 +299,28 @@ impl<'a> FmtVisitor<'a> {
299
299
self . last_pos = item. span . hi ( ) ;
300
300
}
301
301
302
- pub ( crate ) fn rewrite_fn (
302
+ pub ( crate ) fn rewrite_fn_before_block (
303
303
& mut self ,
304
304
indent : Indent ,
305
305
ident : ast:: Ident ,
306
306
fn_sig : & FnSig < ' _ > ,
307
307
span : Span ,
308
- block : & ast:: Block ,
309
- inner_attrs : Option < & [ ast:: Attribute ] > ,
310
- ) -> Option < String > {
308
+ ) -> Option < ( String , FnBraceStyle ) > {
311
309
let context = self . get_context ( ) ;
312
310
313
- let mut newline_brace = newline_for_brace ( self . config , & fn_sig. generics . where_clause ) ;
314
-
315
- let ( mut result, force_newline_brace) =
316
- rewrite_fn_base ( & context, indent, ident, fn_sig, span, newline_brace, true ) ?;
311
+ let mut fn_brace_style = newline_for_brace ( self . config , & fn_sig. generics . where_clause ) ;
312
+ let ( result, force_newline_brace) =
313
+ rewrite_fn_base ( & context, indent, ident, fn_sig, span, fn_brace_style) ?;
317
314
318
315
// 2 = ` {`
319
316
if self . config . brace_style ( ) == BraceStyle :: AlwaysNextLine
320
317
|| force_newline_brace
321
318
|| last_line_width ( & result) + 2 > self . shape ( ) . width
322
319
{
323
- newline_brace = true ;
324
- } else if !result. contains ( '\n' ) {
325
- newline_brace = false ;
320
+ fn_brace_style = FnBraceStyle :: NextLine
326
321
}
327
322
328
- if let rw @ Some ( ..) = self . single_line_fn ( & result, block, inner_attrs) {
329
- rw
330
- } else {
331
- // Prepare for the function body by possibly adding a newline and
332
- // indent.
333
- // FIXME we'll miss anything between the end of the signature and the
334
- // start of the body, but we need more spans from the compiler to solve
335
- // this.
336
- if newline_brace {
337
- result. push_str ( & indent. to_string_with_newline ( self . config ) ) ;
338
- } else {
339
- result. push ( ' ' ) ;
340
- }
341
- Some ( result)
342
- }
323
+ Some ( ( result, fn_brace_style) )
343
324
}
344
325
345
326
pub ( crate ) fn rewrite_required_fn (
@@ -360,8 +341,7 @@ impl<'a> FmtVisitor<'a> {
360
341
ident,
361
342
& FnSig :: from_method_sig ( sig, generics) ,
362
343
span,
363
- false ,
364
- false ,
344
+ FnBraceStyle :: None ,
365
345
) ?;
366
346
367
347
// Re-attach semicolon
@@ -370,7 +350,7 @@ impl<'a> FmtVisitor<'a> {
370
350
Some ( result)
371
351
}
372
352
373
- fn single_line_fn (
353
+ pub ( crate ) fn single_line_fn (
374
354
& self ,
375
355
fn_str : & str ,
376
356
block : & ast:: Block ,
@@ -1980,15 +1960,21 @@ pub(crate) fn is_named_arg(arg: &ast::Arg) -> bool {
1980
1960
}
1981
1961
}
1982
1962
1963
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
1964
+ pub ( crate ) enum FnBraceStyle {
1965
+ SameLine ,
1966
+ NextLine ,
1967
+ None ,
1968
+ }
1969
+
1983
1970
// Return type is (result, force_new_line_for_brace)
1984
1971
fn rewrite_fn_base (
1985
1972
context : & RewriteContext < ' _ > ,
1986
1973
indent : Indent ,
1987
1974
ident : ast:: Ident ,
1988
1975
fn_sig : & FnSig < ' _ > ,
1989
1976
span : Span ,
1990
- newline_brace : bool ,
1991
- has_body : bool ,
1977
+ fn_brace_style : FnBraceStyle ,
1992
1978
) -> Option < ( String , bool ) > {
1993
1979
let mut force_new_line_for_brace = false ;
1994
1980
@@ -2001,7 +1987,7 @@ fn rewrite_fn_base(
2001
1987
result. push_str ( "fn " ) ;
2002
1988
2003
1989
// Generics.
2004
- let overhead = if has_body && !newline_brace {
1990
+ let overhead = if let FnBraceStyle :: SameLine = fn_brace_style {
2005
1991
// 4 = `() {`
2006
1992
4
2007
1993
} else {
@@ -2044,8 +2030,7 @@ fn rewrite_fn_base(
2044
2030
& result,
2045
2031
indent,
2046
2032
ret_str_len,
2047
- newline_brace,
2048
- has_body,
2033
+ fn_brace_style,
2049
2034
multi_line_ret_str,
2050
2035
) ?;
2051
2036
@@ -2237,7 +2222,7 @@ fn rewrite_fn_base(
2237
2222
} else {
2238
2223
WhereClauseSpace :: Newline
2239
2224
} ;
2240
- let mut option = WhereClauseOption :: new ( !has_body , space) ;
2225
+ let mut option = WhereClauseOption :: new ( fn_brace_style == FnBraceStyle :: None , space) ;
2241
2226
if is_args_multi_lined {
2242
2227
option. veto_single_line ( ) ;
2243
2228
}
@@ -2412,30 +2397,25 @@ fn compute_budgets_for_args(
2412
2397
result : & str ,
2413
2398
indent : Indent ,
2414
2399
ret_str_len : usize ,
2415
- newline_brace : bool ,
2416
- has_braces : bool ,
2400
+ fn_brace_style : FnBraceStyle ,
2417
2401
force_vertical_layout : bool ,
2418
2402
) -> Option < ( ( usize , usize , Indent ) ) > {
2419
2403
debug ! (
2420
- "compute_budgets_for_args {} {:?}, {}, {}" ,
2404
+ "compute_budgets_for_args {} {:?}, {}, {:? }" ,
2421
2405
result. len( ) ,
2422
2406
indent,
2423
2407
ret_str_len,
2424
- newline_brace
2408
+ fn_brace_style ,
2425
2409
) ;
2426
2410
// Try keeping everything on the same line.
2427
2411
if !result. contains ( '\n' ) && !force_vertical_layout {
2428
2412
// 2 = `()`, 3 = `() `, space is before ret_string.
2429
2413
let overhead = if ret_str_len == 0 { 2 } else { 3 } ;
2430
2414
let mut used_space = indent. width ( ) + result. len ( ) + ret_str_len + overhead;
2431
- if has_braces {
2432
- if !newline_brace {
2433
- // 2 = `{}`
2434
- used_space += 2 ;
2435
- }
2436
- } else {
2437
- // 1 = `;`
2438
- used_space += 1 ;
2415
+ match fn_brace_style {
2416
+ FnBraceStyle :: None => used_space += 1 , // 1 = `;`
2417
+ FnBraceStyle :: SameLine => used_space += 2 , // 2 = `{}`
2418
+ FnBraceStyle :: NextLine => ( ) ,
2439
2419
}
2440
2420
let one_line_budget = context. budget ( used_space) ;
2441
2421
@@ -2448,7 +2428,10 @@ fn compute_budgets_for_args(
2448
2428
}
2449
2429
IndentStyle :: Visual => {
2450
2430
let indent = indent + result. len ( ) + 1 ;
2451
- let multi_line_overhead = indent. width ( ) + if newline_brace { 2 } else { 4 } ;
2431
+ let multi_line_overhead = match fn_brace_style {
2432
+ FnBraceStyle :: SameLine => 4 ,
2433
+ _ => 2 ,
2434
+ } + indent. width ( ) ;
2452
2435
( indent, context. budget ( multi_line_overhead) )
2453
2436
}
2454
2437
} ;
@@ -2468,16 +2451,21 @@ fn compute_budgets_for_args(
2468
2451
Some ( ( 0 , context. budget ( used_space) , new_indent) )
2469
2452
}
2470
2453
2471
- fn newline_for_brace ( config : & Config , where_clause : & ast:: WhereClause ) -> bool {
2454
+ fn newline_for_brace ( config : & Config , where_clause : & ast:: WhereClause ) -> FnBraceStyle {
2472
2455
let predicate_count = where_clause. predicates . len ( ) ;
2473
2456
2474
2457
if config. where_single_line ( ) && predicate_count == 1 {
2475
- return false ;
2458
+ return FnBraceStyle :: SameLine ;
2476
2459
}
2477
2460
let brace_style = config. brace_style ( ) ;
2478
2461
2479
- brace_style == BraceStyle :: AlwaysNextLine
2480
- || ( brace_style == BraceStyle :: SameLineWhere && predicate_count > 0 )
2462
+ let use_next_line = brace_style == BraceStyle :: AlwaysNextLine
2463
+ || ( brace_style == BraceStyle :: SameLineWhere && predicate_count > 0 ) ;
2464
+ if use_next_line {
2465
+ FnBraceStyle :: NextLine
2466
+ } else {
2467
+ FnBraceStyle :: SameLine
2468
+ }
2481
2469
}
2482
2470
2483
2471
fn rewrite_generics (
@@ -2919,8 +2907,7 @@ impl Rewrite for ast::ForeignItem {
2919
2907
self . ident ,
2920
2908
& FnSig :: new ( fn_decl, generics, self . vis . clone ( ) ) ,
2921
2909
span,
2922
- false ,
2923
- false ,
2910
+ FnBraceStyle :: None ,
2924
2911
)
2925
2912
. map ( |( s, _) | format ! ( "{};" , s) ) ,
2926
2913
ast:: ForeignItemKind :: Static ( ref ty, mutability) => {
0 commit comments