Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: styler
Title: Non-Invasive Pretty Printing of R Code
Version: 1.7.0.9000
Version: 1.7.0.9001
Authors@R:
c(person(given = "Kirill",
family = "Müller",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* new R option `styler.ignore_alignment` controls if alignment should be
detected (and preserved) or not (#932).
* the cache is also invalidated on changing the stylerignore markers (#932).

# styler 1.7.0

* if `else` follows directly after `if`, line breaks are removed (#935).
Expand Down
3 changes: 2 additions & 1 deletion R/rules-spaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ set_space_around_op <- function(pd_flat, strict) {
}
if (sum(pd_flat$lag_newlines) > 2 &&
is_function_call(pd_flat) &&
any(pd_flat$token %in% c("EQ_SUB", "','"))
any(pd_flat$token %in% c("EQ_SUB", "','")) &&
!getOption("styler.ignore_alignment", FALSE)
) {
is_on_aligned_line <- token_is_on_aligned_line(pd_flat)
} else {
Expand Down
8 changes: 6 additions & 2 deletions R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ get_cache_dir <- function(cache_name = cache_get_name()) {
#' Syntactic sugar for creating more specs. This is useful when we want to add
#' more arguments (because we can search for this function in the source code).
#' @keywords internal
cache_more_specs <- function(include_roxygen_examples, base_indention) {
cache_more_specs <- function(include_roxygen_examples,
base_indention) {
list(
include_roxygen_examples = include_roxygen_examples,
base_indention = base_indention
base_indention = base_indention,
ignore_alignment = getOption("styler.ignore_alignment", FALSE),
ignore_start = getOption("styler.ignore_start", .default_ignore_start),
ignore_stop = getOption("styler.ignore_start", .default_ignore_stop)
)
}
8 changes: 6 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
.default_ignore_start <- "styler: off"
.default_ignore_stop <- "styler: on"

.onLoad <- function(libname, pkgname) {
op <- options()
op.styler <- list(
styler.addins_style_transformer = "styler::tidyverse_style()",
styler.cache_root = NULL,
styler.cache_name = styler_version,
styler.colored_print.vertical = TRUE,
styler.ignore_start = "styler: off",
styler.ignore_stop = "styler: on",
styler.ignore_alignment = FALSE,
styler.ignore_start = .default_ignore_start,
styler.ignore_stop = .default_ignore_stop,
styler.quiet = FALSE,
styler.test_dir_writable = TRUE
)
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-cache-interaction-more-specs.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ test_that("base_indention is respected in caching", {
)
})

test_that("ignore_alignment is respected in caching", {
local_test_setup(cache = TRUE)
text <- c("call(", " arxone = 1,", " tw3 = 2", ")")
text_without_alignment <- c("call(", " arxone = 1,", " tw3 = 2", ")")
with_detection <- style_text(text)
withr::local_options(styler.ignore_alignment = TRUE)
without_detection <- style_text(text)
expect_equal(
as.character(without_detection),
as.character(text_without_alignment)
)
expect_equal(cache_info(format = "tabular")$n, 2)
})

test_that("cache is deactivated at end of caching related testthat file", {
expect_false(cache_is_activated())
})
19 changes: 19 additions & 0 deletions tests/testthat/test-interaction-caching-stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ test_that("indention preserved in stylerignore when caching activated", {
)
})

test_that("changing ignore markers invalidates cache", {
opts <- list(
list(styler.ignore_stop = "noqua: stop", n = 1),
list(styler.ignore_start = "noqua: start", n = 3)
)
purrr::walk(opts, function(opt) {
local_test_setup(cache = TRUE)
text7 <- c(
"# styler: off",
"1 + 1",
"# styler: on"
)
style_text(text7)
rlang::exec(withr::local_options, !!!opt[-length(opt)])
style_text(text7)
expect_equal(cache_info(format = "tabular")$n, opt[["n"]])
})
})

test_that("cache is deactivated at end of caching related testthat file", {
expect_false(cache_is_activated())
})
24 changes: 24 additions & 0 deletions tests/testthat/test-public_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,27 @@ test_that("Can display warning on unset styler cache", {
withr::local_seed(7)
expect_silent(ask_to_switch_to_non_default_cache_root(ask = TRUE))
})


test_that("alignment detection can be turned off.", {
withr::local_options(
"styler.ignore_alignment" = TRUE,
"styler.colored_print.vertical" = FALSE
)
text_in <- paste0(
"call(\n",
" xb = 13,\n",
" t = 'a'\n",
")"
)
text_out <- c(
"call(",
" xb = 13,",
" t = \"a\"",
")"
)

expect_true(all(
style_text(text_in) == text_out
))
})