Skip to content
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

Conversation

@lorenzwalthert
Copy link
Collaborator

commented Aug 5, 2019

Also add braces to multi-function declaration if missing for strict = TRUE (closes #535):

library(styler)
style_text("function() h(~ { ... })")
#> function() {
#>   h(~ {
#>     ...
#>   })
#> }

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:

if (x) h(
  x
) 

for (i in x) h(
  x
) 

while (x) h(
  x
) 

if (x) h(
  x
) else k(
  b
)

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. Because indent_without_paren_for_while_fun() does not know if strict = 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 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.

lorenzwalthert added some commits Aug 5, 2019

also resolve case for strict = FALSE
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.

@lorenzwalthert lorenzwalthert force-pushed the lorenzwalthert:feature-curly-for-mutliline-fun-dec branch from 1477a3e to 5f1be90 Aug 5, 2019

@lorenzwalthert lorenzwalthert force-pushed the lorenzwalthert:feature-curly-for-mutliline-fun-dec branch from 5f1be90 to 910772b Aug 5, 2019

@krlmlr
Copy link
Member

left a comment

Awesome, thanks!

What happens with my use case function() h(~ { ... }) ?

@lorenzwalthert

This comment has been minimized.

Copy link
Collaborator Author

commented Aug 5, 2019

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)

@lorenzwalthert lorenzwalthert merged commit a069046 into r-lib:master Aug 5, 2019

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@lorenzwalthert lorenzwalthert deleted the lorenzwalthert:feature-curly-for-mutliline-fun-dec branch Aug 5, 2019

@lorenzwalthert lorenzwalthert restored the lorenzwalthert:feature-curly-for-mutliline-fun-dec branch Aug 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.