Skip to content

Commit 475a391

Browse files
Merge pull request #231 from lorenzwalthert/exclude_files
allow exclusion of files with style_pkg() and style_dir
2 parents eadd31d + 46c658e commit 475a391

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

API

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ create_style_guide(initialize = initialize_attributes, line_break = NULL, space
66
regex_none()
77
specify_math_token_spacing(zero = NULL, one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
88
specify_reindention(regex_pattern = regex_none(), indention = 0, comments_only = TRUE)
9-
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), recursive = TRUE)
9+
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), recursive = TRUE, exclude_files = NULL)
1010
style_file(path, ..., style = tidyverse_style, transformers = style(...))
11-
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...))
11+
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), exclude_files = "R/RcppExports.R")
1212
style_text(text, ..., style = tidyverse_style, transformers = style(...))
1313
tidyverse_math_token_spacing()
1414
tidyverse_reindention()

R/ws.R

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ NULL
1818
#' @param transformers A set of transformer functions. This argument is most
1919
#' conveniently constructed via the `style` argument and `...`. See
2020
#' 'Examples'.
21+
#' @param exclude_files Character vector with paths to files that should be
22+
#' excluded from styling.
2123
#' @section Warning:
2224
#' This function overwrites files (if styling results in a change of the
2325
#' code to be formatted). It is strongly suggested to only style files
@@ -34,12 +36,13 @@ NULL
3436
style_pkg <- function(pkg = ".",
3537
...,
3638
style = tidyverse_style,
37-
transformers = style(...)) {
39+
transformers = style(...),
40+
exclude_files = "R/RcppExports.R") {
3841
pkg_root <- rprojroot::find_package_root_file(path = pkg)
39-
withr::with_dir(pkg_root, prettify_local(transformers))
42+
withr::with_dir(pkg_root, prettify_local(transformers, exclude_files))
4043
}
4144

42-
prettify_local <- function(transformers) {
45+
prettify_local <- function(transformers, exclude_files) {
4346
r_files <- dir(
4447
path = "R", pattern = "[.][rR]$", recursive = TRUE, full.names = TRUE
4548
)
@@ -50,7 +53,7 @@ prettify_local <- function(transformers) {
5053
recursive = TRUE, full.names = TRUE
5154
)
5255

53-
files <- c(r_files, test_files)
56+
files <- setdiff(c(r_files, test_files), exclude_files)
5457

5558
transform_files(files, transformers)
5659
}
@@ -98,9 +101,10 @@ style_dir <- function(path = ".",
98101
...,
99102
style = tidyverse_style,
100103
transformers = style(...),
101-
recursive = TRUE) {
104+
recursive = TRUE,
105+
exclude_files = NULL) {
102106
withr::with_dir(
103-
path, prettify_any(transformers, recursive = recursive)
107+
path, prettify_any(transformers, recursive, exclude_files)
104108
)
105109
}
106110

@@ -110,9 +114,11 @@ style_dir <- function(path = ".",
110114
#' @inheritParams style_pkg
111115
#' @param recursive A logical value indicating whether or not files in subdirectories
112116
#' should be styled as well.
113-
prettify_any <- function(transformers, recursive) {
114-
files <- dir(path = ".", pattern = "[.][rR]$", recursive = recursive, full.names = TRUE)
115-
transform_files(files, transformers)
117+
prettify_any <- function(transformers, recursive, exclude_files) {
118+
files <- dir(
119+
path = ".", pattern = "[.][rR]$", recursive = recursive, full.names = TRUE
120+
)
121+
transform_files(setdiff(files, exclude_files), transformers)
116122

117123
}
118124

man/prettify_any.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_dir.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_pkg.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)