-
Notifications
You must be signed in to change notification settings - Fork 932
Adding where_single_line option #2030
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I think there are few things that could be polished, but otherwise looks good.
src/items.rs
Outdated
@@ -2533,6 +2539,8 @@ fn rewrite_where_clause_rfc_style( | |||
&& 6 + preds_str.len() <= shape.width | |||
{ | |||
String::from(" ") | |||
} else if context.config.where_single_line() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it could be ||
ed on to the previous if
condition
src/items.rs
Outdated
SeparatorTactic::Never | ||
} else { | ||
context.config.trailing_comma() | ||
}; | ||
|
||
let shape_tactic = if context.config.where_single_line() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to check that there is only one item in the where
clause here?
src/items.rs
Outdated
(_, Density::CompressedIfEmpty, _) if where_clause.predicates.len() == 1 && !has_body => { | ||
false | ||
} | ||
(BraceStyle::SameLineWhere, _, _) if !where_clause.predicates.is_empty() => true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this code would be easier to understand if broken up into an if
check before the match.
|
||
|
||
fn lorem_two_items<Ipsum, Dolor, Sit, Amet>() -> T | ||
where Ipsum: Eq, Lorem: Eq { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have regular where clause formatting, not single-lined (where_single_line should only apply if there is a single item in the clause).
where Ipsum: Eq, Lorem: Eq { | ||
// body | ||
} | ||
|
||
fn lorem<Ipsum, Dolor, Sit, Amet>() -> T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a test for a single-item where clause where the rest of the signature covers multiple lines? (That should not get single-lined).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could help me with this one? We already have one single-item where clause. What do you mean by "rest of the signature"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we would put the function signature on multiple lines (e.g., if we use mutliple lines for arguments), then we should always use the multiple line form for the where clause.
Thanks for your feedbacks @nrc. I just refactored some parts of the PR and apologies for that mistake, I thought we want to inline the where clause regardless of the number of items in the where clause. Let me know what you think! |
src/items.rs
Outdated
(BraceStyle::SameLineWhere, _) if !where_clause.predicates.is_empty() => true, | ||
(_, Density::Compressed) if len == 1 => false, | ||
(_, Density::CompressedIfEmpty) if len == 1 && !has_body => false, | ||
(BraceStyle::SameLineWhere, _) if !is_empty => true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could just use len == 0
and avoid the is_empty
variable.
src/items.rs
Outdated
@@ -2282,11 +2282,17 @@ fn compute_budgets_for_args( | |||
} | |||
|
|||
fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause, has_body: bool) -> bool { | |||
let len = where_clause.predicates.len(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call this predicate_count
rather than len
src/items.rs
Outdated
let comma_tactic = if where_clause_option.suppress_comma { | ||
SeparatorTactic::Never | ||
let comma_tactic = | ||
if where_clause_option.suppress_comma || context.config.where_single_line() && len == 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you factor out context.config.where_single_line() && len == 1
into a variable?
Thanks @nrc, I just refactored those parts.
So, something like: fn lorem_multi_line<Ipsum, Dolor, Sit, Amet>(
a: Aaaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbbbb,
c: Ccccccccccccccccc,
d: Ddddddddddddddddddddddddd,
e: Eeeeeeeeeeeeeeeeeee,
) -> T where Ipsum: Eq {
// body
} must be: fn lorem_multi_line<Ipsum, Dolor, Sit, Amet>(
a: Aaaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbbbb,
c: Ccccccccccccccccc,
d: Ddddddddddddddddddddddddd,
e: Eeeeeeeeeeeeeeeeeee,
) -> T
where
Ipsum: Eq,
{
// body
} Is that correct? |
Yes, correct. |
@nrc I'm struggling to finish the last part, to skip the where_single_line option when the rest of the signature is multi-lined. I was thinking about doing a "\n" search and see if we have any new lines in the other parts and then assume that we have to skip (e.g. when there are multiple items in the signature). What do you think about this idea? Is there any better ways to do this? |
Searching for |
@nrc cheers Nick, let me see what can I do this weekend. 🦀 |
@nrc OK, I just pushed the changes you've requested. Let me know what you think. |
Awesome, thank you! |
Thank you Nick! |
@@ -574,6 +574,7 @@ create_config! { | |||
// function decl? | |||
// 2. Currently options `Tall` and `Vertical` produce the same output. | |||
where_density: Density, Density::Vertical, false, "Density of a where clause"; | |||
where_single_line: bool, false, false, "To force single line where layout"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both the name of the option (where single line do what?) and the "documentation" (where what is layout?) are extremely confusing. Configurations.md
does not help; as a user I have no idea what this does.
[edit: filed #2580]
Fixes #1936