Skip to content

Commit a1fe62f

Browse files
Merge pull request #379 from lorenzwalthert/fix-tilde
- Make rule for tilde more specific (#379).
2 parents 5733a83 + 9d7776b commit a1fe62f

File tree

11 files changed

+185
-32
lines changed

11 files changed

+185
-32
lines changed

R/expr-is.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,33 @@ is_curly_expr <- function(pd) {
1313
pd$token[1] == "'{'"
1414
}
1515

16+
17+
#' Check whether a parse table contains a tilde
18+
#'
19+
#'
20+
#' @param pd A parse table.
21+
#' @param tilde_pos Integer vector indicating row-indices that should be
22+
#' checked for tilde. See 'Details'.
23+
#'
24+
#' @details
25+
#' A tilde is on the top row in the parse table if it is an asymmetric tilde
26+
#' expression (like `~column`), in the second row if it is a symmetric tilde
27+
#' expression (like `a~b`).
28+
is_tilde_expr <- function(pd, tilde_pos = c(1, 2)) {
29+
if (is.null(pd) || nrow(pd) == 1) return(FALSE)
30+
pd$token[tilde_pos] == "'~'"
31+
}
32+
33+
#' @rdname is_tilde_expr
34+
is_asymmetric_tilde_expr <- function(pd) {
35+
is_tilde_expr(pd, tilde_pos = 1)
36+
}
37+
38+
#' @rdname is_tilde_expr
39+
is_symmetric_tilde_expr <- function(pd) {
40+
is_tilde_expr(pd, tilde_pos = 2)
41+
}
42+
1643
is_subset_expr <- function(pd) {
1744
if (is.null(pd) || nrow(pd) == 1) return(FALSE)
1845
pd$token[2] == "'['"

R/rules-spacing.R

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ style_space_around_math_token <- function(strict, zero, one, pd_flat) {
3333
# We remove spaces for zero (e.g., around ^ in the tidyverse style guide)
3434
# even for strict = FALSE to be consistent with the : operator
3535
pd_flat %>%
36-
style_space_around_token(strict = TRUE, zero, 0L) %>%
37-
style_space_around_token(strict = strict, one, 1L)
36+
style_space_around_token(
37+
strict = TRUE, tokens = zero, level_before = 0L, level_after = 0L
38+
) %>%
39+
style_space_around_token(
40+
strict = strict, tokens = one, level_before = 1L, level_after = 1L
41+
)
3842
}
3943

4044
#' Set spacing of token to a certain level
@@ -44,19 +48,40 @@ style_space_around_math_token <- function(strict, zero, one, pd_flat) {
4448
#' @param pd_flat A nest or a flat parse table.
4549
#' @param strict Whether the rules should be applied strictly or not.
4650
#' @param tokens Character vector with tokens that should be styled.
47-
#' @param level Scalar indicating the amount of spaces that should be inserted
48-
#' around the `tokens`.
51+
#' @param level_before,level_after Scalar indicating the amount of spaces that
52+
#' should be inserted around the `tokens` on the left and right position
53+
#' respectively.
4954
#' @keywords internal
50-
style_space_around_token <- function(pd_flat, strict, tokens, level) {
55+
style_space_around_token <- function(pd_flat,
56+
strict,
57+
tokens,
58+
level_before,
59+
level_after = level_before) {
5160
op_after <- pd_flat$token %in% tokens
5261
op_before <- lead(op_after, default = FALSE)
5362
idx_before <- op_before & (pd_flat$newlines == 0L)
5463
idx_after <- op_after & (pd_flat$newlines == 0L)
5564
if (strict) {
56-
pd_flat$spaces[idx_before | idx_after] <- level
65+
pd_flat$spaces[idx_before] <- level_before
66+
pd_flat$spaces[idx_after] <- level_after
5767
} else {
58-
pd_flat$spaces[idx_before | idx_after] <-
59-
pmax(pd_flat$spaces[idx_before | idx_after], level)
68+
pd_flat$spaces[idx_before] <-
69+
pmax(pd_flat$spaces[idx_before], level_before)
70+
pd_flat$spaces[idx_after] <-
71+
pmax(pd_flat$spaces[idx_after], level_after)
72+
}
73+
pd_flat
74+
}
75+
76+
style_space_around_tilde <- function(pd_flat, strict) {
77+
if (is_symmetric_tilde_expr(pd_flat)) {
78+
pd_flat <- style_space_around_token(pd_flat,
79+
strict, "'~'", level_before = 1, level_after = 1
80+
)
81+
} else if (is_asymmetric_tilde_expr(pd_flat)) {
82+
pd_flat <- style_space_around_token(pd_flat,
83+
strict = TRUE, "'~'", level_before = 1, level_after = 0
84+
)
6085
}
6186
pd_flat
6287
}

R/style-guides.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ tidyverse_style <- function(scope = "tokens",
8484
math_token_spacing$one
8585
),
8686
style_space_around_tilde = partial(
87-
style_space_around_token, strict = strict, tokens = "'~'", level = 1L),
87+
style_space_around_tilde, strict = strict
88+
),
8889
spacing_around_op = if (strict) {
8990
set_space_around_op
9091
}else {

man/is_tilde_expr.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_space_around_token.Rd

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/strict/non_strict-in.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,8 @@ lm(a~.-1,data=NA)
141141
a~b:c
142142
a~b :c
143143
a ~ b : c
144+
145+
~ a
146+
~gg
147+
b~ k
148+
call(1,~ qq)

tests/testthat/strict/non_strict-in_tree

Lines changed: 36 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/strict/non_strict-out.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,8 @@ lm(a ~ . - 1, data = NA)
140140
a ~ b:c
141141
a ~ b:c
142142
a ~ b:c
143+
144+
~a
145+
~gg
146+
b ~ k
147+
call(1, ~qq)

tests/testthat/strict/strict-in.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,7 @@ lm(a~.-1,data=NA)
141141
a~b:c
142142
a ~ b : c
143143
a~b :c
144+
~ a
145+
~gg
146+
b~k
147+
call(1, ~ qq)

tests/testthat/strict/strict-in_tree

Lines changed: 36 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)