Skip to content

Commit c05d5b1

Browse files
make rule for formula more specific
1 parent 53ae5e1 commit c05d5b1

File tree

11 files changed

+179
-30
lines changed

11 files changed

+179
-30
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: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,40 @@ style_space_around_math_token <- function(strict, zero, one, pd_flat) {
4444
#' @param pd_flat A nest or a flat parse table.
4545
#' @param strict Whether the rules should be applied strictly or not.
4646
#' @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`.
47+
#' @param level_before,level_after Scalar indicating the amount of spaces that
48+
#' should be inserted around the `tokens` on the left and right position
49+
#' respectively.
4950
#' @keywords internal
50-
style_space_around_token <- function(pd_flat, strict, tokens, level) {
51+
style_space_around_token <- function(pd_flat,
52+
strict,
53+
tokens,
54+
level_before,
55+
level_after = level_before) {
5156
op_after <- pd_flat$token %in% tokens
5257
op_before <- lead(op_after, default = FALSE)
5358
idx_before <- op_before & (pd_flat$newlines == 0L)
5459
idx_after <- op_after & (pd_flat$newlines == 0L)
5560
if (strict) {
56-
pd_flat$spaces[idx_before | idx_after] <- level
61+
pd_flat$spaces[idx_before] <- level_before
62+
pd_flat$spaces[idx_after] <- level_after
5763
} else {
58-
pd_flat$spaces[idx_before | idx_after] <-
59-
pmax(pd_flat$spaces[idx_before | idx_after], level)
64+
pd_flat$spaces[idx_before] <-
65+
pmax(pd_flat$spaces[idx_before], level_before)
66+
pd_flat$spaces[idx_after] <-
67+
pmax(pd_flat$spaces[idx_after], level_after)
68+
}
69+
pd_flat
70+
}
71+
72+
style_space_around_tilde <- function(pd_flat, strict) {
73+
if (is_symmetric_tilde_expr(pd_flat)) {
74+
pd_flat <- style_space_around_token(pd_flat,
75+
strict, "'~'", level_before = 1, level_after = 1
76+
)
77+
} else if (is_asymmetric_tilde_expr(pd_flat)) {
78+
pd_flat <- style_space_around_token(pd_flat,
79+
strict = TRUE, "'~'", level_before = 1, level_after = 0
80+
)
6081
}
6182
pd_flat
6283
}

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)