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

Introduce line-breaks in multiline arguments #1244

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
45 changes: 45 additions & 0 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,48 @@ set_line_breaks_between_top_level_exprs <- function(pd, allowed_blank_lines = 2L
pd$lag_newlines <- pmin(pd$lag_newlines, allowed_blank_lines + 1L)
pd
}


set_line_breaks_for_multiline_args <- function(pd) {
if (!any(pd$token == "','") || any(pd$text[1L] == "tribble")) {
return(pd)
}

has_children <- purrr::some(pd$child, purrr::negate(is.null))
if (!has_children || is_function_declaration(pd)) {
return(pd)
}

children <- pd$child
idx_pre_open_brace <- which(pd$token_after == "'{'")
if (length(idx_pre_open_brace)) {
children[idx_pre_open_brace + 1L] <- NULL
}

args_multiline <- children %>%
purrr::discard(is.null) %>%
purrr::map_lgl(~ any(.x$is_multi_line) || sum(.x$newlines, .x$lag_newlines) > 0L)

if (!any(args_multiline)) {
return(pd)
}

idx_paren <- which(pd$token == "'('")[1L]
if (!is.na(idx_paren) && idx_paren < nrow(pd)) {
pd[idx_paren + 1L, "lag_newlines"] <- 1L
}

idx_comma <- which(pd$token == "','")
idx_comma_has_comment <- which(pd$token[idx_comma + 1L] == "COMMENT")
for (i in seq_along(idx_comma)) {
arg_index <- i + 1L
if (arg_index <= length(args_multiline) && args_multiline[arg_index]) {
target_row <- idx_comma[i] + if (i %in% idx_comma_has_comment) 2L else 1L
if (target_row <= nrow(pd)) {
pd[target_row, "lag_newlines"] <- 1L
}
}
}

pd
}
2 changes: 2 additions & 0 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ tidyverse_style <- function(scope = "tokens",
if (strict) remove_line_breaks_in_function_declaration,
set_line_breaks_between_top_level_exprs =
if (strict) set_line_breaks_between_top_level_exprs,
set_line_breaks_for_multiline_args =
if (strict) set_line_breaks_for_multiline_args,
style_line_break_around_curly = partial(
style_line_break_around_curly,
strict
Expand Down
1 change: 1 addition & 0 deletions styler.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: bbbaaf9d-d7a1-475e-8d3b-3e41589b57c8

RestoreWorkspace: No
SaveWorkspace: No
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/cache-with-r-cache/mlflow-1-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ mlflow_conda_bin <- function() {
conda <- if (!is.na(conda_home)) paste(conda_home, "bin", "conda", sep = "/") else "auto"
conda_try <- try(conda_binary(conda = conda), silent = TRUE)
if (class(conda_try) == "try-error") {
msg <- paste(attributes(conda_try)$condition$message,
msg <- paste(
attributes(conda_try)$condition$message,
paste(
" If you are not using conda, you can set the environment variable",
"MLFLOW_PYTHON_BIN to the path of your python executable."
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/indention_multiple/overall-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ a <- function(x) {
))
if (x > 10) {
for (x in 22) { # FIXME in operator only to be surrounded by one space. What about %in%?
prin(x)
print(x)
}
}
})
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/indention_multiple/overall-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#' indented comments
a <- function(x) {
test_that("I want to test", {
out <- c(1, c(
22 + 1
))
out <- c(
1, c(
22 + 1
)
)
if (x > 10) {
for (x in 22) { # FIXME in operator only to be surrounded by one space. What about %in%?
prin(x)
print(x)
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ fun( s = g(x),
gg = a(n == 2) |> b(),
tt |> q(r = 3))

# FIXME closing brace could go on ntext line. Alternative: remove lin breaks completely.
blew(x |>

c(), y = 2)

# FIXME closing brace could go on ntext line. Alternative: move c() up.
blew(y = 2, x |>
c())

Expand Down
15 changes: 9 additions & 6 deletions tests/testthat/line_breaks_and_other/base-pipe-line-breaks-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ fun(
tt |> q(r = 3)
)

# FIXME closing brace could go on ntext line. Alternative: remove lin breaks completely.
blew(x |>
c(), y = 2)
blew(
x |>
c(),
y = 2
)

# FIXME closing brace could go on ntext line. Alternative: move c() up.
blew(y = 2, x |>
c())
blew(
y = 2, x |>
c()
)


{
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/line_breaks_and_other/pipe-line-breaks-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,10 @@ fun( s = g(x),
gg = a(n == 2) %>% b,
tt %>% q(r = 3))

# FIXME closing brace could go on ntext line. Alternative: remove lin breaks completely.
blew(x %>%

c(), y = 2)

# FIXME closing brace could go on ntext line. Alternative: move c() up.
blew(y = 2, x %>%
c())

Expand Down
15 changes: 9 additions & 6 deletions tests/testthat/line_breaks_and_other/pipe-line-breaks-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ fun(
tt %>% q(r = 3)
)

# FIXME closing brace could go on ntext line. Alternative: remove lin breaks completely.
blew(x %>%
c(), y = 2)
blew(
x %>%
c(),
y = 2
)

# FIXME closing brace could go on ntext line. Alternative: move c() up.
blew(y = 2, x %>%
c())
blew(
y = 2, x %>%
c()
)


{
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/line_breaks_fun_call/named_arguments-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ map2(dat1, data2, fun, x, y,
map2(dat1, data2, fun, x = 1, y = 2,
z
)

c(
x, y,
c(
'b'
), m, n, fun(f = 2
# comment-2
)
)
# comment-1
c(
c(
'b'
), fun(
f = 2
), e, f,
g
)
22 changes: 22 additions & 0 deletions tests/testthat/line_breaks_fun_call/named_arguments-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ map2(dat1, data2, fun,
x = 1, y = 2,
z
)

c(
x, y,
c(
"b"
),
m, n, fun(
f = 2
# comment-2
)
)
# comment-1
c(
c(
"b"
),
fun(
f = 2
),
e, f,
g
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ call(call(

call(
1,
call2(3, 4, call(
3,
4, call(5, 6, call(
2
))
))
call2(
3, 4, call(
3,
4, call(
5, 6, call(
2
)
)
)
)
)

# comment lala
Expand All @@ -40,6 +44,8 @@ call(call(
2
))

call(1, call(
23
))
call(
1, call(
23
)
)
18 changes: 12 additions & 6 deletions tests/testthat/line_breaks_fun_call/unindent-out.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
test_that(key(
s
), x = 1)
test_that(
key(
s
),
x = 1
)

test_that(
key(
Expand All @@ -10,9 +13,12 @@ test_that(
)


test_that(key(
s
), x = 1)
test_that(
key(
s
),
x = 1
)


test_that(
Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/roxygen-examples-complete/10-styler-r-ui-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ style_pkg <- function(pkg = ".",
exclude_files = "R/RcppExports.R",
include_roxygen_examples = TRUE) {
pkg_root <- rprojroot::find_package_root_file(path = pkg)
changed <- withr::with_dir(pkg_root, prettify_pkg(
transformers, filetype, exclude_files, include_roxygen_examples
))
changed <- withr::with_dir(
pkg_root, prettify_pkg(
transformers, filetype, exclude_files, include_roxygen_examples
)
)
invisible(changed)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ style_pkg <- function(pkg = ".",
exclude_files = "R/RcppExports.R",
include_roxygen_examples = TRUE) {
pkg_root <- rprojroot::find_package_root_file(path = pkg)
changed <- withr::with_dir(pkg_root, prettify_pkg(
transformers, filetype, exclude_files, include_roxygen_examples
))
changed <- withr::with_dir(
pkg_root, prettify_pkg(
transformers, filetype, exclude_files, include_roxygen_examples
)
)
invisible(changed)
}
2 changes: 1 addition & 1 deletion tests/testthat/scope-AsIs/scope_none-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ a<- function(x){
))
if (x > 10) {
for (x in 22) { # FIXME in operator only to be surrounded by one space. What about %in%?
prin(x)
print(x)
}
}
} )
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/scope-AsIs/scope_none-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ a<- function(x){
))
if (x > 10) {
for (x in 22) { # FIXME in operator only to be surrounded by one space. What about %in%?
prin(x)
print(x)
}
}
} )
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/scope-character/scope_none-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ a<- function(x){
))
if (x > 10) {
for (x in 22) { # FIXME in operator only to be surrounded by one space. What about %in%?
prin(x)
print(x)
}
}
} )
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/scope-character/scope_none-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ a<- function(x){
))
if (x > 10) {
for (x in 22) { # FIXME in operator only to be surrounded by one space. What about %in%?
prin(x)
print(x)
}
}
} )
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-transformers-drop.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ test_that("tidyverse transformers are correctly dropped", {
names_line_break <- c(
"remove_empty_lines_after_opening_and_before_closing_braces",
"set_line_breaks_between_top_level_exprs",
"set_line_breaks_for_multiline_args",
"set_line_break_around_comma_and_or",
"set_line_break_after_assignment",
"set_line_break_after_opening_if_call_is_multi_line",
Expand Down
Loading