Skip to content

Commit e96403e

Browse files
committed
check struct lit ends with comma
1 parent 73b06bb commit e96403e

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/expr.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use chains::rewrite_chain;
2020
use closures;
2121
use codemap::{LineRangeUtils, SpanUtils};
2222
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
23-
rewrite_comment, rewrite_missing_comment, FindUncommented};
23+
rewrite_comment, rewrite_missing_comment, CharClasses, FindUncommented,
24+
FullCodeCharKind};
2425
use config::{Config, ControlBraceStyle, IndentStyle};
2526
use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
2627
struct_lit_shape, struct_lit_tactic, write_list, ListFormatting, ListItem, Separator};
@@ -2365,12 +2366,11 @@ pub fn wrap_args_with_parens(
23652366
}
23662367
}
23672368

2368-
/// Return true if a function call ,a method call or a struct represented by the given span ends with a
2369+
/// Return true if a function call or a method call represented by the given span ends with a
23692370
/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
23702371
/// comma from macro can potentially break the code.
23712372
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
23722373
let mut encountered_closing_paren = false;
2373-
let mut encountered_closing_braces = false;
23742374
for c in context.snippet(span).chars().rev() {
23752375
match c {
23762376
',' => return true,
@@ -2379,11 +2379,6 @@ fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
23792379
} else {
23802380
encountered_closing_paren = true;
23812381
},
2382-
'}' => if encountered_closing_braces {
2383-
return false;
2384-
} else {
2385-
encountered_closing_braces = true;
2386-
},
23872382
_ if c.is_whitespace() => continue,
23882383
_ => return false,
23892384
}
@@ -2487,6 +2482,26 @@ fn struct_lit_can_be_aligned(fields: &[ast::Field], base: &Option<&ast::Expr>) -
24872482
fields.iter().all(|field| !field.is_shorthand)
24882483
}
24892484

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+
24902505
fn rewrite_struct_lit<'a>(
24912506
context: &RewriteContext,
24922507
path: &ast::Path,
@@ -2573,12 +2588,13 @@ fn rewrite_struct_lit<'a>(
25732588
let tactic = struct_lit_tactic(h_shape, context, &item_vec);
25742589
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
25752590

2576-
let ends_with_comma = span_ends_with_comma(context, span);
2591+
let ends_with_comma = struct_lit_ends_with_comma(context, span);
25772592
let force_no_trailing_comma = if context.inside_macro && !ends_with_comma {
25782593
true
25792594
} else {
25802595
false
25812596
};
2597+
25822598
let fmt = struct_lit_formatting(
25832599
nested_shape,
25842600
tactic,

0 commit comments

Comments
 (0)