Open kevinushey opened 1 month ago
I guess a similar question might apply to newlines before and after braces; it looks like styler
removes those as well. However, styler does preserve multiple newlines present between statements in a block.
I am curious as to why you would like to preserve these empty lines? Also, just at the start or also at the end?
Note that (latest-main-)styler will remove blank lines both at the start and end of a scope:
"f <- function() {
x <- 1L
}" -> code
styler::style_text(code)
#> f <- function() {
#> x <- 1L
#> }
Created on 2024-07-25 with reprex v2.1.1
The reason it doesn't get rid of blank lines inside the scope is because it is difficult to know if they are intentional or unintentional. E.g.
Intentional: Users wish to visually distinguish lines of code achieving different subgoals by separating blocks of code with empty lines. Unintentional: Users removed some code but forgot to remove the leftover empty lines.
clean_data <- function() {
# subgoal-1: check for missing data
...
# deliberate empty lines
# subgoal-2: remove duplicates
...
# deliberate empty lines
# etc.
}
The situation is less uncertain for the blank lines at the start and the end, since they are often leftovers either from removal of code, accidental enters, etc., and less likely to be deliberate. But their unchecked presence can visually distort the structure of a program, and, therefore, we believe, it's better to get rid of them.
If you really wish to preserve these lines, you could remove corresponding rules when you use {styler}
:
https://github.com/r-lib/styler/blob/6d2f0b34245b6bc712bf2fcabf240d9ca814f0ef/R/rules-line-breaks.R
Also, we did not remove leading newlines before we started discussing it in https://github.com/r-lib/styler/issues/1014 and eventually implemented it for strict = TRUE
in https://github.com/r-lib/styler/pull/1056.
For example:
This gives:
What do you think about preserving the leading newlines in the file? (I don't think the tidyverse style guide has any recommendations here, but I could be wrong.)