Skip to content

Commit

Permalink
Merge pull request rust-lang#4455 from rust-lang/issue-4059.rs
Browse files Browse the repository at this point in the history
try to write the parameter on a new line in case the attribute/parameter together are over max_width
  • Loading branch information
scampi committed Oct 9, 2020
2 parents 7e31d5d + 85daa36 commit 04705fe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/formatting/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -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();

Expand Down
33 changes: 27 additions & 6 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2142,12 +2142,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,
&param_attrs_result,
&self
.pat
.rewrite(context, Shape::legacy(shape.width, shape.indent))?,
param_name,
span,
shape,
!has_multiple_attr_lines,
Expand All @@ -2161,10 +2162,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)
Expand Down
6 changes: 4 additions & 2 deletions tests/target/issue_4032.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
fn a1(
#[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] a: u8,
#[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
a: u8,
) {
}
fn b1(
#[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] bb: u8,
#[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
bb: u8,
) {
}
fn a2(
Expand Down

0 comments on commit 04705fe

Please sign in to comment.