diff --git a/.github/workflows/check-all-examples.yaml b/.github/workflows/check-all-examples.yaml index 7de30a17f..36415fc90 100644 --- a/.github/workflows/check-all-examples.yaml +++ b/.github/workflows/check-all-examples.yaml @@ -22,7 +22,7 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 with: diff --git a/.github/workflows/check-full.yaml b/.github/workflows/check-full.yaml index f4228d672..dfeb4cc4e 100644 --- a/.github/workflows/check-full.yaml +++ b/.github/workflows/check-full.yaml @@ -38,7 +38,7 @@ jobs: R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 @@ -56,6 +56,7 @@ jobs: - uses: r-lib/actions/check-r-package@v2 with: upload-snapshots: true - error-on: '"note"' + error-on: 'ifelse(getRversion() > "3.6", "warning", "note")' env: _R_CHECK_FORCE_SUGGESTS_: false + _R_CHECK_STOP_ON_INVALID_NUMERIC_VERSION_INPUTS_: true diff --git a/.github/workflows/check-link-rot.yaml b/.github/workflows/check-link-rot.yaml index 666d27c86..3d977b694 100644 --- a/.github/workflows/check-link-rot.yaml +++ b/.github/workflows/check-link-rot.yaml @@ -15,7 +15,7 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 087f0b05f..48e1d1dc7 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -20,7 +20,7 @@ jobs: env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-pandoc@v2 @@ -39,7 +39,7 @@ jobs: - name: Deploy to GitHub pages 🚀 if: github.event_name != 'pull_request' - uses: JamesIves/github-pages-deploy-action@v4.4.1 + uses: JamesIves/github-pages-deploy-action@v4.4.3 with: clean: false branch: gh-pages diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml index 0ab0603db..14b388dea 100644 --- a/.github/workflows/pre-commit.yaml +++ b/.github/workflows/pre-commit.yaml @@ -18,10 +18,10 @@ jobs: ) steps: - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.11.0 + uses: styfle/cancel-workflow-action@0.12.0 with: access_token: ${{ github.token }} - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 - name: Install system dependencies diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index fe87549b5..d9d5c3d89 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -15,7 +15,7 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 with: diff --git a/.github/workflows/touchstone-receive.yaml b/.github/workflows/touchstone-receive.yaml index b1b462650..361368720 100644 --- a/.github/workflows/touchstone-receive.yaml +++ b/.github/workflows/touchstone-receive.yaml @@ -24,7 +24,7 @@ jobs: config: ${{ steps.read_touchstone_config.outputs.config }} steps: - name: Checkout repo - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4bb1af251..d6dd581fa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,7 +6,7 @@ default_language_version: repos: - repo: https://github.com/lorenzwalthert/precommit - rev: v0.3.2.9007 + rev: v0.3.2.9019 hooks: - id: style-files args: @@ -34,8 +34,9 @@ repos: - id: roxygenize additional_dependencies: - r-lib/pkgapi - - dplyr@1.0.9 - - roxygen2@7.2.2 + - dplyr@1.1.2 + - roxygen2@7.2.3 + - rlang@1.1.0 - id: use-tidy-description - id: spell-check exclude: > diff --git a/API b/API index 46b34bbcd..429de55f4 100644 --- a/API +++ b/API @@ -27,7 +27,7 @@ specify_reindention(regex_pattern = NULL, indention = 0L, comments_only = TRUE) specify_transformers_drop(spaces = NULL, indention = NULL, line_breaks = NULL, tokens = NULL) style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), recursive = TRUE, exclude_files = NULL, exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off") style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off") -style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), exclude_files = c("R/RcppExports.R", "R/cpp11.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off") +style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), exclude_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off") style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L) tidyverse_math_token_spacing() tidyverse_reindention() diff --git a/DESCRIPTION b/DESCRIPTION index dd94ea44e..e38798e39 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: styler Title: Non-Invasive Pretty Printing of R Code -Version: 1.9.1 +Version: 1.10.2 Authors@R: c(person(given = "Kirill", family = "Müller", @@ -37,7 +37,6 @@ Imports: Suggests: data.tree (>= 0.1.6), digest, - dplyr, here, knitr, prettycode, diff --git a/NAMESPACE b/NAMESPACE index 11b8927ba..1c39bc4ca 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -48,6 +48,7 @@ importFrom(purrr,pmap) importFrom(purrr,pwalk) importFrom(rlang,"%||%") importFrom(rlang,abort) +importFrom(rlang,check_installed) importFrom(rlang,is_installed) importFrom(rlang,seq2) importFrom(rlang,set_names) diff --git a/NEWS.md b/NEWS.md index e91e30bc6..d189fd61f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,58 @@ +# styler 1.10.2 + +This release was requested by the CRAN team to fix CRAN warning on invalid +numeric version inputs (#1143). + +**Minor changes** + +* Use cli messaging for cache (#1127). +* Use latest (and stable!) pre-commit (#1144). +* Fix CRAN warning on invalid numeric version inputs (#1143). +* Bump JamesIves/github-pages-deploy-action from 4.4.2 to 4.4.3 (#1139). +* fix pre-commit (#1132). +* Don't require dplyr anywhere (#1131). + +We thank everyone who helped making this release possible. + +[@krlmlr](https://github.com/krlmlr), [@lorenzwalthert](https://github.com/lorenzwalthert), [@MichaelChirico](https://github.com/MichaelChirico), [@olivroy](https://github.com/olivroy), [@rkrug](https://github.com/rkrug), and [@rossdrucker](https://github.com/rossdrucker). + +# styler 1.10.1 + +This release was requested by CRAN due to accidentally populating a user cache while building vignettes for R >= 4.3.0. + +* Code quality improvements (#1122). +* Bump JamesIves/github-pages-deploy-action from 4.4.1 to 4.4.2 (#1123). + +Thanks to everyone who contributed to this release: [@olivroy](https://github.com/olivroy) and [@krlmlr](https://github.com/krlmlr). + +# styler 1.10.0 + +This release contains speed-ups between 20% and 40% depending on your use case +thanks to replacing {base} functionality with {vctrs} (#1114). With the speed +boost introduced in version 1.8.0 in Oct. 2022, {styler} is now up to 2x as fast +as before release 1.8.0. + +This release was created upon a request by the CRAN team to actively manage not +just cached files but also the potentially empty cache directories they live in +(#1118). Here are the changes in detail: + + +- Require at least R 3.6 (#1101). +- Prefer {vctrs} functions over slower {base} equivalents (#1114). +- Replace deprecated use of `rlang::with_handlers()` (#1103). +- Remove tail recursion in favor of `repeat` (#1113). +- split `test-public_api.R` for better sharding (#1109). +- 0-pad filenames for sharding (#1110) +- add missing {testthat} snapshots (#1115). +- Bump {touchstone} config (#1104, #1107). +- Bump `actions/checkout` to version 3 in GitHub Actions (#1098). + +Thanks for everyone contributing to this release: + +[@IndrajeetPatil](https://github.com/IndrajeetPatil), [@krlmlr](https://github.com/krlmlr), [@kyleam](https://github.com/kyleam), [@MichaelChirico](https://github.com/MichaelChirico), [@mvanaman](https://github.com/mvanaman), [@olivroy](https://github.com/olivroy), and [@vvarik](https://github.com/vvarik). + # styler 1.9.1 **Bug fixes** diff --git a/R/communicate.R b/R/communicate.R index 5ca445f47..5b1ca30a0 100644 --- a/R/communicate.R +++ b/R/communicate.R @@ -39,10 +39,3 @@ communicate_summary <- function(changed, ruler_width) { cli::cat_rule(width = max(40L, ruler_width)) } } - - -assert_data.tree_installation <- function() { - if (!is_installed("data.tree")) { - abort("The package data.tree needs to be installed for this functionality.") - } -} diff --git a/R/nest.R b/R/nest.R index f56cdc6e3..f6e26d210 100644 --- a/R/nest.R +++ b/R/nest.R @@ -343,7 +343,7 @@ nest_parse_data <- function(pd_flat) { rhs <- nest_(child, "child", setdiff(names(child), "parent_")) - nested <- left_join(internal, rhs, by = c("id" = "parent_")) + nested <- left_join(internal, rhs, by = c(id = "parent_")) children <- nested$child for (i in seq_along(children)) { diff --git a/R/nested-to-tree.R b/R/nested-to-tree.R index f2523f65a..c51445140 100644 --- a/R/nested-to-tree.R +++ b/R/nested-to-tree.R @@ -47,7 +47,7 @@ create_tree_from_pd_with_default_style_attributes <- function(pd, #' } #' @keywords internal create_node_from_nested_root <- function(pd_nested, structure_only) { - assert_data.tree_installation() + check_installed("data.tree") name <- if (structure_only) { "Hierarchical structure" } else { diff --git a/R/parse.R b/R/parse.R index 3aab38f5e..4ddbb72db 100644 --- a/R/parse.R +++ b/R/parse.R @@ -158,9 +158,8 @@ ensure_correct_txt <- function(pd, text) { if (!lines_and_cols_match(new_text)) { abort(paste( - "Error in styler:::ensure_correct_txt().", - "Please file an issue on GitHub (https://github.com/r-lib/styler/issues)" - )) + "Error in styler:::ensure_correct_txt()." + ), .internal = TRUE) } names_to_keep <- setdiff( names(new_text), @@ -183,7 +182,6 @@ ensure_correct_txt <- function(pd, text) { #' changes from "all strings" to "all problematic strings", is partly #' misleading and this approach was chosen for performance reasons only. #' @param pd A parse table. -#' @param text The initial code to style. #' @keywords internal is_insufficiently_parsed_string <- function(pd) { grepl("^\\[", pd$text) & pd$token == "STR_CONST" diff --git a/R/rules-indention.R b/R/rules-indention.R index 7019ef7e6..30cf7b246 100644 --- a/R/rules-indention.R +++ b/R/rules-indention.R @@ -20,7 +20,7 @@ indent_braces <- function(pd, indent_by) { #' @keywords internal unindent_fun_dec <- function(pd, indent_by = 2L) { if (is_function_declaration(pd)) { - idx_closing_brace <- which(pd$token %in% "')'") + idx_closing_brace <- which(pd$token == "')'") fun_dec_head <- seq2(2L, idx_closing_brace) if (is_double_indent_function_declaration(pd, indent_by = indent_by)) { pd$indent[fun_dec_head] <- 2L * indent_by diff --git a/R/rules-line-breaks.R b/R/rules-line-breaks.R index badff81eb..5c6f3345b 100644 --- a/R/rules-line-breaks.R +++ b/R/rules-line-breaks.R @@ -153,7 +153,6 @@ style_line_break_around_curly <- function(strict, pd) { closing_before <- pd$token == "'}'" opening_before <- (pd$token == "'{'") to_break <- lag(opening_before, default = FALSE) | closing_before - len_to_break <- sum(to_break) pd$lag_newlines[to_break] <- ifelse( pd$token[to_break] == "COMMENT", pmin(1L, pd$lag_newlines[to_break]), @@ -268,21 +267,20 @@ set_line_break_after_assignment <- function(pd) { #' Set line break for multi-line function calls #' @param pd A parse table. -#' @param except_token_after A character vector with tokens after "'('" that do -#' not cause a line break after "'('". -#' @param except_text_before A character vector with text before "'('" that do -#' not cause a line break after "'('". -#' @param except_token_before A character vector with text before "')'" that do -#' not cause a line break before "')'". -#' @param force_text_before A character vector with text before "'('" that -#' forces a line break after every argument in the call. +#' @param except_token_before A character vector with tokens that do +#' not cause a line break after them. #' @name set_line_break_if_call_is_multi_line #' #' @keywords internal NULL #' Sets line break after opening parenthesis -#' +#' @param pd The parse table. +#' @param except_token_after The tokens after the token that cause an exception. +#' @param except_text_before A character vector with text before a token that +#' does not cause a line break. +#' @param force_text_before A character vector with text before "'('" that +#' forces a line break after every argument in the call. #' @details #' In general, every call that is multi-line has a line break after the opening #' parenthesis. Exceptions: @@ -341,7 +339,7 @@ set_line_break_after_opening_if_call_is_multi_line <- function(pd, #' position of the first named argument and breaks returns the index of it. #' If there is no named argument, the line is broken right after the opening #' parenthesis. -#' @inheritParams set_line_break_if_call_is_multi_line +#' @param pd A parse table. #' @keywords internal find_line_break_position_in_multiline_call <- function(pd) { candidate <- (which(pd$token == "EQ_SUB") - 1L)[1L] @@ -377,7 +375,8 @@ set_line_break_before_closing_call <- function(pd, except_token_before) { } -#' @rdname set_line_break_if_call_is_multi_line +#' @describeIn set_line_break_if_call_is_multi_line Remove line breaks in +#' function calls. #' @keywords internal remove_line_break_in_fun_call <- function(pd, strict) { if (is_function_call(pd)) { diff --git a/R/set-assert-args.R b/R/set-assert-args.R index fd046e960..7cccd0aa5 100644 --- a/R/set-assert-args.R +++ b/R/set-assert-args.R @@ -9,7 +9,7 @@ set_arg_write_tree <- function(write_tree) { if (is.na(write_tree)) { write_tree <- is_installed("data.tree") } else if (write_tree) { - assert_data.tree_installation() + check_installed("data.tree") } write_tree } @@ -21,29 +21,29 @@ set_arg_write_tree <- function(write_tree) { #' @inheritParams make_transformer #' @keywords internal assert_transformers <- function(transformers) { - version_cutoff <- 2.0 + version_cutoff <- "2.0" no_name <- is.null(transformers$style_guide_name) no_version <- is.null(transformers$style_guide_version) if (no_name || no_version) { action <- if (utils::packageVersion("styler") >= version_cutoff) { "are not supported anymore" } else { - "depreciated and will be removed in a future version of styler." + "deprecated and will be removed in a future version of styler." } message <- paste( "Style guides without a name and a version field are", action, "\nIf you are a user: Open an issue on", "https://github.com/r-lib/styler and provide a reproducible example", "of this error. \nIf you are a developer:", - "When you create a style guide with `styler::create_style_guide()`, the", + "When you create a style guide with {.fn styler::create_style_guide}, the", "argument `style_guide_name` and `style_guide_version` should be", - "non-NULL. See help(\"create_style_guide\") for how to set them." + "non-NULL. See {.help styler::create_style_guide} for how to set them." ) if (utils::packageVersion("styler") >= version_cutoff) { - rlang::abort(message) + cli::cli_abort(message) } else { - rlang::warn(message) + cli::cli_warn(message) } } } diff --git a/R/styler-package.R b/R/styler-package.R index 373a60140..c1558ef4e 100644 --- a/R/styler-package.R +++ b/R/styler-package.R @@ -23,10 +23,8 @@ #' @importFrom magrittr "%>%" #' @importFrom purrr compact partial flatten flatten_int flatten_chr #' @importFrom purrr map map_lgl map_int map_chr map2 map2_chr map_at pmap pwalk -#' @importFrom rlang abort warn seq2 is_installed "%||%" set_names -#' @importFrom vctrs vec_rbind -#' @importFrom vctrs vec_slice -#' @importFrom vctrs vec_split +#' @importFrom rlang abort warn seq2 check_installed is_installed "%||%" set_names +#' @importFrom vctrs vec_rbind vec_slice vec_split ## usethis namespace: end NULL diff --git a/R/stylerignore.R b/R/stylerignore.R index ce558dfd6..4cf0b30bd 100644 --- a/R/stylerignore.R +++ b/R/stylerignore.R @@ -76,9 +76,9 @@ add_stylerignore <- function(pd_flat) { pd_flat$indicator_off <- cumsum_start + cumsum_stop is_invalid <- cumsum_start - cumsum_stop < 0L | cumsum_start - cumsum_stop > 1L if (any(is_invalid)) { - warn(paste0( + cli::cli_warn(paste0( "Invalid stylerignore sequences found, potentially ignoring some of the ", - "markers set.\nSee `help(\"stylerignore\", \"styler\")`." + "markers set.\nSee {.help styler::stylerignore}." )) } diff --git a/R/testing.R b/R/testing.R index b83978360..4b94d7e1b 100644 --- a/R/testing.R +++ b/R/testing.R @@ -327,7 +327,7 @@ local_test_setup <- function(cache = FALSE, .local_envir = parent.frame()) { current_cache <- cache_info(format = "tabular") withr::local_options( - list("styler.quiet" = TRUE, "R.cache.rootPath" = tempfile()), + list(styler.quiet = TRUE, R.cache.rootPath = tempfile()), .local_envir = .local_envir ) if (cache) { @@ -379,5 +379,5 @@ skip_during_parallel <- function() { Sys.getenv("STYLER_TEST_IS_TRULY_PARALLEL", TRUE) %>% toupper() %>% as.logical() %>% - testthat::skip_if() + testthat::skip_if("Running in parallel") } diff --git a/R/transform-code.R b/R/transform-code.R index d8ecebc36..4f2b0337d 100644 --- a/R/transform-code.R +++ b/R/transform-code.R @@ -22,7 +22,7 @@ transform_code <- function(path, fun, ..., dry) { ..., dry = dry ) } else { - abort(paste(path, "is not an R, Rmd or Rnw file")) + abort(paste(path, "is not an R, Rmd, qmd, or Rnw file")) } } diff --git a/R/transform-files.R b/R/transform-files.R index f564b4632..a79c4a61b 100644 --- a/R/transform-files.R +++ b/R/transform-files.R @@ -116,9 +116,12 @@ make_transformer <- function(transformers, use_cache <- FALSE } - if (!use_cache) { + if (use_cache) { + text + } else { transformed_code <- text %>% - parse_transform_serialize_r(transformers, + parse_transform_serialize_r( + transformers, base_indention = base_indention, warn_empty = warn_empty ) @@ -139,8 +142,6 @@ make_transformer <- function(transformers, } transformed_code - } else { - text } } } @@ -418,19 +419,16 @@ verify_roundtrip <- function(old_text, new_text, parsable_only = FALSE) { error = function(e) { rlang::abort(paste0( "Styling resulted in code that isn't parsable. This should not ", - "happen. Please file a bug report on GitHub ", - "(https://github.com/r-lib/styler/issues) using a reprex." - )) + "happen." + ), .internal = TRUE) } ) } else if (!expressions_are_identical(old_text, new_text)) { msg <- paste( "The expression evaluated before the styling is not the same as the", - "expression after styling. This should not happen. Please file a", - "bug report on GitHub (https://github.com/r-lib/styler/issues)", - "using a reprex." + "expression after styling. This should not happen." ) - abort(msg) + abort(msg, .internal = TRUE) } } diff --git a/R/ui-caching.R b/R/ui-caching.R index 9aace1eb1..42e80f529 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -138,7 +138,7 @@ cache_info <- function(cache_name = NULL, format = "both") { #' @export cache_activate <- function(cache_name = NULL, verbose = !getOption("styler.quiet", FALSE)) { - options("styler.cache_name" = cache_name %||% styler_version) + options(styler.cache_name = cache_name %||% styler_version) path <- cache_find_path(cache_name) if (verbose) { @@ -155,7 +155,7 @@ cache_activate <- function(cache_name = NULL, #' @rdname cache_activate #' @export cache_deactivate <- function(verbose = !getOption("styler.quiet", FALSE)) { - options("styler.cache_name" = NULL) + options(styler.cache_name = NULL) if (verbose) { cat("Deactivated cache.\n") diff --git a/R/ui-styling.R b/R/ui-styling.R index bb4cc136c..e25eed10c 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -69,7 +69,7 @@ style_pkg <- function(pkg = ".", style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), - exclude_files = c("R/RcppExports.R", "R/cpp11.R"), + exclude_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, @@ -91,8 +91,8 @@ style_pkg <- function(pkg = ".", #' ".Rmd")`, or `c("r", "rmd")`. Supported values (after standardization) are: #' "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as #' Rmd. -#' @param exclude_files Character vector with paths to files that should be -#' excluded from styling. +#' @param exclude_files Character vector with regular expressions to files +#' that should be excluded from styling. #' @param exclude_dirs Character vector with directories to exclude #' (recursively). Note that the default values were set for consistency with #' [style_dir()] and as these directories are anyways not styled. @@ -107,8 +107,12 @@ prettify_pkg <- function(transformers, dry) { filetype_ <- set_and_assert_arg_filetype(filetype) r_files <- rprofile_files <- vignette_files <- readme <- NULL + all_files <- list.files(".", recursive = TRUE, all.files = TRUE) + exclude_files <- grep(paste0(exclude_files, collapse = "|"), all_files, value = TRUE) + exclude_files <- set_arg_paths(exclude_files) + exclude_files_regex <- paste0(exclude_files[!file.exists(exclude_files)], collapse = "|") exclude_files <- c( - set_arg_paths(exclude_files), + exclude_files, dir_without_.(exclude_dirs, pattern = map_filetype_to_pattern(filetype)) ) if ("\\.r" %in% filetype_) { diff --git a/R/vertical.R b/R/vertical.R index 2dae20f56..8062185a1 100644 --- a/R/vertical.R +++ b/R/vertical.R @@ -24,10 +24,10 @@ print.vertical <- function(x, ..., if (is_installed("prettycode")) { x <- prettycode::highlight(x, style = style) } else { - warn(paste( - "Could not use colored = TRUE, as the package prettycode is not", + cli::cli_warn(paste( + "Could not use `colored = TRUE`, as the package prettycode is not", "installed. Please install it if you want to see colored output", - "or see `?print.vertical` for more information." + "or see {.help styler::print.vertical} for more information." )) } } diff --git a/R/zzz.R b/R/zzz.R index cc838d3a5..9bdc76c48 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -23,6 +23,37 @@ invisible() } +#' Delete a cache or temp directory +#' +#' For safety, `path` is only deleted if it is a sub-directory of a temporary +#' directory or user cache. Since this function relies on `tools::R_user_dir()`, +#' it early returns `FALSE` on `R < 4.0.0`. +#' @param path Absolute path to a directory to delete. +#' @returns `TRUE` if anything was deleted, `FALSE` otherwise. +#' @keywords internal +delete_if_cache_directory <- function(path) { + path <- normalizePath(path) + if (getRversion() < package_version("4.0.0")) { + return(FALSE) + } + designated_cache_path <- normalizePath(tools::R_user_dir("R.cache", which = "cache"), mustWork = FALSE) + is_in_tools_cache <- startsWith(path, designated_cache_path) + temp_dir <- normalizePath(dirname(tempdir())) + is_in_generic_cache <- startsWith(path, temp_dir) + if (is_in_tools_cache || is_in_generic_cache) { + all_files <- list.files(path, + full.names = TRUE, + recursive = TRUE, + all.files = FALSE + ) + if (length(all_files) < 1L) { + unlink(path, recursive = TRUE) + return(TRUE) + } + } + FALSE +} + ask_to_switch_to_non_default_cache_root <- function(ask = interactive()) { if (ask && stats::runif(1L) > 0.9 && is.null(getOption("styler.cache_root"))) { @@ -33,21 +64,31 @@ ask_to_switch_to_non_default_cache_root <- function(ask = interactive()) { ask_to_switch_to_non_default_cache_root_impl <- function() { - rlang::inform(paste0( - "{styler} cache is cleared after 6 days. ", - "See `?styler::caching` to configure differently or silence this message." + cli::cli_inform(paste0( + "{{styler}} cache is cleared after 6 days. ", + "See {.help styler::caching} to configure differently or silence this message." )) } remove_old_cache_files <- function() { + path_version_specific <- R.cache::getCachePath(c("styler", styler_version)) all_cached <- list.files( - R.cache::getCachePath(c("styler", styler_version)), + path_version_specific, full.names = TRUE, recursive = TRUE ) - date_boundary <- Sys.time() - 60L * 60L * 24L * 6L + date_boundary <- Sys.time() - as.difftime(6L, units = "days") file.remove( all_cached[file.info(all_cached)$mtime < date_boundary] ) + path_styler_specific <- dirname(path_version_specific) + path_r_cache_specific <- dirname(path_styler_specific) + paths <- normalizePath( + c(path_version_specific, path_styler_specific, path_r_cache_specific) + ) + purrr::walk( + paths, + delete_if_cache_directory + ) } diff --git a/cran-comments.md b/cran-comments.md index 3408b33e9..747d0b082 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -4,14 +4,16 @@ editor_options: wrap: 79 --- -This is a release to fix critical bugs and release a new feature. +This is a release requested by the CRAN team to comply with +`R_CHECK_STOP_ON_INVALID_NUMERIC_VERSION_INPUTS`. ## Test environments -- ubuntu 20.04 (on GitHub Actions): R devel, R 4.2.1, 4.1.2, R 4.0.5, R 3.6, - R 3.5. -- Windows Server 10 (on GitHub Actions): R devel, R 4.2.1, R 4.1.2, R 3.6. +- ubuntu 20.04 (on GitHub Actions): R devel, R 4.3.0, R 4.2.1, 4.1.2, R 4.0.5, + R 3.6 +- Windows Server 10 (on GitHub Actions): R devel, R 4.3.0, R 4.2.1, R 4.1.2, + R 3.6. - win-builder: R devel ## R CMD check results @@ -37,4 +39,5 @@ compliant with the requirements of CRAN. I also ran R CMD check on all 39 downstream dependencies of styler using the revdepcheck package. -All of them finished R CMD CHECK with zero (0) ERRORS, WARNINGS and NOTES. +All of them finished R CMD CHECK with the same number of ERRORS, WARNINGS and +NOTES. diff --git a/inst/WORDLIST b/inst/WORDLIST index 0ff1db851..1e14ad162 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -107,6 +107,7 @@ iNZightTools io ixmypi ized +JamesIves Jupyterlab Kirill kirill @@ -232,6 +233,7 @@ setCacheRootPath setdiff setenv Shallowify +sharding shinydashboardPlus shinymeta shinyMonacoEditor diff --git a/man/delete_if_cache_directory.Rd b/man/delete_if_cache_directory.Rd new file mode 100644 index 000000000..a79745cab --- /dev/null +++ b/man/delete_if_cache_directory.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/zzz.R +\name{delete_if_cache_directory} +\alias{delete_if_cache_directory} +\title{Delete a cache or temp directory} +\usage{ +delete_if_cache_directory(path) +} +\arguments{ +\item{path}{Absolute path to a directory to delete.} +} +\value{ +\code{TRUE} if anything was deleted, \code{FALSE} otherwise. +} +\description{ +For safety, \code{path} is only deleted if it is a sub-directory of a temporary +directory or user cache. Since this function relies on \code{tools::R_user_dir()}, +it early returns \code{FALSE} on \verb{R < 4.0.0}. +} +\keyword{internal} diff --git a/man/is_insufficiently_parsed_string.Rd b/man/is_insufficiently_parsed_string.Rd index 83cdfb67a..0344443c4 100644 --- a/man/is_insufficiently_parsed_string.Rd +++ b/man/is_insufficiently_parsed_string.Rd @@ -8,8 +8,6 @@ is_insufficiently_parsed_string(pd) } \arguments{ \item{pd}{A parse table.} - -\item{text}{The initial code to style.} } \description{ Identifies strings that were not fully parsed due to their vast length. diff --git a/man/prettify_any.Rd b/man/prettify_any.Rd index c4b7fc5a1..b1db51e44 100644 --- a/man/prettify_any.Rd +++ b/man/prettify_any.Rd @@ -28,8 +28,8 @@ Rmd.} \item{recursive}{A logical value indicating whether or not files in subdirectories should be styled as well.} -\item{exclude_files}{Character vector with paths to files that should be -excluded from styling.} +\item{exclude_files}{Character vector with regular expressions to files +that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude (recursively). Note that the default values were set for consistency with diff --git a/man/prettify_pkg.Rd b/man/prettify_pkg.Rd index 4e9a40cb8..0c999550c 100644 --- a/man/prettify_pkg.Rd +++ b/man/prettify_pkg.Rd @@ -23,8 +23,8 @@ be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".R "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as Rmd.} -\item{exclude_files}{Character vector with paths to files that should be -excluded from styling.} +\item{exclude_files}{Character vector with regular expressions to files +that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude (recursively). Note that the default values were set for consistency with diff --git a/man/set_line_break_after_opening_if_call_is_multi_line.Rd b/man/set_line_break_after_opening_if_call_is_multi_line.Rd index 417c04a1a..174bee142 100644 --- a/man/set_line_break_after_opening_if_call_is_multi_line.Rd +++ b/man/set_line_break_after_opening_if_call_is_multi_line.Rd @@ -11,6 +11,17 @@ set_line_break_after_opening_if_call_is_multi_line( force_text_before = NULL ) } +\arguments{ +\item{pd}{The parse table.} + +\item{except_token_after}{The tokens after the token that cause an exception.} + +\item{except_text_before}{A character vector with text before a token that +does not cause a line break.} + +\item{force_text_before}{A character vector with text before "'('" that +forces a line break after every argument in the call.} +} \description{ Sets line break after opening parenthesis } diff --git a/man/set_line_break_if_call_is_multi_line.Rd b/man/set_line_break_if_call_is_multi_line.Rd index a8482a378..b7ad972eb 100644 --- a/man/set_line_break_if_call_is_multi_line.Rd +++ b/man/set_line_break_if_call_is_multi_line.Rd @@ -13,17 +13,8 @@ remove_line_break_in_fun_call(pd, strict) \arguments{ \item{pd}{A parse table.} -\item{except_token_before}{A character vector with text before "')'" that do -not cause a line break before "')'".} - -\item{except_token_after}{A character vector with tokens after "'('" that do -not cause a line break after "'('".} - -\item{except_text_before}{A character vector with text before "'('" that do -not cause a line break after "'('".} - -\item{force_text_before}{A character vector with text before "'('" that -forces a line break after every argument in the call.} +\item{except_token_before}{A character vector with tokens that do +not cause a line break after them.} } \description{ Set line break for multi-line function calls @@ -33,5 +24,8 @@ Set line break for multi-line function calls \item \code{set_line_break_before_closing_call()}: Sets line break before closing parenthesis. +\item \code{remove_line_break_in_fun_call()}: Remove line breaks in +function calls. + }} \keyword{internal} diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 9f8294354..7f0f61a58 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -41,8 +41,8 @@ Rmd.} \item{recursive}{A logical value indicating whether or not files in sub directories of \code{path} should be styled as well.} -\item{exclude_files}{Character vector with paths to files that should be -excluded from styling.} +\item{exclude_files}{Character vector with regular expressions to files +that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude (recursively).} diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index 303dc7693..0eca9278e 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -10,7 +10,8 @@ style_pkg( style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), - exclude_files = c("R/RcppExports.R", "R/cpp11.R"), + exclude_files = c("R/RcppExports\\\\.R", "R/cpp11\\\\.R", + "R/import-standalone.*\\\\.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, @@ -37,8 +38,8 @@ be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".R "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as Rmd.} -\item{exclude_files}{Character vector with paths to files that should be -excluded from styling.} +\item{exclude_files}{Character vector with regular expressions to files +that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude (recursively). Note that the default values were set for consistency with diff --git a/tests/testthat/_snaps/roundtrip.md b/tests/testthat/_snaps/roundtrip.md index 4c326e73e..f0f66b9bc 100644 --- a/tests/testthat/_snaps/roundtrip.md +++ b/tests/testthat/_snaps/roundtrip.md @@ -5,5 +5,7 @@ # corrupt styling does give an error - The expression evaluated before the styling is not the same as the expression after styling. This should not happen. Please file a bug report on GitHub (https://github.com/r-lib/styler/issues) using a reprex. + The expression evaluated before the styling is not the same as the expression after styling. This should not happen. + i This is an internal error that was detected in the styler package. + Please report it at with a reprex () and the full backtrace. diff --git a/tests/testthat/test-cache-clean-up.R b/tests/testthat/test-cache-clean-up.R new file mode 100644 index 000000000..66adce823 --- /dev/null +++ b/tests/testthat/test-cache-clean-up.R @@ -0,0 +1,23 @@ +test_that("styler tests did not use R.cache in user root", { + skip_on_cran() + skip_on_covr() + skip_during_parallel() + expect_true( + length(list.files(R.cache::getCachePath("styler"), recursive = TRUE)) == 0L + ) +}) + +test_that("clear Cache", { + # if R CMD CHECK is detected, R.cache root is set to a temp + # directory by default. + # https://github.com/HenrikBengtsson/R.cache/commit/c7ac171f15f035674346d5504049c38cf07c268f + # Hence, this clean up won't clean up the user directory. + skip_during_parallel() + cache_path <- R.cache::getCachePath("styler") + R.cache::clearCache(cache_path, recursive = TRUE, prompt = FALSE) + skip_on_cran() + skip_on_covr() + expect_true( + length(list.dirs(R.cache::getCachePath("styler"))) == 1L + ) +}) diff --git a/tests/testthat/test-cache-low-level-api.R b/tests/testthat/test-cache-low-level-api.R index 0c9ed6acd..f4b4694ea 100644 --- a/tests/testthat/test-cache-low-level-api.R +++ b/tests/testthat/test-cache-low-level-api.R @@ -1,16 +1,16 @@ test_that("caching utils make right blocks with semi-colon", { blocks_simple_uncached <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>% - dplyr::mutate(is_cached = FALSE) %>% + base::transform(is_cached = FALSE) %>% cache_find_block() expect_equal(blocks_simple_uncached, c(1, 1, 1, 1)) blocks_simple_cached <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>% - dplyr::mutate(is_cached = TRUE) %>% + base::transform(is_cached = TRUE) %>% cache_find_block() expect_equal(blocks_simple_cached, c(1, 1, 1, 1)) blocks_edge <- compute_parse_data_nested(c("1 + 1", "2; 1+1")) %>% - dplyr::mutate(is_cached = c(TRUE, TRUE, FALSE, FALSE)) %>% + base::transform(is_cached = c(TRUE, TRUE, FALSE, FALSE)) %>% cache_find_block() expect_equal(blocks_edge, c(1, 2, 2, 2)) }) @@ -30,7 +30,7 @@ test_that("caching utils make right blocks with comments", { blocks_simple_uncached <- compute_parse_data_nested(text) %>% - dplyr::mutate(is_cached = c( + base::transform(is_cached = c( FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE )) %>% @@ -48,7 +48,7 @@ test_that("caching utils make right blocks with comments", { tau1 = 1 # here? " blocks_simple_cached <- compute_parse_data_nested(text) %>% - dplyr::mutate(is_cached = c( + base::transform(is_cached = c( FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE )) %>% cache_find_block() @@ -101,17 +101,17 @@ test_that("blank lines are correctly identified", { test_that("caching utils make right blocks with comments", { blocks_simple_uncached <- compute_parse_data_nested(c("1 + 1", "2 # comment")) %>% - dplyr::mutate(is_cached = FALSE) %>% + base::transform(is_cached = FALSE) %>% cache_find_block() expect_equal(blocks_simple_uncached, c(1, 1, 1)) blocks_simple_cached <- compute_parse_data_nested(c("1 + 1", "2 # comment2")) %>% - dplyr::mutate(is_cached = TRUE) %>% + base::transform(is_cached = TRUE) %>% cache_find_block() expect_equal(blocks_simple_cached, c(1, 1, 1)) blocks_edge <- compute_parse_data_nested(c("1 + 1", "2 # 1+1")) %>% - dplyr::mutate(is_cached = c(TRUE, TRUE, FALSE)) %>% + base::transform(is_cached = c(TRUE, TRUE, FALSE)) %>% cache_find_block() expect_equal(blocks_edge, c(1, 2, 2)) }) diff --git a/tests/testthat/test-create_token.R b/tests/testthat/test-create_token.R index c00892808..bf99b6dcb 100644 --- a/tests/testthat/test-create_token.R +++ b/tests/testthat/test-create_token.R @@ -1,5 +1,3 @@ - - test_that("can create a token that has relevant columns", { pd_names <- c( "token", "text", "short", "lag_newlines", "newlines", "pos_id", diff --git a/tests/testthat/test-create_tree.R b/tests/testthat/test-create_tree.R index b3b0ebb7f..e6ab27068 100644 --- a/tests/testthat/test-create_tree.R +++ b/tests/testthat/test-create_tree.R @@ -1,7 +1,6 @@ - - test_that("create_trees outputs identical structure if trees have same structure", { skip_if_not_installed("DiagrammeR") + skip_if_not_installed("data.tree") eq <- "a <- fun(a = b)" arrow <- "a <- data.frame(x = qq)" expect_equal( @@ -12,6 +11,7 @@ test_that("create_trees outputs identical structure if trees have same structure test_that("create_trees outputs are not identical structure if trees have different structure", { skip_if_not_installed("DiagrammeR") + skip_if_not_installed("data.tree") eq <- "a <- fun(a = 1:3)" arrow <- "a <- data.frame(x = qq)" expect_true( diff --git a/tests/testthat/test-curly-curly.R b/tests/testthat/test-curly-curly.R index d61299e75..7d1008cf5 100644 --- a/tests/testthat/test-curly-curly.R +++ b/tests/testthat/test-curly-curly.R @@ -1,5 +1,3 @@ - - test_that("curly-culry", { expect_warning(test_collection("curly-curly", "mixed", diff --git a/tests/testthat/test-exception_handling.R b/tests/testthat/test-exception_handling.R index 11e1cbb71..7bf1b9f1d 100644 --- a/tests/testthat/test-exception_handling.R +++ b/tests/testthat/test-exception_handling.R @@ -1,5 +1,3 @@ - - test_that("style_text returns custom error", { expect_error(style_text("a <- 3 4"), "unexpected numeric constant") }) @@ -32,6 +30,6 @@ test_that("warning is given when transformers does not contain a version", { } expect_fun( assert_transformers(sg), - "name and a version field are depreciated and will be removed in a future version of styler" + "name and a version field are deprecated and will be removed in a future version of styler" ) }) diff --git a/tests/testthat/test-helpers.R b/tests/testthat/test-helpers.R index 55c654375..139a86314 100644 --- a/tests/testthat/test-helpers.R +++ b/tests/testthat/test-helpers.R @@ -1,6 +1,5 @@ - - test_that("can construct and print vertical", { + skip_if_not_installed("prettycode") expect_snapshot({ construct_vertical(c("1 + 1", "nw")) }) diff --git a/tests/testthat/test-identify-roxygen-examples.R b/tests/testthat/test-identify-roxygen-examples.R index 8eee143e2..0b89c6fde 100644 --- a/tests/testthat/test-identify-roxygen-examples.R +++ b/tests/testthat/test-identify-roxygen-examples.R @@ -1,5 +1,3 @@ - - #' Things to consider: #' * one function declaration or many #' * example(s) is last tag or not? diff --git a/tests/testthat/test-indention_curly.R b/tests/testthat/test-indention_curly.R index 5064dcb4d..fe0e1d928 100644 --- a/tests/testthat/test-indention_curly.R +++ b/tests/testthat/test-indention_curly.R @@ -1,5 +1,3 @@ - - test_that("indention on one-liner curley only is not changed", { expect_warning(test_collection("indention_curly_brackets", "one_line_curly", diff --git a/tests/testthat/test-indention_fun_calls.R b/tests/testthat/test-indention_fun_calls.R index f44c037b8..eddb4e639 100644 --- a/tests/testthat/test-indention_fun_calls.R +++ b/tests/testthat/test-indention_fun_calls.R @@ -1,5 +1,3 @@ - - test_that("edge cases work", { expect_warning(test_collection("indention_fun_calls", transformer = style_text, strict = FALSE diff --git a/tests/testthat/test-indention_multiple.R b/tests/testthat/test-indention_multiple.R index a99634f3d..c9d68feac 100644 --- a/tests/testthat/test-indention_multiple.R +++ b/tests/testthat/test-indention_multiple.R @@ -1,5 +1,3 @@ - - test_that("multiple round brackets don't cause extraindention", { expect_warning(test_collection("indention_multiple", "round_only", diff --git a/tests/testthat/test-indention_operators.R b/tests/testthat/test-indention_operators.R index 3e62f85f8..2e7d1d4ff 100644 --- a/tests/testthat/test-indention_operators.R +++ b/tests/testthat/test-indention_operators.R @@ -1,5 +1,3 @@ - - test_that("pipe is indended correctly", { expect_warning(test_collection("indention_operators", "pipe", diff --git a/tests/testthat/test-indention_round_brackets.R b/tests/testthat/test-indention_round_brackets.R index c0c6a9409..1e01ade44 100644 --- a/tests/testthat/test-indention_round_brackets.R +++ b/tests/testthat/test-indention_round_brackets.R @@ -1,5 +1,3 @@ - - test_that("one-line function call yields correct indention", { expect_warning(test_collection("indention_round_brackets", "one_line", diff --git a/tests/testthat/test-insertion_comment_interaction.R b/tests/testthat/test-insertion_comment_interaction.R index 698b58d03..e9b97cdc4 100644 --- a/tests/testthat/test-insertion_comment_interaction.R +++ b/tests/testthat/test-insertion_comment_interaction.R @@ -1,5 +1,3 @@ - - ## ............................................................................ ## strict = TRUE #### diff --git a/tests/testthat/test-line_breaks_and_other.R b/tests/testthat/test-line_breaks_and_other.R index 7568ac71f..512f09fe7 100644 --- a/tests/testthat/test-line_breaks_and_other.R +++ b/tests/testthat/test-line_breaks_and_other.R @@ -1,5 +1,3 @@ - - test_that("line breaks involing curly brackets", { expect_warning(test_collection("line_breaks_and_other", "curly", transformer = style_text diff --git a/tests/testthat/test-line_breaks_fun_call.R b/tests/testthat/test-line_breaks_fun_call.R index 939a95d1b..375232630 100644 --- a/tests/testthat/test-line_breaks_fun_call.R +++ b/tests/testthat/test-line_breaks_fun_call.R @@ -1,4 +1,3 @@ - test_that("line breaks work in general", { expect_warning(test_collection("line_breaks_fun_call", "token_dependent_mixed", diff --git a/tests/testthat/test-math_token_spacing.R b/tests/testthat/test-math_token_spacing.R index 9b1aa9009..dd03370f0 100644 --- a/tests/testthat/test-math_token_spacing.R +++ b/tests/testthat/test-math_token_spacing.R @@ -1,5 +1,3 @@ - - test_that("invalid tokens return error", { expect_error(test_collection( "math_token_spacing", "non_strict_math_spacing_all", diff --git a/tests/testthat/test-parsing.R b/tests/testthat/test-parsing.R index c5cb2463a..f64314009 100644 --- a/tests/testthat/test-parsing.R +++ b/tests/testthat/test-parsing.R @@ -1,5 +1,3 @@ - - test_that("repreated parsing solves wrong parent assignment", { expect_warning( test_collection( diff --git a/tests/testthat/test-public_api-0.R b/tests/testthat/test-public_api-0.R index e0d46f105..70e90cbec 100644 --- a/tests/testthat/test-public_api-0.R +++ b/tests/testthat/test-public_api-0.R @@ -32,7 +32,7 @@ test_that("styler can style package and exclude some directories and files", { capture_output(expect_true({ styled <- style_pkg(testthat_file("public-api", "xyzpackage"), exclude_dirs = "tests", - exclude_files = ".Rprofile" + exclude_files = "\\.Rprofile" ) nrow(styled) == 1 })) @@ -40,10 +40,18 @@ test_that("styler can style package and exclude some directories and files", { capture_output(expect_true({ styled <- style_pkg(testthat_file("public-api", "xyzpackage"), exclude_dirs = "tests", - exclude_files = "./.Rprofile" + exclude_files = ".*ofile" ) nrow(styled) == 1 })) + + capture_output(expect_true({ + styled <- style_pkg(testthat_file("public-api", "xyzpackage"), + exclude_dirs = "tests", + exclude_files = "hello" + ) + nrow(styled) == 0 + })) }) diff --git a/tests/testthat/test-public_api-3.R b/tests/testthat/test-public_api-3.R index 7c1235f7a..8b1e926fb 100644 --- a/tests/testthat/test-public_api-3.R +++ b/tests/testthat/test-public_api-3.R @@ -153,12 +153,13 @@ test_that("Can display warning on unset styler cache", { withr::local_seed(7) expect_message( ask_to_switch_to_non_default_cache_root(ask = TRUE), - "See `?styler::caching`", + regexp = "styler::caching", fixed = TRUE ) }) test_that("No sensitive to decimal option", { + skip_if_not_installed("prettycode") withr::local_options(OutDec = ",") expect_snapshot({ style_text("1") diff --git a/tests/testthat/test-relocate_eq_assign.R b/tests/testthat/test-relocate_eq_assign.R index 9718c5655..0fbcd40b8 100644 --- a/tests/testthat/test-relocate_eq_assign.R +++ b/tests/testthat/test-relocate_eq_assign.R @@ -1,7 +1,7 @@ - # Tests code in R/relevel.R test_that("tree hierarchy is the same no matter whether = or <- is used", { skip_if_not_installed("DiagrammeR") + skip_if_not_installed("data.tree") assign_left <- create_tree( "x <- 5 diff --git a/tests/testthat/test-rmd.R b/tests/testthat/test-rmd.R index 6ff803d80..79811ba60 100644 --- a/tests/testthat/test-rmd.R +++ b/tests/testthat/test-rmd.R @@ -1,5 +1,3 @@ - - test_that("can style .Rmd files", { expect_warning(test_collection("rmd", "simple", transformer = transform_mixed, diff --git a/tests/testthat/test-rnw.R b/tests/testthat/test-rnw.R index 1a9158d3f..206d63b89 100644 --- a/tests/testthat/test-rnw.R +++ b/tests/testthat/test-rnw.R @@ -1,5 +1,3 @@ - - test_that("can style .Rnw files", { expect_warning(test_collection( "rnw", "008-outdec", diff --git a/tests/testthat/test-roundtrip.R b/tests/testthat/test-roundtrip.R index d50242bc2..a92dd8598 100644 --- a/tests/testthat/test-roundtrip.R +++ b/tests/testthat/test-roundtrip.R @@ -1,6 +1,3 @@ - - - test_that("parse_tree_must_be_identical works", { expect_true( parse_tree_must_be_identical(tidyverse_style(scope = "line_breaks")) diff --git a/tests/testthat/test-roxygen-examples-parse.R b/tests/testthat/test-roxygen-examples-parse.R index d733b1585..5a1a56c96 100644 --- a/tests/testthat/test-roxygen-examples-parse.R +++ b/tests/testthat/test-roxygen-examples-parse.R @@ -1,5 +1,3 @@ - - test_that("simple examples can be parsed", { expected_out <- c("\n", "x <- 1\n") expect_equal(parse_roxygen(c("#' @examples", "#' x <- 1"))$text, expected_out) diff --git a/tests/testthat/test-scope-AsIs.R b/tests/testthat/test-scope-AsIs.R index 97eec4ebf..5ea573ca2 100644 --- a/tests/testthat/test-scope-AsIs.R +++ b/tests/testthat/test-scope-AsIs.R @@ -1,5 +1,3 @@ - - test_that("no indention manipulation but spaces manipulation", { expect_warning(test_collection( "scope-AsIs", "scope_spaces-", diff --git a/tests/testthat/test-scope-character.R b/tests/testthat/test-scope-character.R index 256f7d670..ef7ee0d2d 100644 --- a/tests/testthat/test-scope-character.R +++ b/tests/testthat/test-scope-character.R @@ -1,5 +1,3 @@ - - test_that("no indention manipulation but spaces manipulation", { expect_warning(test_collection( "scope-character", "scope_spaces", diff --git a/tests/testthat/test-serialize_tests.R b/tests/testthat/test-serialize_tests.R index 2b858a77a..cab2100e3 100644 --- a/tests/testthat/test-serialize_tests.R +++ b/tests/testthat/test-serialize_tests.R @@ -1,5 +1,3 @@ - - test_that("No files to compare returns error", { expect_error(test_collection("serialize_tests", "xyz", transformer = as_is diff --git a/tests/testthat/test-spacing.R b/tests/testthat/test-spacing.R index 9caadf9e4..7fdb6e55a 100644 --- a/tests/testthat/test-spacing.R +++ b/tests/testthat/test-spacing.R @@ -1,5 +1,3 @@ - - test_that("curly braces", { expect_warning(test_collection( "spacing", "round", diff --git a/tests/testthat/test-square_brackets.R b/tests/testthat/test-square_brackets.R index a400c1f30..7f4c4d481 100644 --- a/tests/testthat/test-square_brackets.R +++ b/tests/testthat/test-square_brackets.R @@ -1,5 +1,3 @@ - - test_that("square brackets cause indention", { expect_warning(test_collection( "indention_square_brackets", diff --git a/tests/testthat/test-start_line.R b/tests/testthat/test-start_line.R index f154dc555..bb17a2aad 100644 --- a/tests/testthat/test-start_line.R +++ b/tests/testthat/test-start_line.R @@ -1,5 +1,3 @@ - - test_that("leading spaces are preserved at start of text", { expect_warning(test_collection("start_line", transformer = style_empty diff --git a/tests/testthat/test-strict.R b/tests/testthat/test-strict.R index 774096980..da8ea93e4 100644 --- a/tests/testthat/test-strict.R +++ b/tests/testthat/test-strict.R @@ -1,5 +1,3 @@ - - test_that("can style example source file with strict = TRUE", { expect_warning(test_collection( "strict", "strict", diff --git a/tests/testthat/test-tidyeval.R b/tests/testthat/test-tidyeval.R index 76b7a2457..5d1c074f6 100644 --- a/tests/testthat/test-tidyeval.R +++ b/tests/testthat/test-tidyeval.R @@ -1,5 +1,3 @@ - - test_that("no spaces within bang-bang operator !!!", { expect_warning(test_collection("tidyeval", "bang_bang", transformer = style_text diff --git a/tests/testthat/test-token_adding_removing.R b/tests/testthat/test-token_adding_removing.R index cd0026a0f..47ad6703d 100644 --- a/tests/testthat/test-token_adding_removing.R +++ b/tests/testthat/test-token_adding_removing.R @@ -1,5 +1,3 @@ - - test_that("other manipulations are correct (add braces, semi-colon etc.)", { expect_warning(test_collection("token_adding_removing", "mixed_token", transformer = style_text diff --git a/tests/testthat/test-unary.R b/tests/testthat/test-unary.R index 8fe52eb0c..264e58ae7 100644 --- a/tests/testthat/test-unary.R +++ b/tests/testthat/test-unary.R @@ -1,5 +1,3 @@ - - test_that("no spaces before unary operator", { expect_warning(test_collection("unary_spacing", "unary_simple", diff --git a/tests/testthat/test-unindention.R b/tests/testthat/test-unindention.R index cd06de29c..ea48778d1 100644 --- a/tests/testthat/test-unindention.R +++ b/tests/testthat/test-unindention.R @@ -1,5 +1,3 @@ - - test_that("round brackets are unindented correctly", { expect_warning(test_collection("unindention", "mixed", diff --git a/tests/testthat/test-unindention_regex.R b/tests/testthat/test-unindention_regex.R index 7e364fa46..1eace2b0b 100644 --- a/tests/testthat/test-unindention_regex.R +++ b/tests/testthat/test-unindention_regex.R @@ -1,4 +1,3 @@ - test_that("forced regex token-dependent indention", { expect_warning(test_collection( "unindention_regex", "regex_force_with", diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index ed43923ff..f11e241fd 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -1,5 +1,3 @@ - - test_that("non-comment-helpers", { pd <- compute_parse_data_nested("a <- # hi \n x %>% b()") child <- pd$child[[1]] diff --git a/tests/testthat/test-varia.R b/tests/testthat/test-varia.R index 422363a0d..effc0f865 100644 --- a/tests/testthat/test-varia.R +++ b/tests/testthat/test-varia.R @@ -1,5 +1,3 @@ - - test_that("ensure_last_n_empty", { expect_equal( ensure_last_n_empty("x"), diff --git a/tests/testthat/test-zzz.R b/tests/testthat/test-zzz.R index 3b0b8da0c..dbbee5082 100644 --- a/tests/testthat/test-zzz.R +++ b/tests/testthat/test-zzz.R @@ -1,16 +1,13 @@ -test_that("styler tests did not use R.cache in user root", { +test_that("can delete empty cache directory", { + skip_if(getRversion() < package_version("4.0.0")) skip_on_cran() - skip_on_covr() - expect_true( - length(list.files(R.cache::getCachePath("styler"), recursive = TRUE)) == 0L - ) -}) - -test_that("clear Cache", { - R.cache::clearCache(R.cache::getCachePath("styler"), recursive = TRUE) - skip_on_cran() - skip_on_covr() - expect_true( - length(list.dirs(R.cache::getCachePath("styler"))) == 1L - ) + tmpdir <- withr::local_tempdir() + withr::local_dir(tmpdir) + dir.create("xxx") + expect_true(delete_if_cache_directory(file.path(getwd(), "xxx"))) + dir.create("xxx") + file.create("xxx/yyy") + list.files("xxx") + expect_false(delete_if_cache_directory(file.path(getwd(), "xxx"))) + expect_true(file.exists(tmpdir)) }) diff --git a/tests/testthat/tests-cache-require-serial.R b/tests/testthat/tests-cache-require-serial.R index 96c861247..ae7226b3c 100644 --- a/tests/testthat/tests-cache-require-serial.R +++ b/tests/testthat/tests-cache-require-serial.R @@ -25,7 +25,7 @@ test_that("top-level test: Caches top-level expressions efficiently on style_tex partially_cached_benchmark["elapsed"] * 1.5, not_cached_benchmark["elapsed"] ) - expect_lt(full_cached_benchmark["elapsed"] * 35, benchmark["elapsed"]) + expect_lt(full_cached_benchmark["elapsed"] * 30, benchmark["elapsed"]) }) diff --git a/vignettes/caching.Rmd b/vignettes/caching.Rmd index b5f9a7553..b15c7b92a 100644 --- a/vignettes/caching.Rmd +++ b/vignettes/caching.Rmd @@ -12,6 +12,8 @@ knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) +options(styler.colored_print.vertical = FALSE) +styler::cache_deactivate() ``` ```{r setup} diff --git a/vignettes/customizing_styler.Rmd b/vignettes/customizing_styler.Rmd index 5a681c435..7742b9e18 100644 --- a/vignettes/customizing_styler.Rmd +++ b/vignettes/customizing_styler.Rmd @@ -7,6 +7,15 @@ vignette: > %\VignetteEncoding{UTF-8} --- +```{r} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +options(styler.colored_print.vertical = FALSE) +styler::cache_deactivate() +``` + This vignette provides a high-level overview of how styler works and how you can define your own style guide and format code according to it. If you simply want to customize the tidyverse style guide to your needs, check out `vignette("styler")`, to remove some rules, have a look at `vignette("remove_rules")`. How to distribute a custom style guide is described in `vignette("distribute_custom_style_guides")`. # How styler works @@ -23,8 +32,8 @@ The `transformers` argument is, apart from the code to style, the key argument o ```{r, message = FALSE} library("styler") +library("magrittr") cache_deactivate() -library("dplyr") names(tidyverse_style()) str(tidyverse_style(), give.attr = FALSE, list.len = 3) ``` @@ -41,8 +50,9 @@ As the name says, this function removes spaces after the opening parenthesis. Bu string_to_format <- "call( 3)" pd <- styler:::compute_parse_data_nested(string_to_format) %>% styler:::pre_visit_one(default_style_guide_attributes) -pd$child[[1]] %>% - select(token, terminal, text, newlines, spaces) + +cols <- c('token', 'terminal', 'text', 'newlines', 'spaces') +pd$child[[1]][, cols] ``` `default_style_guide_attributes()` is called to initialize some variables, it does not actually transform the parse table. @@ -50,8 +60,7 @@ pd$child[[1]] %>% All the function `remove_space_after_opening_paren()` now does is to look for the opening bracket and set the column `spaces` of the token to zero. Note that it is very important to check whether there is also a line break following after that token. If so, `spaces` should not be touched because of the way `spaces` and `newlines` are defined. `spaces` are the number of spaces after a token and `newlines`. Hence, if a line break follows, spaces are not EOL spaces, but rather the spaces directly before the next token. If there was a line break after the token and the rule did not check for that, indention for the token following `(` would be removed. This would be unwanted for example if `use_raw_indention` is set to `TRUE` (which means indention should not be touched). If we apply the rule to our parse table, we can see that the column `spaces` changes and is now zero for all tokens: ```{r} -styler:::remove_space_after_opening_paren(pd$child[[1]]) %>% - select(token, terminal, text, newlines, spaces) +styler:::remove_space_after_opening_paren(pd$child[[1]])[, cols] ``` All top-level styling functions have a `style` argument (which defaults to `tidyverse_style`). If you check out the help file, you can see that the argument `style` is only used to create the default `transformers` argument, which defaults to `style(...)`. This allows for the styling options to be set without having to specify them inside the function passed to `transformers`. diff --git a/vignettes/detect-alignment.Rmd b/vignettes/detect-alignment.Rmd index 937d2921e..b3872d7f5 100644 --- a/vignettes/detect-alignment.Rmd +++ b/vignettes/detect-alignment.Rmd @@ -8,7 +8,12 @@ vignette: > --- ```{r, include=FALSE} -knitr::opts_chunk$set(eval = FALSE) +knitr::opts_chunk$set( + eval = FALSE, + collapse = TRUE, + comment = "#>" +) +styler::cache_deactivate() ``` # Overview diff --git a/vignettes/distribute_custom_style_guides.Rmd b/vignettes/distribute_custom_style_guides.Rmd index 0284facc8..031a13876 100644 --- a/vignettes/distribute_custom_style_guides.Rmd +++ b/vignettes/distribute_custom_style_guides.Rmd @@ -12,6 +12,8 @@ knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) +styler::cache_deactivate() +options(styler.colored_print.vertical = FALSE) ``` This vignette describes how you can distribute your own style guide. It builds on `vignette("customizing_styler")` and assumes you understand how to create a style guide with `create_style_guide()`. diff --git a/vignettes/remove_rules.Rmd b/vignettes/remove_rules.Rmd index 11c79ae63..657538c76 100644 --- a/vignettes/remove_rules.Rmd +++ b/vignettes/remove_rules.Rmd @@ -15,6 +15,7 @@ knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) +styler::cache_deactivate() ``` ```{r, echo = FALSE, include = FALSE} @@ -25,8 +26,7 @@ If you want to change the behavior of styler to match your desired style, there are multiple ways: - Use the tidyverse style guide, but not with the default options. Starting - point for this approach is the [help - file](https://styler.r-lib.org/reference/tidyverse_style.html) for the + point for this approach is the `help("tidyverse_style")` for the function `tidyverse_style()`, which returns the transformer functions that prettify your code. Most of these options are explained in `vignette("styler")`. @@ -61,7 +61,9 @@ Here are the steps required to deactivate a rule you don't like Lets assume you want to remove the rule that turns `=` into `<-` for assignment. That means you want - string = "hi there" +``` +string = "hi there" +``` to remain unchanged after applying styler. This is not the case if you use the default style guide of styler: @@ -164,7 +166,7 @@ styler. - Then pinpoint the probable rule type (e.g. line breaks if you want less new lines). -- In a local styler clone, add e.g. a `return(pd` at the top of the body to +- In a local styler clone, add e.g. a `return(pd)` at the top of the body to deactivate the rule quickly, or add a `print(pd)` or `browser()` call in the functions of that type (e.g. the different functions of `R/rules-line-breaks.R`), `load_all()`, run your example, see if that diff --git a/vignettes/strict.Rmd b/vignettes/strict.Rmd index f73d98679..880a5db10 100644 --- a/vignettes/strict.Rmd +++ b/vignettes/strict.Rmd @@ -14,6 +14,8 @@ knitr::opts_chunk$set( results = "hide" ) +styler::cache_deactivate() + knitr::knit_engines$set(list( styler = function(options) { options$comment <- "" diff --git a/vignettes/styler.Rmd b/vignettes/styler.Rmd index 2428465fa..dcb451adc 100644 --- a/vignettes/styler.Rmd +++ b/vignettes/styler.Rmd @@ -21,6 +21,7 @@ knitr::knit_engines$set(list( )) options(styler.colored_print.vertical = FALSE) +styler::cache_deactivate() ``` # Entry-points @@ -35,14 +36,18 @@ styler provides the following API to format code: - RStudio Addins for styling the active file, styling the current package and styling the highlighted selection, see `help("styler_addins")`. -Beyond that, styler can be used through other tools documented in the `vignette("third-party-integrations")`. +Beyond that, styler can be used through other tools documented in the `vignette("third-party-integrations")`. Let's get started. +```{r} +library(styler) +``` + + ### Passing arguments to the style guide styler separates the abstract definition of a style guide from the application of it. That's why you must supply a style guide via `transformers` when styling (in case you don't want to rely on the defaults): ```{r} -library(styler) style_text("a + b", transformers = tidyverse_style(scope = "indention")) ```