styler
styler copied to clipboard
Should we remove leading blank lines in a file?
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"
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.
if we wanted, we can just set that to line 1 for
strict=TRUE. Then, forstrict=FALSE, we could continue with the current behaviour.
That sounds like a good solution to me!
I tried this, and it works as expected, but there are two things I am not sure about:
- Setting
start_lineto1Lhere also changes roxygen examples.
E.g.
#' @examples
#' 1 + 1
becomes
#' @examples 1 + 1
- How to access
strictargument fromparse_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.
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.