Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for scope #1176

Merged
merged 8 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 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 .R, .qmd, .Rmd, and .Rnw files.")
}
rstudioapi::modifyRange(
c(1L, 1L, length(context$contents) + 1L, 1L),
Expand Down Expand Up @@ -98,10 +98,10 @@ save_after_styling_is_active <- function() {
op_new <- getOption("styler.save_after_styling", default = "")
if (!is.na(op_old)) {
rlang::warn(paste(
olivroy marked this conversation as resolved.
Show resolved Hide resolved
"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."
"Using the environment variable `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. If both are set,",
"the R option is used."
))
}

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
13 changes: 6 additions & 7 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,19 @@ 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,
olivroy marked this conversation as resolved.
Show resolved Hide resolved
values = levels,
multiple = TRUE
)

if (inherits(scope, "AsIs")) {
factor(as.character(scope), levels = levels, ordered = TRUE)
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/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 an R, Rmd, qmd, or Rnw file.")
olivroy marked this conversation as resolved.
Show resolved Hide resolved
}
}

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
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
Loading