Skip to content

Commit eec45e4

Browse files
Merge pull request #932 from r-lib/issue-834
new R option to ignore alignment
2 parents 28d92df + 00cae2e commit eec45e4

8 files changed

+76
-6
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: styler
33
Title: Non-Invasive Pretty Printing of R Code
4-
Version: 1.7.0.9000
4+
Version: 1.7.0.9001
55
Authors@R:
66
c(person(given = "Kirill",
77
family = "Müller",

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* new R option `styler.ignore_alignment` controls if alignment should be
2+
detected (and preserved) or not (#932).
3+
* the cache is also invalidated on changing the stylerignore markers (#932).
4+
15
# styler 1.7.0
26

37
* if `else` follows directly after `if`, line breaks are removed (#935).

R/rules-spaces.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ set_space_around_op <- function(pd_flat, strict) {
1818
}
1919
if (sum(pd_flat$lag_newlines) > 2 &&
2020
is_function_call(pd_flat) &&
21-
any(pd_flat$token %in% c("EQ_SUB", "','"))
21+
any(pd_flat$token %in% c("EQ_SUB", "','")) &&
22+
!getOption("styler.ignore_alignment", FALSE)
2223
) {
2324
is_on_aligned_line <- token_is_on_aligned_line(pd_flat)
2425
} else {

R/utils-cache.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,13 @@ get_cache_dir <- function(cache_name = cache_get_name()) {
204204
#' Syntactic sugar for creating more specs. This is useful when we want to add
205205
#' more arguments (because we can search for this function in the source code).
206206
#' @keywords internal
207-
cache_more_specs <- function(include_roxygen_examples, base_indention) {
207+
cache_more_specs <- function(include_roxygen_examples,
208+
base_indention) {
208209
list(
209210
include_roxygen_examples = include_roxygen_examples,
210-
base_indention = base_indention
211+
base_indention = base_indention,
212+
ignore_alignment = getOption("styler.ignore_alignment", FALSE),
213+
ignore_start = getOption("styler.ignore_start", .default_ignore_start),
214+
ignore_stop = getOption("styler.ignore_start", .default_ignore_stop)
211215
)
212216
}

R/zzz.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
.default_ignore_start <- "styler: off"
2+
.default_ignore_stop <- "styler: on"
3+
14
.onLoad <- function(libname, pkgname) {
25
op <- options()
36
op.styler <- list(
47
styler.addins_style_transformer = "styler::tidyverse_style()",
58
styler.cache_root = NULL,
69
styler.cache_name = styler_version,
710
styler.colored_print.vertical = TRUE,
8-
styler.ignore_start = "styler: off",
9-
styler.ignore_stop = "styler: on",
11+
styler.ignore_alignment = FALSE,
12+
styler.ignore_start = .default_ignore_start,
13+
styler.ignore_stop = .default_ignore_stop,
1014
styler.quiet = FALSE,
1115
styler.test_dir_writable = TRUE
1216
)

tests/testthat/test-cache-interaction-more-specs.R

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ test_that("base_indention is respected in caching", {
1010
)
1111
})
1212

13+
test_that("ignore_alignment is respected in caching", {
14+
local_test_setup(cache = TRUE)
15+
text <- c("call(", " arxone = 1,", " tw3 = 2", ")")
16+
text_without_alignment <- c("call(", " arxone = 1,", " tw3 = 2", ")")
17+
with_detection <- style_text(text)
18+
withr::local_options(styler.ignore_alignment = TRUE)
19+
without_detection <- style_text(text)
20+
expect_equal(
21+
as.character(without_detection),
22+
as.character(text_without_alignment)
23+
)
24+
expect_equal(cache_info(format = "tabular")$n, 2)
25+
})
26+
1327
test_that("cache is deactivated at end of caching related testthat file", {
1428
expect_false(cache_is_activated())
1529
})

tests/testthat/test-interaction-caching-stylerignore.R

+19
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ test_that("indention preserved in stylerignore when caching activated", {
168168
)
169169
})
170170

171+
test_that("changing ignore markers invalidates cache", {
172+
opts <- list(
173+
list(styler.ignore_stop = "noqua: stop", n = 1),
174+
list(styler.ignore_start = "noqua: start", n = 3)
175+
)
176+
purrr::walk(opts, function(opt) {
177+
local_test_setup(cache = TRUE)
178+
text7 <- c(
179+
"# styler: off",
180+
"1 + 1",
181+
"# styler: on"
182+
)
183+
style_text(text7)
184+
rlang::exec(withr::local_options, !!!opt[-length(opt)])
185+
style_text(text7)
186+
expect_equal(cache_info(format = "tabular")$n, opt[["n"]])
187+
})
188+
})
189+
171190
test_that("cache is deactivated at end of caching related testthat file", {
172191
expect_false(cache_is_activated())
173192
})

tests/testthat/test-public_api.R

+24
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,27 @@ test_that("Can display warning on unset styler cache", {
496496
withr::local_seed(7)
497497
expect_silent(ask_to_switch_to_non_default_cache_root(ask = TRUE))
498498
})
499+
500+
501+
test_that("alignment detection can be turned off.", {
502+
withr::local_options(
503+
"styler.ignore_alignment" = TRUE,
504+
"styler.colored_print.vertical" = FALSE
505+
)
506+
text_in <- paste0(
507+
"call(\n",
508+
" xb = 13,\n",
509+
" t = 'a'\n",
510+
")"
511+
)
512+
text_out <- c(
513+
"call(",
514+
" xb = 13,",
515+
" t = \"a\"",
516+
")"
517+
)
518+
519+
expect_true(all(
520+
style_text(text_in) == text_out
521+
))
522+
})

0 commit comments

Comments
 (0)