Skip to content

Commit f672907

Browse files
Merge pull request #891 from lorenzwalthert/issue-890
Rules that create tokens must be stylerignore aware
2 parents 7175da0 + d6b43e3 commit f672907

21 files changed

+1043
-24
lines changed

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
* Piped function without brackets `substitute(x %>% y)` don't get `()` added
2929
anymore, as this can change outcome of the code (#876).
30+
* rules that add tokens don't break stylerignore sequences anymore (#891).
3031
* Add vignette on distributing style guide (#846, #861).
3132
* Alignment detection respects stylerignore (#850).
3233
* `Warning: Unknown or uninitialised column:` was fixed (#885).
@@ -35,9 +36,9 @@
3536
* ensure a trailing blank line also if the input is cached (#867).
3637
* Preserve trailing blank line in roxygen examples to simplify concatenation of
3738
examples (#880).
38-
* Fix argument name `filetype` in Example for `style_dir()` (#855).
3939
* An error is now thrown on styling if input unicode characters can't be
4040
correctly parsed for Windows and R < 4.2 (#883).
41+
* Fix argument name `filetype` in Example for `style_dir()` (#855).
4142

4243

4344
**Infrastructure**

R/detect-alignment-utils.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ alignment_ensure_trailing_comma <- function(pd_by_line) {
6161
lag_newlines = 0L,
6262
spaces = 0L,
6363
pos_ids = NA,
64+
stylerignore = last_pd$stylerignore[1],
65+
indents = last_pd$indent[1]
6466
)
6567
tokens$.lag_spaces <- 0
6668

R/relevel.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ wrap_expr_in_expr <- function(pd) {
9696
"expr", "",
9797
pos_ids = create_pos_ids(pd, 1, after = FALSE),
9898
child = pd,
99-
terminal = FALSE
99+
terminal = FALSE,
100+
stylerignore = pd$stylerignore[1],
101+
indents = pd$indent[1]
100102
)
101103
}
102104

@@ -209,6 +211,8 @@ find_block_id <- function(pd) {
209211
relocate_eq_assign_one <- function(pd) {
210212
idx_eq_assign <- which(pd$token == "EQ_ASSIGN")
211213
eq_ind <- seq2(idx_eq_assign[1] - 1L, last(idx_eq_assign) + 1L)
214+
# initialize because wrap_expr_in_expr -> create_tokens -> requires it
215+
pd$indent <- 0
212216
eq_expr <- pd[eq_ind, ] %>%
213217
wrap_expr_in_expr() %>%
214218
add_line_col_to_wrapped_expr() %>%
@@ -218,6 +222,7 @@ relocate_eq_assign_one <- function(pd) {
218222
))
219223
eq_expr$id <- NA
220224
eq_expr$parent <- NA
225+
pd$indent <- NULL
221226
non_eq_expr <- pd[-eq_ind, ]
222227
pd <- bind_rows(eq_expr, non_eq_expr) %>%
223228
arrange_pos_id()

R/rules-tokens.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,21 @@ add_brackets_in_pipe_one <- function(pd, pos) {
3535
child <- pd$child[[next_non_comment]]
3636
new_pos_ids <- create_pos_ids(child, 1, after = TRUE, n = 2)
3737
new_pd <- create_tokens(
38-
tokens = c("'('", "')'"),
3938
texts = c("(", ")"),
39+
lag_newlines = rep(0L, 2),
40+
spaces = 0,
4041
pos_ids = new_pos_ids,
41-
lag_newlines = rep(0L, 2)
42+
token_before = c(child$token[1], "'('"),
43+
token_after = c("')'", child$token_after[1]),
44+
indention_ref_pos_ids = NA,
45+
indents = child$indent[1],
46+
tokens = c("'('", "')'"),
47+
terminal = TRUE,
48+
child = NULL,
49+
stylerignore = child$stylerignore[1],
50+
# block???
51+
block = NA,
52+
is_cached = FALSE
4253
)
4354
pd$child[[next_non_comment]] <- bind_rows(
4455
pd$child[[next_non_comment]],

R/token-create.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#' not.
2020
#' @param child The children of the tokens.
2121
#' @param stylerignore Boolean to indicate if the line should be ignored by
22-
#' styler.
22+
#' styler. Must take value from token before, can't have a default.
2323
#' @param block The block (of caching) to which the token belongs. An integer.
2424
#' @param is_cached Whether the token is cached already.
2525
#' @family token creators
@@ -32,10 +32,10 @@ create_tokens <- function(tokens,
3232
token_before = NA,
3333
token_after = NA,
3434
indention_ref_pos_ids = NA,
35-
indents = 0L,
35+
indents,
3636
terminal = TRUE,
3737
child = NULL,
38-
stylerignore = FALSE,
38+
stylerignore,
3939
block = NA,
4040
is_cached = FALSE) {
4141
len_text <- length(texts)
@@ -149,14 +149,16 @@ wrap_expr_in_curly <- function(pd,
149149
opening <- create_tokens("'{'", "{",
150150
pos_ids = create_pos_ids(pd, 1, after = FALSE),
151151
spaces = 1 - as.integer(stretch_out[1]),
152-
stylerignore = pd$stylerignore[1]
152+
stylerignore = pd$stylerignore[1],
153+
indents = pd$indent[1]
153154
)
154155

155156
closing <- create_tokens(
156157
"'}'", "}",
157158
spaces = space_after, lag_newlines = as.integer(stretch_out[2]),
158159
pos_ids = create_pos_ids(pd, nrow(pd), after = TRUE),
159-
stylerignore = pd$stylerignore[1]
160+
stylerignore = pd$stylerignore[1],
161+
indents = pd$indent[1]
160162
)
161163

162164
bind_rows(opening, pd, closing) %>%

man/create_tokens.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
*.dll
12
*.o
23
*.so
3-
*.dll
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
*.dll
12
*.o
23
*.so
3-
*.dll

tests/testthat/test-create_token.R

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ test_that("can create a token that has relevant columns", {
99
)
1010

1111
expect_equal(
12-
names(create_tokens("'{'", "{", pos_ids = 3)),
12+
names(create_tokens("'{'", "{", pos_ids = 3, stylerignore = FALSE, indents = 0)),
1313
pd_names
1414
)
1515
})
1616

1717
test_that("pos_id can be created", {
18-
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3)
18+
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3, stylerignore = FALSE, indents = 0)
1919
new_id <- create_pos_ids(pd, 1L, by = 0.4)
2020
expect_error(
2121
bind_rows(
22-
create_tokens("XZY_TEST", "test", pos_ids = new_id),
22+
create_tokens("XZY_TEST", "test",
23+
pos_ids = new_id,
24+
stylerignore = FALSE, indents = 0
25+
),
2326
pd
2427
),
2528
NA
@@ -28,21 +31,30 @@ test_that("pos_id can be created", {
2831

2932

3033
test_that("unambiguous pos_id won't be created (down)", {
31-
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3)
34+
pd <- create_tokens("XZY_TEST", "test",
35+
pos_ids = 3,
36+
stylerignore = FALSE, indents = 0
37+
)
3238
new_id <- create_pos_ids(pd, 1L, by = 0.4)
3339
pd <- bind_rows(
34-
create_tokens("XZY_TEST", "test", pos_ids = new_id),
40+
create_tokens("XZY_TEST", "test",
41+
pos_ids = new_id,
42+
stylerignore = FALSE, indents = 0
43+
),
3544
pd
3645
)
3746
expect_error(create_pos_id(pd, 1L, by = 0.4))
3847
})
3948

4049
test_that("unambiguous pos_id won't be created (up)", {
41-
pd <- create_tokens("XZY_TEST", "test", pos_ids = 3)
50+
pd <- create_tokens("XZY_TEST", "test",
51+
pos_ids = 3,
52+
stylerignore = FALSE, indents = 0
53+
)
4254
new_id <- create_pos_ids(pd, 1L, by = 0.4, after = TRUE)
4355

4456
pd <- bind_rows(
45-
create_tokens("XZY_TEST", "test", pos_ids = new_id),
57+
create_tokens("XZY_TEST", "test", pos_ids = new_id, stylerignore = FALSE, indents = 0),
4658
pd
4759
)
4860
expect_error(create_pos_id(pd, 1L, by = 0.4, after = TRUE))

tests/testthat/test-token_adding_removing.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ test_that("braces only added to pipe if RHS is a symbol", {
3838
})
3939

4040

41+
4142
test_that("No braces are added if conditional statement is within pipe", {
4243
expect_warning(test_collection("token_adding_removing", "else-pipe",
4344
transformer = style_text
@@ -49,3 +50,16 @@ test_that("No brace is added within `substitute()`", {
4950
transformer = style_text
5051
), NA)
5152
})
53+
54+
55+
test_that("stylreignore interacts correctly with wrap_expr_in_curly", {
56+
expect_warning(test_collection("token_adding_removing", "if_else_stylerignore",
57+
transformer = style_text
58+
), NA)
59+
})
60+
61+
test_that("stylreignore interacts correctly with wrap_expr_in_curly", {
62+
expect_warning(test_collection("token_adding_removing", "for_while_stylerignore",
63+
transformer = style_text
64+
), NA)
65+
})

0 commit comments

Comments
 (0)