styler icon indicating copy to clipboard operation
styler copied to clipboard

Should we remove leading blank lines in a file?

Open IndrajeetPatil opened this issue 3 years ago • 2 comments

We remove blank lines at the end of the file, but not at the start.

Is there a good reason why we shouldn't do this?

Current

f <- withr::local_tempfile(fileext = ".R")
cat("\n\nx <- 1\n\n", file = f)

readLines(f)
#> [1] ""       ""       "x <- 1" ""

styler::style_file(f)
#> Styling  1  files:
#>  /var/folders/xr/v_vddzvs33q5wg7jv9mr__4h0000gn/T//RtmpT2JVsQ/file24d31e51a2a5.R ℹ 
#> ───────────────────────────────────────────────────────────────────────────────
#> Status   Count   Legend 
#> ✔    0   File unchanged.
#> ℹ    1   File changed.
#> ✖    0   Styling threw an error.
#> ───────────────────────────────────────────────────────────────────────────────
#> Please review the changes carefully!

readLines(f)
#> [1] ""       ""       "x <- 1"

Created on 2022-09-28 with reprex v2.0.2

Proposed

readLines(f)
#> [1] "x <- 1"

IndrajeetPatil avatar Sep 28 '22 13:09 IndrajeetPatil

That was actually a concious decision to make {styler} non-invasive. parse_transform_serialize_r_block() takes an argument start_line, that is previously derived from the code to style. Hence, if we wanted, we can just set that to line 1 for strict=TRUE. Then, for strict=FALSE, we could continue with the current behaviour.

lorenzwalthert avatar Oct 04 '22 18:10 lorenzwalthert

if we wanted, we can just set that to line 1 for strict=TRUE. Then, for strict=FALSE, we could continue with the current behaviour.

That sounds like a good solution to me!

IndrajeetPatil avatar Oct 05 '22 10:10 IndrajeetPatil

I tried this, and it works as expected, but there are two things I am not sure about:

  • Setting start_line to 1L here also changes roxygen examples.

E.g.

#' @examples
#' 1 + 1

becomes

#' @examples 1 + 1
  • How to access strict argument from parse_transform_serialize_r_block(). I tried to trace this to UI functions, but found it difficult to figure out from where this argument should be accessed.

IndrajeetPatil avatar Nov 08 '22 16:11 IndrajeetPatil

Setting start_line to 1L here also changes roxygen examples.

Then, we need to set start_line to 1L or 0L, depending on whether or not it's a {roxygen2} example.

Regarding how to access strict in parse_transform_serialize_r_block(): transformers$more_specs_style_guide$strict.

lorenzwalthert avatar Nov 09 '22 08:11 lorenzwalthert