Skip to content

Commit c8ce5de

Browse files
authored
Merge pull request #292 from jonmcalder/style-dir
- `style_dir()` and `style_pkg()` now also handle `.Rmd` files (#292, @jonmcalder).
2 parents 2ced371 + db5e7ab commit c8ce5de

38 files changed

+536
-104
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, exclude_files = NULL)
9+
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", recursive = TRUE, exclude_files = NULL)
1010
style_file(path, ..., style = tidyverse_style, transformers = style(...))
11-
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), exclude_files = "R/RcppExports.R")
11+
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = "R", exclude_files = "R/RcppExports.R")
1212
style_text(text, ..., style = tidyverse_style, transformers = style(...))
1313
tidyverse_math_token_spacing()
1414
tidyverse_reindention()

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Collate:
5454
'rules-spacing.R'
5555
'serialize.R'
5656
'serialized_tests.R'
57+
'set-assert-args.R'
5758
'style_guides.R'
5859
'styler.R'
5960
'token-create.R'

R/expr-is.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Check whether a parse table corresponds to a a certain expression
1+
#' Check whether a parse table corresponds to a certain expression
22
#'
33
#' @param pd A parse table.
44
#' @name pd_is
@@ -39,7 +39,7 @@ contains_else_expr <- function(pd) {
3939
#' Checks whether an else expression in a nest needs braces. Note that for
4040
#' if-else-if expressions, there is no need to add braces since the if in
4141
#' else-if will be visited separately with the visitor. This applies to all
42-
#' conditional statmements with more than one alternative.
42+
#' conditional statements with more than one alternative.
4343
#' @param pd A parse table
4444
contains_else_expr_that_needs_braces <- function(pd) {
4545
else_idx <- which(pd$token == "ELSE")

R/serialized_tests.R

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ testthat_file <- function(...) {
187187
file.path(rprojroot::find_testthat_root_file(), ...)
188188
}
189189

190+
190191
#' Copy a file to a temporary directory
191192
#'
192193
#' Takes the path to a file as input and returns the path where the temporary
@@ -201,25 +202,6 @@ copy_to_tempdir <- function(path_perm = testthat_file()) {
201202
}
202203

203204

204-
#' Set arguments
205-
#' @param write_tree Whether or not to write tree.
206-
#' @name set_args
207-
NULL
208-
209-
#' @describeIn set_args Sets the argument `write_tree` in
210-
#' [test_collection()] to be `TRUE` for R versions higher or equal to 3.2, and
211-
#' `FALSE` otherwise since the second-level dependency `DiagrammeR` from
212-
#' `data.table` is not available for R < 3.2.
213-
set_arg_write_tree <- function(write_tree) {
214-
sufficient_version <- getRversion() >= 3.2
215-
if (is.na(write_tree)) {
216-
write_tree <- ifelse(sufficient_version, TRUE, FALSE)
217-
} else if (!sufficient_version & write_tree) {
218-
stop_insufficient_r_version()
219-
}
220-
write_tree
221-
}
222-
223205
stop_insufficient_r_version <- function() {
224206
stop(paste0(
225207
"Can't write tree with R version ", getRversion(),

R/set-assert-args.R

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#' Set the write_tree argument
2+
#'
3+
#' Sets the argument `write_tree` in [test_collection()] to be `TRUE` for R
4+
#' versions higher or equal to 3.2, and `FALSE` otherwise since the second-level
5+
#' dependency `DiagrammeR` from `data.table` is not available for R < 3.2.
6+
#' @param write_tree Whether or not to write tree.
7+
set_arg_write_tree <- function(write_tree) {
8+
sufficient_version <- getRversion() >= 3.2
9+
if (is.na(write_tree)) {
10+
write_tree <- ifelse(sufficient_version, TRUE, FALSE)
11+
} else if (!sufficient_version & write_tree) {
12+
stop_insufficient_r_version()
13+
}
14+
write_tree
15+
}
16+
17+
#' Set the file type argument
18+
#'
19+
#' Sets and asserts the file type argument to a standard format for further internal
20+
#' processing.
21+
#' @param filetype A character vector with file types to convert to the internal
22+
#' standard format.
23+
#' @examples
24+
#' styler:::set_and_assert_arg_filetype("rMd")
25+
#' \dontrun{
26+
#' styler:::set_and_assert_arg_filetype("xyz")
27+
#' }
28+
set_and_assert_arg_filetype <- function(filetype) {
29+
without_dot <- gsub("^\\.", "", tolower(filetype))
30+
assert_filetype(without_dot)
31+
paste0("\\.", without_dot)
32+
}
33+
34+
#' Make sure all supplied file types are allowed
35+
#'
36+
#' @param lowercase_filetype A vector with file types to check, all lower case.
37+
assert_filetype <- function(lowercase_filetype) {
38+
if (!all(lowercase_filetype %in% c("r", "rmd"))) {
39+
stop(
40+
"filetype must not contain other values than 'R'",
41+
"or 'Rmd' (case is ignored).", call. = FALSE
42+
)
43+
}
44+
}
45+
46+
47+
#' Assert text to be of positive length and replace it with the empty
48+
#' string otherwise.
49+
#' @param text The input to style.
50+
assert_text <- function(text) {
51+
if (length(text) < 1) {
52+
text <- ""
53+
}
54+
text
55+
}
56+
57+
58+
#' Check token validity
59+
#'
60+
#' Check whether one or more tokens exist and have a unique token-text mapping
61+
#' @param tokens Tokens to check.
62+
assert_tokens <- function(tokens) {
63+
invalid_tokens <- tokens[!(tokens %in% lookup_tokens()$token)]
64+
if (length(invalid_tokens) > 0) {
65+
stop(
66+
"Token(s) ", paste0(invalid_tokens, collapse = ", "), " are invalid. ",
67+
"You can lookup all valid tokens and their text ",
68+
"with styler:::lookup_tokens(). Make sure you supply the values of ",
69+
"the column 'token', not 'text'."
70+
)
71+
}
72+
}
73+

R/style_guides.R

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,3 @@ specify_math_token_spacing <-
285285
tidyverse_math_token_spacing <- function() {
286286
specify_math_token_spacing(one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
287287
}
288-
289-
#' Check token validity
290-
#'
291-
#' Check whether one or more tokens exist and have a unique token-text mapping
292-
#' @param tokens Tokens to check.
293-
assert_tokens <- function(tokens) {
294-
invalid_tokens <- tokens[!(tokens %in% lookup_tokens()$token)]
295-
if (length(invalid_tokens) > 0) {
296-
stop(
297-
"Token(s) ", paste0(invalid_tokens, collapse = ", "), " are invalid. ",
298-
"You can lookup all valid tokens and their text ",
299-
"with styler:::looup_tokens(). Make sure you supply the values of ",
300-
"the column 'token', not 'text'."
301-
)
302-
}
303-
}

R/ui.R

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ 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 filetype Vector of file extensions indicating which filetypes should
22+
#' be styled. Case is ignored, and the `.` is optional, e.g. `c(".R", ".Rmd")`
23+
#' or `c("r", "rmd")`.
2124
#' @param exclude_files Character vector with paths to files that should be
2225
#' excluded from styling.
2326
#' @section Warning:
@@ -37,25 +40,36 @@ style_pkg <- function(pkg = ".",
3740
...,
3841
style = tidyverse_style,
3942
transformers = style(...),
43+
filetype = "R",
4044
exclude_files = "R/RcppExports.R") {
4145
pkg_root <- rprojroot::find_package_root_file(path = pkg)
42-
changed <- withr::with_dir(pkg_root, prettify_local(transformers, exclude_files))
46+
changed <- withr::with_dir(pkg_root,
47+
prettify_pkg(transformers, filetype, exclude_files)
48+
)
4349
invisible(changed)
4450
}
4551

46-
prettify_local <- function(transformers, exclude_files) {
47-
r_files <- dir(
48-
path = "R", pattern = "[.][rR]$", recursive = TRUE, full.names = TRUE
49-
)
52+
prettify_pkg <- function(transformers, filetype, exclude_files) {
5053

51-
r_files <- grep("/RcppExports[.]R$", r_files, invert = TRUE, value = TRUE)
52-
test_files <- dir(
53-
path = "tests/testthat", pattern = "[.][rR]$",
54-
recursive = TRUE, full.names = TRUE
55-
)
54+
filetype <- set_and_assert_arg_filetype(filetype)
55+
r_files <- vignette_files <- readme <- NULL
5656

57-
files <- setdiff(c(r_files, test_files), exclude_files)
57+
if ("\\.r" %in% filetype) {
58+
r_files <- dir(
59+
path = c("R", "tests", "data-raw"), pattern = "\\.r$",
60+
ignore.case = TRUE, recursive = TRUE, full.names = TRUE
61+
)
62+
}
5863

64+
if ("\\.rmd" %in% filetype) {
65+
vignette_files <- dir(
66+
path = "vignettes", pattern = "\\.rmd$",
67+
ignore.case = TRUE, recursive = TRUE, full.names = TRUE
68+
)
69+
readme <- dir(pattern = "^readme\\.rmd$", ignore.case = TRUE)
70+
}
71+
72+
files <- setdiff(c(r_files, vignette_files, readme), exclude_files)
5973
transform_files(files, transformers)
6074
}
6175

@@ -103,10 +117,11 @@ style_dir <- function(path = ".",
103117
...,
104118
style = tidyverse_style,
105119
transformers = style(...),
120+
filetype = "R",
106121
recursive = TRUE,
107122
exclude_files = NULL) {
108123
changed <- withr::with_dir(
109-
path, prettify_any(transformers, recursive, exclude_files)
124+
path, prettify_any(transformers, filetype, recursive, exclude_files)
110125
)
111126
invisible(changed)
112127
}
@@ -117,12 +132,12 @@ style_dir <- function(path = ".",
117132
#' @inheritParams style_pkg
118133
#' @param recursive A logical value indicating whether or not files in subdirectories
119134
#' should be styled as well.
120-
prettify_any <- function(transformers, recursive, exclude_files) {
135+
prettify_any <- function(transformers, filetype, recursive, exclude_files) {
121136
files <- dir(
122-
path = ".", pattern = "[.][rR]$", recursive = recursive, full.names = TRUE
137+
path = ".", pattern = map_filetype_to_pattern(filetype),
138+
ignore.case = TRUE, recursive = recursive, full.names = TRUE
123139
)
124140
transform_files(setdiff(files, exclude_files), transformers)
125-
126141
}
127142

128143
#' Style `.R` and/or `.Rmd` files

R/utils.R

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ calls_sys <- function(sys_call, ...) {
4040
}
4141
}
4242

43-
#' Assert text to be of positive length and replace it with the empty
44-
#' string otherwise.
45-
#' @param text The input to style.
46-
assert_text <- function(text) {
47-
if (length(text) < 1) {
48-
text <- ""
49-
}
50-
text
51-
}
52-
5343
is_plain_r_file <- function(path) {
5444
grepl("\\.R$", path, ignore.case = TRUE)
5545
}
@@ -85,3 +75,12 @@ extend_if_comment <- function(pd, pos) {
8575
pos
8676
}
8777
}
78+
79+
#' Map the file type to a corresponding regular expression
80+
#'
81+
#' @param filetype The file type to map to a regex.
82+
#' @examples
83+
#' styler:::map_filetype_to_pattern(c(".rMd", "R"))
84+
map_filetype_to_pattern <- function(filetype) {
85+
paste0("(", paste(set_and_assert_arg_filetype(filetype), collapse = "|"), ")$")
86+
}

man/assert_filetype.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/assert_text.Rd

Lines changed: 1 addition & 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)