-
Notifications
You must be signed in to change notification settings - Fork 73
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
Function declarations should gain braces for body if strict = TRUE #536
Merged
lorenzwalthert
merged 10 commits into
r-lib:master
from
lorenzwalthert:feature-curly-for-mutliline-fun-dec
Aug 5, 2019
Merged
Function declarations should gain braces for body if strict = TRUE #536
lorenzwalthert
merged 10 commits into
r-lib:master
from
lorenzwalthert:feature-curly-for-mutliline-fun-dec
Aug 5, 2019
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Performance of any function() {} not affected. Because indent_without_paren_for_while_fun does not know if strict = TRUE, a problem is created for function declarations where the body is not wrapped in {}: (1) indent_without_paren_for_while_fun() does not indent on FUNCTION because { follows. (2) wrap_subexpr_in_curly() creates a curly expression from the body and wants to unindent the body, which was not indented. This creates negative indention. (3) serialization fails due to negative indention. This is avoided by lower-bounding the indention adjustment in wrap_subexpr_in_curly to 0. It seemed more robust than trying to establish at runtime in indent_without_paren_for_while_fun if wrap_subexpr_in_curly() will be called later.
1477a3e
to
5f1be90
Compare
5f1be90
to
910772b
Compare
krlmlr
reviewed
Aug 5, 2019
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.
Awesome, thanks!
What happens with my use case function() h(~ { ... })
?
It seems this: library(styler)
style_text("function() h(~ { ... })")
#> function() {
#> h(~ {
#> ...
#> })
#> }
style_text("function() h(~ { ... })", strict = FALSE)
#> function() h(~ {
#> ...
#> }) Created on 2019-08-05 by the reprex package (v0.3.0) |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Also add braces to multi-function declaration if missing for
strict = TRUE
(closes #535):Created on 2019-08-05 by the reprex package (v0.3.0)
Calls of that form had too much indention, see #535.
For
strict = FALSE
, no braces are added and the following cases were fixed to remain as is:They all used to have the second line twice indented.
The solution is in almost entirely in 7c26637. Simply check if a new line follows after the closing brace. Does not need to use
needs_indention()
because if a token follows on)
on the same line, it will do the indention if necessary, which is exactly the case if it is multi-line. The other cornerstone is to lower-bound indention at 0. Becauseindent_without_paren_for_while_fun()
does not know ifstrict = TRUE
and curly braces will be added later, a problem is created for function declarations where the body is not wrapped in{...}
: (1)indent_without_paren_for_while_fun()
does not indent onFUNCTION
because{
follows. (2)wrap_subexpr_in_curly()
creates a curly expression from the body and wants to unindent the body, which was not indented. This creates negative indention. (3) serialization fails due to negative indention. This is avoided by lower-bounding the indention adjustment inwrap_subexpr_in_curly()
to 0. It seemed more robust than trying to establish at runtime inindent_without_paren_for_while_fun()
ifwrap_subexpr_in_curly()
will be called later.