Skip to content

Commit 2fce44c

Browse files
Merge pull request #593 from lorenzwalthert/issue-592
- Fix caching for multi-token one-line expression with partial caching (#592).
2 parents 7679c80 + ead2608 commit 2fce44c

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

R/nest.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ add_terminal_token_before <- function(pd_flat) {
243243
#' @describeIn add_token_terminal Initializes `newlines` and `lag_newlines`.
244244
#' @keywords internal
245245
add_attributes_caching <- function(pd_flat, transformers) {
246-
pd_flat$block <- pd_flat$is_cached <- rep(NA, nrow(pd_flat))
246+
pd_flat$block <- rep(NA, nrow(pd_flat))
247+
pd_flat$is_cached <- rep(FALSE, nrow(pd_flat))
247248
if (cache_is_activated()) {
248249
is_parent <- pd_flat$parent == 0
249250
pd_flat$is_cached[is_parent] <- map_lgl(

R/token-create.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ create_tokens <- function(tokens,
3737
child = NULL,
3838
stylerignore = FALSE,
3939
block = NA,
40-
is_cached = NA) {
40+
is_cached = FALSE) {
4141
len_text <- length(texts)
4242
new_tibble(
4343
list(

R/visit.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ context_towards_terminals <- function(pd_nested,
128128
#' @keywords internal
129129
extract_terminals <- function(pd_nested) {
130130
bind_rows(
131-
ifelse(pd_nested$terminal, split(pd_nested, seq_len(nrow(pd_nested))),
131+
ifelse(pd_nested$terminal | pd_nested$is_cached, split(pd_nested, seq_len(nrow(pd_nested))),
132132
pd_nested$child
133133
)
134134
)

man/create_tokens.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-cache-high-level-api.R

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ text <- c(
2424
rep(10)
2525

2626
capture.output(test_that("activated cache brings speedup on style_text() API on character vector", {
27-
2827
activate_testthat_cache()
2928
on.exit(clear_testthat_cache())
3029
clear_testthat_cache()
@@ -36,7 +35,6 @@ capture.output(test_that("activated cache brings speedup on style_text() API on
3635
}))
3736

3837
capture.output(test_that("activated cache brings speedup on style_text() API on character scalar", {
39-
4038
activate_testthat_cache()
4139
on.exit(clear_testthat_cache())
4240
clear_testthat_cache()
@@ -75,7 +73,8 @@ test_that("trailing line breaks are ignored for caching in one scalar", {
7573
second <- system.time(
7674
styler::style_text(
7775
paste0(paste0(text, collapse = "\n"), "\n", "\n", "\n", "\n", collapse = "")
78-
))
76+
)
77+
)
7978
expect_true(first["elapsed"] / 2 > second["elapsed"])
8079
# check we only have three different expressions. Top-level, example and fun.
8180
cache_info <- cache_info()
@@ -86,7 +85,6 @@ test_that("trailing line breaks are ignored for caching in one scalar", {
8685
})
8786

8887
capture.output(test_that("no speedup when tranformer changes", {
89-
9088
activate_testthat_cache()
9189
on.exit(clear_testthat_cache())
9290
clear_testthat_cache()
@@ -112,11 +110,11 @@ capture.output(
112110
first <- system.time(styler::style_text(text))
113111
second <- system.time(styler::style_text(paste0(text, collapse = "\n")))
114112
expect_true(first["elapsed"] / 2 > second["elapsed"])
115-
}))
113+
})
114+
)
116115

117116

118117
capture.output(test_that("unactivated cache does not bring speedup", {
119-
120118
on.exit(clear_testthat_cache())
121119
clear_testthat_cache()
122120
cache_deactivate()
@@ -127,7 +125,6 @@ capture.output(test_that("unactivated cache does not bring speedup", {
127125

128126

129127
capture.output(test_that("avoid deleting comments #584 (see commit messages)", {
130-
131128
on.exit(clear_testthat_cache())
132129
clear_testthat_cache()
133130
activate_testthat_cache()
@@ -136,7 +133,7 @@ capture.output(test_that("avoid deleting comments #584 (see commit messages)", {
136133
"# Comment",
137134
"# another",
138135
"NULL"
139-
)
136+
)
140137
style_text(text)
141138
text2 <- c(
142139
"1 + 1",
@@ -152,7 +149,6 @@ capture.output(test_that("avoid deleting comments #584 (see commit messages)", {
152149

153150

154151
capture.output(test_that("avoid removing roxygen mask (see commit messages in #584)", {
155-
156152
on.exit(clear_testthat_cache())
157153
clear_testthat_cache()
158154
activate_testthat_cache()
@@ -179,3 +175,26 @@ capture.output(test_that("avoid removing roxygen mask (see commit messages in #5
179175
text2
180176
)
181177
}))
178+
179+
180+
capture.output(test_that("partial caching of multiple expressions on one line works", {
181+
on.exit(clear_testthat_cache())
182+
clear_testthat_cache()
183+
activate_testthat_cache()
184+
text <- "1"
185+
style_text(text)
186+
text2 <- "1 # comment"
187+
styled <- style_text(text2)
188+
expect_equal(
189+
as.character(styled),
190+
text2
191+
)
192+
193+
style_text("mtcars")
194+
style_text(c("mtcars %>%", "f()"))
195+
final_text <- c("mtcars %>%", " f() #")
196+
expect_equal(
197+
as.character(style_text(final_text)),
198+
final_text
199+
)
200+
}))

0 commit comments

Comments
 (0)