From 3a1ffa7db7c0a641cf2aa04171815a564317416a Mon Sep 17 00:00:00 2001 From: topecongiro Date: Wed, 29 Mar 2017 20:15:27 +0900 Subject: [PATCH] Split long fields in structs This commit splits long fields in structs. Closes #1412. --- src/items.rs | 65 +++++++++++++++++++++++++++++++---------- tests/source/structs.rs | 11 +++++++ tests/target/structs.rs | 16 ++++++++-- 3 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/items.rs b/src/items.rs index c218d629d19..2cb4e46ecb5 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1220,25 +1220,60 @@ impl Rewrite for ast::StructField { } let type_annotation_spacing = type_annotation_spacing(context.config); - let result = match name { - Some(name) => { - format!("{}{}{}{}:{}", - attr_str, - vis, - name, - type_annotation_spacing.0, - type_annotation_spacing.1) - } + let mut result = match name { + Some(name) => format!("{}{}{}{}:", attr_str, vis, name, type_annotation_spacing.0), None => format!("{}{}", attr_str, vis), }; - let last_line_width = last_line_width(&result); + let type_offset = shape.indent.block_indent(context.config); + let rewrite_type_in_next_line = || { + let budget = try_opt!(context + .config + .max_width + .checked_sub(type_offset.width())); + self.ty + .rewrite(context, Shape::legacy(budget, type_offset)) + }; + + let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len(); let budget = try_opt!(shape.width.checked_sub(last_line_width)); - let rewrite = try_opt!(self.ty - .rewrite(context, - Shape::legacy(budget, - shape.indent + last_line_width))); - Some(result + &rewrite) + let ty_rewritten = self.ty + .rewrite(context, + Shape::legacy(budget, shape.indent + last_line_width)); + match ty_rewritten { + Some(ref ty) if ty.contains('\n') => { + let new_ty = rewrite_type_in_next_line(); + match new_ty { + Some(ref new_ty) if !new_ty.contains('\n') && + new_ty.len() + type_offset.width() <= + context.config.max_width => { + Some(format!("{}\n{}{}", + result, + type_offset.to_string(&context.config), + &new_ty)) + } + _ => { + if name.is_some() { + result.push_str(type_annotation_spacing.1); + } + Some(result + &ty) + } + } + } + Some(ty) => { + if name.is_some() { + result.push_str(type_annotation_spacing.1); + } + Some(result + &ty) + } + None => { + let ty = try_opt!(rewrite_type_in_next_line()); + Some(format!("{}\n{}{}", + result, + type_offset.to_string(&context.config), + &ty)) + } + } } } diff --git a/tests/source/structs.rs b/tests/source/structs.rs index d24b2ba1a77..d7cfe7ec548 100644 --- a/tests/source/structs.rs +++ b/tests/source/structs.rs @@ -167,3 +167,14 @@ struct Foo { } struct Foo { /* comment */ } struct Foo(); + +struct LongStruct { + a: A, + the_quick_brown_fox_jumps_over_the_lazy_dog:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, +} + +struct Deep { + deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep: node::Handle>, + Type, + NodeType>, +} diff --git a/tests/target/structs.rs b/tests/target/structs.rs index 2969438eadd..7b4eb031f9f 100644 --- a/tests/target/structs.rs +++ b/tests/target/structs.rs @@ -117,9 +117,8 @@ struct FieldsWithAttributes { } struct Deep { - deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep: node::Handle>, - Type, - NodeType>, + deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep: + node::Handle>, Type, NodeType>, } struct Foo(T); @@ -172,3 +171,14 @@ struct Foo { } struct Foo { /* comment */ } struct Foo(); + +struct LongStruct { + a: A, + the_quick_brown_fox_jumps_over_the_lazy_dog: + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, +} + +struct Deep { + deeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep: + node::Handle>, Type, NodeType>, +}