From 85daa3660eedc7fd1ffc6235be786128c29980e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Tue, 6 Oct 2020 03:44:48 +0200 Subject: [PATCH] try to write the parameter on a new line in case the attribute/parameter together are over max_width --- src/formatting/comment.rs | 14 ++++++++------ src/formatting/items.rs | 33 +++++++++++++++++++++++++++------ tests/target/issue_4032.rs | 6 ++++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/formatting/comment.rs b/src/formatting/comment.rs index cbe0458e95d..b1f28d7fae8 100644 --- a/src/formatting/comment.rs +++ b/src/formatting/comment.rs @@ -12,7 +12,8 @@ use crate::formatting::{ string::{rewrite_string, StringFormat}, utils::{ count_newlines, first_line_width, format_code_block, last_line_width, tab_to_spaces, - trim_end_unless_two_whitespaces, trim_left_preserve_layout, unicode_str_width, + trim_end_unless_two_whitespaces, trim_left_preserve_layout, trimmed_last_line_width, + unicode_str_width, }, }; @@ -177,11 +178,12 @@ pub(crate) fn combine_strs_with_missing_comments( String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128); result.push_str(prev_str); let mut allow_one_line = !prev_str.contains('\n') && !next_str.contains('\n'); - let first_sep = if prev_str.is_empty() || next_str.is_empty() { - "" - } else { - " " - }; + let first_sep = + if prev_str.is_empty() || next_str.is_empty() || trimmed_last_line_width(prev_str) == 0 { + "" + } else { + " " + }; let mut one_line_width = last_line_width(prev_str) + first_line_width(next_str) + first_sep.len(); diff --git a/src/formatting/items.rs b/src/formatting/items.rs index 615526f92ff..d43eece31c0 100644 --- a/src/formatting/items.rs +++ b/src/formatting/items.rs @@ -2085,12 +2085,13 @@ impl Rewrite for ast::Param { has_multiple_attr_lines, ) } else if is_named_param(self) { + let param_name = &self + .pat + .rewrite(context, Shape::legacy(shape.width, shape.indent))?; let mut result = combine_strs_with_missing_comments( context, ¶m_attrs_result, - &self - .pat - .rewrite(context, Shape::legacy(shape.width, shape.indent))?, + param_name, span, shape, !has_multiple_attr_lines, @@ -2104,10 +2105,30 @@ impl Rewrite for ast::Param { result.push_str(&after_comment); let overhead = last_line_width(&result); let max_width = shape.width.checked_sub(overhead)?; - let ty_str = self + if let Some(ty_str) = self .ty - .rewrite(context, Shape::legacy(max_width, shape.indent))?; - result.push_str(&ty_str); + .rewrite(context, Shape::legacy(max_width, shape.indent)) + { + result.push_str(&ty_str); + } else { + result = combine_strs_with_missing_comments( + context, + &(param_attrs_result + &shape.to_string_with_newline(context.config)), + param_name, + span, + shape, + !has_multiple_attr_lines, + )?; + result.push_str(&before_comment); + result.push_str(colon_spaces(context.config)); + result.push_str(&after_comment); + let overhead = last_line_width(&result); + let max_width = shape.width.checked_sub(overhead)?; + let ty_str = self + .ty + .rewrite(context, Shape::legacy(max_width, shape.indent))?; + result.push_str(&ty_str); + } } Some(result) diff --git a/tests/target/issue_4032.rs b/tests/target/issue_4032.rs index 9c926ceaaba..2e7e624ca6e 100644 --- a/tests/target/issue_4032.rs +++ b/tests/target/issue_4032.rs @@ -1,9 +1,11 @@ fn a1( - #[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] a: u8, + #[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] + a: u8, ) { } fn b1( - #[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] bb: u8, + #[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] + bb: u8, ) { } fn a2(