diff --git a/API b/API index 98900f5f0..072513601 100644 --- a/API +++ b/API @@ -6,9 +6,9 @@ create_style_guide(initialize = default_style_guide_attributes, line_break = NUL default_style_guide_attributes(pd_flat) specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'")) specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE) -style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL, include_roxygen_examples = TRUE) +style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile"), recursive = TRUE, exclude_files = NULL, include_roxygen_examples = TRUE) style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE) -style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", exclude_files = "R/RcppExports.R", include_roxygen_examples = TRUE) +style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile"), exclude_files = "R/RcppExports.R", include_roxygen_examples = TRUE) style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE) tidyverse_math_token_spacing() tidyverse_reindention() diff --git a/NEWS.md b/NEWS.md index c072df7f8..f193e0868 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,8 @@ * curlyl-curly (`{{`) syntactic sugar introduced with rlang 0.4.0 is now explicitly handled, as opposed previously where it was just treated as two consequtive curly braces (#528). +* `style_pkg()`, `style_dir()` and the Addins can now style `.Rprofile`, and + hidden files are now also styled (#530). ## Minor improvements and fixes diff --git a/R/addins.R b/R/addins.R index 1f9c2c711..367b9ce9e 100644 --- a/R/addins.R +++ b/R/addins.R @@ -52,12 +52,17 @@ style_active_file <- function() { transformer <- make_transformer(get_addins_style_transformer(), include_roxygen_examples = TRUE, warn_empty = is_plain_r_file(context$path) ) + is_r_file <- any( + is_plain_r_file(context$path), + is_unsaved_file(context$path), + is_rprofile_file(context$path) + ) if (is_rmd_file(context$path)) { out <- transform_mixed(context$contents, transformer, filetype = "Rmd") } else if (is_rnw_file(context$path)) { out <- transform_mixed(context$contents, transformer, filetype = "Rnw") - } else if (is_plain_r_file(context$path) | is_unsaved_file(context$path)) { + } else if (is_r_file) { out <- try_transform_as_r_file(context, transformer) } else { abort("Can only style .R, .Rmd and .Rnw files.") diff --git a/R/set-assert-args.R b/R/set-assert-args.R index b96563dce..4f0d57295 100644 --- a/R/set-assert-args.R +++ b/R/set-assert-args.R @@ -38,9 +38,9 @@ set_and_assert_arg_filetype <- function(filetype) { #' @importFrom rlang abort #' @keywords internal assert_filetype <- function(lowercase_filetype) { - if (!all(lowercase_filetype %in% c("r", "rmd", "rnw"))) { + if (!all(lowercase_filetype %in% c("r", "rmd", "rnw", "rprofile"))) { abort(paste( - "filetype must not contain other values than 'R',", + "filetype must not contain other values than 'R', 'Rprofile',", "'Rmd' or 'Rnw' (case is ignored)." )) } diff --git a/R/transform-code.R b/R/transform-code.R index 931969515..cc425d571 100644 --- a/R/transform-code.R +++ b/R/transform-code.R @@ -9,7 +9,7 @@ #' @importFrom rlang abort #' @keywords internal transform_code <- function(path, fun, ...) { - if (is_plain_r_file(path)) { + if (is_plain_r_file(path) || is_rprofile_file(path)) { transform_utf8(path, fun = fun, ...) } else if (is_rmd_file(path)) { transform_utf8(path, diff --git a/R/ui.R b/R/ui.R index 13b879967..02d2a84c3 100644 --- a/R/ui.R +++ b/R/ui.R @@ -20,8 +20,9 @@ NULL #' conveniently constructed via the `style` argument and `...`. See #' 'Examples'. #' @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", -#' ".Rnw")` or `c("r", "rmd", "rnw")`. +#' 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", "rnw". #' @param exclude_files Character vector with paths to files that should be #' excluded from styling. #' @param include_roxygen_examples Whether or not to style code in roxygen @@ -71,7 +72,7 @@ style_pkg <- function(pkg = ".", ..., style = tidyverse_style, transformers = style(...), - filetype = "R", + filetype = c("R", "Rprofile"), exclude_files = "R/RcppExports.R", include_roxygen_examples = TRUE) { pkg_root <- rprojroot::find_package_root_file(path = pkg) @@ -86,7 +87,7 @@ prettify_pkg <- function(transformers, exclude_files, include_roxygen_examples) { filetype <- set_and_assert_arg_filetype(filetype) - r_files <- vignette_files <- readme <- NULL + r_files <- rprofile_files <- vignette_files <- readme <- NULL if ("\\.r" %in% filetype) { r_files <- dir( @@ -95,6 +96,13 @@ prettify_pkg <- function(transformers, ) } + if ("\\.rprofile" %in% filetype) { + rprofile_files <- dir( + path = ".", pattern = "^\\.rprofile$", + ignore.case = TRUE, recursive = FALSE, full.names = TRUE, + all.files = TRUE + ) + } if ("\\.rmd" %in% filetype) { vignette_files <- dir( path = "vignettes", pattern = "\\.rmd$", @@ -113,7 +121,10 @@ prettify_pkg <- function(transformers, ) } - files <- setdiff(c(r_files, vignette_files, readme), exclude_files) + files <- setdiff( + c(r_files, rprofile_files, vignette_files, readme), + exclude_files + ) transform_files(files, transformers, include_roxygen_examples) } @@ -167,7 +178,7 @@ style_dir <- function(path = ".", ..., style = tidyverse_style, transformers = style(...), - filetype = "R", + filetype = c("R", "Rprofile"), recursive = TRUE, exclude_files = NULL, include_roxygen_examples = TRUE) { @@ -193,7 +204,8 @@ prettify_any <- function(transformers, include_roxygen_examples) { files <- dir( path = ".", pattern = map_filetype_to_pattern(filetype), - ignore.case = TRUE, recursive = recursive, full.names = TRUE + ignore.case = TRUE, recursive = recursive, full.names = TRUE, + all.files = TRUE ) transform_files( setdiff(files, exclude_files), transformers, include_roxygen_examples diff --git a/R/utils-files.R b/R/utils-files.R index a86056a04..d1c0c6db4 100644 --- a/R/utils-files.R +++ b/R/utils-files.R @@ -2,6 +2,9 @@ is_plain_r_file <- function(path) { grepl("\\.R$", path, ignore.case = TRUE) } +is_rprofile_file <- function(path) { + grepl(".rprofile", path, ignore.case = TRUE) +} is_rmd_file <- function(path) { grepl("\\.Rmd$", path, ignore.case = TRUE) } diff --git a/man/prettify_any.Rd b/man/prettify_any.Rd index bc2fa9c97..e6ffed554 100644 --- a/man/prettify_any.Rd +++ b/man/prettify_any.Rd @@ -13,7 +13,9 @@ conveniently constructed via the \code{style} argument and \code{...}. See 'Examples'.} \item{filetype}{Vector of file extensions indicating which file types should -be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".Rmd", ".Rnw")} or \code{c("r", "rmd", "rnw")}.} +be styled. Case is ignored, and the \code{.} is optional, e.g. +\code{c(".R", ".Rmd")}, or \code{c("r", "rmd")}. Supported values (after +standardization) are: "r", "rprofile", "rmd", "rnw".} \item{recursive}{A logical value indicating whether or not files in subdirectories should be styled as well.} diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 3976cf43a..1affe1753 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -5,8 +5,9 @@ \title{Prettify arbitrary R code} \usage{ style_dir(path = ".", ..., style = tidyverse_style, - transformers = style(...), filetype = "R", recursive = TRUE, - exclude_files = NULL, include_roxygen_examples = TRUE) + transformers = style(...), filetype = c("R", "Rprofile"), + recursive = TRUE, exclude_files = NULL, + include_roxygen_examples = TRUE) } \arguments{ \item{path}{Path to a directory with files to transform.} @@ -23,7 +24,9 @@ conveniently constructed via the \code{style} argument and \code{...}. See 'Examples'.} \item{filetype}{Vector of file extensions indicating which file types should -be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".Rmd", ".Rnw")} or \code{c("r", "rmd", "rnw")}.} +be styled. Case is ignored, and the \code{.} is optional, e.g. +\code{c(".R", ".Rmd")}, or \code{c("r", "rmd")}. Supported values (after +standardization) are: "r", "rprofile", "rmd", "rnw".} \item{recursive}{A logical value indicating whether or not files in subdirectories of \code{path} should be styled as well.} diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index 2463dd0f0..044fe21f2 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -5,7 +5,7 @@ \title{Prettify R source code} \usage{ style_pkg(pkg = ".", ..., style = tidyverse_style, - transformers = style(...), filetype = "R", + transformers = style(...), filetype = c("R", "Rprofile"), exclude_files = "R/RcppExports.R", include_roxygen_examples = TRUE) } \arguments{ @@ -23,7 +23,9 @@ conveniently constructed via the \code{style} argument and \code{...}. See 'Examples'.} \item{filetype}{Vector of file extensions indicating which file types should -be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".Rmd", ".Rnw")} or \code{c("r", "rmd", "rnw")}.} +be styled. Case is ignored, and the \code{.} is optional, e.g. +\code{c(".R", ".Rmd")}, or \code{c("r", "rmd")}. Supported values (after +standardization) are: "r", "rprofile", "rmd", "rnw".} \item{exclude_files}{Character vector with paths to files that should be excluded from styling.} diff --git a/tests/testthat/public-api/xyzpackage/.Rprofile b/tests/testthat/public-api/xyzpackage/.Rprofile new file mode 100644 index 000000000..8d2f0971e --- /dev/null +++ b/tests/testthat/public-api/xyzpackage/.Rprofile @@ -0,0 +1 @@ +1 + 1