r-lib / styler

Non-invasive pretty printing of R code
https://styler.r-lib.org
Other
713 stars 70 forks source link

preserve initial newlines when styling file? #1224

Open kevinushey opened 1 month ago

kevinushey commented 1 month ago

For example:

owd <- setwd(tempdir())
on.exit(setwd(owd), add = TRUE)

writeLines("

# Empty line above
1+1
2+2
3+3
", con = "test.R")

readChar("test.R", 128)
styler::style_file("test.R")
readChar("test.R", 128)

This gives:

> readChar("test.R", 128)
[1] "\n\n# Empty line above\n1+1\n2+2\n3+3\n\n"
> styler::style_file("test.R")
Styling  1  files:
 test.R ℹ 
────────────────────────────────────────
Status  Count   Legend 
✔   0   File unchanged.
ℹ   1   File changed.
✖   0   Styling threw an error.
────────────────────────────────────────
Please review the changes carefully!
> readChar("test.R", 128)
[1] "# Empty line above\n1 + 1\n2 + 2\n3 + 3\n"

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

kevinushey commented 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.

IndrajeetPatil commented 1 month ago

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

lorenzwalthert commented 1 month ago

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.