-
Notifications
You must be signed in to change notification settings - Fork 930
fix: wrap function without a body exceeding 100 characters (#6539) #6579
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
base: master
Are you sure you want to change the base?
fix: wrap function without a body exceeding 100 characters (#6539) #6579
Conversation
@lukewilson2002 I suggested those changes not @chrisduerr 😅, but I'm glad to see that you've picked them up for this PR. In addition to the test case you've added here I'd also like to see cases that include a As an example, using Input pub trait Trait
{
fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64;
fn a_one_hundred_column_fn_decl_no_body_and_where_clause<T>(&self, aaaaa: T, bbbbbb: f64) -> f64 where T: Debug;
fn a_one_hundred_column_fn_decl_with_body(&self, aaaa: f64, bbb: f64, ccc: f64, ddd: f64) -> f64 {}
fn a_one_hundred_column_fn_decl_with_body_and_where_clause<T>(&self, aaaa: f64, bbb: f64) -> f64 where T: Debug {}
} I'd expect it to produce this output: output pub trait Trait
{
fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64;
fn a_one_hundred_column_fn_decl_no_body_and_where_clause<T>(&self, aaaaa: T, bbbbbb: f64) -> f64
where
T: Debug;
fn a_one_hundred_column_fn_decl_with_body(&self, aaaa: f64, bbb: f64, ccc: f64, ddd: f64) -> f64
{
}
fn a_one_hundred_column_fn_decl_with_body_and_where_clause<T>(&self, aaaa: f64, bbb: f64) -> f64
where
T: Debug,
{
}
} And using output pub trait Trait
{
fn a_one_hundred_column_fn_decl_no_body(&self, aaa: f64, b: f64, c: f64, d: f64, e: f64) -> f64;
fn a_one_hundred_column_fn_decl_no_body_and_where_clause<T>(&self, aaaaa: T, bbbbbb: f64) -> f64
where T: Debug;
fn a_one_hundred_column_fn_decl_with_body(&self, aaaa: f64, bbb: f64, ccc: f64, ddd: f64) -> f64
{
}
fn a_one_hundred_column_fn_decl_with_body_and_where_clause<T>(&self, aaaa: f64, bbb: f64) -> f64
where T: Debug
{
}
} |
After some additional investigation I noticed that Lines 2905 to 2909 in ef94a72
I think the following patch resolves the diff --git a/src/items.rs b/src/items.rs
index be01a2af..65b990be 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -2500,6 +2500,7 @@ fn rewrite_fn_base(
ret_str_len,
fn_brace_style,
multi_line_ret_str,
+ where_clause,
);
debug!(
@@ -2899,6 +2900,7 @@ fn compute_budgets_for_params(
ret_str_len: usize,
fn_brace_style: FnBraceStyle,
force_vertical_layout: bool,
+ where_clause: &ast::WhereClause,
) -> (usize, usize, Indent) {
debug!(
"compute_budgets_for_params {} {:?}, {}, {:?}",
@@ -2913,7 +2915,15 @@ fn compute_budgets_for_params(
let overhead = if ret_str_len == 0 { 2 } else { 3 };
let mut used_space = indent.width() + result.len() + ret_str_len + overhead;
match fn_brace_style {
- FnBraceStyle::None => used_space += 1, // 1 = `;`
+ _ if context.config.style_edition() >= StyleEdition::Edition2027
+ && where_clause.predicates.len() > 0 =>
+ {
+ // Don't add anything to `used_space` if we have a where clause.
+ // For all `FnBraceStyle` values if we have a where cluase that can't fit
+ // on the current line it'll be written to the next line.
+ // Therefore, we don't need to account for a trailing `;` or `{}`
+ }
+ FnBraceStyle::None => used_space += 1, // 1 = `;`
FnBraceStyle::SameLine => used_space += 2, // 2 = `{}`
FnBraceStyle::NextLine => (),
} |
Thank you @ytmimi for clarifying! I don't know where the confusion came from because I was reading that issue quite a lot. Just a silly mistake I guess. 😄 I'll update the commit. I greatly appreciate the feedback and the test cases you suggested. I will work on this right now. |
…#6539) Co-authored-by: Yacin Tmimi <yacintmimi@gmail.com>
5bb64dd
to
e554eba
Compare
…s + tests for issue 6539 Co-authored-by: Yacin Tmimi <yacintmimi@gmail.com>
27932f2
to
14b815d
Compare
@ytmimi I incorporated the patch, but I couldn't really figure out how to write a test that would cover it. If you have a suggestion let me know and I'd be happy to incorporate one. The test files contain the same functions, except that I can tell some of these tests are unlikely to change between the test flags, but I just assumed they should all be included so to make it easier to read. Let me know if a test is unnecessary or if I missed anything. |
@lukewilson2002 Thanks for expanding on the test cases! Let's keep the examples that you've added. I think it's still valuable to show cases where things will wrap. To cover our based I'd also want to include test files for the the remaining combinations of
Also, if you're able to expand on the test cases a little more that would be great! The function that we'er modifying in this PR ( It would be great to include some examples of top level functions, as well as functions where the return type is formatted over multiple lines. |
From Yacin Tmimi:
Hopefully these changes suggested by @ytmimi and the test case I provided fix the original problem. Let me know if more tests are needed!
Closes #6539
Running the new tests without the proposed changes to src/items.rs:
Tests are passing with these changes.
EDIT: Corrected attribution.