Skip to content

Commit b302ec1

Browse files
Merge pull request #585 from lorenzwalthert/issue-584
- Fix caching (#585).
2 parents 9999659 + 885ecc9 commit b302ec1

File tree

9 files changed

+108
-20
lines changed

9 files changed

+108
-20
lines changed

R/nest.R

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ add_cache_block <- function(pd_nested) {
3939
if (cache_is_activated()) {
4040
pd_nested$block <- cache_find_block(pd_nested)
4141
} else {
42-
pd_nested$block <- rep(1, nrow(pd_nested))
42+
pd_nested$block <- rep(1, length(pd_nested$block))
4343
}
4444
pd_nested
4545
}
@@ -101,16 +101,27 @@ drop_cached_children <- function(pd) {
101101
} else {
102102
pd
103103
}
104-
105104
}
106105

106+
#' Find the pos ids to keep
107+
#'
108+
#' To make a parse table shallow, we must know which ids to keep.
109+
#' `split(cumsum(pd_parent_first$parent < 1))` above puts comments with negative
110+
#' parents in the same block as proceeding expressions. `find_pos_id_to_keep()`
111+
#' must hence alyways keep comments. We did not use
112+
#' `split(cumsum(pd_parent_first$parent < 1))` because then every comment is an
113+
#' expression on its own and processing takes much longer for typical roxygen
114+
#' annotated code
115+
#' @param pd A temporary top level nest where the first expression is always a
116+
#' top level expression, potentially cached.
117+
#' @keywords internal
107118
find_pos_id_to_keep <- function(pd) {
108-
if (pd$is_cached[1]) {
109-
pd$pos_id[1]
110-
} else {
111-
pd$pos_id
112-
}
119+
if (pd$is_cached[1]) {
120+
pd$pos_id[c(TRUE, pd[-1L, "token"] == "COMMENT")]
121+
} else {
122+
pd$pos_id
113123
}
124+
}
114125

115126

