Skip to content

Commit ac7e98f

Browse files
Merge pull request #764 from lorenzwalthert/issue-763
- Add base_indention for cached top-level expression (#763).
2 parents 28ca478 + 7a38af0 commit ac7e98f

File tree

5 files changed

+141
-3
lines changed

5 files changed

+141
-3
lines changed

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# styler 1.4.0.9000 (Development)
22

3-
* Hexadecimal integers now preserve the trailing `L` when styled (#761).
3+
* Fix interaction between cache and `base_indention` (#764).
44
* Add more examples to `styler_*` helpfiles (#762).
5+
* Hexadecimal integers now preserve the trailing `L` when styled (#761).
56
* Add a pre-push hook to make sure news bullets are added to each PR (#765).
67

8+
79
# styler 1.4.0
810

911
## API Changes

R/transform-block.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ parse_transform_serialize_r_block <- function(pd_nested,
4141
} else {
4242
serialized_transformed_text <- map2(
4343
c(0, find_blank_lines_to_next_expr(pd_nested)[-1] - 1L),
44-
pd_nested$text,
44+
paste0(rep_char(" ", base_indention), pd_nested$text),
4545
~ c(rep("", .x), .y)
4646
) %>%
4747
unlist()

R/utils-cache.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ cache_by_expression <- function(text,
155155
} else {
156156
expressions$stylerignore <- rep(FALSE, length(expressions$text))
157157
}
158+
# TODO base_indention should be set to 0 on write and on read for expressions
159+
# (only) to make it possible to use the cache for expressions with different
160+
# indention. when not the whole input text is cached, we go trough all expressions and
161+
# check if they are cached, if yes, we take the input (from which the indention
162+
# was removed via parse, same as it is in cache_by_expression) and add the
163+
# base indention.
158164
expressions[expressions$parent == 0 & expressions$token != "COMMENT" & !expressions$stylerignore, "text"] %>%
159165
map(~ cache_write(.x, transformers = transformers, more_specs))
160166
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ test_that("speedup higher when cached roxygen example code is multiple expressio
106106
)
107107
# the speed gain for longer expression is 1.1x higher
108108
expect_true(
109-
speedup_multiple_roygen_example / speedup_many_roygen_examples > 1.1
109+
speedup_multiple_roygen_example / speedup_many_roygen_examples > 1.05
110110
)
111111
})
112112

tests/testthat/test-cache-interaction-base-indention.R

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,136 @@ test_that("include_roxygen_exmples is respected in caching", {
2525
})
2626

2727

28+
test_that("expression caching when first expression does not comply", {
29+
on.exit(clear_testthat_cache())
30+
fresh_testthat_cache()
31+
more <- 'x<- 1
32+
"multi
33+
line string"
34+
c(a = 3)
35+
another(
36+
"x", y = 4
37+
)
38+
'
39+
expect_out <- c(
40+
" x <- 1",
41+
' "multi',
42+
'line string"',
43+
" c(a = 3)",
44+
" another(",
45+
' "x",',
46+
" y = 4",
47+
" )"
48+
)
49+
out <- style_text(more, base_indention = 3) %>%
50+
as.character()
51+
expect_equal(
52+
out,
53+
expect_out
54+
)
55+
out <- style_text(more, base_indention = 3) %>%
56+
as.character()
57+
expect_equal(
58+
out,
59+
expect_out
60+
)
61+
out <- style_text(more, base_indention = 4) %>%
62+
as.character()
63+
expect_equal(
64+
out,
65+
c(
66+
" x <- 1",
67+
' "multi',
68+
'line string"',
69+
" c(a = 3)",
70+
" another(",
71+
' "x",',
72+
" y = 4",
73+
" )"
74+
)
75+
)
76+
sg <- tidyverse_style()
77+
# TODO caching with base indention 3
78+
expect_true(
79+
is_cached("x <- 1", sg, more_specs = cache_more_specs(TRUE, 4))
80+
)
81+
82+
sg <- tidyverse_style()
83+
expect_true(
84+
is_cached("x <- 1", sg, more_specs = cache_more_specs(TRUE, 3))
85+
)
86+
})
87+
88+
test_that("expression caching when last expression does not comply", {
89+
on.exit(clear_testthat_cache())
90+
fresh_testthat_cache()
91+
more <- ' x <- 1
92+
"multi
93+
line string"
94+
c(a = 3)
95+
another(
96+
"x", y = 4)
97+
'
98+
expect_out <- c(
99+
" x <- 1",
100+
' "multi',
101+
'line string"',
102+
" c(a = 3)",
103+
" another(",
104+
' "x",',
105+
" y = 4",
106+
" )"
107+
)
108+
out <- style_text(more, base_indention = 3) %>%
109+
as.character()
110+
expect_equal(
111+
out,
112+
expect_out
113+
)
114+
out <- style_text(more, base_indention = 3) %>%
115+
as.character()
116+
expect_equal(
117+
out,
118+
expect_out
119+
)
120+
})
121+
122+
test_that("expression caching when middle expression does not comply", {
123+
on.exit(clear_testthat_cache())
124+
fresh_testthat_cache()
125+
more <- ' x <- 1
126+
"multi
127+
line string"
128+
c(a= 3)
129+
another(
130+
"x", y = 4
131+
)
132+
'
133+
expect_out <- c(
134+
" x <- 1",
135+
' "multi',
136+
'line string"',
137+
" c(a = 3)",
138+
" another(",
139+
' "x",',
140+
" y = 4",
141+
" )"
142+
)
143+
out <- style_text(more, base_indention = 3) %>%
144+
as.character()
145+
expect_equal(
146+
out,
147+
expect_out
148+
)
149+
out <- style_text(more, base_indention = 3) %>%
150+
as.character()
151+
expect_equal(
152+
out,
153+
expect_out
154+
)
155+
})
156+
157+
28158
test_that("cache is deactivated at end of caching related testthat file", {
29159
expect_false(cache_is_activated())
30160
})

0 commit comments

Comments
 (0)