Skip to content

Commit 2abb042

Browse files
lorenzwalthertjennybc
authored andcommitted
new use_tidy_style() (#197)
Closes #72 * add use_tidy_style * add warning (c/p from styler). * add a test * rename testthat file * add styler reference * add check_uncommitted_changed * dispatch between style_pkg and style_dir * adapt use_dev_version() style * avoid EOF warning * switch to usethis:::proj_path() * adapt to review comments * fiddle with tests... remove capture_output for now as suggested by reviewer to see what happens. * mask testthat call * printing * fix test to not include dplyr-dependency edge case in styler. * skip tests on R < 3.2 because git2r::discover_repository() not working on R 3.1. * remove print statements * fix bug for non-packages * capture output * remove testthat mask * adapt help file and test description for non-packages * final tweaks
1 parent da1a8c2 commit 2abb042

File tree

7 files changed

+97
-6
lines changed

7 files changed

+97
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Imports:
3333
rmarkdown,
3434
rprojroot,
3535
rstudioapi,
36+
styler,
3637
whisker
3738
Suggests:
3839
covr,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export(use_tidy_contributing)
6565
export(use_tidy_description)
6666
export(use_tidy_eval)
6767
export(use_tidy_issue_template)
68+
export(use_tidy_style)
6869
export(use_tidy_support)
6970
export(use_tidy_versions)
7071
export(use_travis)

R/dev-version.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
#' @export
77
use_dev_version <- function() {
88
check_is_package("use_dev_version()")
9-
if (uses_git() && git_uncommitted()) {
10-
stop(
11-
"Uncommited changes. Please commit to git before continuing",
12-
call. = FALSE
13-
)
14-
}
9+
check_uncommitted_changes()
1510

1611
ver <- desc::desc_get_version(proj_get())
1712
if (length(unlist(ver)) > 3) {

R/git-utils.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ git_uncommitted <- function(path = ".") {
33
st <- vapply(git2r::status(r), length, integer(1))
44
any(st != 0)
55
}
6+
7+
check_uncommitted_changes <- function(path = proj_get()) {
8+
if (uses_git(path) && git_uncommitted(path)) {
9+
stop(
10+
"Uncommited changes. Please commit to git before continuing",
11+
call. = FALSE
12+
)
13+
}
14+
}

R/styler.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#' Style according to the tidyverse style guide
2+
#'
3+
#' Styles the source code of a package, project or directory according to the
4+
#' [tidyverse style guide](http://style.tidyverse.org) with the
5+
#' package [styler](http://styler.r-lib.org).
6+
#' @param strict Boolean indicating whether or not a strict version of styling
7+
#' should be applied. See [styler::tidyverse_style()] for details.
8+
#' @section Warning:
9+
#' This function overwrites files (if styling results in a change of the
10+
#' code to be formatted). It is strongly suggested to only style files
11+
#' that are under version control or to create a backup copy.
12+
#' @return
13+
#' Invisibly returns a data frame that indicates for each file considered for
14+
#' styling whether or not it was actually changed.
15+
#' @export
16+
use_tidy_style <- function(strict = TRUE) {
17+
check_installed("styler")
18+
check_uncommitted_changes()
19+
if (is_package()) {
20+
styled <- styler::style_pkg(proj_get(),
21+
style = styler::tidyverse_style, strict = strict
22+
)
23+
} else {
24+
styled <- styler::style_dir(proj_get(),
25+
style = styler::tidyverse_style, strict = strict
26+
)
27+
}
28+
cat_line()
29+
done("Styled project according to the tidyverse style guide")
30+
invisible(styled)
31+
}

man/use_tidy_style.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
context("use_tidy_style")
2+
3+
test_that("styling the package works", {
4+
skip_if(getRversion() < 3.2)
5+
skip_if_no_git_config()
6+
pkg <- scoped_temporary_package()
7+
capture_output(use_r("bad_style"))
8+
path_to_bad_style <- proj_path("R/bad_style.R")
9+
write_utf8(path_to_bad_style, "a++2\n")
10+
capture_output(use_tidy_style())
11+
expect_identical(readLines(path_to_bad_style), "a + +2")
12+
unlink(path_to_bad_style)
13+
})
14+
15+
16+
test_that("styling of non-packages works", {
17+
skip_if(getRversion() < 3.2)
18+
skip_if_no_git_config()
19+
proj <- scoped_temporary_project()
20+
path_to_bad_style <- proj_path("R/bad_style.R")
21+
capture_output(use_r("bad_style"))
22+
write_utf8(path_to_bad_style, "a++22\n")
23+
capture_output(use_tidy_style())
24+
expect_identical(readLines(path_to_bad_style), "a + +22")
25+
unlink(path_to_bad_style)
26+
})

0 commit comments

Comments
 (0)