diff --git a/NEWS.md b/NEWS.md index fb36ad84c..7b08eb736 100644 --- a/NEWS.md +++ b/NEWS.md @@ -47,6 +47,7 @@ there are comments and line breaks under certain circumstances (#905). * rules that add tokens don't break stylerignore sequences anymore (#891). * Alignment detection respects stylerignore (#850). +* `~` causes now indention, like `+`, `-`, `|` etc. (#902). * `Warning: Unknown or uninitialised column:` was fixed (#885). * Unaligned expressions with quoted key (e.g. `c("x" = 2)`) are now correctly detected (#881). diff --git a/R/relevel.R b/R/relevel.R index f098ecfb9..71cee0a10 100644 --- a/R/relevel.R +++ b/R/relevel.R @@ -28,7 +28,7 @@ flatten_operators_one <- function(pd_nested) { pd_token_left <- c(special_token, "PIPE", math_token, "'$'") pd_token_right <- c( special_token, "PIPE", "LEFT_ASSIGN", if (parser_version_get() > 1) "EQ_ASSIGN", - "'+'", "'-'" + "'+'", "'-'", "'~'" ) pd_nested %>% flatten_pd(pd_token_left, left = TRUE) %>% diff --git a/R/rules-indention.R b/R/rules-indention.R index 2ebab1a9b..71347e362 100644 --- a/R/rules-indention.R +++ b/R/rules-indention.R @@ -38,7 +38,8 @@ indent_op <- function(pd, "PIPE", "LEFT_ASSIGN", "EQ_ASSIGN", - "'$'" + "'$'", + "'~'" )) { indent_indices <- compute_indent_indices(pd, token) pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by diff --git a/man/update_indention.Rd b/man/update_indention.Rd index 3dd87ec98..9834753ef 100644 --- a/man/update_indention.Rd +++ b/man/update_indention.Rd @@ -20,7 +20,7 @@ indent_op( pd, indent_by, token = c(math_token, logical_token, special_token, "PIPE", "LEFT_ASSIGN", - "EQ_ASSIGN", "'$'") + "EQ_ASSIGN", "'$'", "'~'") ) indent_eq_sub(pd, indent_by, token = c("EQ_SUB", "EQ_FORMALS")) diff --git a/tests/testthat/indention_operators/tilde-in.R b/tests/testthat/indention_operators/tilde-in.R new file mode 100644 index 000000000..c74602490 --- /dev/null +++ b/tests/testthat/indention_operators/tilde-in.R @@ -0,0 +1,7 @@ +y ~ +x+ +y + + +x ~ + 1 + (x|b) diff --git a/tests/testthat/indention_operators/tilde-in_tree b/tests/testthat/indention_operators/tilde-in_tree new file mode 100644 index 000000000..7f3155dd1 --- /dev/null +++ b/tests/testthat/indention_operators/tilde-in_tree @@ -0,0 +1,28 @@ +ROOT (token: short_text [lag_newlines/spaces] {pos_id}) + ¦--expr: y ~ +x [0/0] {1} + ¦ ¦--expr: y [0/1] {3} + ¦ ¦ °--SYMBOL: y [0/0] {2} + ¦ ¦--'~': ~ [0/0] {4} + ¦ ¦--expr: x [1/0] {7} + ¦ ¦ °--SYMBOL: x [0/0] {6} + ¦ ¦--'+': + [0/0] {8} + ¦ °--expr: y [1/0] {10} + ¦ °--SYMBOL: y [0/0] {9} + °--expr: x ~ + [3/0] {11} + ¦--expr: x [0/1] {13} + ¦ °--SYMBOL: x [0/0] {12} + ¦--'~': ~ [0/2] {14} + ¦--expr: 1 [1/1] {17} + ¦ °--NUM_CONST: 1 [0/0] {16} + ¦--'+': + [0/1] {18} + °--expr: (x|b) [0/0] {19} + ¦--'(': ( [0/0] {20} + ¦--expr: x|b [0/0] {21} + ¦ ¦--expr: x [0/0] {23} + ¦ ¦ °--SYMBOL: x [0/0] {22} + ¦ ¦--OR: | [0/0] {24} + ¦ °--expr: b [0/0] {26} + ¦ °--SYMBOL: b [0/0] {25} + °--')': ) [0/0] {27} diff --git a/tests/testthat/indention_operators/tilde-out.R b/tests/testthat/indention_operators/tilde-out.R new file mode 100644 index 000000000..094eee463 --- /dev/null +++ b/tests/testthat/indention_operators/tilde-out.R @@ -0,0 +1,7 @@ +y ~ + x + + y + + +x ~ + 1 + (x | b) diff --git a/tests/testthat/test-indention_operators.R b/tests/testthat/test-indention_operators.R index a3c111c75..94697add7 100644 --- a/tests/testthat/test-indention_operators.R +++ b/tests/testthat/test-indention_operators.R @@ -141,6 +141,14 @@ test_that("indents eq_formals correctly with various levels of scope", { ), NA) }) +test_that("tilde causes indention and is flattened out", { + expect_warning(test_collection("indention_operators", + "tilde", + transformer = style_text + ), NA) +}) + + test_that("overall", { expect_warning(test_collection("indention_operators", "overall",