7
7
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
-
11
10
use std:: borrow:: Cow ;
12
11
use std:: cmp:: min;
13
12
use std:: iter:: repeat;
@@ -20,8 +19,7 @@ use chains::rewrite_chain;
20
19
use closures;
21
20
use codemap:: { LineRangeUtils , SpanUtils } ;
22
21
use comment:: { combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
23
- rewrite_comment, rewrite_missing_comment, CharClasses , FindUncommented ,
24
- FullCodeCharKind } ;
22
+ rewrite_comment, rewrite_missing_comment, CharClasses , FindUncommented } ;
25
23
use config:: { Config , ControlBraceStyle , IndentStyle } ;
26
24
use lists:: { definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
27
25
struct_lit_shape, struct_lit_tactic, write_list, ListFormatting , ListItem , Separator } ;
@@ -2371,9 +2369,24 @@ pub fn wrap_args_with_parens(
2371
2369
/// comma from macro can potentially break the code.
2372
2370
fn span_ends_with_comma ( context : & RewriteContext , span : Span ) -> bool {
2373
2371
let mut encountered_closing_paren = false ;
2374
- for c in context. snippet ( span) . chars ( ) . rev ( ) {
2372
+ let mut encountered_closing_braces = false ;
2373
+
2374
+ let snippet = context. snippet ( span) ;
2375
+ let mut snippet_string = snippet. to_string ( ) ;
2376
+ for ( kind, c) in CharClasses :: new ( snippet. chars ( ) ) {
2377
+ match kind. is_comment ( ) {
2378
+ true => snippet_string. retain ( |i| c != i) ,
2379
+ false => continue ,
2380
+ }
2381
+ }
2382
+ for c in snippet_string. as_str ( ) . chars ( ) . rev ( ) {
2375
2383
match c {
2376
2384
',' => return true ,
2385
+ '}' => if encountered_closing_braces {
2386
+ return false ;
2387
+ } else {
2388
+ encountered_closing_braces = true ;
2389
+ } ,
2377
2390
')' => if encountered_closing_paren {
2378
2391
return false ;
2379
2392
} else {
@@ -2482,26 +2495,6 @@ fn struct_lit_can_be_aligned(fields: &[ast::Field], base: &Option<&ast::Expr>) -
2482
2495
fields. iter ( ) . all ( |field| !field. is_shorthand )
2483
2496
}
2484
2497
2485
- #[ allow( dead_code) ]
2486
- /// Check if a struct representation ends with comma by match on the chars of struct
2487
- /// snippet reversely, once a FullCodeCharKind::Normal comma matched, returns true
2488
- fn struct_lit_ends_with_comma ( context : & RewriteContext , span : Span ) -> bool {
2489
- // FIXME: implement DoubleEndedIterator trait for CharClasses
2490
- //
2491
- // for (char_kind, c) in CharClasses::new(context.snippet(span).chars()).rev() {
2492
- // match c {
2493
- // ',' => match char_kind {
2494
- // FullCodeCharKind::Normal => return true,
2495
- // _ => continue,
2496
- // },
2497
- // _ => continue,
2498
- // }
2499
- // }
2500
- //
2501
- // false
2502
- unimplemented ! ( )
2503
- }
2504
-
2505
2498
fn rewrite_struct_lit < ' a > (
2506
2499
context : & RewriteContext ,
2507
2500
path : & ast:: Path ,
@@ -2588,7 +2581,7 @@ fn rewrite_struct_lit<'a>(
2588
2581
let tactic = struct_lit_tactic ( h_shape, context, & item_vec) ;
2589
2582
let nested_shape = shape_for_tactic ( tactic, h_shape, v_shape) ;
2590
2583
2591
- let ends_with_comma = struct_lit_ends_with_comma ( context, span) ;
2584
+ let ends_with_comma = span_ends_with_comma ( context, span) ;
2592
2585
let force_no_trailing_comma = if context. inside_macro && !ends_with_comma {
2593
2586
true
2594
2587
} else {
0 commit comments