From e24dc6812316ba0cb7506a4978ac0f27a163571f Mon Sep 17 00:00:00 2001 From: Olivier Roy Date: Wed, 28 Jun 2023 15:39:12 -0400 Subject: [PATCH 1/3] Exclude R/import-standalone.+R files from restyling. --- R/ui-styling.R | 12 ++++++++---- man/prettify_any.Rd | 4 ++-- man/prettify_pkg.Rd | 4 ++-- man/style_dir.Rd | 4 ++-- man/style_pkg.Rd | 4 ++-- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/R/ui-styling.R b/R/ui-styling.R index bb4cc136c..a5a3bd3e8 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -69,7 +69,7 @@ style_pkg <- function(pkg = ".", style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), - exclude_files = c("R/RcppExports.R", "R/cpp11.R"), + 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, @@ -91,8 +91,8 @@ style_pkg <- function(pkg = ".", #' ".Rmd")`, or `c("r", "rmd")`. Supported values (after standardization) are: #' "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as #' Rmd. -#' @param exclude_files Character vector with paths to files that should be -#' excluded from styling. +#' @param exclude_files Character vector with paths or regular expressions to files +#' that should be excluded from styling. #' @param exclude_dirs Character vector with directories to exclude #' (recursively). Note that the default values were set for consistency with #' [style_dir()] and as these directories are anyways not styled. @@ -107,8 +107,10 @@ prettify_pkg <- function(transformers, dry) { filetype_ <- set_and_assert_arg_filetype(filetype) r_files <- rprofile_files <- vignette_files <- readme <- NULL + exclude_files <- set_arg_paths(exclude_files) + exclude_files_regex <- paste0(exclude_files[!file.exists(exclude_files)], collapse = "|") exclude_files <- c( - set_arg_paths(exclude_files), + exclude_files, dir_without_.(exclude_dirs, pattern = map_filetype_to_pattern(filetype)) ) if ("\\.r" %in% filetype_) { @@ -171,6 +173,8 @@ prettify_pkg <- function(transformers, c(r_files, rprofile_files, vignette_files, readme), exclude_files ) + # Remove the regex. + files <- files[!grepl(exclude_files_regex, files)] transform_files(files, transformers = transformers, include_roxygen_examples = include_roxygen_examples, diff --git a/man/prettify_any.Rd b/man/prettify_any.Rd index c4b7fc5a1..590560522 100644 --- a/man/prettify_any.Rd +++ b/man/prettify_any.Rd @@ -28,8 +28,8 @@ Rmd.} \item{recursive}{A logical value indicating whether or not files in subdirectories should be styled as well.} -\item{exclude_files}{Character vector with paths to files that should be -excluded from styling.} +\item{exclude_files}{Character vector with paths or regular expressions to files +that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude (recursively). Note that the default values were set for consistency with diff --git a/man/prettify_pkg.Rd b/man/prettify_pkg.Rd index 4e9a40cb8..53a946575 100644 --- a/man/prettify_pkg.Rd +++ b/man/prettify_pkg.Rd @@ -23,8 +23,8 @@ be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".R "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as Rmd.} -\item{exclude_files}{Character vector with paths to files that should be -excluded from styling.} +\item{exclude_files}{Character vector with paths or regular expressions to files +that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude (recursively). Note that the default values were set for consistency with diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 9f8294354..3913c53ff 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -41,8 +41,8 @@ Rmd.} \item{recursive}{A logical value indicating whether or not files in sub directories of \code{path} should be styled as well.} -\item{exclude_files}{Character vector with paths to files that should be -excluded from styling.} +\item{exclude_files}{Character vector with paths or regular expressions to files +that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude (recursively).} diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index 303dc7693..59232b1dc 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -37,8 +37,8 @@ be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".R "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as Rmd.} -\item{exclude_files}{Character vector with paths to files that should be -excluded from styling.} +\item{exclude_files}{Character vector with paths or regular expressions to files +that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude (recursively). Note that the default values were set for consistency with From 6d2f25d2fac86cf234d39b7a1c322d35198d2215 Mon Sep 17 00:00:00 2001 From: Olivier Roy Date: Wed, 28 Jun 2023 15:52:30 -0400 Subject: [PATCH 2/3] document --- API | 2 +- man/style_pkg.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/API b/API index 46b34bbcd..b392a14b4 100644 --- a/API +++ b/API @@ -27,7 +27,7 @@ specify_reindention(regex_pattern = NULL, indention = 0L, comments_only = TRUE) specify_transformers_drop(spaces = NULL, indention = NULL, line_breaks = NULL, tokens = NULL) 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") style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off") -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"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off") +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") style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L) tidyverse_math_token_spacing() tidyverse_reindention() diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index 59232b1dc..9d07b235a 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -10,7 +10,7 @@ style_pkg( style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile", "Rmd", "Rmarkdown", "Rnw", "Qmd"), - exclude_files = c("R/RcppExports.R", "R/cpp11.R"), + 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, From a2b8ad472310975c0dc8e9eba24805969302ecad Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 16 Jul 2023 20:13:53 +0200 Subject: [PATCH 3/3] exclude files is not interpreteted as a regex instead of plain file names and R/import-standalone.*\\.R is added --- API | 2 +- R/ui-styling.R | 8 ++++---- man/prettify_any.Rd | 2 +- man/prettify_pkg.Rd | 2 +- man/style_dir.Rd | 2 +- man/style_pkg.Rd | 5 +++-- tests/testthat/test-public_api-0.R | 12 ++++++++++-- 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/API b/API index b392a14b4..429de55f4 100644 --- a/API +++ b/API @@ -27,7 +27,7 @@ specify_reindention(regex_pattern = NULL, indention = 0L, comments_only = TRUE) specify_transformers_drop(spaces = NULL, indention = NULL, line_breaks = NULL, tokens = NULL) 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") style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L, dry = "off") -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") +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") style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L) tidyverse_math_token_spacing() tidyverse_reindention() diff --git a/R/ui-styling.R b/R/ui-styling.R index a5a3bd3e8..e25eed10c 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -69,7 +69,7 @@ style_pkg <- function(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_files = c("R/RcppExports\\.R", "R/cpp11\\.R", "R/import-standalone.*\\.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, @@ -91,7 +91,7 @@ style_pkg <- function(pkg = ".", #' ".Rmd")`, or `c("r", "rmd")`. Supported values (after standardization) are: #' "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as #' Rmd. -#' @param exclude_files Character vector with paths or regular expressions to files +#' @param exclude_files Character vector with regular expressions to files #' that should be excluded from styling. #' @param exclude_dirs Character vector with directories to exclude #' (recursively). Note that the default values were set for consistency with @@ -107,6 +107,8 @@ prettify_pkg <- function(transformers, dry) { filetype_ <- set_and_assert_arg_filetype(filetype) r_files <- rprofile_files <- vignette_files <- readme <- NULL + all_files <- list.files(".", recursive = TRUE, all.files = TRUE) + exclude_files <- grep(paste0(exclude_files, collapse = "|"), all_files, value = TRUE) exclude_files <- set_arg_paths(exclude_files) exclude_files_regex <- paste0(exclude_files[!file.exists(exclude_files)], collapse = "|") exclude_files <- c( @@ -173,8 +175,6 @@ prettify_pkg <- function(transformers, c(r_files, rprofile_files, vignette_files, readme), exclude_files ) - # Remove the regex. - files <- files[!grepl(exclude_files_regex, files)] transform_files(files, transformers = transformers, include_roxygen_examples = include_roxygen_examples, diff --git a/man/prettify_any.Rd b/man/prettify_any.Rd index 590560522..b1db51e44 100644 --- a/man/prettify_any.Rd +++ b/man/prettify_any.Rd @@ -28,7 +28,7 @@ Rmd.} \item{recursive}{A logical value indicating whether or not files in subdirectories should be styled as well.} -\item{exclude_files}{Character vector with paths or regular expressions to files +\item{exclude_files}{Character vector with regular expressions to files that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude diff --git a/man/prettify_pkg.Rd b/man/prettify_pkg.Rd index 53a946575..0c999550c 100644 --- a/man/prettify_pkg.Rd +++ b/man/prettify_pkg.Rd @@ -23,7 +23,7 @@ be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".R "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as Rmd.} -\item{exclude_files}{Character vector with paths or regular expressions to files +\item{exclude_files}{Character vector with regular expressions to files that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 3913c53ff..7f0f61a58 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -41,7 +41,7 @@ Rmd.} \item{recursive}{A logical value indicating whether or not files in sub directories of \code{path} should be styled as well.} -\item{exclude_files}{Character vector with paths or regular expressions to files +\item{exclude_files}{Character vector with regular expressions to files that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index 9d07b235a..0eca9278e 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -10,7 +10,8 @@ style_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_files = c("R/RcppExports\\\\.R", "R/cpp11\\\\.R", + "R/import-standalone.*\\\\.R"), exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0L, @@ -37,7 +38,7 @@ be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".R "r", "rprofile", "rmd", "rmarkdown", "rnw", "qmd". Rmarkdown is treated as Rmd.} -\item{exclude_files}{Character vector with paths or regular expressions to files +\item{exclude_files}{Character vector with regular expressions to files that should be excluded from styling.} \item{exclude_dirs}{Character vector with directories to exclude diff --git a/tests/testthat/test-public_api-0.R b/tests/testthat/test-public_api-0.R index e0d46f105..70e90cbec 100644 --- a/tests/testthat/test-public_api-0.R +++ b/tests/testthat/test-public_api-0.R @@ -32,7 +32,7 @@ test_that("styler can style package and exclude some directories and files", { capture_output(expect_true({ styled <- style_pkg(testthat_file("public-api", "xyzpackage"), exclude_dirs = "tests", - exclude_files = ".Rprofile" + exclude_files = "\\.Rprofile" ) nrow(styled) == 1 })) @@ -40,10 +40,18 @@ test_that("styler can style package and exclude some directories and files", { capture_output(expect_true({ styled <- style_pkg(testthat_file("public-api", "xyzpackage"), exclude_dirs = "tests", - exclude_files = "./.Rprofile" + exclude_files = ".*ofile" ) nrow(styled) == 1 })) + + capture_output(expect_true({ + styled <- style_pkg(testthat_file("public-api", "xyzpackage"), + exclude_dirs = "tests", + exclude_files = "hello" + ) + nrow(styled) == 0 + })) })