Skip to content

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

Merged
merged 9 commits into from
Nov 5, 2017

Conversation

afshinm
Copy link
Contributor

@afshinm afshinm commented Oct 4, 2017

Fixes #1936

Copy link
Member

@nrc nrc left a 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() {
Copy link
Member

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() {
Copy link
Member

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,
Copy link
Member

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 {
Copy link
Member

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
Copy link
Member

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).

Copy link
Contributor Author

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"?

Copy link
Member

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.

@afshinm
Copy link
Contributor Author

afshinm commented Oct 6, 2017

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,
Copy link
Member

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();
Copy link
Member

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 {
Copy link
Member

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?

@afshinm
Copy link
Contributor Author

afshinm commented Oct 7, 2017

Thanks @nrc, I just refactored those parts.

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.

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?

@nrc
Copy link
Member

nrc commented Oct 13, 2017

Is that correct?

Yes, correct.

@afshinm
Copy link
Contributor Author

afshinm commented Oct 22, 2017

@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?

@nrc
Copy link
Member

nrc commented Oct 26, 2017

What do you think about this idea? Is there any better ways to do this?

Searching for \n is a fine way to do this - we do it often elsewhere.

@afshinm
Copy link
Contributor Author

afshinm commented Oct 27, 2017

@nrc cheers Nick, let me see what can I do this weekend. 🦀

@afshinm
Copy link
Contributor Author

afshinm commented Nov 4, 2017

@nrc OK, I just pushed the changes you've requested. Let me know what you think.

@nrc
Copy link
Member

nrc commented Nov 5, 2017

Awesome, thank you!

@nrc nrc merged commit f412c87 into rust-lang:master Nov 5, 2017
@afshinm
Copy link
Contributor Author

afshinm commented Nov 6, 2017

Thank you Nick!

@afshinm afshinm deleted the afshinm-single-line-where branch November 6, 2017 08:35
@@ -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";
Copy link
Contributor

@remram44 remram44 Mar 31, 2018

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants