Fix an off-by-one error due to the comma in a struct lit field

Also allows splitting the field expression on a new line after the field name.

Fixes #698
This commit is contained in:
Nick Cameron 2016-01-14 20:26:15 +13:00
parent a37309ca0d
commit f0ca7aec24
4 changed files with 38 additions and 3 deletions

View File

@ -1372,6 +1372,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
// Foo { a: Foo } - indent is +3, width is -5. // Foo { a: Foo } - indent is +3, width is -5.
let h_budget = width.checked_sub(path_str.len() + 5).unwrap_or(0); let h_budget = width.checked_sub(path_str.len() + 5).unwrap_or(0);
// The 1 taken from the v_budget is for the comma.
let (indent, v_budget) = match context.config.struct_lit_style { let (indent, v_budget) = match context.config.struct_lit_style {
StructLitStyle::Visual => (offset + path_str.len() + 3, h_budget), StructLitStyle::Visual => (offset + path_str.len() + 3, h_budget),
StructLitStyle::Block => { StructLitStyle::Block => {
@ -1416,7 +1417,10 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
|item| { |item| {
match *item { match *item {
StructLitField::Regular(ref field) => { StructLitField::Regular(ref field) => {
rewrite_field(inner_context, &field, v_budget, indent) rewrite_field(inner_context,
&field,
v_budget.checked_sub(1).unwrap_or(0),
indent)
} }
StructLitField::Base(ref expr) => { StructLitField::Base(ref expr) => {
// 2 = .. // 2 = ..
@ -1502,7 +1506,19 @@ fn rewrite_field(context: &RewriteContext,
let expr = field.expr.rewrite(context, let expr = field.expr.rewrite(context,
try_opt!(width.checked_sub(overhead)), try_opt!(width.checked_sub(overhead)),
offset + overhead); offset + overhead);
expr.map(|s| format!("{}: {}", name, s))
match expr {
Some(e) => Some(format!("{}: {}", name, e)),
None => {
let expr_offset = offset.block_indent(&context.config);
let expr = field.expr.rewrite(context,
try_opt!(context.config
.max_width
.checked_sub(expr_offset.width())),
expr_offset);
expr.map(|s| format!("{}:\n{}{}", name, expr_offset.to_string(&context.config), s))
}
}
} }
pub fn rewrite_tuple<'a, I>(context: &RewriteContext, pub fn rewrite_tuple<'a, I>(context: &RewriteContext,

View File

@ -663,7 +663,7 @@ fn format_struct_struct(context: &RewriteContext,
} }
let item_indent = offset.block_indent(context.config); let item_indent = offset.block_indent(context.config);
// 2 = "," // 1 = ","
let item_budget = try_opt!(context.config.max_width.checked_sub(item_indent.width() + 1)); let item_budget = try_opt!(context.config.max_width.checked_sub(item_indent.width() + 1));
let items = itemize_list(context.codemap, let items = itemize_list(context.codemap,

View File

@ -114,3 +114,12 @@ fn issue491() {
Foo { a: aaaaaaaaaa, b: bbbbbbbb, c: cccccccccc, d: dddddddddd, /* a comment */ Foo { a: aaaaaaaaaa, b: bbbbbbbb, c: cccccccccc, d: dddddddddd, /* a comment */
e: eeeeeeeee }; e: eeeeeeeee };
} }
fn issue698() {
Record {
ffffffffffffffffffffffffffields: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
};
Record {
ffffffffffffffffffffffffffields: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
}
}

View File

@ -149,3 +149,13 @@ fn issue491() {
e: eeeeeeeee, e: eeeeeeeee,
}; };
} }
fn issue698() {
Record {
ffffffffffffffffffffffffffields: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
};
Record {
ffffffffffffffffffffffffffields:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
}
}