-
Notifications
You must be signed in to change notification settings - Fork 73
Force brace expression in function calls be on their own line #543
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
Force brace expression in function calls be on their own line #543
Conversation
function arguments that consist of a braced expression always need to start on a new line, unless it's the last argument and all other arguments fit on the line of the function call or they are named.
34526e0
to
64b8689
Compare
will merge conflict with 7ed140e
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.
A big leap forward, thanks a lot!
I have added more test cases in 0c844fa, are they covered already?
call( | ||
{ | ||
1 | ||
}, a + b, |
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 the a + b
needs to go on a separate line. Is this related to this pull request at all?
Rationale: Multiline expressions in function calls don't contain code from other arguments.
This may be missing from the style guide, but seems obvious to me: If an argument is so complex that it needs multiple lines, we don't want it interspersed with other arguments.
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.
In the above example, I agree. However, when we have
call(
{
x + y
}, {
f(x)
call(x = 2, f = max(n))
}
)
I'd leave it as is opposed to changing it into
call(
{
x + y
},
{
f(x)
call(x = 2, f = max(n))
}
)
Because indention helps seeing what belongs together and }, {
for me just reads as a separator of two expressions (i.e. I don't read them as two braces anymore). What do you think.
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.
Can we enforce the second option in strict
mode but leave the first in permissive
mode?
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'd leave the first case as is, but I would not change the second into the first. So if your code has }, \n{
, styler should not change it to }, {
, and vice versa. I think that's a fair compromise 😄 . Whenever there is brace + comma + not a brace, I agree to break lines in any case. The strict
argument is already too convoluted I think. If your suggestion gets accepted in tidyverse/style, I am happy to also implement it for strict = TRUE
, but I think it's a detail anyways.
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.
Works for me. Thanks for looking into it!
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.
Ok no this is more complicated to implement. I'll stick to your rule.
- Add more test cases (#13).
Codecov Report
@@ Coverage Diff @@
## master #543 +/- ##
=========================================
Coverage ? 90.83%
=========================================
Files ? 43
Lines ? 1801
Branches ? 0
=========================================
Hits ? 1636
Misses ? 165
Partials ? 0
Continue to review full report at Codecov.
|
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.
Amazing! Looking forward to using it on my code!
}) | ||
|
||
call( | ||
"test", { |
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.
So the newline isn't added here because it's the last argument? Can we easily check if the brace is on the same line as the opening parenthesis of the function call?
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.
Not easily but we can in eaac448d22ec000d99030a367852c5bae9b6ceb5
(pushed to master). :-)
1 | ||
}, | ||
a + b, | ||
{ |
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 love this! But why is the newline added here and not in the example above?
The main motivation is to avoid
tryCatch()
expressions to be re-formatted compactly:Created on 2019-09-17 by the reprex package (v0.3.0)
While at the same time, preserve formatting of
testthat()
expressions:Created on 2019-09-17 by the reprex package (v0.3.0)
The abstract rule was formulated in #428 (comment).
The last part of the sentence or they are named. is needed for this case:
Where we should rearrange to
Closes #428.