diff --git a/NEWS.md b/NEWS.md index 7b0e36e1c..84fade6d7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -47,6 +47,8 @@ * Preserve trailing blank line in roxygen examples to simplify concatenation of examples (#880). * added guarantee that styled code is parsable (#892). +* `indenty_by` is now also respected when curly braces are added to an if + statement by {styler} (#915). * An error is now thrown on styling if input unicode characters can't be correctly parsed for Windows and R < 4.2 (#883). * styling of text does not error anymore when the R option `OutDec` is set to diff --git a/R/style-guides.R b/R/style-guides.R index 157a2c556..d35b5679d 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -169,7 +169,12 @@ tidyverse_style <- function(scope = "tokens", resolve_semicolon = resolve_semicolon, add_brackets_in_pipe = add_brackets_in_pipe, wrap_if_else_while_for_fun_multi_line_in_curly = - if (strict) wrap_if_else_while_for_fun_multi_line_in_curly + if (strict) { + purrr::partial( + wrap_if_else_while_for_fun_multi_line_in_curly, + indent_by = indent_by + ) + } ) } diff --git a/tests/testthat/indention_curly_brackets/custom-in.R b/tests/testthat/indention_curly_brackets/custom-in.R new file mode 100644 index 000000000..40156b509 --- /dev/null +++ b/tests/testthat/indention_curly_brackets/custom-in.R @@ -0,0 +1,10 @@ + +value <- 5 +if (value > 0) + print(value) + + + +if (value > 0) { +print(value) +} diff --git a/tests/testthat/indention_curly_brackets/custom-in_tree b/tests/testthat/indention_curly_brackets/custom-in_tree new file mode 100644 index 000000000..454cd1522 --- /dev/null +++ b/tests/testthat/indention_curly_brackets/custom-in_tree @@ -0,0 +1,45 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--expr: value [0/0] {1} + ¦ ¦--expr: value [0/1] {3} + ¦ ¦ °--SYMBOL: value [0/0] {2} + ¦ ¦--LEFT_ASSIGN: <- [0/1] {4} + ¦ °--expr: 5 [0/0] {6} + ¦ °--NUM_CONST: 5 [0/0] {5} + ¦--expr: if (v [1/0] {7} + ¦ ¦--IF: if [0/1] {8} + ¦ ¦--'(': ( [0/0] {9} + ¦ ¦--expr: value [0/0] {10} + ¦ ¦ ¦--expr: value [0/1] {12} + ¦ ¦ ¦ °--SYMBOL: value [0/0] {11} + ¦ ¦ ¦--GT: > [0/1] {13} + ¦ ¦ °--expr: 0 [0/0] {15} + ¦ ¦ °--NUM_CONST: 0 [0/0] {14} + ¦ ¦--')': ) [0/4] {16} + ¦ °--expr: print [1/0] {17} + ¦ ¦--expr: print [0/0] {19} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: print [0/0] {18} + ¦ ¦--'(': ( [0/0] {20} + ¦ ¦--expr: value [0/0] {22} + ¦ ¦ °--SYMBOL: value [0/0] {21} + ¦ °--')': ) [0/0] {23} + °--expr: if (v [4/0] {24} + ¦--IF: if [0/1] {25} + ¦--'(': ( [0/0] {26} + ¦--expr: value [0/0] {27} + ¦ ¦--expr: value [0/1] {29} + ¦ ¦ °--SYMBOL: value [0/0] {28} + ¦ ¦--GT: > [0/1] {30} + ¦ °--expr: 0 [0/0] {32} + ¦ °--NUM_CONST: 0 [0/0] {31} + ¦--')': ) [0/1] {33} + °--expr: { +pri [0/0] {34} + ¦--'{': { [0/0] {35} + ¦--expr: print [1/0] {36} + ¦ ¦--expr: print [0/0] {38} + ¦ ¦ °--SYMBOL_FUNCTION_CALL: print [0/0] {37} + ¦ ¦--'(': ( [0/0] {39} + ¦ ¦--expr: value [0/0] {41} + ¦ ¦ °--SYMBOL: value [0/0] {40} + ¦ °--')': ) [0/0] {42} + °--'}': } [1/0] {43} diff --git a/tests/testthat/indention_curly_brackets/custom-out.R b/tests/testthat/indention_curly_brackets/custom-out.R new file mode 100644 index 000000000..28ac4fd10 --- /dev/null +++ b/tests/testthat/indention_curly_brackets/custom-out.R @@ -0,0 +1,11 @@ + +value <- 5 +if (value > 0) { + print(value) +} + + + +if (value > 0) { + print(value) +} diff --git a/tests/testthat/test-indention_curly.R b/tests/testthat/test-indention_curly.R index 4ea089c07..448ac209a 100644 --- a/tests/testthat/test-indention_curly.R +++ b/tests/testthat/test-indention_curly.R @@ -22,6 +22,13 @@ test_that("indention with multi-line curley and round is correct", { ), NA) }) +test_that("custom indention for curly braces is corretct ", { + expect_warning(test_collection("indention_curly_brackets", + "custom", + transformer = style_text, indent_by = 4 + ), NA) +}) + test_that(paste(