diff --git a/DESCRIPTION b/DESCRIPTION index 741ed31ae..7ebad6820 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NEWS.md b/NEWS.md index 89b23fd57..e7c31f3ed 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/rules-spaces.R b/R/rules-spaces.R index 9dd6fdcb0..56532f0e9 100644 --- a/R/rules-spaces.R +++ b/R/rules-spaces.R @@ -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 { diff --git a/R/utils-cache.R b/R/utils-cache.R index 517ddd6c8..c41501433 100644 --- a/R/utils-cache.R +++ b/R/utils-cache.R @@ -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) ) } diff --git a/R/zzz.R b/R/zzz.R index 9666a62c0..d5843ed3b 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,3 +1,6 @@ +.default_ignore_start <- "styler: off" +.default_ignore_stop <- "styler: on" + .onLoad <- function(libname, pkgname) { op <- options() op.styler <- list( @@ -5,8 +8,9 @@ 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 ) diff --git a/tests/testthat/test-cache-interaction-more-specs.R b/tests/testthat/test-cache-interaction-more-specs.R index 101232c73..9d3dd1af7 100644 --- a/tests/testthat/test-cache-interaction-more-specs.R +++ b/tests/testthat/test-cache-interaction-more-specs.R @@ -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()) }) diff --git a/tests/testthat/test-interaction-caching-stylerignore.R b/tests/testthat/test-interaction-caching-stylerignore.R index b9e3a94c1..c380b5fd7 100644 --- a/tests/testthat/test-interaction-caching-stylerignore.R +++ b/tests/testthat/test-interaction-caching-stylerignore.R @@ -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()) }) diff --git a/tests/testthat/test-public_api.R b/tests/testthat/test-public_api.R index cda27b549..cade28061 100644 --- a/tests/testthat/test-public_api.R +++ b/tests/testthat/test-public_api.R @@ -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 + )) +})