Skip to content

Commit 39e901f

Browse files
Merge pull request #655 from lorenzwalthert/issue-641
- correct cache invalidation for include_roxygen_examples and base_indention (#655).
2 parents ca437ba + 664fe55 commit 39e901f

27 files changed

+253
-81
lines changed

API

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cache_activate(cache_name = NULL, verbose = TRUE)
66
cache_clear(cache_name = NULL, ask = TRUE)
77
cache_deactivate(verbose = TRUE)
88
cache_info(cache_name = NULL, format = "both")
9-
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention(), style_guide_name = NULL, style_guide_version = NULL, more_specs = NULL)
9+
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention(), style_guide_name = NULL, style_guide_version = NULL, more_specs_style_guide = NULL)
1010
default_style_guide_attributes(pd_flat)
1111
specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'"))
1212
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)

R/nest.R

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
#'
33
#' Parses `text` to a flat parse table and subsequently changes its
44
#' representation into a nested parse table with [nest_parse_data()].
5-
#' @param text A character vector to parse.
5+
#' @inheritParams text_to_flat_pd
66
#' @return A nested parse table. See [tokenize()] for details on the columns
77
#' of the parse table.
88
#' @importFrom purrr when
99
#' @keywords internal
1010
compute_parse_data_nested <- function(text,
11-
transformers) {
12-
parse_data <- text_to_flat_pd(text, transformers)
11+
transformers,
12+
more_specs) {
13+
parse_data <- text_to_flat_pd(text, transformers, more_specs = more_specs)
1314
env_add_stylerignore(parse_data)
1415
parse_data$child <- rep(list(NULL), length(parse_data$text))
1516
pd_nested <- parse_data %>%
@@ -37,12 +38,12 @@ compute_parse_data_nested <- function(text,
3738
#' Note that the parse table might be shallow if caching is enabled and some
3839
#' values are cached.
3940
#' @keywords internal
40-
text_to_flat_pd <- function(text, transformers) {
41+
text_to_flat_pd <- function(text, transformers, more_specs) {
4142
tokenize(text) %>%
4243
add_terminal_token_before() %>%
4344
add_terminal_token_after() %>%
4445
add_stylerignore() %>%
45-
add_attributes_caching(transformers) %>%
46+
add_attributes_caching(transformers, more_specs = more_specs) %>%
4647
drop_cached_children()
4748
}
4849

@@ -274,18 +275,18 @@ add_terminal_token_before <- function(pd_flat) {
274275
#' [default_style_guide_attributes()] (when using [tidyverse_style()]) because
275276
#' for cached code, we don't build up the nested structure and leave it shallow
276277
#' (to speed up things), see also [drop_cached_children()].
277-
#' @param transformers A list with transformer functions, used to check if
278-
#' the code is cached.
278+
#' @inheritParams is_cached
279279
#' @describeIn add_token_terminal Initializes `newlines` and `lag_newlines`.
280280
#' @keywords internal
281-
add_attributes_caching <- function(pd_flat, transformers) {
281+
add_attributes_caching <- function(pd_flat, transformers, more_specs) {
282282
pd_flat$block <- rep(NA, nrow(pd_flat))
283283
pd_flat$is_cached <- rep(FALSE, nrow(pd_flat))
284284
if (cache_is_activated()) {
285285
is_parent <- pd_flat$parent == 0
286286
pd_flat$is_cached[is_parent] <- map_lgl(
287287
pd_flat$text[pd_flat$parent == 0],
288-
is_cached, transformers
288+
is_cached, transformers,
289+
more_specs = more_specs
289290
)
290291
is_comment <- pd_flat$token == "COMMENT"
291292
pd_flat$is_cached[is_comment] <- rep(FALSE, sum(is_comment))

R/roxygen-examples.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ style_roxygen_example_snippet <- function(code_snippet,
7777
code_snippet <- post_parse_roxygen(code_snippet)
7878

7979
cache_is_active <- cache_is_activated()
80-
is_cached <- is_cached(code_snippet, transformers)
80+
is_cached <- is_cached(
81+
code_snippet, transformers,
82+
cache_more_specs(
83+
include_roxygen_examples = TRUE,
84+
base_indention = base_indention
85+
)
86+
)
8187
if (!is_cached || !cache_is_active) {
8288
code_snippet <- code_snippet %>%
8389
parse_transform_serialize_r(transformers,
@@ -88,7 +94,12 @@ style_roxygen_example_snippet <- function(code_snippet,
8894
}
8995

9096
if (!is_cached && cache_is_active) {
91-
cache_write(code_snippet, transformers)
97+
cache_write(
98+
code_snippet, transformers,
99+
cache_more_specs(
100+
include_roxygen_examples = TRUE, base_indention = base_indention
101+
)
102+
)
92103
}
93104

94105
if (is_dont) {

R/style-guides.R

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,17 @@ tidyverse_style <- function(scope = "tokens",
167167
style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib"
168168
create_style_guide(
169169
# transformer functions
170-
initialize = default_style_guide_attributes,
171-
line_break = line_break_manipulators,
172-
space = space_manipulators,
173-
token = token_manipulators,
174-
indention = indention_modifier,
170+
initialize = default_style_guide_attributes,
171+
line_break = line_break_manipulators,
172+
space = space_manipulators,
173+
token = token_manipulators,
174+
indention = indention_modifier,
175175
# transformer options
176-
use_raw_indention = use_raw_indention,
177-
reindention = reindention,
178-
style_guide_name = style_guide_name,
179-
style_guide_version = styler_version,
180-
more_specs = args
176+
use_raw_indention = use_raw_indention,
177+
reindention = reindention,
178+
style_guide_name = style_guide_name,
179+
style_guide_version = styler_version,
180+
more_specs_style_guide = args
181181
)
182182
}
183183

@@ -213,8 +213,9 @@ tidyverse_style <- function(scope = "tokens",
213213
#' attribute inside the created style guide, for example for caching. This
214214
#' should correspond to the version of the R package that exports the
215215
#' style guide.
216-
#' @param more_specs Named vector (coercible to character) specifying arguments
217-
#' `args` in `transformer <- list(t1 = purrr::partial(f, arg)` because when
216+
#' @param more_specs_style_guide Named vector (coercible to character)
217+
#' specifying arguments `args` in
218+
#' `transformer <- list(t1 = purrr::partial(f, arg)` because when
218219
#' such functions are converted to characters in [styler::cache_make_key()],
219220
#' they will yield generic code and we loose the specific value of `arg` (see
220221
#' [styler::cache_make_key()]), even when unquoting these inputs with `!!`
@@ -245,7 +246,7 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
245246
reindention = tidyverse_reindention(),
246247
style_guide_name = NULL,
247248
style_guide_version = NULL,
248-
more_specs = NULL) {
249+
more_specs_style_guide = NULL) {
249250
lst(
250251
# transformer functions
251252
initialize = lst(initialize),
@@ -258,7 +259,7 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
258259
reindention,
259260
style_guide_name,
260261
style_guide_version,
261-
more_specs
262+
more_specs_style_guide
262263
) %>%
263264
map(compact)
264265
}

R/testing.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,7 @@ fresh_testthat_cache <- function() {
337337
clear_testthat_cache()
338338
activate_testthat_cache()
339339
}
340+
341+
cache_more_specs_default <- function() {
342+
cache_more_specs(include_roxygen_examples = TRUE, base_indention = 0)
343+
}

R/transform-files.R

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ make_transformer <- function(transformers,
9292
should_use_cache <- cache_is_activated()
9393

9494
if (should_use_cache) {
95-
use_cache <- is_cached(text, transformers)
95+
use_cache <- is_cached(
96+
text, transformers,
97+
cache_more_specs(
98+
include_roxygen_examples = include_roxygen_examples,
99+
base_indention = base_indention
100+
)
101+
)
96102
} else {
97103
use_cache <- FALSE
98104
}
@@ -111,7 +117,10 @@ make_transformer <- function(transformers,
111117
~.
112118
)
113119
if (should_use_cache) {
114-
cache_write(transformed_code, transformers)
120+
cache_write(
121+
transformed_code, transformers,
122+
cache_more_specs(include_roxygen_examples, base_indention)
123+
)
115124
}
116125
transformed_code
117126
} else {
@@ -202,9 +211,12 @@ parse_transform_serialize_r <- function(text,
202211
transformers,
203212
base_indention,
204213
warn_empty = TRUE) {
205-
text <- assert_text(text)
206-
pd_nested <- compute_parse_data_nested(text, transformers)
214+
more_specs <- cache_more_specs(
215+
include_roxygen_examples = TRUE, base_indention = base_indention
216+
)
207217

218+
text <- assert_text(text)
219+
pd_nested <- compute_parse_data_nested(text, transformers, more_specs)
208220
blank_lines_to_next_expr <- find_blank_lines_to_next_block(pd_nested)
209221
if (nrow(pd_nested) == 0) {
210222
if (warn_empty) {
@@ -228,7 +240,7 @@ parse_transform_serialize_r <- function(text,
228240
}
229241
text_out <- convert_newlines_to_linebreaks(text_out)
230242
if (cache_is_activated()) {
231-
cache_by_expression(text_out, transformers)
243+
cache_by_expression(text_out, transformers, more_specs = more_specs)
232244
}
233245
text_out
234246
}

R/utils-cache.R

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ hash_standardize <- function(text) {
1616
#' Check if text is cached
1717
#'
1818
#' This boils down to check if the hash exists at the caching dir as a file.
19-
#' @param text,transformers Passed to [cache_make_key()] to generate a key.
19+
#' @param text,transformers,more_specs Passed to [cache_make_key()] to generate
20+
#' a key.
2021
#' @param cache_dir The caching directory relative to the `.Rcache` root to
2122
#' look for a cached value.
2223
#' @keywords internal
23-
is_cached <- function(text, transformers, cache_dir = cache_dir_default()) {
24+
is_cached <- function(text,
25+
transformers,
26+
more_specs,
27+
cache_dir = cache_dir_default()) {
2428
R.cache::generateCache(
25-
key = cache_make_key(text, transformers),
29+
key = cache_make_key(text, transformers, more_specs),
2630
dirs = cache_dir
2731
) %>%
2832
file.exists()
@@ -37,6 +41,9 @@ is_cached <- function(text, transformers, cache_dir = cache_dir_default()) {
3741
#' @param transformers A list of transformer functions, because we can only
3842
#' know if text is already correct if we know which transformer function it
3943
#' should be styled with.
44+
#' @param more_args A named vector coercible to it character that determine the
45+
#' styling but are style guide independent, such as `include_roxygen_examples`
46+
#' or `base_indention`.
4047
#' @details
4148
#' We need to compare:
4249
#'
@@ -55,7 +62,7 @@ is_cached <- function(text, transformers, cache_dir = cache_dir_default()) {
5562
#' Remaining problem: `purrr::partial()` calls will render generic code, e.g.
5663
#' see `as.character(list(purrr::partial(sum, x = 4)))`. For that reason,
5764
#' all arguments passed to a `purrr::partial()` call must be put in the
58-
#' style guide under `more_specs`.
65+
#' style guide under `more_specs_style_guide`.
5966
#' @section Experiments:
6067
#'
6168
#' There is unexplainable behavior in conjunction with hashing and
@@ -86,13 +93,14 @@ is_cached <- function(text, transformers, cache_dir = cache_dir_default()) {
8693
#' identical(digest::digest(add1), digest::digest(add2))
8794
#' identical(digest::digest(styler::tidyverse_style()), digest::digest(styler::tidyverse_style()))
8895
#' @keywords internal
89-
cache_make_key <- function(text, transformers) {
96+
cache_make_key <- function(text, transformers, more_specs) {
9097
list(
9198
text = hash_standardize(text),
9299
style_guide_name = transformers$style_guide_name,
93100
style_guide_version = transformers$style_guide_version,
94-
more_specs = as.character(transformers$more_specs) %>%
95-
set_names(names(transformers$more_specs))
101+
more_specs_style_guide = as.character(transformers$more_specs_style_guide) %>%
102+
set_names(names(transformers$more_specs_style_guide)),
103+
more_specs = more_specs
96104
)
97105
}
98106

@@ -134,9 +142,11 @@ cache_is_activated <- function(cache_name = NULL) {
134142
#' guide outside a stylerignore sequence and wrongly think we should leave it as
135143
#' is.
136144
#' @param text A character vector with one or more expressions.
137-
#' @param transformers The transformers.
145+
#' @inheritParams cache_write
138146
#' @keywords internal
139-
cache_by_expression <- function(text, transformers) {
147+
cache_by_expression <- function(text,
148+
transformers,
149+
more_specs) {
140150
expressions <- parse(text = text, keep.source = TRUE) %>%
141151
utils::getParseData(includeText = TRUE)
142152
if (env_current$any_stylerignore) {
@@ -146,13 +156,17 @@ cache_by_expression <- function(text, transformers) {
146156
expressions$stylerignore <- rep(FALSE, length(expressions$text))
147157
}
148158
expressions[expressions$parent == 0 & expressions$token != "COMMENT" & !expressions$stylerignore, "text"] %>%
149-
map(~ cache_write(.x, transformers = transformers))
159+
map(~ cache_write(.x, transformers = transformers, more_specs))
150160
}
151161

152162

153-
cache_write <- function(text, transformers) {
163+
#' Write to the cache
164+
#'
165+
#' @inheritParams cache_make_key
166+
#' @keywords internal
167+
cache_write <- function(text, transformers, more_specs) {
154168
R.cache::generateCache(
155-
key = cache_make_key(text, transformers),
169+
key = cache_make_key(text, transformers, more_specs),
156170
dirs = cache_dir_default()
157171
) %>%
158172
file.create()
@@ -177,3 +191,16 @@ cache_get_or_derive_name <- function(cache_name) {
177191
cache_dir_default <- function() {
178192
c("styler", cache_get_name())
179193
}
194+
195+
196+
#' Create more specs
197+
#'
198+
#' Syntactic suggar for creating more specs. This is useful when we want to add
199+
#' more arguments (because we can search for this function in the source code).
200+
#' @keywords internal
201+
cache_more_specs <- function(include_roxygen_examples, base_indention) {
202+
list(
203+
include_roxygen_examples = include_roxygen_examples,
204+
base_indention = base_indention
205+
)
206+
}

man/add_token_terminal.Rd

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

man/cache_by_expression.Rd

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

man/cache_make_key.Rd

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

0 commit comments

Comments
 (0)