Skip to content

Commit 4f6a890

Browse files
document and more tests
1 parent cdbe1bf commit 4f6a890

File tree

8 files changed

+56
-11
lines changed

8 files changed

+56
-11
lines changed

R/nest.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ add_cache_block <- function(pd_nested) {
6363
#' level token. For that
6464
#' reason, the nested parse table can, at the rows where these expressions are
6565
#' located, be shallow, i.e. it does not have to contain a child, because it
66-
#' will neither be transformed nor serialized anytime. This function drop all
66+
#' will neither be transformed nor serialized anytime. This function drops all
6767
#' associated tokens except the top-level token for such expressions, which will
6868
#' result in large speed improvements in [compute_parse_data_nested()] because
6969
#' nesting is expensive and will not be done for cached expressions.

R/relevel.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ relocate_eq_assign_nest <- function(pd) {
183183
#' Two assignment tokens `EQ_ASSIGN` belong to the same block if they are not
184184
#' separated by more than one token. Token between `EQ_ASSIGN` tokens belong
185185
#' to the `EQ_ASSIGN` token occurring before them, except the token right before
186-
#' `EQ_ASSING` already belongs to the `EQ_ASSING` after it.
186+
#' `EQ_ASSING` already belongs to the `EQ_ASSING` after it. Note that this
187+
#' notion is unrelated to the column *block* in the parse table, which is used
188+
#' to [parse_transform_serialize_r()] code blocks and leave out the ones that
189+
#' are cached.
187190
#' @param pd A parse table.
188191
#' @keywords internal
189192
find_block_id <- function(pd) {

R/transform-block.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,18 @@ cache_find_block <- function(pd) {
7979
#' @return The line number on which the first token occurs.
8080
#' @keywords internal
8181
find_blank_lines_to_next_expr <- function(pd_nested) {
82-
# TODO think about naming: prefix with cache here also or just ui functions?
8382
pd_nested$line1 - lag(pd_nested$line2, default = 0)
8483
}
8584

8685
#' Number of lines between cache blocks
8786
#'
8887
#' This is relevant when putting expressions together into a block and preserve
89-
#' blank lines between them.
88+
#' blank lines between them. Note that because code does not need to start on
89+
#' line 1, the first element of the output is the number of lines until the
90+
#' first block.
9091
#' @param pd A top level nest.
9192
find_blank_lines_to_next_block <- function(pd) {
9293
block_boundary <- pd$block != lag(pd$block, default = 0)
93-
# TODO everywhere: block is ambiguous. use cache block since we also have
94-
# block_id and other things in other places
9594
find_blank_lines_to_next_expr(pd)[block_boundary]
9695
}
9796

R/transform-files.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ make_transformer <- function(transformers,
9292
}
9393

9494
if (!use_cache) {
95-
# TODO just info: if the whole expression is in the cache, don't even
96-
# compute the parse data nested
9795
transformed_code <- text %>%
9896
parse_transform_serialize_r(transformers, warn_empty = warn_empty) %>%
9997
when(

man/drop_cached_children.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.

man/find_blank_lines_to_next_block.Rd

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

man/find_block_id.Rd

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

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
test_that("caching utils make right blocks with semi-colon", {
2+
23
blocks_simple_uncached <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>%
34
dplyr::mutate(is_cached = FALSE) %>%
45
cache_find_block()
@@ -15,6 +16,45 @@ test_that("caching utils make right blocks with semi-colon", {
1516
expect_equal(blocks_edge, c(1, 2, 2, 2))
1617
})
1718

19+
test_that("blank lines are correctly identified", {
20+
on.exit(clear_testthat_cache())
21+
clear_testthat_cache()
22+
activate_testthat_cache()
23+
text <- c(
24+
"1 + 1",
25+
"",
26+
"",
27+
"f(x)",
28+
"",
29+
"",
30+
"",
31+
"x < 3",
32+
"function() NULL"
33+
)
34+
# when not cached, all code in same block
35+
pd_nested <- compute_parse_data_nested(text, tidyverse_style())
36+
cache_by_expression(text, tidyverse_style())
37+
expect_equal(
38+
pd_nested$block, rep(1, 4)
39+
)
40+
41+
expect_equal(
42+
find_blank_lines_to_next_block(pd_nested),
43+
1
44+
)
45+
46+
# when partly cached, not all code in same block
47+
text[4] <- "f (x)"
48+
pd_nested <- compute_parse_data_nested(text, tidyverse_style())
49+
expect_equal(
50+
pd_nested$block, c(1, 2, 3, 3)
51+
)
52+
53+
expect_equal(
54+
find_blank_lines_to_next_block(pd_nested),
55+
c(1, 3, 4)
56+
)
57+
})
1858

1959
test_that("caching utils make right blocks with comments", {
2060
blocks_simple_uncached <- compute_parse_data_nested(c("1 + 1", "2 # comment")) %>%

0 commit comments

Comments
 (0)