Skip to content

Commit ad8085d

Browse files
find correct block in example where there first_on_line_idx is not a sequence from 1 to n.
1 parent 2fce44c commit ad8085d

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

R/nest.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ add_cache_block <- function(pd_nested) {
4646

4747
#' Drop all children of a top level expression that are cached
4848
#'
49-
#' Note that we do cache top-level comments. Because package code has a lot of
50-
#' roxygen comments and each of them is a top level expresion, checking is
51-
#' very expensive.
49+
#' Note that we do not cache top-level comments. Because package code has a lot
50+
#' of roxygen comments and each of them is a top level expresion, checking is
51+
#' very expensive. More expensive than styling, because comments are always
52+
#' terminals.
5253
#' @param pd A top-level nest.
5354
#' @details
5455
#' Because we process in blocks of expressions for speed, a cached expression

R/transform-block.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ cache_find_block <- function(pd) {
6060

6161
first_on_line_idx <- which(!not_first_on_line)
6262
valid_replacements <- map_int(invalid_turning_point_idx, function(x) {
63-
last(which(x > first_on_line_idx))
63+
first_on_line_idx[last(which(x > first_on_line_idx))]
6464
})
6565
sort(unique(c(
6666
setdiff(which(first_after_cache_state_switch), invalid_turning_point_idx),

man/drop_cached_children.Rd

Lines changed: 4 additions & 3 deletions
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,48 @@ test_that("caching utils make right blocks with semi-colon", {
1616
expect_equal(blocks_edge, c(1, 2, 2, 2))
1717
})
1818

19+
test_that("caching utils make right blocks with comments", {
20+
text <- '
21+
### comment
22+
x = 1 ### comment
23+
y = 2 # comment
24+
x<-1 ###comment
25+
y <- 2 # comment
26+
"a string here"
27+
28+
# something something
29+
tau1 = 1 # here?
30+
'
31+
32+
33+
blocks_simple_uncached <- compute_parse_data_nested(text) %>%
34+
dplyr::mutate(is_cached = c(
35+
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE,
36+
TRUE, FALSE, FALSE, FALSE)
37+
) %>%
38+
cache_find_block()
39+
expect_equal(blocks_simple_uncached, c(1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4, 4, 4))
40+
41+
text <- '
42+
### comment
43+
x = 1
44+
y = 2 # comment
45+
x<-1
46+
y <- 2 # comment
47+
48+
# something something
49+
tau1 = 1 # here?
50+
'
51+
blocks_simple_cached <- compute_parse_data_nested(text) %>%
52+
dplyr::mutate(is_cached = c(
53+
FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE)
54+
) %>%
55+
cache_find_block()
56+
expect_equal(blocks_simple_cached, c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2))
57+
58+
})
59+
60+
1961
test_that("blank lines are correctly identified", {
2062
on.exit(clear_testthat_cache())
2163
clear_testthat_cache()

0 commit comments

Comments
 (0)