116127
#' Turn off styling for parts of the code
@@ -235,9 +246,10 @@ add_terminal_token_before <- function(pd_flat) {
235246
add_attributes_caching <- function(pd_flat, transformers) {
236247
pd_flat$block <- pd_flat$is_cached <- rep(NA, nrow(pd_flat))
237248
if (cache_is_activated()) {
238-
pd_flat$is_cached[pd_flat$parent == 0] <- map_lgl(
249+
is_parent <- pd_flat$parent == 0
250+
pd_flat$is_cached[is_parent] <- map_lgl(
239251
pd_flat$text[pd_flat$parent == 0],
240-
is_cached, transformers, cache_dir_default()
252+
is_cached, transformers
241253
)
242254
is_comment <- pd_flat$token == "COMMENT"
243255
pd_flat$is_cached[is_comment] <- rep(FALSE, sum(is_comment))

R/roxygen-examples.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ style_roxygen_example_snippet <- function(code_snippet,
6868
mask <- decomposed$mask
6969
}
7070
code_snippet <- post_parse_roxygen(code_snippet) %>%
71-
paste0(collapse = "\n") %>%
7271
parse_transform_serialize_r(transformers, warn_empty = FALSE)
7372

7473
if (is_dont) {

R/serialize.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
serialize_parse_data_flattened <- function(flattened_pd) {
77
flattened_pd <- apply_stylerignore(flattened_pd)
88
flattened_pd$lag_newlines[1] <- 0 # resolve start_line elsewhere
9-
res <- with(
9+
with(
1010
flattened_pd,
1111
paste0(
1212
collapse = "",
1313
map(lag_newlines, add_newlines), map(lag_spaces, add_spaces), text
1414
)
1515
)
16-
convert_newlines_to_linebreaks(res)
1716
}

R/transform-files.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ parse_transform_serialize_r <- function(text,
212212
if (can_verify_roundtrip(transformers)) {
213213
verify_roundtrip(text, text_out)
214214
}
215-
215+
text_out <- convert_newlines_to_linebreaks(text_out)
216216
if (cache_is_activated()) {
217217
cache_by_expression(text_out, transformers)
218218
}

R/zzz.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
op <- options()
44
op.styler <- list(
55
styler.colored_print.vertical = TRUE,
6-
styler.cache_name = NULL,
6+
styler.cache_name = styler_version,
77
styler.addins_style_transformer = "styler::tidyverse_style()",
88
styler.ignore_start = "# styler: off",
99
styler.ignore_stop = "# styler: on"
1010
)
11-
rlang::warn("Caching feature temporarily disabled and not encouraged to use. See https://github.com/r-lib/styler/issues/584")
1211
toset <- !(names(op.styler) %in% names(op))
1312
if (any(toset)) options(op.styler[toset])
1413
invisible()

man/find_pos_id_to_keep.Rd

Lines changed: 22 additions & 0 deletions
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: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,67 @@ capture.output(
8282

8383
capture.output(test_that("unactivated cache does not bring speedup", {
8484
skip_if_not_installed("R.cache")
85-
on.exit(clear_testthat_cache)
85+
on.exit(clear_testthat_cache())
8686
clear_testthat_cache()
8787
cache_deactivate()
8888
first <- system.time(styler::style_file(test_path("reference-objects/caching.R")))
8989
second <- system.time(styler::style_file(test_path("reference-objects/caching.R")))
9090
expect_false(first["elapsed"] / 2 > second["elapsed"])
9191
}))
92+
93+
94+
capture.output(test_that("avoid deleting comments #584 (see commit messages)", {
95+
96+
skip_if_not_installed("R.cache")
97+
on.exit(clear_testthat_cache())
98+
clear_testthat_cache()
99+
activate_testthat_cache()
100+
text <- c(
101+
"1 + 1",
102+
"# Comment",
103+
"# another",
104+
"NULL"
105+
)
106+
style_text(text)
107+
text2 <- c(
108+
"1 + 1",
109+
"# x",
110+
"# another",
111+
"NULL"
112+
)
113+
expect_equal(
114+
as.character(style_text(text2)),
115+
text2
116+
)
117+
}))
118+
119+
120+
capture.output(test_that("avoid removing roxygen mask (see commit messages in #584)", {
121+
122+
skip_if_not_installed("R.cache")
123+
on.exit(clear_testthat_cache())
124+
clear_testthat_cache()
125+
activate_testthat_cache()
126+
text <- c(
127+
"c(",
128+
" 1, 2,",
129+
" x - 2",
130+
")"
131+
)
132+
style_text(text)
133+
text2 <- c(
134+
"#' Stuff",
135+
"#'",
136+
"#' @examples",
137+
"#' c(",
138+
"#' 1, 2,",
139+
"#' x - 2",
140+
"#' )",
141+
"#' x",
142+
"NULL"
143+
)
144+
expect_equal(
145+
as.character(style_text(text2)),
146+
text2
147+
)
148+
}))

tests/testthat/test-cache-with-r-cache.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ capture.output(test_that("Cache management works when R.cache is installed", {
3838
)
3939
expect_false(cache_info(format = "tabular")$activated)
4040
expect_equal(getOption("styler.cache_location"), NULL)
41-
expect_error(cache_clear(ask = FALSE), NA)
41+
expect_error(cache_clear("testthat", ask = FALSE), NA)
4242
}))
4343

4444
test_that("top-level test: Caches top-level expressions efficiently on style_text()", {
@@ -56,7 +56,7 @@ test_that("top-level test: Caches top-level expressions efficiently on style_tex
5656
cache_deactivate()
5757
not_cached_benchmark <- system.time(style_text(text_styled))
5858
expect_lt(
59-
partially_cached_benchmark['elapsed'] * 5,
59+
partially_cached_benchmark['elapsed'] * 3,
6060
not_cached_benchmark['elapsed']
6161
)
6262
})

tests/testthat/test-cache-without-r-cache.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
test_that("Cache management fails mostly when R.cache is not installed", {
22
skip_if(rlang::is_installed("R.cache"))
33
expect_error(cache_info(), "is needed when the caching feature is activated")
4-
expect_error(cache_activate(), "is needed when the caching feature is activated")
5-
expect_error(cache_clear(), "is needed when the caching feature is activated")
4+
expect_error(activate_testthat_cache(), "is needed when the caching feature is activated")
5+
expect_error(cache_clear("testthat"), "is needed when the caching feature is activated")
66
expect_error(capture.output(cache_deactivate()), NA)
77
expect_silent(assert_R.cache_installation())
88
expect_error(

0 commit comments

Comments
 (0)