From a32b0e7627ea31d1967568aa034f74a082de4bec Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Thu, 16 Jul 2015 14:03:52 +1200 Subject: [PATCH] Fix some bugs --- src/expr.rs | 19 ++++++++++-------- tests/source/struct_lits.rs | 4 +++- tests/target/struct_lits.rs | 39 +++++++++++++++---------------------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index e6390a08793..23caa933e1d 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -192,14 +192,17 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, } let path_str = pprust::path_to_string(path); - // Foo { a: Foo } - indent is +3, width is -5. - let (indent, budget) = match context.config.struct_lit_style { + let (indent, h_budget, v_budget) = match context.config.struct_lit_style { StructLitStyle::VisualIndent => { - (offset + path_str.len() + 3, width - (path_str.len() + 5)) + // Foo { a: Foo } - indent is +3, width is -5. + let budget = width - (path_str.len() + 5); + (offset + path_str.len() + 3, budget, budget) } StructLitStyle::BlockIndent => { + // If we are all on one line, then we'll ignore the indent, and we + // have a smaller budget. let indent = context.block_indent + context.config.tab_spaces; - (indent, width - indent) + (indent, width - (path_str.len() + 5), width - indent) } }; @@ -227,13 +230,13 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, |item| { match *item { StructLitField::Regular(ref field) => { - rewrite_field(context, &field, budget, indent) + rewrite_field(context, &field, h_budget, indent) .unwrap_or(context.codemap.span_to_snippet(field.span) .unwrap()) }, StructLitField::Base(ref expr) => { // 2 = .. - expr.rewrite(context, budget - 2, indent + 2) + expr.rewrite(context, h_budget - 2, indent + 2) .map(|s| format!("..{}", s)) .unwrap_or(context.codemap.span_to_snippet(expr.span) .unwrap()) @@ -252,8 +255,8 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, context.config.struct_lit_trailing_comma }, indent: indent, - h_width: budget, - v_width: budget, + h_width: h_budget, + v_width: v_budget, ends_with_newline: true, }; let fields_str = write_list(&items, &fmt); diff --git a/tests/source/struct_lits.rs b/tests/source/struct_lits.rs index b7e1a854b0b..f2726821e55 100644 --- a/tests/source/struct_lits.rs +++ b/tests/source/struct_lits.rs @@ -8,7 +8,9 @@ fn main() { Foo { a: foo() /* comment*/, /* comment*/ b: bar(), ..something }; - Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), }; + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), }; + + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), }; Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Comment diff --git a/tests/target/struct_lits.rs b/tests/target/struct_lits.rs index befeede14df..293a2a97b7a 100644 --- a/tests/target/struct_lits.rs +++ b/tests/target/struct_lits.rs @@ -13,33 +13,25 @@ fn main() { ..something }; - Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar() }; + + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar(), }; - Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Comment - a: foo(), /* C - * o - * m - * m - * e - * n - * t */ + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { // Comment - b: bar(), /* C - * o - * m - * m - * e - * n - * t */ + a: foo(), // Comment + // Comment + b: bar(), /* Comment */ }; Foo { a: Bar, b: foo() }; - A { // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit - // amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante + A { + // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed + // sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante // hendrerit. Donec et mollis dolor. first: item(), // Praesent et diam eget libero egestas mattis sit amet vitae augue. @@ -47,11 +39,12 @@ fn main() { second: Item, }; - Diagram { // o This graph demonstrates how - // / \ significant whitespace is - // o o preserved. - // /|\ \ - // o o o o + Diagram { + // o This graph demonstrates how + // / \ significant whitespace is + // o o preserved. + // /|\ \ + // o o o o graph: G, } }