Skip to content

Commit

Permalink
Merge pull request #1176 from olivroy/scope
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil authored Feb 2, 2024
2 parents dbaa169 + 2bb93a4 commit 775c399
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 68 deletions.
2 changes: 1 addition & 1 deletion API
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 11 additions & 10 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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."
))
}

Expand All @@ -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(),
Expand Down Expand Up @@ -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 == "") {
Expand Down
2 changes: 1 addition & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
if (getRversion() < "4.2") {
is_unicode_parsing_error <- grepl("^\"<U\\+[0-9]+>\"$", 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}"
))
}
}
Expand Down
4 changes: 2 additions & 2 deletions R/set-assert-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)."
))
}
}
Expand Down
9 changes: 2 additions & 7 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,15 @@ 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")))
#' @family third-party style guide helpers
#' @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)
Expand Down
9 changes: 4 additions & 5 deletions R/styler-package.R
Original file line number Diff line number Diff line change
@@ -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)")
Expand Down
7 changes: 4 additions & 3 deletions R/stylerignore.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}."
))
}

Expand Down
2 changes: 1 addition & 1 deletion R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion R/transform-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/ui-caching.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions R/ui-styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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."
))
}

Expand Down
7 changes: 4 additions & 3 deletions man/prettify_any.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/prettify_pkg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/style_dir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions man/style_pkg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/styler-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions tests/testthat/test-public_api-3.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down
2 changes: 1 addition & 1 deletion vignettes/styler.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 775c399

Please sign in to comment.