diff --git a/R/rules-line-breaks.R b/R/rules-line-breaks.R index fa801bc27..c31126b76 100644 --- a/R/rules-line-breaks.R +++ b/R/rules-line-breaks.R @@ -455,3 +455,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 +} diff --git a/R/style-guides.R b/R/style-guides.R index 6a5366c52..01a0df4aa 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -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 diff --git a/styler.Rproj b/styler.Rproj index 734d4c4b8..f4c33d288 100644 --- a/styler.Rproj +++ b/styler.Rproj @@ -1,5 +1,5 @@ Version: 1.0 -ProjectId: 0778f065-a50f-4f3e-8b43-841eded0d216 +ProjectId: bbbaaf9d-d7a1-475e-8d3b-3e41589b57c8 RestoreWorkspace: No SaveWorkspace: No diff --git a/tests/testthat/cache-with-r-cache/mlflow-1-in.R b/tests/testthat/cache-with-r-cache/mlflow-1-in.R index eb0ebb231..556977249 100644 --- a/tests/testthat/cache-with-r-cache/mlflow-1-in.R +++ b/tests/testthat/cache-with-r-cache/mlflow-1-in.R @@ -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." diff --git a/tests/testthat/indention_multiple/overall-in.R b/tests/testthat/indention_multiple/overall-in.R index 6f9113e38..45fb3fe1a 100644 --- a/tests/testthat/indention_multiple/overall-in.R +++ b/tests/testthat/indention_multiple/overall-in.R @@ -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) } } }) diff --git a/tests/testthat/indention_multiple/overall-out.R b/tests/testthat/indention_multiple/overall-out.R index 4db2b90fc..9fb263f2f 100644 --- a/tests/testthat/indention_multiple/overall-out.R +++ b/tests/testthat/indention_multiple/overall-out.R @@ -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) } } }) diff --git a/tests/testthat/line_breaks_and_other/base-pipe-line-breaks-in.R b/tests/testthat/line_breaks_and_other/base-pipe-line-breaks-in.R index 641e72d52..ac44b0602 100644 --- a/tests/testthat/line_breaks_and_other/base-pipe-line-breaks-in.R +++ b/tests/testthat/line_breaks_and_other/base-pipe-line-breaks-in.R @@ -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()) diff --git a/tests/testthat/line_breaks_and_other/base-pipe-line-breaks-out.R b/tests/testthat/line_breaks_and_other/base-pipe-line-breaks-out.R index 346e0303c..519a6ce7c 100644 --- a/tests/testthat/line_breaks_and_other/base-pipe-line-breaks-out.R +++ b/tests/testthat/line_breaks_and_other/base-pipe-line-breaks-out.R @@ -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() +) { diff --git a/tests/testthat/line_breaks_and_other/pipe-line-breaks-in.R b/tests/testthat/line_breaks_and_other/pipe-line-breaks-in.R index a60021669..6c584b68d 100644 --- a/tests/testthat/line_breaks_and_other/pipe-line-breaks-in.R +++ b/tests/testthat/line_breaks_and_other/pipe-line-breaks-in.R @@ -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()) diff --git a/tests/testthat/line_breaks_and_other/pipe-line-breaks-out.R b/tests/testthat/line_breaks_and_other/pipe-line-breaks-out.R index 093655a80..beef79f5f 100644 --- a/tests/testthat/line_breaks_and_other/pipe-line-breaks-out.R +++ b/tests/testthat/line_breaks_and_other/pipe-line-breaks-out.R @@ -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() +) { diff --git a/tests/testthat/line_breaks_fun_call/named_arguments-in.R b/tests/testthat/line_breaks_fun_call/named_arguments-in.R index f9c70a872..c0de22255 100644 --- a/tests/testthat/line_breaks_fun_call/named_arguments-in.R +++ b/tests/testthat/line_breaks_fun_call/named_arguments-in.R @@ -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 +) diff --git a/tests/testthat/line_breaks_fun_call/named_arguments-out.R b/tests/testthat/line_breaks_fun_call/named_arguments-out.R index 485e4d609..e61ff5aa8 100644 --- a/tests/testthat/line_breaks_fun_call/named_arguments-out.R +++ b/tests/testthat/line_breaks_fun_call/named_arguments-out.R @@ -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 +) diff --git a/tests/testthat/line_breaks_fun_call/token_dependent_complex_strict-out.R b/tests/testthat/line_breaks_fun_call/token_dependent_complex_strict-out.R index 8e0309e98..0ee13430a 100644 --- a/tests/testthat/line_breaks_fun_call/token_dependent_complex_strict-out.R +++ b/tests/testthat/line_breaks_fun_call/token_dependent_complex_strict-out.R @@ -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 @@ -40,6 +44,8 @@ call(call( 2 )) -call(1, call( - 23 -)) +call( + 1, call( + 23 + ) +) diff --git a/tests/testthat/line_breaks_fun_call/unindent-out.R b/tests/testthat/line_breaks_fun_call/unindent-out.R index 6452a6c74..2c05c02ae 100644 --- a/tests/testthat/line_breaks_fun_call/unindent-out.R +++ b/tests/testthat/line_breaks_fun_call/unindent-out.R @@ -1,6 +1,9 @@ -test_that(key( - s -), x = 1) +test_that( + key( + s + ), + x = 1 +) test_that( key( @@ -10,9 +13,12 @@ test_that( ) -test_that(key( - s -), x = 1) +test_that( + key( + s + ), + x = 1 +) test_that( diff --git a/tests/testthat/roxygen-examples-complete/10-styler-r-ui-out.R b/tests/testthat/roxygen-examples-complete/10-styler-r-ui-out.R index a7cbf3aca..3a4548e2b 100644 --- a/tests/testthat/roxygen-examples-complete/10-styler-r-ui-out.R +++ b/tests/testthat/roxygen-examples-complete/10-styler-r-ui-out.R @@ -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) } diff --git a/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-out.R b/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-out.R index 35a804639..81c9fb987 100644 --- a/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-out.R +++ b/tests/testthat/roxygen-examples-complete/11-start-with-dontrun-out.R @@ -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) } diff --git a/tests/testthat/scope-AsIs/scope_none-in.R b/tests/testthat/scope-AsIs/scope_none-in.R index 2c770501f..9948a34df 100644 --- a/tests/testthat/scope-AsIs/scope_none-in.R +++ b/tests/testthat/scope-AsIs/scope_none-in.R @@ -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) } } } ) diff --git a/tests/testthat/scope-AsIs/scope_none-out.R b/tests/testthat/scope-AsIs/scope_none-out.R index 2c770501f..9948a34df 100644 --- a/tests/testthat/scope-AsIs/scope_none-out.R +++ b/tests/testthat/scope-AsIs/scope_none-out.R @@ -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) } } } ) diff --git a/tests/testthat/scope-character/scope_none-in.R b/tests/testthat/scope-character/scope_none-in.R index 2c770501f..9948a34df 100644 --- a/tests/testthat/scope-character/scope_none-in.R +++ b/tests/testthat/scope-character/scope_none-in.R @@ -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) } } } ) diff --git a/tests/testthat/scope-character/scope_none-out.R b/tests/testthat/scope-character/scope_none-out.R index 2c770501f..9948a34df 100644 --- a/tests/testthat/scope-character/scope_none-out.R +++ b/tests/testthat/scope-character/scope_none-out.R @@ -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) } } } ) diff --git a/tests/testthat/test-transformers-drop.R b/tests/testthat/test-transformers-drop.R index ad10908f5..42030f7cd 100644 --- a/tests/testthat/test-transformers-drop.R +++ b/tests/testthat/test-transformers-drop.R @@ -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",