Skip to content

try to write the parameter on a new line in case the attribute/parameter together are over max_width #4455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,
&param_attrs_result,
&self
.pat
.rewrite(context, Shape::legacy(shape.width, shape.indent))?,
param_name,
span,
shape,
!has_multiple_attr_lines,
Expand All @@ -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)
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