Skip to content

Commit 775c399

Browse files
Merge pull request #1176 from olivroy/scope
2 parents dbaa169 + 2bb93a4 commit 775c399

20 files changed

+68
-68
lines changed

API

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ specify_reindention(regex_pattern = NULL, indention = 0L, comments_only = TRUE)
2727
specify_transformers_drop(spaces = NULL, indention = NULL, line_breaks = NULL, tokens = NULL)
2828
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")
2929
style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off")
30-
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")
30+
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")
3131
style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L)
3232
tidyverse_math_token_spacing()
3333
tidyverse_reindention()

R/addins.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ style_active_file <- function() {
6868
} else if (is_r_file) {
6969
out <- try_transform_as_r_file(context, transformer)
7070
} else {
71-
abort("Can only style .R, .Rmd and .Rnw files.")
71+
abort("Can only style .qmd, .R, .Rmd, and .Rnw files.")
7272
}
7373
rstudioapi::modifyRange(
7474
c(1L, 1L, length(context$contents) + 1L, 1L),
@@ -97,11 +97,12 @@ save_after_styling_is_active <- function() {
9797
op_old <- as.logical(toupper(Sys.getenv("save_after_styling")))
9898
op_new <- getOption("styler.save_after_styling", default = "")
9999
if (!is.na(op_old)) {
100-
rlang::warn(paste(
101-
"Using the environment variable save_after_styling is depreciated and",
102-
"won't work in a future version of styler. Please use the R option",
103-
"`styler.save_after_styling` to control the behavior. If both are set,",
104-
"the R option is taken."
100+
cli::cli_warn(c(
101+
"Using the environment variable {.envvar save_after_styling} is \\
102+
deprecated and won't work in a future version of styler. ",
103+
"!" = "Please use `options(styler.save_after_styling)` \\
104+
to control the behavior.",
105+
i = "If both are set, the R option is used."
105106
))
106107
}
107108

@@ -123,7 +124,7 @@ style_selection <- function() {
123124
communicate_addins_style_transformers()
124125
context <- get_rstudio_context()
125126
text <- context$selection[[1L]]$text
126-
if (all(nchar(text) == 0L)) abort("No code selected")
127+
if (!any(nzchar(text))) abort("No code selected")
127128
out <- style_text(
128129
text,
129130
transformers = get_addins_style_transformer(),
@@ -212,9 +213,9 @@ try_transform_as_r_file <- function(context, transformer) {
212213
transformer(context$contents),
213214
error = function(e) {
214215
preamble_for_unsaved <- paste(
215-
"Styling of unsaved files is only supported for R files with valid ",
216-
"code. Please save the file (as .R or .Rmd) and make sure that the R ",
217-
"code in it can be parsed. Then, try to style again."
216+
"Styling of unsaved files is only supported for R files with valid",
217+
"code. Please save the file (as .qmd, .R, or .Rmd) and make sure that",
218+
"the R code in it can be parsed. Then, try to style again."
218219
)
219220

220221
if (context$path == "") {

R/io.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ transform_utf8 <- function(path, fun, dry) {
1919
#' styling are not identical.
2020
#' @keywords internal
2121
transform_utf8_one <- function(path, fun, dry) {
22-
rlang::arg_match(dry, c("on", "off", "fail"))
22+
rlang::arg_match0(dry, c("on", "off", "fail"))
2323
rlang::try_fetch(
2424
{
2525
file_with_info <- read_utf8(path)

R/parse.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ get_parse_data <- function(text, include_text = TRUE, ...) {
9595
if (getRversion() < "4.2") {
9696
is_unicode_parsing_error <- grepl("^\"<U\\+[0-9]+>\"$", pd$text)
9797
if (any(is_unicode_parsing_error)) {
98-
rlang::abort(paste0(
99-
"Can't parse input due to unicode restriction in base R. Please ",
100-
"upgrade R to >= 4.2 to style this input. ",
101-
"Context: https://github.com/r-lib/styler/issues/847"
98+
cli::cli_abort(c(
99+
"Can't parse input due to unicode restriction in base R.",
100+
i = "Please upgrade R to >= 4.2 to style this input.",
101+
"Context: {.url https://github.com/r-lib/styler/issues/847}"
102102
))
103103
}
104104
}

R/set-assert-args.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ assert_filetype <- function(lowercase_filetype) {
7373
allowed_types <- c("r", "rmd", "rmarkdown", "rnw", "rprofile", "qmd")
7474
if (!all(lowercase_filetype %in% allowed_types)) {
7575
abort(paste(
76-
"filetype must not contain other values than 'R', 'Rprofile',",
77-
"'Rmd', 'Rmarkdown', 'qmd' or 'Rnw' (case is ignored)."
76+
"filetype must not contain other values than 'qmd', 'R',",
77+
"'Rmarkdown', 'Rmd', 'Rnw', or 'Rprofile' (case is ignored)."
7878
))
7979
}
8080
}

R/style-guides.R

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,20 +471,15 @@ tidyverse_reindention <- function() {
471471
#' @param scope A character vector of length one or a vector of class `AsIs`.
472472
#' @param name The name of the character vector to be displayed if the
473473
#' construction of the factor fails.
474-
474+
#'
475475
#' @examples
476476
#' scope_normalize(I("tokens"))
477477
#' scope_normalize(I(c("indention", "tokens")))
478478
#' @family third-party style guide helpers
479479
#' @export
480480
scope_normalize <- function(scope, name = substitute(scope)) {
481481
levels <- c("none", "spaces", "indention", "line_breaks", "tokens")
482-
if (!all((scope %in% levels))) {
483-
abort(paste(
484-
"all values in", name, "must be one of the following:",
485-
toString(levels)
486-
))
487-
}
482+
rlang::arg_match(scope, values = levels, multiple = TRUE)
488483

489484
if (inherits(scope, "AsIs")) {
490485
factor(as.character(scope), levels = levels, ordered = TRUE)

R/styler-package.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
#' Non-invasive pretty printing of R code
2-
#'
3-
#' styler allows you to format `.R`, `.Rmd`, `.Rmarkdown` and/or
4-
#' `.qmd`, `.Rnw` files, R packages, or entire R source trees
1+
#' @description
2+
#' styler allows you to format `.qmd`, `.R`, `.Rmd`, `.Rmarkdown`,
3+
#' `.Rnw`, and/or `.Rprofile` files, R packages, or entire R source trees
54
#' according to a style guide.
65
#' The following functions can be used for styling:
76
#' * [style_text()] to style a character vector.
87
#' * [style_file()] to style a single file.
98
#' * [style_dir()] to style all files in a directory.
109
#' * [style_pkg()] to style the source files of an R package.
11-
#' * [styler_addins] (RStudio Addins) to style either selected code or the
10+
#' * [RStudio Addins][styler_addins] to style either selected code or the
1211
#' active file.
1312
#' @examples
1413
#' style_text("call( 1)")

R/stylerignore.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ add_stylerignore <- function(pd_flat) {
7676
pd_flat$indicator_off <- cumsum_start + cumsum_stop
7777
is_invalid <- cumsum_start - cumsum_stop < 0L | cumsum_start - cumsum_stop > 1L
7878
if (any(is_invalid)) {
79-
cli::cli_warn(paste0(
80-
"Invalid stylerignore sequences found, potentially ignoring some of the ",
81-
"markers set.\nSee {.help styler::stylerignore}."
79+
cli::cli_warn(c(
80+
"Invalid stylerignore sequences found, potentially ignoring some of the \\
81+
markers set.",
82+
i = "See {.topic styler::stylerignore}."
8283
))
8384
}
8485

R/testing.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ n_times_faster_with_cache <- function(x1, x2 = x1, ...,
237237
fun = styler::style_text,
238238
n = 3L,
239239
clear = "always") {
240-
rlang::arg_match(clear, c("always", "final", "never", "all but last"))
240+
rlang::arg_match0(clear, c("always", "final", "never", "all but last"))
241241

242242
out <- purrr::map(1L:n, n_times_faster_bench,
243243
x1 = x1, x2 = x2, fun = fun,

R/transform-code.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ transform_code <- function(path, fun, ..., dry) {
2222
..., dry = dry
2323
)
2424
} else {
25-
abort(paste(path, "is not an R, Rmd, qmd, or Rnw file"))
25+
cli::cli_abort("{.path {path}} is not a qmd, R, Rmd, or Rnw file.")
2626
}
2727
}
2828

0 commit comments

Comments
 (0)