diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cf22944df..4f34c10b7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,7 +34,6 @@ repos: )$ - id: roxygenize additional_dependencies: - - r-lib/pkgapi - dplyr - roxygen2 - rlang @@ -102,7 +101,7 @@ repos: )$ - id: pkgdown - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 + rev: v5.0.0 hooks: - id: check-added-large-files args: ["--maxkb=200"] @@ -120,7 +119,7 @@ repos: tests/testthat/_snaps/.*| )$ - repo: https://github.com/lorenzwalthert/gitignore-tidy - rev: 517cddbf1d8514ddaf43159686617ae65895dc99 + rev: 0.1.2 hooks: - id: tidy-gitignore - repo: local diff --git a/DESCRIPTION b/DESCRIPTION index 42229a06c..a7d401163 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -52,7 +52,7 @@ Roxygen: list(markdown = TRUE, roclets = c( "rd", "namespace", "collate", if (rlang::is_installed("pkgapi")) "pkgapi::api_roclet" else { warning("Please install r-lib/pkgapi to make sure the file API is kept up to date"); NULL})) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Language: en-US Config/testthat/edition: 3 Config/testthat/parallel: true diff --git a/R/rules-line-breaks.R b/R/rules-line-breaks.R index 8fc492643..ecbea2dfe 100644 --- a/R/rules-line-breaks.R +++ b/R/rules-line-breaks.R @@ -149,15 +149,21 @@ set_line_break_around_comma_and_or <- function(pd, strict) { } style_line_break_around_curly <- function(strict, pd) { - if (is_curly_expr(pd) && nrow(pd) > 2L) { - closing_before <- pd$token == "'}'" - opening_before <- (pd$token == "'{'") - to_break <- lag(opening_before, default = FALSE) | closing_before - pd$lag_newlines[to_break] <- ifelse( - pd$token[to_break] == "COMMENT", - pmin(1L, pd$lag_newlines[to_break]), - if (strict) 1L else pmax(1L, pd$lag_newlines[to_break]) - ) + if (is_curly_expr(pd)) { + n_row <- nrow(pd) + if (n_row > 2L) { + closing_before <- pd$token == "'}'" + opening_before <- (pd$token == "'{'") + to_break <- lag(opening_before, default = FALSE) | closing_before + pd$lag_newlines[to_break] <- ifelse( + pd$token[to_break] == "COMMENT", + pmin(1L, pd$lag_newlines[to_break]), + if (strict) 1L else pmax(1L, pd$lag_newlines[to_break]) + ) + } else if (n_row == 2L) { + # pd represents {} + pd$lag_newlines[2L] <- 0L + } } else { is_else <- pd$token == "ELSE" if (any(pd$token_before[is_else] == "'}'")) { diff --git a/R/rules-spaces.R b/R/rules-spaces.R index c96584d6c..54f4bf289 100644 --- a/R/rules-spaces.R +++ b/R/rules-spaces.R @@ -174,8 +174,9 @@ add_space_after_for_if_while <- function(pd_flat) { #' @rdname set_line_break_around_curly_curly #' @keywords internal -set_space_in_curly_curly <- function(pd) { +set_space_in_curly <- function(pd) { if (is_curly_expr(pd)) { + # curly-curly after_inner_opening <- pd$token == "'{'" & pd$token_before == "'{'" before_inner_closing <- lead(pd$token == "'}'" & pd$token_after == "'}'") is_curly_curly_inner <- any(after_inner_opening, na.rm = TRUE) && @@ -193,6 +194,10 @@ set_space_in_curly_curly <- function(pd) { pd$spaces[after_outer_opening] <- 0L pd$spaces[before_outer_closing] <- 0L } + + # empty curly + after_is_empty_curly <- lead(pd$token == "'}'" & pd$token_before == "'{'") + pd$spaces[after_is_empty_curly] <- 0L } pd } diff --git a/R/style-guides.R b/R/style-guides.R index 1445586ac..39fc30118 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -122,7 +122,7 @@ tidyverse_style <- function(scope = "tokens", }, set_space_between_levels = set_space_between_levels, set_space_between_eq_sub_and_comma = set_space_between_eq_sub_and_comma, - set_space_in_curly_curly = set_space_in_curly_curly + set_space_in_curly = set_space_in_curly ) } @@ -209,7 +209,7 @@ tidyverse_style <- function(scope = "tokens", start_comments_with_space = "COMMENT", remove_space_after_unary_pm_nested = c("'+'", "'-'"), spacing_before_comments = "COMMENT", - set_space_in_curly_curly = c("'{'", "'}'") + set_space_in_curly = c("'{'", "'}'") ), indention = list( # indent_braces = c("'('", "'['", "'{'", "')'", "']'", "'}'"), diff --git a/man/set_line_break_around_curly_curly.Rd b/man/set_line_break_around_curly_curly.Rd index 06fd2fa9e..1215ff6d4 100644 --- a/man/set_line_break_around_curly_curly.Rd +++ b/man/set_line_break_around_curly_curly.Rd @@ -2,12 +2,12 @@ % Please edit documentation in R/rules-line-breaks.R, R/rules-spaces.R \name{set_line_break_around_curly_curly} \alias{set_line_break_around_curly_curly} -\alias{set_space_in_curly_curly} +\alias{set_space_in_curly} \title{Styling around \verb{\\\{\\\{}} \usage{ set_line_break_around_curly_curly(pd) -set_space_in_curly_curly(pd) +set_space_in_curly(pd) } \arguments{ \item{pd}{A parse table.} diff --git a/tests/testthat/indention_multiple/edge_strict_mixed-out.R b/tests/testthat/indention_multiple/edge_strict_mixed-out.R index dcfe06a07..0d95d3468 100644 --- a/tests/testthat/indention_multiple/edge_strict_mixed-out.R +++ b/tests/testthat/indention_multiple/edge_strict_mixed-out.R @@ -23,5 +23,4 @@ ))) -function(x, y, z) { -} +function(x, y, z) {} diff --git a/tests/testthat/indention_operators/eq_formals_complex_tokens-out.R b/tests/testthat/indention_operators/eq_formals_complex_tokens-out.R index 612a4beed..27b8d7e7c 100644 --- a/tests/testthat/indention_operators/eq_formals_complex_tokens-out.R +++ b/tests/testthat/indention_operators/eq_formals_complex_tokens-out.R @@ -30,6 +30,4 @@ function(a = f = d, c = 3, d = - 4) { - -} + 4) {} diff --git a/tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R b/tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R index 14e0fadc3..340e1eaf8 100644 --- a/tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R +++ b/tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R @@ -1,8 +1,6 @@ test( "x", - { - - }, + {}, a + b, { s(x = sd) @@ -11,9 +9,7 @@ test( test( "x", - { - - }, + {}, a + b, { s(x = sd) @@ -22,9 +18,7 @@ test( test( "x", - { - - }, + {}, a + b, { s(x = sd) @@ -34,9 +28,7 @@ test( test( "x", - { - - }, + {}, a + b, { s(x = sd) @@ -45,9 +37,7 @@ test( test( "x", - { - - }, # h + {}, # h a + b, { s(x = sd) @@ -56,9 +46,7 @@ test( test( "x", - { - - }, # h + {}, # h a + b, # k { @@ -68,9 +56,7 @@ test( test( "x", - { - - }, + {}, a + b, # k { s(x = sd) diff --git a/tests/testthat/line_breaks_and_other/curly-in.R b/tests/testthat/line_breaks_and_other/curly-in.R index 9f44b5716..8da4db732 100644 --- a/tests/testthat/line_breaks_and_other/curly-in.R +++ b/tests/testthat/line_breaks_and_other/curly-in.R @@ -41,3 +41,21 @@ test_that( # comment expect_equal(1 + 1, 2) }) + + +while (TRUE) { } + +while (TRUE) + { } + +while (TRUE){ + + } + +while (TRUE){ +# + } + + +while (TRUE){# + } diff --git a/tests/testthat/line_breaks_and_other/curly-out.R b/tests/testthat/line_breaks_and_other/curly-out.R index c55508290..22730a44b 100644 --- a/tests/testthat/line_breaks_and_other/curly-out.R +++ b/tests/testthat/line_breaks_and_other/curly-out.R @@ -38,3 +38,18 @@ test_that( expect_equal(1 + 1, 2) } ) + + +while (TRUE) {} + +while (TRUE) {} + +while (TRUE) {} + +while (TRUE) { + # +} + + +while (TRUE) { # +} diff --git a/tests/testthat/line_breaks_fun_call/token_dependent_complex_non_strict-out.R b/tests/testthat/line_breaks_fun_call/token_dependent_complex_non_strict-out.R index 8ef14cf81..c4783fb6c 100644 --- a/tests/testthat/line_breaks_fun_call/token_dependent_complex_non_strict-out.R +++ b/tests/testthat/line_breaks_fun_call/token_dependent_complex_non_strict-out.R @@ -5,8 +5,7 @@ call(call( call(call(1, 2)) # multi-line: no indention based on first vall -call(a(b(c({ -})))) +call(a(b(c({})))) call(call( 2), 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 093fd65cf..8e0309e98 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 @@ -7,8 +7,7 @@ call(call( 2 )) # multi-line: no indention based on first vall -call(a(b(c({ -})))) +call(a(b(c({})))) call( call( diff --git a/tests/testthat/parse_comments/spinning_code_chunk_headers-out.R b/tests/testthat/parse_comments/spinning_code_chunk_headers-out.R index 88ab28999..743269425 100644 --- a/tests/testthat/parse_comments/spinning_code_chunk_headers-out.R +++ b/tests/testthat/parse_comments/spinning_code_chunk_headers-out.R @@ -1,7 +1,5 @@ # A comment -a <- function() { - -} +a <- function() {} #+ chunk-label, opt1=value1 "chunk-content" diff --git a/tests/testthat/public-api/xyz-r-and-rmd-dir/subdir/random-script-in-sub-dir.R b/tests/testthat/public-api/xyz-r-and-rmd-dir/subdir/random-script-in-sub-dir.R index 1337474bf..4b6e446d9 100644 --- a/tests/testthat/public-api/xyz-r-and-rmd-dir/subdir/random-script-in-sub-dir.R +++ b/tests/testthat/public-api/xyz-r-and-rmd-dir/subdir/random-script-in-sub-dir.R @@ -1,4 +1,3 @@ # random this(is_a_call(x)) -if (x) { -} +if (x) {} diff --git a/tests/testthat/public-api/xyzdir/subdir/random-script-in-sub-dir.R b/tests/testthat/public-api/xyzdir/subdir/random-script-in-sub-dir.R index 1337474bf..4b6e446d9 100644 --- a/tests/testthat/public-api/xyzdir/subdir/random-script-in-sub-dir.R +++ b/tests/testthat/public-api/xyzdir/subdir/random-script-in-sub-dir.R @@ -1,4 +1,3 @@ # random this(is_a_call(x)) -if (x) { -} +if (x) {} diff --git a/tests/testthat/spacing/round_curly-out.R b/tests/testthat/spacing/round_curly-out.R index e49f328b3..8f2a4c49a 100644 --- a/tests/testthat/spacing/round_curly-out.R +++ b/tests/testthat/spacing/round_curly-out.R @@ -1,5 +1,4 @@ -a <- function(x) { -} +a <- function(x) {} if (a) { 3 @@ -13,5 +12,4 @@ if (x) { y } else if (x) { x -} else { -} +} else {} diff --git a/tests/testthat/strict/non_strict-out.R b/tests/testthat/strict/non_strict-out.R index 08f773a73..64a698bb5 100644 --- a/tests/testthat/strict/non_strict-out.R +++ b/tests/testthat/strict/non_strict-out.R @@ -51,17 +51,13 @@ test <- function() { call }) - braced("unnamed reduces space", { - }) + braced("unnamed reduces space", {}) - braced("unnamed adds space space", { - }) + braced("unnamed adds space space", {}) - braced(named_reduces_space = { - }) + braced(named_reduces_space = {}) - braced(named_adds_space = { - }) + braced(named_adds_space = {}) braced({ empty_removes_space diff --git a/tests/testthat/strict/strict-out.R b/tests/testthat/strict/strict-out.R index 3422f4e5b..688cf7f88 100644 --- a/tests/testthat/strict/strict-out.R +++ b/tests/testthat/strict/strict-out.R @@ -34,17 +34,13 @@ test <- function() { call }) - braced("unnamed reduces space", { - }) + braced("unnamed reduces space", {}) - braced("unnamed adds space space", { - }) + braced("unnamed adds space space", {}) - braced(named_reduces_space = { - }) + braced(named_reduces_space = {}) - braced(named_adds_space = { - }) + braced(named_adds_space = {}) braced({ empty_removes_space