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

preserve initial newlines when styling file? #1224

Open
kevinushey opened this issue Jul 25, 2024 · 3 comments
Open

preserve initial newlines when styling file? #1224

kevinushey opened this issue Jul 25, 2024 · 3 comments

Comments

@kevinushey
Copy link

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Collaborator

lorenzwalthert commented Jul 25, 2024

Also, we did not remove leading newlines before we started discussing it in #1014 and eventually implemented it for strict = TRUE in #1056.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants