Skip to content

Commit ebbf3c1

Browse files
Merge pull request #647 from lorenzwalthert/issue-646
Invalidate cache on style guide argument change
2 parents 9c5d883 + cc8e40e commit ebbf3c1

File tree

8 files changed

+60
-14
lines changed

8 files changed

+60
-14
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)
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)
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/style-guides.R

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ tidyverse_style <- function(scope = "tokens",
5757
start_comments_with_one_space = FALSE,
5858
reindention = tidyverse_reindention(),
5959
math_token_spacing = tidyverse_math_token_spacing()) {
60+
args <- as.list(environment())
6061
scope <- character_to_ordered(
6162
scope,
6263
c("none", "spaces", "indention", "line_breaks", "tokens")
@@ -175,7 +176,8 @@ tidyverse_style <- function(scope = "tokens",
175176
use_raw_indention = use_raw_indention,
176177
reindention = reindention,
177178
style_guide_name = style_guide_name,
178-
style_guide_version = styler_version
179+
style_guide_version = styler_version,
180+
more_specs = args
179181
)
180182
}
181183

@@ -184,8 +186,12 @@ tidyverse_style <- function(scope = "tokens",
184186
#' This is a helper function to create a style guide, which is technically
185187
#' speaking a named list of groups of transformer functions where each
186188
#' transformer function corresponds to one styling rule. The output of this
187-
#' function can be used as an argument for \code{style} in top level functions
188-
#' like [style_text()] and friends.
189+
#' function can be used as an argument for `style` in top level functions
190+
#' like [style_text()] and friends. Note that for caching to work properly,
191+
#' unquote all inputs to the transformer function if possible with rlang's `!!`,
192+
#' otherwise, they will be passed as references (generic variable names) instead
193+
#' of literals and `styler:::is_cached()` won't pick up changes. See how it's
194+
#' done in [tidyverse_style()] with `indent_by` and other arguments.
189195
#' @param initialize The bare name of a function that initializes various
190196
#' variables on each level of nesting.
191197
#' @param line_break A list of transformer functions that manipulate line_break
@@ -207,6 +213,12 @@ tidyverse_style <- function(scope = "tokens",
207213
#' attribute inside the created style guide, for example for caching. This
208214
#' should correspond to the version of the R package that exports the
209215
#' 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
218+
#' such functions are converted to characters in [styler::cache_make_key()],
219+
#' they will yield generic code and we loose the specific value of `arg` (see
220+
#' [styler::cache_make_key()]), even when unquoting these inputs with `!!`
221+
#' beforehand in `purrr::partial()`.
210222
#' @examples
211223
#' set_line_break_before_curly_opening <- function(pd_flat) {
212224
#' op <- pd_flat$token %in% "'{'"
@@ -232,7 +244,8 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
232244
use_raw_indention = FALSE,
233245
reindention = tidyverse_reindention(),
234246
style_guide_name = NULL,
235-
style_guide_version = NULL) {
247+
style_guide_version = NULL,
248+
more_specs = NULL) {
236249
lst(
237250
# transformer functions
238251
initialize = lst(initialize),
@@ -244,7 +257,8 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
244257
use_raw_indention,
245258
reindention,
246259
style_guide_name,
247-
style_guide_version
260+
style_guide_version,
261+
more_specs
248262
) %>%
249263
map(compact)
250264
}

R/testing-public-api.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ test_dry <- function(path, styler, styled = FALSE) {
5151
testthat::expect_true(identical(before, readLines(path)))
5252

5353
if (styled) {
54-
expect_error(styler(path, dry = "fail"), NA)
54+
testthat::expect_error(styler(path, dry = "fail"), NA)
5555
} else {
56-
expect_error(styler(path, dry = "fail"), "would be modified")
56+
testthat::expect_error(styler(path, dry = "fail"), "would be modified")
5757
}
58-
expect_error(styler(path, dry = "other option"), "one of")
58+
testthat::expect_error(styler(path, dry = "other option"), "one of")
5959
}

R/utils-cache.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ is_cached <- function(text, transformers, cache_dir = cache_dir_default()) {
5252
#' is to replace a transformer with the same function body (but changing
5353
#' the function definition of the functions called in that body) interactively
5454
#' without changing version number of name at the same time.
55+
#' Remaining problem: `purrr::partial()` calls will render generic code, e.g.
56+
#' see `as.character(list(purrr::partial(sum, x = 4)))`. For that reason,
57+
#' all arguments passed to a `purrr::partial()` call must be put in the
58+
#' style guide under `more_specs`.
5559
#' @section Experiments:
5660
#'
5761
#' There is unexplainable behavior in conjunction with hashing and
@@ -87,7 +91,9 @@ cache_make_key <- function(text, transformers) {
8791
text = hash_standardize(text),
8892
style_guide_name = transformers$style_guide_name,
8993
style_guide_version = transformers$style_guide_version,
90-
style_guide_text = as.character(transformers)
94+
style_guide_text = as.character(transformers),
95+
more_specs = as.character(transformers$more_specs) %>%
96+
set_names(names(transformers$more_specs))
9197
)
9298
}
9399

inst/WORDLIST

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cancelling
1212
chnages
1313
CMD
1414
codecov
15+
coercible
1516
compat
1617
config
1718
CONST
@@ -28,6 +29,7 @@ dplyr
2829
DSLs
2930
emacs
3031
emph
32+
env
3133
EOF
3234
EOL
3335
EOLs
@@ -38,10 +40,12 @@ fileext
3840
filetype
3941
forcond
4042
funct
43+
getChecksum
4144
getOption
4245
github
4346
gitsum
4447
GSOC
48+
href
4549
http
4650
https
4751
infinitively
@@ -51,13 +55,15 @@ interaces
5155
invasiveness
5256
io
5357
ixmypi
58+
ized
5459
Kirill
5560
knitr
5661
labelled
5762
levelName
5863
LF
5964
lifecycle
6065
lorenzwalthert
66+
lst
6167
macOS
6268
md
6369
Müller
@@ -75,6 +81,8 @@ precommit
7581
prefill
7682
prettycode
7783
PRs
84+
purrr
85+
purrrpartial
7886
RcppExports
7987
rds
8088
readme
@@ -88,6 +96,7 @@ renv
8896
repo
8997
reprex
9098
rlang
99+
rlang's
91100
rmarkdown
92101
RMarkdown
93102
rmd
@@ -105,6 +114,7 @@ RStudio
105114
RStudio's
106115
saamwerk
107116
seealso
117+
stackoverflow
108118
StackOverflow
109119
STR
110120
styler

man/cache_make_key.Rd

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

man/create_style_guide.Rd

Lines changed: 14 additions & 2 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ capture.output(test_that("no speedup when tranformer changes", {
117117
fresh_testthat_cache()
118118
t1 <- tidyverse_style()
119119
first <- system.time(style_text(text, transformers = t1))
120-
t1$use_raw_indention <- !t1$use_raw_indention
120+
t1 <- tidyverse_style(indent_by = 4)
121121
second <- system.time(style_text(text, transformers = t1))
122-
expect_false(first["elapsed"] / 2 > second["elapsed"])
122+
expect_false(first["elapsed"] / 1.2 > second["elapsed"])
123123
}))
124124

125125

0 commit comments

Comments
 (0)