diff --git a/NEWS.md b/NEWS.md index 27ff33359..26157a478 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,16 @@ +# styler 1.3.2.9000 (Development) + +## Major changes + + +## Minor chnages and fixes + +- overhaul pgkdown site: Add search (#623), group function in Reference (#625). +- always strip trailing spaces and make cache insensitive to it (#626). +- typos in documentation (#618, #614). + + + # styler 1.3.2 Release upon request by the CRAN team. diff --git a/R/rules-line-break.R b/R/rules-line-break.R index db1432791..a1ef35b84 100644 --- a/R/rules-line-break.R +++ b/R/rules-line-break.R @@ -90,12 +90,13 @@ set_line_break_before_curly_opening <- function(pd) { } -set_line_break_around_comma <- function(pd) { +set_line_break_around_comma <- function(pd, strict) { comma_with_line_break_that_can_be_removed_before <- (pd$token == "','") & (pd$lag_newlines > 0) & (pd$token_before != "COMMENT") & (lag(pd$token) != "'['") + pd$lag_newlines[comma_with_line_break_that_can_be_removed_before] <- 0L pd$lag_newlines[lag(comma_with_line_break_that_can_be_removed_before)] <- 1L pd @@ -267,9 +268,17 @@ set_line_break_before_closing_call <- function(pd, except_token_before) { #' @rdname set_line_break_if_call_is_multi_line #' @keywords internal -remove_line_break_in_empty_fun_call <- function(pd) { - if (is_function_call(pd) && nrow(pd) == 3) { - pd$lag_newlines[3] <- 0L +remove_line_break_in_fun_call <- function(pd, strict) { + if (is_function_call(pd)) { + # no blank lines within function calls + if (strict) { + pd$lag_newlines[lag(pd$token == "','") & pd$lag_newlines > 1] <- 1L + + pd$lag_newlines[lag(pd$token == "COMMENT") & pd$lag_newlines > 0] <- 1L + } + if (nrow(pd) == 3) { + pd$lag_newlines[3] <- 0L + } } pd } diff --git a/R/style-guides.R b/R/style-guides.R index 79d55950d..794261a81 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -139,7 +139,7 @@ tidyverse_style <- function(scope = "tokens", except_token_before = "COMMENT" ) }, - remove_line_break_in_empty_fun_call, + purrr::partial(remove_line_break_in_fun_call, strict = strict), add_line_break_after_pipe = if (strict) add_line_break_after_pipe, set_linebreak_after_ggplot2_plus = if (strict) set_linebreak_after_ggplot2_plus ) diff --git a/R/transform-files.R b/R/transform-files.R index 68e44f428..fe1719097 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -80,6 +80,7 @@ make_transformer <- function(transformers, assert_transformers(transformers) function(text) { + text <- trimws(text, which = "right") should_use_cache <- cache_is_activated() if (should_use_cache) { diff --git a/man/set_line_break_if_call_is_multi_line.Rd b/man/set_line_break_if_call_is_multi_line.Rd index 7be17b1f9..a599c9938 100644 --- a/man/set_line_break_if_call_is_multi_line.Rd +++ b/man/set_line_break_if_call_is_multi_line.Rd @@ -4,7 +4,7 @@ \alias{set_line_break_if_call_is_multi_line} \alias{set_line_break_after_opening_if_call_is_multi_line} \alias{set_line_break_before_closing_call} -\alias{remove_line_break_in_empty_fun_call} +\alias{remove_line_break_in_fun_call} \title{Set line break for multi-line function calls} \usage{ set_line_break_after_opening_if_call_is_multi_line( @@ -15,7 +15,7 @@ set_line_break_after_opening_if_call_is_multi_line( set_line_break_before_closing_call(pd, except_token_before) -remove_line_break_in_empty_fun_call(pd) +remove_line_break_in_fun_call(pd, strict) } \arguments{ \item{pd}{A parse table.} diff --git a/tests/testthat/alignment/named-out.R b/tests/testthat/alignment/named-out.R index 53b1da23d..4985a203e 100644 --- a/tests/testthat/alignment/named-out.R +++ b/tests/testthat/alignment/named-out.R @@ -63,7 +63,6 @@ call( # algorithm: aligned. human: aligned. call( x = 1, n = 33, z = "333", - xy = 2, ) diff --git a/tests/testthat/line_breaks_fun_call/blank-non-strict-in.R b/tests/testthat/line_breaks_fun_call/blank-non-strict-in.R new file mode 100644 index 000000000..21225f5f8 --- /dev/null +++ b/tests/testthat/line_breaks_fun_call/blank-non-strict-in.R @@ -0,0 +1,18 @@ +call( + + + 1 +) + +call( + # comment + + 1 +) + +call( + x = 2, + 1, + + "w" +) diff --git a/tests/testthat/line_breaks_fun_call/blank-non-strict-in_tree b/tests/testthat/line_breaks_fun_call/blank-non-strict-in_tree new file mode 100644 index 000000000..9632f334c --- /dev/null +++ b/tests/testthat/line_breaks_fun_call/blank-non-strict-in_tree @@ -0,0 +1,31 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--expr: call( [0/0] {1} + ¦ ¦--expr: call [0/0] {3} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {2} + ¦ ¦--'(': ( [0/2] {4} + ¦ ¦--expr: 1 [3/0] {6} + ¦ ¦ °--NUM_CONST: 1 [0/0] {5} + ¦ °--')': ) [1/0] {7} + ¦--expr: call( [2/0] {8} + ¦ ¦--expr: call [0/0] {10} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {9} + ¦ ¦--'(': ( [0/2] {11} + ¦ ¦--COMMENT: # com [1/2] {12} + ¦ ¦--expr: 1 [2/0] {14} + ¦ ¦ °--NUM_CONST: 1 [0/0] {13} + ¦ °--')': ) [1/0] {15} + °--expr: call( [2/0] {16} + ¦--expr: call [0/0] {18} + ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {17} + ¦--'(': ( [0/2] {19} + ¦--SYMBOL_SUB: x [1/1] {20} + ¦--EQ_SUB: = [0/1] {21} + ¦--expr: 2 [0/0] {23} + ¦ °--NUM_CONST: 2 [0/0] {22} + ¦--',': , [0/2] {24} + ¦--expr: 1 [1/0] {26} + ¦ °--NUM_CONST: 1 [0/0] {25} + ¦--',': , [0/2] {27} + ¦--expr: "w" [2/0] {29} + ¦ °--STR_CONST: "w" [0/0] {28} + °--')': ) [1/0] {30} diff --git a/tests/testthat/line_breaks_fun_call/blank-non-strict-out.R b/tests/testthat/line_breaks_fun_call/blank-non-strict-out.R new file mode 100644 index 000000000..21225f5f8 --- /dev/null +++ b/tests/testthat/line_breaks_fun_call/blank-non-strict-out.R @@ -0,0 +1,18 @@ +call( + + + 1 +) + +call( + # comment + + 1 +) + +call( + x = 2, + 1, + + "w" +) diff --git a/tests/testthat/line_breaks_fun_call/blank-strict-in.R b/tests/testthat/line_breaks_fun_call/blank-strict-in.R new file mode 100644 index 000000000..21225f5f8 --- /dev/null +++ b/tests/testthat/line_breaks_fun_call/blank-strict-in.R @@ -0,0 +1,18 @@ +call( + + + 1 +) + +call( + # comment + + 1 +) + +call( + x = 2, + 1, + + "w" +) diff --git a/tests/testthat/line_breaks_fun_call/blank-strict-in_tree b/tests/testthat/line_breaks_fun_call/blank-strict-in_tree new file mode 100644 index 000000000..9632f334c --- /dev/null +++ b/tests/testthat/line_breaks_fun_call/blank-strict-in_tree @@ -0,0 +1,31 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--expr: call( [0/0] {1} + ¦ ¦--expr: call [0/0] {3} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {2} + ¦ ¦--'(': ( [0/2] {4} + ¦ ¦--expr: 1 [3/0] {6} + ¦ ¦ °--NUM_CONST: 1 [0/0] {5} + ¦ °--')': ) [1/0] {7} + ¦--expr: call( [2/0] {8} + ¦ ¦--expr: call [0/0] {10} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {9} + ¦ ¦--'(': ( [0/2] {11} + ¦ ¦--COMMENT: # com [1/2] {12} + ¦ ¦--expr: 1 [2/0] {14} + ¦ ¦ °--NUM_CONST: 1 [0/0] {13} + ¦ °--')': ) [1/0] {15} + °--expr: call( [2/0] {16} + ¦--expr: call [0/0] {18} + ¦ °--SYMBOL_FUNCTION_CALL: call [0/0] {17} + ¦--'(': ( [0/2] {19} + ¦--SYMBOL_SUB: x [1/1] {20} + ¦--EQ_SUB: = [0/1] {21} + ¦--expr: 2 [0/0] {23} + ¦ °--NUM_CONST: 2 [0/0] {22} + ¦--',': , [0/2] {24} + ¦--expr: 1 [1/0] {26} + ¦ °--NUM_CONST: 1 [0/0] {25} + ¦--',': , [0/2] {27} + ¦--expr: "w" [2/0] {29} + ¦ °--STR_CONST: "w" [0/0] {28} + °--')': ) [1/0] {30} diff --git a/tests/testthat/line_breaks_fun_call/blank-strict-out.R b/tests/testthat/line_breaks_fun_call/blank-strict-out.R new file mode 100644 index 000000000..bffb39422 --- /dev/null +++ b/tests/testthat/line_breaks_fun_call/blank-strict-out.R @@ -0,0 +1,14 @@ +call( + 1 +) + +call( + # comment + 1 +) + +call( + x = 2, + 1, + "w" +) diff --git a/tests/testthat/test-line_breaks_fun_call.R b/tests/testthat/test-line_breaks_fun_call.R index 674b2a3a8..826c254e6 100644 --- a/tests/testthat/test-line_breaks_fun_call.R +++ b/tests/testthat/test-line_breaks_fun_call.R @@ -11,6 +11,19 @@ test_that("line breaks work in general", { ), NA) }) +test_that("blank lines in function calls are removed for strict = TRUE", { + expect_warning(test_collection("line_breaks_fun_call", + "blank-strict", + transformer = style_text + ), NA) + + expect_warning(test_collection("line_breaks_fun_call", + "blank-non-strict", + transformer = style_text, strict = FALSE + ), NA) +}) + + test_that("line breaks are not applied with non-strict", { expect_warning(test_collection("line_breaks_fun_call", "token_dependent_complex_non_strict", diff --git a/tests/testthat/test-stylerignore.R b/tests/testthat/test-stylerignore.R index c732597ea..f42f49095 100644 --- a/tests/testthat/test-stylerignore.R +++ b/tests/testthat/test-stylerignore.R @@ -6,7 +6,7 @@ test_that("gives warning markers are not correct", { ))) }) -test_that("trailing spaces are stripped when checking marker, but not written back", { +test_that("trailing spaces are stripped when checking marker and written back", { expect_equal( style_text(c( "# styler: off ", @@ -14,7 +14,7 @@ test_that("trailing spaces are stripped when checking marker, but not written ba "# styler: on " )) %>% as.character(), - c("# styler: off ", "1+1", "# styler: on") + c("# styler: off", "1+1", "# styler: on") ) })