Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c2f0e99

Browse files
scampicalebcartwright
authored andcommitted
try to write the parameter on a new line in case the attribute/parameter together are over max_width
1 parent a59cac2 commit c2f0e99

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/comment.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::rewrite::RewriteContext;
1010
use crate::shape::{Indent, Shape};
1111
use crate::string::{rewrite_string, StringFormat};
1212
use crate::utils::{
13-
count_newlines, first_line_width, last_line_width, trim_left_preserve_layout, unicode_str_width,
13+
count_newlines, first_line_width, last_line_width, trim_left_preserve_layout,
14+
trimmed_last_line_width, unicode_str_width,
1415
};
1516
use crate::{ErrorKind, FormattingError};
1617

@@ -171,11 +172,12 @@ pub(crate) fn combine_strs_with_missing_comments(
171172
String::with_capacity(prev_str.len() + next_str.len() + shape.indent.width() + 128);
172173
result.push_str(prev_str);
173174
let mut allow_one_line = !prev_str.contains('\n') && !next_str.contains('\n');
174-
let first_sep = if prev_str.is_empty() || next_str.is_empty() {
175-
""
176-
} else {
177-
" "
178-
};
175+
let first_sep =
176+
if prev_str.is_empty() || next_str.is_empty() || trimmed_last_line_width(prev_str) == 0 {
177+
""
178+
} else {
179+
" "
180+
};
179181
let mut one_line_width =
180182
last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
181183

src/items.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,12 +1980,13 @@ impl Rewrite for ast::Param {
19801980
has_multiple_attr_lines,
19811981
)
19821982
} else if is_named_param(self) {
1983+
let param_name = &self
1984+
.pat
1985+
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
19831986
let mut result = combine_strs_with_missing_comments(
19841987
context,
19851988
&param_attrs_result,
1986-
&self
1987-
.pat
1988-
.rewrite(context, Shape::legacy(shape.width, shape.indent))?,
1989+
param_name,
19891990
span,
19901991
shape,
19911992
!has_multiple_attr_lines,
@@ -1999,10 +2000,30 @@ impl Rewrite for ast::Param {
19992000
result.push_str(&after_comment);
20002001
let overhead = last_line_width(&result);
20012002
let max_width = shape.width.checked_sub(overhead)?;
2002-
let ty_str = self
2003+
if let Some(ty_str) = self
20032004
.ty
2004-
.rewrite(context, Shape::legacy(max_width, shape.indent))?;
2005-
result.push_str(&ty_str);
2005+
.rewrite(context, Shape::legacy(max_width, shape.indent))
2006+
{
2007+
result.push_str(&ty_str);
2008+
} else {
2009+
result = combine_strs_with_missing_comments(
2010+
context,
2011+
&(param_attrs_result + &shape.to_string_with_newline(context.config)),
2012+
param_name,
2013+
span,
2014+
shape,
2015+
!has_multiple_attr_lines,
2016+
)?;
2017+
result.push_str(&before_comment);
2018+
result.push_str(colon_spaces(context.config));
2019+
result.push_str(&after_comment);
2020+
let overhead = last_line_width(&result);
2021+
let max_width = shape.width.checked_sub(overhead)?;
2022+
let ty_str = self
2023+
.ty
2024+
.rewrite(context, Shape::legacy(max_width, shape.indent))?;
2025+
result.push_str(&ty_str);
2026+
}
20062027
}
20072028

20082029
Some(result)

0 commit comments

Comments
 (0)