diff --git a/API b/API index 429de55f4..e22a357e4 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", "R/import-standalone.*\\.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/R/addins.R b/R/addins.R index 61dcb24e3..8cbea4cb9 100644 --- a/R/addins.R +++ b/R/addins.R @@ -68,7 +68,7 @@ style_active_file <- function() { } else if (is_r_file) { out <- try_transform_as_r_file(context, transformer) } else { - abort("Can only style .R, .Rmd and .Rnw files.") + abort("Can only style .qmd, .R, .Rmd, and .Rnw files.") } rstudioapi::modifyRange( c(1L, 1L, length(context$contents) + 1L, 1L), @@ -97,11 +97,12 @@ save_after_styling_is_active <- function() { op_old <- as.logical(toupper(Sys.getenv("save_after_styling"))) op_new <- getOption("styler.save_after_styling", default = "") if (!is.na(op_old)) { - rlang::warn(paste( - "Using the environment variable save_after_styling is depreciated and", - "won't work in a future version of styler. Please use the R option", - "`styler.save_after_styling` to control the behavior. If both are set,", - "the R option is taken." + cli::cli_warn(c( + "Using the environment variable {.envvar save_after_styling} is \\ + deprecated and won't work in a future version of styler. ", + "!" = "Please use `options(styler.save_after_styling)` \\ + to control the behavior.", + i = "If both are set, the R option is used." )) } @@ -123,7 +124,7 @@ style_selection <- function() { communicate_addins_style_transformers() context <- get_rstudio_context() text <- context$selection[[1L]]$text - if (all(nchar(text) == 0L)) abort("No code selected") + if (!any(nzchar(text))) abort("No code selected") out <- style_text( text, transformers = get_addins_style_transformer(), @@ -212,9 +213,9 @@ try_transform_as_r_file <- function(context, transformer) { transformer(context$contents), error = function(e) { preamble_for_unsaved <- paste( - "Styling of unsaved files is only supported for R files with valid ", - "code. Please save the file (as .R or .Rmd) and make sure that the R ", - "code in it can be parsed. Then, try to style again." + "Styling of unsaved files is only supported for R files with valid", + "code. Please save the file (as .qmd, .R, or .Rmd) and make sure that", + "the R code in it can be parsed. Then, try to style again." ) if (context$path == "") { diff --git a/R/io.R b/R/io.R index 26ad6d9fe..fdb4260fb 100644 --- a/R/io.R +++ b/R/io.R @@ -19,7 +19,7 @@ transform_utf8 <- function(path, fun, dry) { #' styling are not identical. #' @keywords internal transform_utf8_one <- function(path, fun, dry) { - rlang::arg_match(dry, c("on", "off", "fail")) + rlang::arg_match0(dry, c("on", "off", "fail")) rlang::try_fetch( { file_with_info <- read_utf8(path) diff --git a/R/parse.R b/R/parse.R index 629203934..cf2babe49 100644 --- a/R/parse.R +++ b/R/parse.R @@ -95,10 +95,10 @@ get_parse_data <- function(text, include_text = TRUE, ...) { if (getRversion() < "4.2") { is_unicode_parsing_error <- grepl("^\"\"$", pd$text) if (any(is_unicode_parsing_error)) { - rlang::abort(paste0( - "Can't parse input due to unicode restriction in base R. Please ", - "upgrade R to >= 4.2 to style this input. ", - "Context: https://github.com/r-lib/styler/issues/847" + cli::cli_abort(c( + "Can't parse input due to unicode restriction in base R.", + i = "Please upgrade R to >= 4.2 to style this input.", + "Context: {.url https://github.com/r-lib/styler/issues/847}" )) } } diff --git a/R/set-assert-args.R b/R/set-assert-args.R index 7cccd0aa5..649a57f9a 100644 --- a/R/set-assert-args.R +++ b/R/set-assert-args.R @@ -73,8 +73,8 @@ assert_filetype <- function(lowercase_filetype) { allowed_types <- c("r", "rmd", "rmarkdown", "rnw", "rprofile", "qmd") if (!all(lowercase_filetype %in% allowed_types)) { abort(paste( - "filetype must not contain other values than 'R', 'Rprofile',", - "'Rmd', 'Rmarkdown', 'qmd' or 'Rnw' (case is ignored)." + "filetype must not contain other values than 'qmd', 'R',", + "'Rmarkdown', 'Rmd', 'Rnw', or 'Rprofile' (case is ignored)." )) } } diff --git a/R/style-guides.R b/R/style-guides.R index e6a68ae7d..edff2cba1 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -471,7 +471,7 @@ tidyverse_reindention <- function() { #' @param scope A character vector of length one or a vector of class `AsIs`. #' @param name The name of the character vector to be displayed if the #' construction of the factor fails. - +#' #' @examples #' scope_normalize(I("tokens")) #' scope_normalize(I(c("indention", "tokens"))) @@ -479,12 +479,7 @@ tidyverse_reindention <- function() { #' @export scope_normalize <- function(scope, name = substitute(scope)) { levels <- c("none", "spaces", "indention", "line_breaks", "tokens") - if (!all((scope %in% levels))) { - abort(paste( - "all values in", name, "must be one of the following:", - toString(levels) - )) - } + rlang::arg_match(scope, values = levels, multiple = TRUE) if (inherits(scope, "AsIs")) { factor(as.character(scope), levels = levels, ordered = TRUE) diff --git a/R/styler-package.R b/R/styler-package.R index 2a688583d..c307a9284 100644 --- a/R/styler-package.R +++ b/R/styler-package.R @@ -1,14 +1,13 @@ -#' Non-invasive pretty printing of R code -#' -#' styler allows you to format `.R`, `.Rmd`, `.Rmarkdown` and/or -#' `.qmd`, `.Rnw` files, R packages, or entire R source trees +#' @description +#' styler allows you to format `.qmd`, `.R`, `.Rmd`, `.Rmarkdown`, +#' `.Rnw`, and/or `.Rprofile` files, R packages, or entire R source trees #' according to a style guide. #' The following functions can be used for styling: #' * [style_text()] to style a character vector. #' * [style_file()] to style a single file. #' * [style_dir()] to style all files in a directory. #' * [style_pkg()] to style the source files of an R package. -#' * [styler_addins] (RStudio Addins) to style either selected code or the +#' * [RStudio Addins][styler_addins] to style either selected code or the #' active file. #' @examples #' style_text("call( 1)") diff --git a/R/stylerignore.R b/R/stylerignore.R index 4cf0b30bd..64fc05337 100644 --- a/R/stylerignore.R +++ b/R/stylerignore.R @@ -76,9 +76,10 @@ 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)) { - cli::cli_warn(paste0( - "Invalid stylerignore sequences found, potentially ignoring some of the ", - "markers set.\nSee {.help styler::stylerignore}." + cli::cli_warn(c( + "Invalid stylerignore sequences found, potentially ignoring some of the \\ + markers set.", + i = "See {.topic styler::stylerignore}." )) } diff --git a/R/testing.R b/R/testing.R index 4b94d7e1b..f2c8ffe6b 100644 --- a/R/testing.R +++ b/R/testing.R @@ -237,7 +237,7 @@ n_times_faster_with_cache <- function(x1, x2 = x1, ..., fun = styler::style_text, n = 3L, clear = "always") { - rlang::arg_match(clear, c("always", "final", "never", "all but last")) + rlang::arg_match0(clear, c("always", "final", "never", "all but last")) out <- purrr::map(1L:n, n_times_faster_bench, x1 = x1, x2 = x2, fun = fun, diff --git a/R/transform-code.R b/R/transform-code.R index 4f2b0337d..91b495e3a 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, qmd, or Rnw file")) + cli::cli_abort("{.path {path}} is not a qmd, R, Rmd, or Rnw file.") } } diff --git a/R/ui-caching.R b/R/ui-caching.R index 42e80f529..1ab58e1c4 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -93,7 +93,7 @@ NULL #' @family cache managers #' @export cache_info <- function(cache_name = NULL, format = "both") { - rlang::arg_match(format, c("tabular", "lucid", "both")) + rlang::arg_match0(format, c("tabular", "lucid", "both")) path_cache <- cache_find_path(cache_name) files <- list.files(path_cache, full.names = TRUE) file_info <- file.info(files) diff --git a/R/ui-styling.R b/R/ui-styling.R index e25eed10c..97e250aa2 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -68,7 +68,7 @@ style_pkg <- function(pkg = ".", ..., style = tidyverse_style, transformers = style(...), - filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), + 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, @@ -87,10 +87,10 @@ style_pkg <- function(pkg = ".", #' Prettify a package #' #' @param filetype Vector of file extensions indicating which file types should -#' be styled. Case is ignored, and the `.` is optional, e.g. `c(".R", -#' ".Rmd")`, or `c("r", "rmd")`. Supported values (after standardization) are: -#' "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as -#' Rmd. +#' be styled. Case is ignored, and the `.` is optional, e.g. `c(".R",".Rmd")`, +#' or `c("r", "rmd")`. Supported values (after standardization) are: +#' "qmd", "r", "rmd", "rmarkdown", "rnw", and "rprofile". +#' Rmarkdown is treated as Rmd. #' @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 diff --git a/R/zzz.R b/R/zzz.R index 9bdc76c48..c9b03c603 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -66,7 +66,7 @@ ask_to_switch_to_non_default_cache_root <- function(ask = interactive()) { ask_to_switch_to_non_default_cache_root_impl <- function() { cli::cli_inform(paste0( "{{styler}} cache is cleared after 6 days. ", - "See {.help styler::caching} to configure differently or silence this message." + "See {.topic styler::caching} to configure differently or silence this message." )) } diff --git a/man/prettify_any.Rd b/man/prettify_any.Rd index b1db51e44..89d8a72b2 100644 --- a/man/prettify_any.Rd +++ b/man/prettify_any.Rd @@ -21,9 +21,10 @@ conveniently constructed via the \code{style} argument and \code{...}. See 'Examples'.} \item{filetype}{Vector of file extensions indicating which file types should -be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".Rmd")}, or \code{c("r", "rmd")}. Supported values (after standardization) are: -"r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as -Rmd.} +be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R",".Rmd")}, +or \code{c("r", "rmd")}. Supported values (after standardization) are: +"qmd", "r", "rmd", "rmarkdown", "rnw", and "rprofile". +Rmarkdown is treated as Rmd.} \item{recursive}{A logical value indicating whether or not files in subdirectories should be styled as well.} diff --git a/man/prettify_pkg.Rd b/man/prettify_pkg.Rd index 0c999550c..d973e8a90 100644 --- a/man/prettify_pkg.Rd +++ b/man/prettify_pkg.Rd @@ -19,9 +19,10 @@ prettify_pkg( parse tables.} \item{filetype}{Vector of file extensions indicating which file types should -be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".Rmd")}, or \code{c("r", "rmd")}. Supported values (after standardization) are: -"r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as -Rmd.} +be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R",".Rmd")}, +or \code{c("r", "rmd")}. Supported values (after standardization) are: +"qmd", "r", "rmd", "rmarkdown", "rnw", and "rprofile". +Rmarkdown is treated as Rmd.} \item{exclude_files}{Character vector with regular expressions to files that should be excluded from styling.} diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 7f0f61a58..62c80e264 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -34,9 +34,10 @@ conveniently constructed via the \code{style} argument and \code{...}. See 'Examples'.} \item{filetype}{Vector of file extensions indicating which file types should -be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".Rmd")}, or \code{c("r", "rmd")}. Supported values (after standardization) are: -"r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as -Rmd.} +be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R",".Rmd")}, +or \code{c("r", "rmd")}. Supported values (after standardization) are: +"qmd", "r", "rmd", "rmarkdown", "rnw", and "rprofile". +Rmarkdown is treated as Rmd.} \item{recursive}{A logical value indicating whether or not files in sub directories of \code{path} should be styled as well.} diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index 0eca9278e..637cbc9a7 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -9,7 +9,7 @@ style_pkg( ..., style = tidyverse_style, transformers = style(...), - filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), + 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"), @@ -34,9 +34,10 @@ conveniently constructed via the \code{style} argument and \code{...}. See 'Examples'.} \item{filetype}{Vector of file extensions indicating which file types should -be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".Rmd")}, or \code{c("r", "rmd")}. Supported values (after standardization) are: -"r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as -Rmd.} +be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R",".Rmd")}, +or \code{c("r", "rmd")}. Supported values (after standardization) are: +"qmd", "r", "rmd", "rmarkdown", "rnw", and "rprofile". +Rmarkdown is treated as Rmd.} \item{exclude_files}{Character vector with regular expressions to files that should be excluded from styling.} diff --git a/man/styler-package.Rd b/man/styler-package.Rd index 0995015ad..94adfc316 100644 --- a/man/styler-package.Rd +++ b/man/styler-package.Rd @@ -4,10 +4,10 @@ \name{styler-package} \alias{styler} \alias{styler-package} -\title{Non-invasive pretty printing of R code} +\title{styler: Non-Invasive Pretty Printing of R Code} \description{ -styler allows you to format \code{.R}, \code{.Rmd}, \code{.Rmarkdown} and/or -\code{.qmd}, \code{.Rnw} files, R packages, or entire R source trees +styler allows you to format \code{.qmd}, \code{.R}, \code{.Rmd}, \code{.Rmarkdown}, +\code{.Rnw}, and/or \code{.Rprofile} files, R packages, or entire R source trees according to a style guide. The following functions can be used for styling: \itemize{ @@ -15,7 +15,7 @@ The following functions can be used for styling: \item \code{\link[=style_file]{style_file()}} to style a single file. \item \code{\link[=style_dir]{style_dir()}} to style all files in a directory. \item \code{\link[=style_pkg]{style_pkg()}} to style the source files of an R package. -\item \link{styler_addins} (RStudio Addins) to style either selected code or the +\item \link[=styler_addins]{RStudio Addins} to style either selected code or the active file. } } diff --git a/tests/testthat/test-public_api-3.R b/tests/testthat/test-public_api-3.R index 8b1e926fb..de74e8bfd 100644 --- a/tests/testthat/test-public_api-3.R +++ b/tests/testthat/test-public_api-3.R @@ -120,31 +120,31 @@ test_that("scope can be specified as is", { test_that("Can properly determine style_after_saving", { withr::with_envvar(list(save_after_styling = TRUE), { - expect_warning(op <- save_after_styling_is_active(), "is depreciated") - expect_equal(op, TRUE) + expect_warning(op <- save_after_styling_is_active(), "is deprecated") + expect_true(op) }) withr::with_envvar(list(save_after_styling = FALSE), { - expect_warning(op <- save_after_styling_is_active(), "is depreciated") - expect_equal(op, FALSE) + expect_warning(op <- save_after_styling_is_active(), "is deprecated") + expect_false(op) }) withr::with_options(list(styler.save_after_styling = TRUE), { expect_silent(op <- save_after_styling_is_active()) - expect_equal(op, TRUE) + expect_true(op) }) withr::with_options(list(styler.save_after_styling = TRUE), { withr::with_envvar(list(save_after_styling = FALSE), { - expect_warning(op <- save_after_styling_is_active(), "is depreciated") - expect_equal(op, TRUE) + expect_warning(op <- save_after_styling_is_active(), "is deprecated") + expect_true(op) }) }) withr::with_options(list(styler.save_after_styling = FALSE), { expect_silent(op <- save_after_styling_is_active()) - expect_equal(op, FALSE) + expect_false(op) }) }) diff --git a/vignettes/styler.Rmd b/vignettes/styler.Rmd index dcb451adc..1b5387112 100644 --- a/vignettes/styler.Rmd +++ b/vignettes/styler.Rmd @@ -28,7 +28,7 @@ styler::cache_deactivate() styler provides the following API to format code: -- `style_file()` styles `.R`, `.qmd`, `.Rmd`, `.Rmarkdown`, `.Rnw`, and `.Rprofile` files. +- `style_file()` styles `.qmd`, `.R`, `.Rmd`, `.Rmarkdown`, `.Rnw`, and `.Rprofile` files. - `style_dir()` styles all these files in a directory.