From f523ec58ab25596ae9f7139dae322376a94c5b1a Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 24 Dec 2017 13:57:29 +0900 Subject: [PATCH] Do not give up rewriting struct field when attribute is long --- src/items.rs | 24 ++++++++++++------------ tests/source/struct-field-attributes.rs | 6 ++++++ tests/target/struct-field-attributes.rs | 6 ++++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/items.rs b/src/items.rs index d09d95699b7..e313179fae9 100644 --- a/src/items.rs +++ b/src/items.rs @@ -22,7 +22,7 @@ use codemap::{LineRangeUtils, SpanUtils}; use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented}; use config::{BraceStyle, Config, Density, IndentStyle}; -use expr::{choose_rhs, format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, +use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_call_inner, ExprType}; use lists::{definitive_tactic, itemize_list, write_list, DefinitiveListTactic, ListFormatting, ListItem, ListTactic, Separator, SeparatorPlace, SeparatorTactic}; @@ -1430,8 +1430,7 @@ pub fn rewrite_struct_field( lhs_max_width: usize, ) -> Option { if contains_skip(&field.attrs) { - let snippet = context.snippet(mk_sp(field.attrs[0].span.lo(), field.span.hi())); - return Some(snippet.to_owned()); + return Some(context.snippet(field.span()).to_owned()); } let type_annotation_spacing = type_annotation_spacing(context.config); @@ -1468,24 +1467,25 @@ pub fn rewrite_struct_field( if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() { spacing.push(' '); } - let ty_shape = shape.offset_left(overhead + spacing.len())?; - let mut orig_ty = field.ty.rewrite(context, ty_shape); + let orig_ty = shape + .offset_left(overhead + spacing.len()) + .and_then(|ty_shape| field.ty.rewrite(context, ty_shape)); if let Some(ref ty) = orig_ty { if !ty.contains('\n') { return Some(attr_prefix + &spacing + ty); } } + let is_prefix_empty = prefix.is_empty(); // We must use multiline. We are going to put attributes and a field on different lines. - // 1 = " " - let rhs_shape = shape.offset_left(last_line_width(&prefix) + 1)?; - orig_ty = field.ty.rewrite(context, rhs_shape); - let field_str = if prefix.is_empty() { - orig_ty? + let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, shape)?; + // Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct. + let field_str = if is_prefix_empty { + field_str.trim_left() } else { - prefix + &choose_rhs(context, &*field.ty, rhs_shape, orig_ty)? + &field_str }; - combine_strs_with_missing_comments(context, &attrs_str, &field_str, missing_span, shape, false) + combine_strs_with_missing_comments(context, &attrs_str, field_str, missing_span, shape, false) } pub struct StaticParts<'a> { diff --git a/tests/source/struct-field-attributes.rs b/tests/source/struct-field-attributes.rs index b66ed1ed8f1..76d6eda8853 100644 --- a/tests/source/struct-field-attributes.rs +++ b/tests/source/struct-field-attributes.rs @@ -44,3 +44,9 @@ pub enum State { struct Fields( #[cfg_attr(feature = "serde_derive", serde(state_with = "::base::serialization::shared"))] Arc>, ); + +// #2309 +pub struct A { +#[doc="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"] +pub foos:Vec +} diff --git a/tests/target/struct-field-attributes.rs b/tests/target/struct-field-attributes.rs index d9d790c00af..8bf10fae3e1 100644 --- a/tests/target/struct-field-attributes.rs +++ b/tests/target/struct-field-attributes.rs @@ -46,3 +46,9 @@ struct Fields( #[cfg_attr(feature = "serde_derive", serde(state_with = "::base::serialization::shared"))] Arc>, ); + +// #2309 +pub struct A { + #[doc = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"] + pub foos: Vec, +}