Skip to content

Commit 10e808b

Browse files
committed
almost done
1 parent e96403e commit 10e808b

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

rustfmt-core/tests/source/issue-2445.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ test!(RunPassPretty {
44
mode: "pretty",
55
suite: "run-pass",
66
default: false,
7-
host: true // comment
7+
host: true // should force no trailing comma here
8+
});
9+
10+
test!(RunPassPretty {
11+
// comment
12+
path: "src/test/run-pass/pretty",
13+
mode: "pretty",
14+
suite: "run-pass",
15+
default: false,
16+
host: true, // should preserve the trailing comma
817
});
918

1019
test!(Test{

rustfmt-core/tests/target/issue-2445.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@ test!(RunPassPretty {
44
mode: "pretty",
55
suite: "run-pass",
66
default: false,
7-
host: true // comment
7+
host: true // should force no trailing comma here
8+
});
9+
10+
test!(RunPassPretty {
11+
// comment
12+
path: "src/test/run-pass/pretty",
13+
mode: "pretty",
14+
suite: "run-pass",
15+
default: false,
16+
host: true, // should preserve the trailing comma
817
});
918

1019
test!(Test {
11-
field: i32 // comment
20+
field: i32, // comment
1221
});

src/expr.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10-
1110
use std::borrow::Cow;
1211
use std::cmp::min;
1312
use std::iter::repeat;
@@ -20,8 +19,7 @@ use chains::rewrite_chain;
2019
use closures;
2120
use codemap::{LineRangeUtils, SpanUtils};
2221
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};
2523
use config::{Config, ControlBraceStyle, IndentStyle};
2624
use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
2725
struct_lit_shape, struct_lit_tactic, write_list, ListFormatting, ListItem, Separator};
@@ -2371,9 +2369,24 @@ pub fn wrap_args_with_parens(
23712369
/// comma from macro can potentially break the code.
23722370
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
23732371
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() {
23752383
match c {
23762384
',' => return true,
2385+
'}' => if encountered_closing_braces {
2386+
return false;
2387+
} else {
2388+
encountered_closing_braces = true;
2389+
},
23772390
')' => if encountered_closing_paren {
23782391
return false;
23792392
} else {
@@ -2482,26 +2495,6 @@ fn struct_lit_can_be_aligned(fields: &[ast::Field], base: &Option<&ast::Expr>) -
24822495
fields.iter().all(|field| !field.is_shorthand)
24832496
}
24842497

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-
25052498
fn rewrite_struct_lit<'a>(
25062499
context: &RewriteContext,
25072500
path: &ast::Path,
@@ -2588,7 +2581,7 @@ fn rewrite_struct_lit<'a>(
25882581
let tactic = struct_lit_tactic(h_shape, context, &item_vec);
25892582
let nested_shape = shape_for_tactic(tactic, h_shape, v_shape);
25902583

2591-
let ends_with_comma = struct_lit_ends_with_comma(context, span);
2584+
let ends_with_comma = span_ends_with_comma(context, span);
25922585
let force_no_trailing_comma = if context.inside_macro && !ends_with_comma {
25932586
true
25942587
} else {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![feature(decl_macro)]
1313
#![feature(match_default_bindings)]
1414
#![feature(type_ascription)]
15+
#![feature(string_retain)]
1516

1617
#[macro_use]
1718
extern crate derive_new;

0 commit comments

Comments
 (0)