From 2409bd57d3be1aeba0ca8259524db2a44655c3c6 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 25 Oct 2020 23:03:31 +0100 Subject: [PATCH 1/4] remove the majority of package prefixes to see if it brings speed gain --- NAMESPACE | 13 +++++++++++++ R/addins.R | 13 ++++++++----- R/compat-dplyr.R | 3 ++- R/io.R | 16 +++++++++------- R/parse.R | 3 ++- R/roxygen-examples-find.R | 2 +- R/roxygen-examples-parse.R | 3 ++- R/rules-spacing.R | 3 ++- R/style-guides.R | 4 ++-- R/ui-caching.R | 6 ++++-- R/utils-cache.R | 14 +++++++++----- R/utils-files.R | 3 ++- R/utils.R | 3 ++- 13 files changed, 58 insertions(+), 28 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 67f3fcd43..ab8940408 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,22 @@ 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,documentSave) +importFrom(rstudioapi,getActiveDocumentContext) +importFrom(rstudioapi,modifyRange) +importFrom(rstudioapi,setCursorPosition) +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..11b6a5051 100644 --- a/R/addins.R +++ b/R/addins.R @@ -45,6 +45,7 @@ NULL #' @importFrom rlang abort +#' @importFrom rstudioapi modifyRange documentSave setCursorPosition #' @keywords internal style_active_file <- function() { communicate_addins_style_transformers() @@ -69,15 +70,15 @@ style_active_file <- function() { } else { abort("Can only style .R, .Rmd and .Rnw files.") } - rstudioapi::modifyRange( + modifyRange( c(1, 1, length(context$contents) + 1, 1), paste0(ensure_last_n_empty(out), collapse = "\n"), id = context$id ) if (Sys.getenv("save_after_styling") == TRUE && context$path != "") { - rstudioapi::documentSave(context$id) + documentSave(context$id) } - rstudioapi::setCursorPosition(context$selection[[1]]$range) + setCursorPosition(context$selection[[1]]$range) } #' Wrapper around [style_pkg()] for access via Addin. @@ -89,6 +90,7 @@ style_active_pkg <- function() { #' Styles the highlighted selection in a `.R` or `.Rmd` file. #' @importFrom rlang abort +#' @importFrom rstudioapi documentSave #' @keywords internal style_selection <- function() { communicate_addins_style_transformers() @@ -101,12 +103,13 @@ style_selection <- function() { id = context$id ) if (Sys.getenv("save_after_styling") == TRUE && context$path != "") { - invisible(rstudioapi::documentSave(context$id)) + invisible(documentSave(context$id)) } } +#' @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..b49f3d76d 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,6 +94,7 @@ 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) { x <- readLines(con, encoding = "UTF-8", warn = warn) i <- invalid_utf8(x) @@ -103,7 +105,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.")) } } From 1dbb3f4a0c961a6460ba82232c55f88c39a77343 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 25 Oct 2020 23:19:44 +0100 Subject: [PATCH 2/4] more imports --- NAMESPACE | 1 + R/addins.R | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index ab8940408..0de44f4c6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -52,6 +52,7 @@ importFrom(rstudioapi,documentSave) importFrom(rstudioapi,getActiveDocumentContext) importFrom(rstudioapi,modifyRange) importFrom(rstudioapi,setCursorPosition) +importFrom(rstudioapi,showPrompt) importFrom(tools,parse_Rd) importFrom(utils,capture.output) importFrom(utils,getParseData) diff --git a/R/addins.R b/R/addins.R index 11b6a5051..c591865cb 100644 --- a/R/addins.R +++ b/R/addins.R @@ -90,7 +90,7 @@ style_active_pkg <- function() { #' Styles the highlighted selection in a `.R` or `.Rmd` file. #' @importFrom rlang abort -#' @importFrom rstudioapi documentSave +#' @importFrom rstudioapi documentSave modifyRange #' @keywords internal style_selection <- function() { communicate_addins_style_transformers() @@ -98,7 +98,7 @@ style_selection <- function() { text <- context$selection[[1]]$text if (all(nchar(text) == 0)) abort("No code selected") out <- style_text(text, transformers = get_addins_style_transformer()) - rstudioapi::modifyRange( + modifyRange( context$selection[[1]]$range, paste0(c(out, if (context$selection[[1]]$range$end[2] == 1) ""), collapse = "\n"), id = context$id ) @@ -116,10 +116,11 @@ get_rstudio_context <- function() { #' @importFrom rlang abort #' @keywords internal #' @importFrom rlang with_handlers abort +#' @importFrom rstudioapi showPrompt set_style_transformers <- function() { current_style <- get_addins_style_transformer_name() new_style <- - rstudioapi::showPrompt( + showPrompt( "Select a style", "Enter the name of a style transformer, e.g. `styler::tidyverse_style()`", current_style From 2e2b850c61942ca0b2d6a9849ad5041a864abef2 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 1 Nov 2020 16:45:09 +0100 Subject: [PATCH 3/4] suggested packages need ::-prefix --- DESCRIPTION | 2 +- NAMESPACE | 4 ---- R/addins.R | 15 ++++++--------- 3 files changed, 7 insertions(+), 14 deletions(-) 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 0de44f4c6..621ce1505 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -48,11 +48,7 @@ importFrom(rlang,is_named) importFrom(rlang,seq2) importFrom(rlang,warn) importFrom(rlang,with_handlers) -importFrom(rstudioapi,documentSave) importFrom(rstudioapi,getActiveDocumentContext) -importFrom(rstudioapi,modifyRange) -importFrom(rstudioapi,setCursorPosition) -importFrom(rstudioapi,showPrompt) importFrom(tools,parse_Rd) importFrom(utils,capture.output) importFrom(utils,getParseData) diff --git a/R/addins.R b/R/addins.R index c591865cb..3c4cb3023 100644 --- a/R/addins.R +++ b/R/addins.R @@ -45,7 +45,6 @@ NULL #' @importFrom rlang abort -#' @importFrom rstudioapi modifyRange documentSave setCursorPosition #' @keywords internal style_active_file <- function() { communicate_addins_style_transformers() @@ -70,15 +69,15 @@ style_active_file <- function() { } else { abort("Can only style .R, .Rmd and .Rnw files.") } - modifyRange( + rstudioapi::modifyRange( c(1, 1, length(context$contents) + 1, 1), paste0(ensure_last_n_empty(out), collapse = "\n"), id = context$id ) if (Sys.getenv("save_after_styling") == TRUE && context$path != "") { - documentSave(context$id) + rstudioapi::documentSave(context$id) } - setCursorPosition(context$selection[[1]]$range) + rstudioapi::setCursorPosition(context$selection[[1]]$range) } #' Wrapper around [style_pkg()] for access via Addin. @@ -90,7 +89,6 @@ style_active_pkg <- function() { #' Styles the highlighted selection in a `.R` or `.Rmd` file. #' @importFrom rlang abort -#' @importFrom rstudioapi documentSave modifyRange #' @keywords internal style_selection <- function() { communicate_addins_style_transformers() @@ -98,12 +96,12 @@ style_selection <- function() { text <- context$selection[[1]]$text if (all(nchar(text) == 0)) abort("No code selected") out <- style_text(text, transformers = get_addins_style_transformer()) - modifyRange( + rstudioapi::modifyRange( context$selection[[1]]$range, paste0(c(out, if (context$selection[[1]]$range$end[2] == 1) ""), collapse = "\n"), id = context$id ) if (Sys.getenv("save_after_styling") == TRUE && context$path != "") { - invisible(documentSave(context$id)) + invisible(rstudioapi::documentSave(context$id)) } } @@ -116,11 +114,10 @@ get_rstudio_context <- function() { #' @importFrom rlang abort #' @keywords internal #' @importFrom rlang with_handlers abort -#' @importFrom rstudioapi showPrompt set_style_transformers <- function() { current_style <- get_addins_style_transformer_name() new_style <- - showPrompt( + rstudioapi::showPrompt( "Select a style", "Enter the name of a style transformer, e.g. `styler::tidyverse_style()`", current_style From fd6829c60a7708716c429169e489c4c7e9836505 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 1 Nov 2020 17:37:47 +0100 Subject: [PATCH 4/4] adapt from xfun due to a failing build on R 3.6 on Windows (https://github.com/r-lib/styler/pull/685/checks?check_run_id=1338739147) --- R/io.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/io.R b/R/io.R index b49f3d76d..d6d7d10c0 100644 --- a/R/io.R +++ b/R/io.R @@ -96,6 +96,8 @@ read_utf8 <- function(path) { #' @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)