Skip to content

support Rprofile styling #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions API
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
4 changes: 2 additions & 2 deletions R/set-assert-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)."
))
}
Expand Down
2 changes: 1 addition & 1 deletion R/transform-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 19 additions & 7 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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$",
Expand All @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions R/utils-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion man/prettify_any.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/style_dir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/style_pkg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/public-api/xyzpackage/.Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 + 1