diff --git a/DESCRIPTION b/DESCRIPTION index 7df9745b5..d684c2a17 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,6 +25,7 @@ Imports: rematch2 (>= 2.0.1), rlang (>= 0.1.1), rprojroot (>= 1.1), + rstudioapi (>= 0.7), tibble (>= 1.4.2), tools, withr (>= 1.0.0), @@ -37,7 +38,6 @@ Suggests: knitr, prettycode, rmarkdown, - rstudioapi (>= 0.7), testthat (>= 2.1.0) VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 67f3fcd43..621ce1505 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,9 @@ export(tidyverse_math_token_spacing) export(tidyverse_reindention) export(tidyverse_style) import(tibble) +importFrom(R.cache,clearCache) +importFrom(R.cache,generateCache) +importFrom(R.cache,getCachePath) importFrom(magrittr,"%>%") importFrom(magrittr,set_names) importFrom(purrr,as_mapper) @@ -36,12 +39,19 @@ importFrom(purrr,partial) importFrom(purrr,pmap) importFrom(purrr,pwalk) importFrom(purrr,when) +importFrom(rematch2,re_match) importFrom(rlang,abort) +importFrom(rlang,arg_match) importFrom(rlang,is_empty) importFrom(rlang,is_installed) +importFrom(rlang,is_named) importFrom(rlang,seq2) importFrom(rlang,warn) importFrom(rlang,with_handlers) +importFrom(rstudioapi,getActiveDocumentContext) +importFrom(tools,parse_Rd) importFrom(utils,capture.output) +importFrom(utils,getParseData) +importFrom(utils,head) importFrom(utils,tail) importFrom(utils,write.table) diff --git a/R/addins.R b/R/addins.R index 2edc83d83..3c4cb3023 100644 --- a/R/addins.R +++ b/R/addins.R @@ -105,8 +105,9 @@ style_selection <- function() { } } +#' @importFrom rstudioapi getActiveDocumentContext get_rstudio_context <- function() { - rstudioapi::getActiveDocumentContext() + getActiveDocumentContext() } #' Asks the user to supply a style diff --git a/R/compat-dplyr.R b/R/compat-dplyr.R index 5bf60eec3..6896b3429 100644 --- a/R/compat-dplyr.R +++ b/R/compat-dplyr.R @@ -52,8 +52,9 @@ filter <- function(.data, ...) { subset(.data, ...) } +#' @importFrom rlang is_named left_join <- function(x, y, by) { - if (rlang::is_named(by)) { + if (is_named(by)) { by_x <- names(by) by_y <- unname(by) } else { diff --git a/R/io.R b/R/io.R index 9caad1a67..d6d7d10c0 100644 --- a/R/io.R +++ b/R/io.R @@ -19,10 +19,10 @@ transform_utf8 <- function(path, fun, dry) { #' latter returns an error if the input code is not identical to the result #' of styling. "off", the default, writes back if the input and output of #' styling are not identical. -#' @importFrom rlang with_handlers warn +#' @importFrom rlang with_handlers warn arg_match abort #' @keywords internal transform_utf8_one <- function(path, fun, dry) { - rlang::arg_match(dry, c("on", "off", "fail")) + arg_match(dry, c("on", "off", "fail")) with_handlers( { file_with_info <- read_utf8(path) @@ -32,7 +32,7 @@ transform_utf8_one <- function(path, fun, dry) { identical <- identical_content && !file_with_info$missing_EOF_line_break if (!identical) { if (dry == "fail") { - rlang::abort( + abort( paste0( "File `", path, "` would be modified by styler and argument dry", " is set to 'fail'." @@ -51,7 +51,7 @@ transform_utf8_one <- function(path, fun, dry) { }, error = function(e) { if (inherits(e, "dryError")) { - rlang::abort(conditionMessage(e)) + abort(conditionMessage(e)) } else { warn(paste0("When processing ", path, ": ", conditionMessage(e))) } @@ -69,8 +69,9 @@ transform_utf8_one <- function(path, fun, dry) { #' with this implementation. #' @param path A path to a file to read. #' @keywords internal +#' @importFrom rlang with_handlers abort read_utf8 <- function(path) { - out <- rlang::with_handlers( + out <- with_handlers( read_utf8_bare(path), warning = function(w) w, error = function(e) e @@ -81,7 +82,7 @@ read_utf8 <- function(path) { missing_EOF_line_break = FALSE ) } else if (inherits(out, "error")) { - rlang::abort(out$message) + abort(out$message) } else if (inherits(out, "warning")) { list( text = read_utf8_bare(path, warn = FALSE), @@ -93,7 +94,10 @@ read_utf8 <- function(path) { #' Drop-in replacement for `xfun::read_utf8()`, with an optional `warn` #' argument. #' @keywords internal +#' @importFrom utils head read_utf8_bare <- function(con, warn = TRUE) { + opts <- options(encoding = "native.enc") + on.exit(options(opts), add = TRUE) x <- readLines(con, encoding = "UTF-8", warn = warn) i <- invalid_utf8(x) n <- length(i) @@ -103,7 +107,7 @@ read_utf8_bare <- function(con, warn = TRUE) { "The file ", con, " is not encoded in UTF-8. ", "These lines contain invalid UTF-8 characters: " ), - paste(c(utils::head(i), if (n > 6) "..."), collapse = ", ") + paste(c(head(i), if (n > 6) "..."), collapse = ", ") ) } x diff --git a/R/parse.R b/R/parse.R index ddbeedcc5..64fa62311 100644 --- a/R/parse.R +++ b/R/parse.R @@ -90,12 +90,13 @@ tokenize <- function(text) { #' @param include_text Passed to [utils::getParseData()] as `includeText`. #' @param ... Other arguments passed to [utils::getParseData()]. #' @keywords internal +#' @importFrom utils getParseData get_parse_data <- function(text, include_text = TRUE, ...) { # avoid https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16041 parse_safely(text, keep.source = TRUE) parsed <- parse_safely(text, keep.source = TRUE) pd <- as_tibble( - utils::getParseData(parsed, includeText = include_text), + getParseData(parsed, includeText = include_text), .name_repair = "minimal" ) %>% add_id_and_short() diff --git a/R/roxygen-examples-find.R b/R/roxygen-examples-find.R index 9e7cd1a24..7fb74e522 100644 --- a/R/roxygen-examples-find.R +++ b/R/roxygen-examples-find.R @@ -22,7 +22,7 @@ identify_start_to_stop_of_roxygen_examples_from_text <- function(text) { } identify_start_to_stop_of_roxygen_examples <- function(path) { - content <- xfun::read_utf8(path) + content <- read_utf8_bare(path, warn = FALSE) identify_start_to_stop_of_roxygen_examples_from_text(content) } diff --git a/R/roxygen-examples-parse.R b/R/roxygen-examples-parse.R index 129037f86..c1c14a87e 100644 --- a/R/roxygen-examples-parse.R +++ b/R/roxygen-examples-parse.R @@ -14,11 +14,12 @@ #' "#'1+ 1" #' )) #' @keywords internal +#' @importFrom tools parse_Rd parse_roxygen <- function(roxygen) { connection <- remove_roxygen_mask(roxygen) %>% textConnection() parsed <- connection %>% - tools::parse_Rd(fragment = TRUE) %>% + parse_Rd(fragment = TRUE) %>% as.character(deparse = TRUE) is_line_break <- parsed[1] == "\n" close(connection) diff --git a/R/rules-spacing.R b/R/rules-spacing.R index cfc1ee268..6f112e817 100644 --- a/R/rules-spacing.R +++ b/R/rules-spacing.R @@ -292,6 +292,7 @@ set_space_between_levels <- function(pd_flat) { #' @param force_one Whether or not to force one space or allow multiple spaces #' after the regex "^#+'*". #' @importFrom purrr map_chr +#' @importFrom rematch2 re_match #' @keywords internal start_comments_with_space <- function(pd, force_one = FALSE) { is_comment <- is_comment(pd) @@ -305,7 +306,7 @@ start_comments_with_space <- function(pd, force_one = FALSE) { return(pd) } - comments <- rematch2::re_match( + comments <- re_match( pd$text[is_comment], "^(?#+['\\*]*)(? *)(?.*)$" ) diff --git a/R/style-guides.R b/R/style-guides.R index d8e79b066..f89eb06ac 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -86,7 +86,7 @@ tidyverse_style <- function(scope = "tokens", style_space_around_tilde, strict = strict ), - spacing_around_op = purrr::partial(set_space_around_op, + spacing_around_op = partial(set_space_around_op, strict = strict ), remove_space_after_opening_paren, @@ -140,7 +140,7 @@ tidyverse_style <- function(scope = "tokens", except_token_before = "COMMENT" ) }, - purrr::partial(remove_line_break_in_fun_call, strict = strict), + partial(remove_line_break_in_fun_call, strict = strict), add_line_break_after_pipe = if (strict) add_line_break_after_pipe, set_linebreak_after_ggplot2_plus = if (strict) set_linebreak_after_ggplot2_plus ) diff --git a/R/ui-caching.R b/R/ui-caching.R index 585b0b04c..f14f1988c 100644 --- a/R/ui-caching.R +++ b/R/ui-caching.R @@ -12,10 +12,11 @@ #' potentially different with different versions of styler. #' @param ask Whether or not to interactively ask the user again. #' @family cache managers +#' @importFrom R.cache clearCache #' @export cache_clear <- function(cache_name = NULL, ask = TRUE) { path_cache <- cache_find_path(cache_name) - R.cache::clearCache(path_cache, prompt = ask) + clearCache(path_cache, prompt = ask) cache_deactivate(verbose = FALSE) } @@ -77,9 +78,10 @@ NULL #' "tabular" for a tabular summary from [base::file.info()] or "both" for #' both. #' @family cache managers +#' @importFrom rlang arg_match #' @export cache_info <- function(cache_name = NULL, format = "both") { - rlang::arg_match(format, c("tabular", "lucid", "both")) + arg_match(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/utils-cache.R b/R/utils-cache.R index 1c8845a54..fee5ccbed 100644 --- a/R/utils-cache.R +++ b/R/utils-cache.R @@ -21,11 +21,12 @@ hash_standardize <- function(text) { #' @param cache_dir The caching directory relative to the `.Rcache` root to #' look for a cached value. #' @keywords internal +#' @importFrom R.cache generateCache is_cached <- function(text, transformers, more_specs, cache_dir = cache_dir_default()) { - R.cache::generateCache( + generateCache( key = cache_make_key(text, transformers, more_specs), dirs = cache_dir ) %>% @@ -109,9 +110,10 @@ cache_make_key <- function(text, transformers, more_specs) { #' Finds the path to the cache and creates it if it does not exist. #' @inheritParams cache_clear #' @keywords internal +#' @importFrom R.cache getCachePath cache_find_path <- function(cache_name = NULL) { cache_name <- cache_get_or_derive_name(cache_name) - R.cache::getCachePath(c("styler", cache_name)) + getCachePath(c("styler", cache_name)) } #' Check if a cache is activated @@ -143,12 +145,13 @@ cache_is_activated <- function(cache_name = NULL) { #' is. #' @param text A character vector with one or more expressions. #' @inheritParams cache_write +#' @importFrom utils getParseData #' @keywords internal -cache_by_expression <- function(text, +cache_by_expression <- function(text, transformers, more_specs) { expressions <- parse(text = text, keep.source = TRUE) %>% - utils::getParseData(includeText = TRUE) + getParseData(includeText = TRUE) if (env_current$any_stylerignore) { expressions <- expressions %>% add_stylerignore() @@ -164,8 +167,9 @@ cache_by_expression <- function(text, #' #' @inheritParams cache_make_key #' @keywords internal +#' @importFrom R.cache generateCache cache_write <- function(text, transformers, more_specs) { - R.cache::generateCache( + generateCache( key = cache_make_key(text, transformers, more_specs), dirs = cache_dir_default() ) %>% diff --git a/R/utils-files.R b/R/utils-files.R index 9272bb05d..464da4b1b 100644 --- a/R/utils-files.R +++ b/R/utils-files.R @@ -38,10 +38,11 @@ map_filetype_to_pattern <- function(filetype) { #' @param ... Passed to [base::dir()]. #' @seealso set_and_assert_arg_paths #' @keywords internal +#' @importFrom purrr map #' @examples #' setdiff("./file.R", "file.R") # you want to standardize first. dir_without_. <- function(path, ...) { - purrr::map(path, dir_without_._one, ...) %>% + map(path, dir_without_._one, ...) %>% unlist() } diff --git a/R/utils.R b/R/utils.R index abc867000..1be0bf216 100644 --- a/R/utils.R +++ b/R/utils.R @@ -93,11 +93,12 @@ calls_sys <- function(sys_call, ...) { #' @param error_if_not_found Whether or not an error should be returned if the #' option was not set. #' @keywords internal +#' @importFrom rlang abort option_read <- function(x, default = NULL, error_if_not_found = TRUE) { if (x %in% names(options()) | !error_if_not_found) { getOption(x, default) } else { - rlang::abort(paste("R option", x, "most be set.")) + abort(paste("R option", x, "most be set.")) } }