Open gavinsimpson opened 2 years ago
Just to focus in on the ~
indentation. This illustrates the intended (?) behaviour with simpler code
txt <- '
f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) + ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year))
'
style_text(txt, scope = "indention")
producing
f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) + ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year))
but as soon as we break up the long first formula with a line break, the indentation breaks
txt <- '
f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year))
'
style_text(txt, scope = "indention")
yielding
f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year))
Ok. Probably not intended but since most people don’t use a non-default scope value, it has not been detected…
@lorenzwalthert Fair enough, but the first example I showed doesn't work as expected with the defaults either:
txt <- '
m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year)),
method = "REML",
data = month_maxima,
family = gevlss(link = list("identity", "identity", "logit")),
knots = knots,
control = ctrl,
optimizer = "efs")'
style_text(txt, scope = "tokens")
yielding
m2 <- gam(list(
max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year)
),
method = "REML",
data = month_maxima,
family = gevlss(link = list("identity", "identity", "logit")),
knots = knots,
control = ctrl,
optimizer = "efs"
)
True, then let's fix that as well 😉
Actually not related to ~
, seems like only happens to unnamed arguments:
txt <- '
test_that(key(
s),
x = 1)'
styler::style_text(txt, scope = "tokens")
#>
#> test_that(key(
#> s
#> ),
#> x = 1
#> )
txt <- 'test_that(key(
1),
1)'
styler::style_text(txt)
#> test_that(
#> key(
#> 1
#> ),
#> 1
#> )
Created on 2022-08-14 by the reprex package (v2.0.1)
That's great; thanks!
As you mentioned that the reported behaviour in the original issue was "probably not intended", I note that the reported behaviour remains in
> packageVersion("styler")
[1] ‘1.10.2.9000’
With
txt <- '
m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year)),
method = "REML",
data = month_maxima,
family = gevlss(link = list("identity", "identity", "logit")),
knots = knots,
control = ctrl,
optimizer = "efs")'
style_text(txt, scope = "indention", indent_by = 2)
I'm still seeing
m2 <- gam(list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year)),
method = "REML",
data = month_maxima,
family = gevlss(link = list("identity", "identity", "logit")),
knots = knots,
control = ctrl,
optimizer = "efs")
whereas with scope = "tokens"
what I think is the desirable formatting is achieved:
m2 <- gam(
list(
max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year)
),
method = "REML",
data = month_maxima,
family = gevlss(link = list("identity", "identity", "logit")),
knots = knots,
control = ctrl,
optimizer = "efs"
)
The simpler version I mentioned also doesn't indent nicely with scope = "indentation"
:
txt <- '
f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year))
'
style_text(txt, scope = "indention")
yields
f <- list(max_temp ~ s(month, bs = "cc", k = 12) + s(year) +
ti(month, year, bs = c("cc", "cr"), k = c(12, 10)),
~ s(month, bs = "cc", k = 12),
~ s(month, bs = "cc", k = 12) + s(year))
Is this the expected behaviour (with
styler::tidyverse_style(scope = "indention")
)?output:
I would have expected something like
or (perhaps preferably - ideally?)
where the tildes are aligned with one another
If that's not the expected indentation, is there a way to stop the reindention associated with the
~
? Assuming that's what is causing the failure to indent subsequent arguments 2 spaces as per the tidyverse style guide?session info