Skip to content
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

Allow excluding directories when styling #568

Merged
merged 11 commits into from
Dec 16, 2019
4 changes: 2 additions & 2 deletions API
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,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 = c("R", "Rprofile"), 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, exclude_dirs = c("packrat", "renv"), 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 = c("R", "Rprofile"), 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", exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE)
style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE)
tidyverse_math_token_spacing()
tidyverse_reindention()
Expand Down
21 changes: 18 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# styler 1.2.0.9000

## Breaking changes

* `style_pkg()` and `style_dir()` gain a new argument `exclude_dirs` to exclude
directories from styling, by default `renv` and `packrat`. Note that the
defaults won't change the behavior of `style_pkg()` because it does anyways
does not style these directories and they were set for consistency.


## New features

* ignore certain lines using `# styler: off` and `#styler: on` or custom
markers, see `help("stylerignore")` (#560).

* styler caches results of styling, so applying styler to code it has styled
before will be instantaneous. This brings large speed boosts in many
situations, e.g. when `style_pkg()` is run but only a few files have changed
Expand All @@ -8,12 +21,14 @@
for details (#538).


* ignore certain lines using `# styler: off` and `#styler: on` or custom
markers, see `help("stylerignore")` (#560).
## Minor changes and fixes

* `style_file()` and friends now strip `./` in file paths returned invisibly,
i.e. `./script.R` becomes `script.R`.

* function documentation now contains many more linebreaks due to roxygen2
update to version 7.0.1 (#566).

# styler 1.2.0

## Breaking changes
Expand Down
14 changes: 14 additions & 0 deletions R/set-assert-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ assert_tokens <- function(tokens) {
))
}
}

#' Standardize paths in root
#'
#' Standardization required to use `setdiff()` with paths.
#' @param path A path.
#' @keywords internal
#' @seealso dir_without_.
#' @examples
#' styler:::set_arg_paths(c("./file.R", "file.R", "../another-file.R"))
set_arg_paths <- function(path) {
starts_with_. <- substr(path, 1, 2) == "./"
path[starts_with_.] <- substring(path[starts_with_.], 3)
path
}
76 changes: 53 additions & 23 deletions R/ui-styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ NULL
#' standardization) are: "r", "rprofile", "rmd", "rnw".
#' @param exclude_files Character vector with paths to files that should be
#' excluded from styling.
#' @param exclude_dirs Character vector with directories to exclude. Note that
#' the default values were set for consistency with [style_dir()] and as
#' these directories are anyways not styled.
#' @param include_roxygen_examples Whether or not to style code in roxygen
#' examples.
#' @section Warning:
Expand Down Expand Up @@ -74,49 +77,58 @@ style_pkg <- function(pkg = ".",
transformers = style(...),
filetype = c("R", "Rprofile"),
exclude_files = "R/RcppExports.R",
exclude_dirs = c("packrat", "renv"),
include_roxygen_examples = TRUE) {
pkg_root <- rprojroot::find_package_root_file(path = pkg)
changed <- withr::with_dir(pkg_root, prettify_pkg(
transformers, filetype, exclude_files, include_roxygen_examples
transformers,
filetype, exclude_files, exclude_dirs, include_roxygen_examples
))
invisible(changed)
}

prettify_pkg <- function(transformers,
filetype,
exclude_files,
exclude_dirs,
include_roxygen_examples) {
filetype <- set_and_assert_arg_filetype(filetype)
r_files <- rprofile_files <- vignette_files <- readme <- NULL

exclude_files <- set_arg_paths(exclude_files)
exclude_dirs <- set_arg_paths(exclude_dirs)
without_excluded <- purrr::partial(setdiff, y = exclude_dirs)
if ("\\.r" %in% filetype) {
r_files <- dir(
path = c("R", "tests", "data-raw", "demo"), pattern = "\\.r$",
ignore.case = TRUE, recursive = TRUE, full.names = TRUE
r_files <- dir_without_.(
path = without_excluded(c("R", "tests", "data-raw", "demo")),
pattern = "\\.r$",
ignore.case = TRUE,
recursive = TRUE
)
}

if ("\\.rprofile" %in% filetype) {
rprofile_files <- dir(
path = ".", pattern = "^\\.rprofile$",
ignore.case = TRUE, recursive = FALSE, full.names = TRUE,
all.files = TRUE
rprofile_files <- dir_without_.(
path = without_excluded("."), pattern = "^\\.rprofile$",
ignore.case = TRUE, recursive = FALSE, all.files = TRUE
)
}
if ("\\.rmd" %in% filetype) {
vignette_files <- dir(
path = "vignettes", pattern = "\\.rmd$",
ignore.case = TRUE, recursive = TRUE, full.names = TRUE
vignette_files <- dir_without_.(
path = without_excluded("vignettes"), pattern = "\\.rmd$",
ignore.case = TRUE, recursive = TRUE
)
readme <- dir_without_.(
path = ".",
pattern = without_excluded("^readme\\.rmd$"), ignore.case = TRUE
)
readme <- dir(pattern = "^readme\\.rmd$", ignore.case = TRUE)
}

if ("\\.rnw" %in% filetype) {
vignette_files <- append(
vignette_files,
dir(
path = "vignettes", pattern = "\\.rnw$",
ignore.case = TRUE, recursive = TRUE, full.names = TRUE
dir_without_.(
path = without_excluded("vignettes"), pattern = "\\.rnw$",
ignore.case = TRUE, recursive = TRUE
)
)
}
Expand All @@ -128,7 +140,6 @@ prettify_pkg <- function(transformers,
transform_files(files, transformers, include_roxygen_examples)
}


#' Style a string
#'
#' Styles a character vector. Each element of the character vector corresponds
Expand Down Expand Up @@ -164,7 +175,8 @@ style_text <- function(text,
#' @param path Path to a directory with files to transform.
#' @param recursive A logical value indicating whether or not files in subdirectories
#' of `path` should be styled as well.
#' @inheritParams style_pkg
#' @param exclude_dirs Character vector with directories to exclude.
##' @inheritParams style_pkg
#' @inheritSection transform_files Value
#' @inheritSection style_pkg Warning
#' @inheritSection style_pkg Round trip validation
Expand All @@ -181,10 +193,12 @@ style_dir <- function(path = ".",
filetype = c("R", "Rprofile"),
recursive = TRUE,
exclude_files = NULL,
exclude_dirs = c("packrat", "renv"),
include_roxygen_examples = TRUE) {
changed <- withr::with_dir(
path, prettify_any(
transformers, filetype, recursive, exclude_files, include_roxygen_examples
transformers,
filetype, recursive, exclude_files, exclude_dirs, include_roxygen_examples
)
)
invisible(changed)
Expand All @@ -201,14 +215,29 @@ prettify_any <- function(transformers,
filetype,
recursive,
exclude_files,
exclude_dirs,
include_roxygen_examples) {
files <- dir(
exclude_files <- set_arg_paths(exclude_files)
exclude_dirs <- set_arg_paths(exclude_dirs)
files_root <- dir(
path = ".", pattern = map_filetype_to_pattern(filetype),
ignore.case = TRUE, recursive = recursive, full.names = TRUE,
all.files = TRUE
ignore.case = TRUE, recursive = FALSE, all.files = TRUE
)
if (recursive) {
files_other <- list.dirs(full.names = FALSE, recursive = TRUE) %>%
setdiff(c("", exclude_dirs)) %>%
dir_without_.(
pattern = map_filetype_to_pattern(filetype),
ignore.case = TRUE, recursive = FALSE,
all.files = TRUE
)

} else {
files_other <- c()
}
transform_files(
setdiff(files, exclude_files), transformers, include_roxygen_examples
setdiff(c(files_root, files_other), exclude_files),
transformers, include_roxygen_examples
)
}

Expand Down Expand Up @@ -239,6 +268,7 @@ style_file <- function(path,
style = tidyverse_style,
transformers = style(...),
include_roxygen_examples = TRUE) {
path <- set_arg_paths(path)
changed <- transform_files(path, transformers, include_roxygen_examples)
invisible(changed)
}
30 changes: 30 additions & 0 deletions R/utils-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,33 @@ is_unsaved_file <- function(path) {
map_filetype_to_pattern <- function(filetype) {
paste0("(", paste(set_and_assert_arg_filetype(filetype), collapse = "|"), ")$")
}

#' `dir()`, but without dot-prefix
#'
#' When using `dir()`, you can set `full.names = FALSE`, but then you can only
#' pass a character vector of lenght one as `path` to not loose the information
#' about where the files are. This function solves that case. It's needed when
#' one wants to standardize paths to use set operations on them, i.e. when the
#' user supplied input does not have a dot prefix. See 'Examples'.
#' @param path A path.
#' @param ... Passed to [base::dir()].
#' @seealso set_and_assert_arg_paths
#' @keywords internal.
#' @examples
#' setdiff("./file.R", "file.R") # you want to standardize first.
dir_without_. <- function(path, ...) {
purrr::map(path, dir_without_._one, ...) %>%
unlist()
}

dir_without_._one <- function(path, ...) {
relative <- dir(
path = path,
full.names = FALSE,
...
)
if (path == ".") {
return(relative)
}
file.path(path, relative)
}
27 changes: 27 additions & 0 deletions man/dir_without_..Rd

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

5 changes: 5 additions & 0 deletions man/prettify_any.Rd

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

21 changes: 21 additions & 0 deletions man/set_arg_paths.Rd

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

3 changes: 3 additions & 0 deletions man/style_dir.Rd

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

5 changes: 5 additions & 0 deletions man/style_pkg.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/public-api/renvpkg/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Package: xyzpackage
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.3.3)
License: What license is it under?
Encoding: UTF-8
LazyData: true
Suggests: testthat
2 changes: 2 additions & 0 deletions tests/testthat/public-api/renvpkg/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by roxygen2: fake comment so roxygen2 overwrites silently.
exportPattern("^[^\\.]")
3 changes: 3 additions & 0 deletions tests/testthat/public-api/renvpkg/renv/hello-world.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hello_world <- function() {
print("hello, world")
}
4 changes: 4 additions & 0 deletions tests/testthat/public-api/renvpkg/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(xyzpackage)

test_check("xyzpackage")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
context("testing styler on package")

test_that("hi there", {
I(am(a(package(x))))
})
Loading