Skip to content

Commit

Permalink
Merge 52cef76 into 9305b62
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert authored Dec 16, 2021
2 parents 9305b62 + 52cef76 commit deff5e9
Show file tree
Hide file tree
Showing 74 changed files with 733 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tests/testthat/**/*_tree linguist-generated=true
tests/testthat/**/*_tree linguist-generated=true
5 changes: 3 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ jobs:
matrix:
config:
- {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'devel'}
- {os: windows-latest, r: 'release'}
- {os: windows-latest, r: '3.6', rspm: "https://packagemanager.rstudio.com/cran/latest"}
- {os: ubuntu-18.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest", http-user-agent: "R/4.0.0 (ubuntu-18.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
- {os: windows-latest, r: '3.6', rspm: "https://packagemanager.rstudio.com/cran/latest"}
- {os: ubuntu-18.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest", http-user-agent: "R/4.0.0 (ubuntu-18.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
- {os: ubuntu-18.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-18.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
- {os: ubuntu-18.04, r: '3.6', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
Expand Down
Empty file removed .github/workflows/check-full.yaml
Empty file.
2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
on:
push:
branches: master
branches: main

name: pkgdown

Expand Down
55 changes: 55 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: pre-commit
on:
push:
branches-ignore:
- 'master'
- 'main'
pull_request:
types: [opened, synchronize, reopened, ready_for_review]

jobs:
pre-commit:
runs-on: ubuntu-18.04
if: >-
!contains(github.event.head_commit.message, 'ci skip') &&
(
startsWith(github.ref, 'refs/heads') ||
github.event.pull_request.draft == false
)
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install system dependencies
if: runner.os == 'Linux'
run: |
# your system installation code here
# sudo apt-get install -y libcurl4-openssl-dev
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.8"
architecture: "x64"
- name: Run pre-commit
uses: pre-commit/[email protected]
- name: Commit files
if: failure() && startsWith(github.ref, 'refs/heads')
run: |
if [[ `git status --porcelain --untracked-files=no` ]]; then
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git checkout -- .github/workflows
git commit -m "pre-commit" -a
fi
- name: Push changes
if: failure() && startsWith(github.ref, 'refs/heads')
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
env:
RENV_CONFIG_CACHE_ENABLED: FALSE
4 changes: 2 additions & 2 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main

name: test-coverage

Expand Down
25 changes: 19 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ default_stages: [commit]

repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.1.3.9014
rev: v0.1.3.9139
hooks:
- id: style-files
args: [--style_pkg=styler, --style_fun=tidyverse_style]
exclude: >
(?x)^(
tests/testthat/.*/.*\.R(md)?|
tests/testthat/.*/.*\.R(md|nw)?|
vignettes/customizing_styler\.Rmd|
tests/testthat/public-api/xyzfile-rnw/random4\.Rnw|
vignettes/detect-alignment\.Rmd|
tests/testmanual/addins/.*invalid.*|
tests/testmanual/addins/r-valid\.R|
)$
- id: roxygenize
additional_dependencies:
- r-lib/pkgapi
- id: use-tidy-description
- id: spell-check
exclude: >
Expand Down Expand Up @@ -49,7 +54,8 @@ repos:
tests/testthat/public-api/xyzaddin/addin_region-.*|
tests/testmanual/addins/r-invalid\.R|
tests/testthat/escaping/basic-escape-out\.R|
tests/testthat/indention_operators/base_pipe_and_assignment-.*|
tests/testthat/indention_operators/.*pipe.*|
tests/testthat/line_breaks_and_other/.*pipe.*|
tests/testthat/exception_handling/parser-error.R|
)$
- id: no-browser-statement
Expand All @@ -58,14 +64,15 @@ repos:
tests/testthat/public-api/xyzaddin/addin_region-.*|
tests/testmanual/addins/r-invalid\.R|
tests/testthat/escaping/basic-escape-out\.R|
tests/testthat/indention_operators/base_pipe_and_assignment-.*|
tests/testthat/indention_operators/.*pipe.*|
tests/testthat/line_breaks_and_other/.*pipe.*|
tests/testthat/exception_handling/parser-error.R|
)$
- id: deps-in-desc
exclude: >
(?x)^(
touchstone/.*|
tests/testmanual/addins/r-invalid\.R|
tests/testmanual/addins/.*invalid.*|
tests/testthat/escaping/basic-escape-out\.R|
tests/testthat/rnw/011-conditional-eval-out\.Rnw|
tests/testthat/.*\.R(md)?
Expand All @@ -76,7 +83,13 @@ repos:
- id: check-added-large-files
args: ['--maxkb=200']
- id: end-of-file-fixer
exclude: '\.Rd'
exclude: >
(?x)^(
\.Rd|
tests/testthat/exception_handling/empty_file\.R|
tests/testthat/parse_comments/eol_eof_spaces-.*|
tests/testthat/reference-objects/.*|
)$
- repo: local
hooks:
- id: forbid-to-commit
Expand Down
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: styler
Title: Non-Invasive Pretty Printing of R Code
Version: 1.6.2
Version: 1.6.2.9000
Authors@R:
c(person(given = "Kirill",
family = "Müller",
Expand All @@ -16,8 +16,9 @@ Description: Pretty-prints R code without changing the user's formatting
License: MIT + file LICENSE
URL: https://github.com/r-lib/styler, https://styler.r-lib.org
BugReports: https://github.com/r-lib/styler/issues
Depends:
R (>= 3.4.0)
Imports:
backports (>= 1.1.0),
cli (>= 1.1.0),
glue,
magrittr (>= 2.0.0),
Expand All @@ -29,7 +30,6 @@ Imports:
tibble (>= 1.4.2),
tools,
withr (>= 1.0.0),
xfun (>= 0.1)
Suggests:
data.tree (>= 0.1.6),
digest,
Expand All @@ -46,7 +46,7 @@ VignetteBuilder:
Encoding: UTF-8
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate",
"pkgapi::api_roclet"))
RoxygenNote: 7.1.1.9001
RoxygenNote: 7.1.2
Collate:
'addins.R'
'communicate.R'
Expand Down
35 changes: 34 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
# styler 1.2
# styler 1.6.2.9000 (Development version)

* Alignment detection respects stylerignore (#850).
* Add vignette on distributing style guide (#846, #861).
* Enable pre-commit.ci (#843).
* new R option `styler.cache_root` (defaulting to `"styler"`) that determines
the sub-directory under the {R.cache} cache directory that {styler} uses. Non-
default caches won't be cleaned up by {styler}. We suggest `"styler-perm"`
(also used by {precommit}).
* rename default branch to main (#859).
* Fix argument name `filetype` in Example for `style_dir()` (#855).
* ensure a trailing blank line also if the input is cached (#867).
* Bump minimal R requirement to 3.4 in line with the [tidyverse](https://www.tidyverse.org/blog/2019/04/r-version-support/), which
allowed to remove the dependency at {backports} and some exception handling.
* Remove dependency on {xfun} (#866).
* use pre-commit via GitHub Actions (#872).

* stylerignore markers are now interpreted as regular expressions instead of
comments that must match exactly. This allows to specify multiple markers in
one regular expression for `styler.ignore_start` and `styler.ignore_stop`,
e.g. to use markers for lintr and styler on the same line, you can use
`options(styler.ignore_start = "nolint start|styler: off"`:

```r
# nolint start, styler: off
1 +1
# nolint end
# styler: on
```
As a consequence of this approach, the defaults for `styler.ignore_start` and
`styler.ignore_stop` omit the `#` (#849).


# styler 1.6.2

* clean up cache files older than one week (#842).

Expand Down
4 changes: 2 additions & 2 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' Helper functions for styling via RStudio Addins.
#' @section Addins:
#' - Set style: Select the style transformers to use. For flexibility, the user
#' input is passed to the `transformers` argument, not the `style` argument, so
#' entering `styler::tidyverse_style(scope = "spaces")` in the Addin is
#' input is passed to the `transformers` argument, not the `style` argument,
#' so entering `styler::tidyverse_style(scope = "spaces")` in the Addin is
#' equivalent to `styler::style_text("1+1", scope = "spaces")` and
#' `styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))`
#' if the text to style is `1+1`. The style transformers are memorized
Expand Down
2 changes: 1 addition & 1 deletion R/communicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @inheritParams can_verify_roundtrip
#' @keywords internal
communicate_warning <- function(changed, transformers) {
if (any(changed, na.rm = TRUE) &&
if (any(changed, na.rm = TRUE) &&
!can_verify_roundtrip(transformers) &&
!getOption("styler.quiet", FALSE)
) {
Expand Down
10 changes: 9 additions & 1 deletion R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ token_is_on_aligned_line <- function(pd_flat) {
pd_flat$lag_newlines <- pd_flat$pos_id <- NULL
pd_flat$.lag_spaces <- lag(pd_flat$spaces)
pd_by_line <- split(pd_flat, line_idx)
pd_by_line[purrr::map_lgl(pd_by_line, ~ any(.x$stylerignore))] <- NULL
if (length(pd_by_line) < 1) {
return(TRUE)
}
last_line_is_closing_brace_only <- nrow(last(pd_by_line)) == 1
relevant_idx <- seq2(2, ifelse(last_line_is_closing_brace_only,
length(pd_by_line) - 1,
Expand Down Expand Up @@ -86,7 +90,11 @@ token_is_on_aligned_line <- function(pd_flat) {
}

pd_by_line <- alignment_drop_comments(pd_by_line) %>%
alignment_ensure_no_closing_brace(last_line_is_closing_brace_only) %>%
alignment_ensure_no_closing_brace(last_line_is_closing_brace_only)
if (length(pd_by_line) < 1) {
return(TRUE)
}
pd_by_line <- pd_by_line %>%
alignment_ensure_trailing_comma()
# now, pd only contains arguments separated by values, ideal for iterating
# over columns.
Expand Down
9 changes: 8 additions & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ transform_utf8_one <- function(path, fun, dry) {
} else if (dry == "on") {
# don't do anything
} else if (dry == "off") {
xfun::write_utf8(new, path)
write_utf8(new, path)
} else {
# not implemented
}
Expand Down Expand Up @@ -114,3 +114,10 @@ read_utf8_bare <- function(con, warn = TRUE) {
invalid_utf8 <- function(x) {
which(!is.na(x) & is.na(iconv(x, "UTF-8", "UTF-8")))
}

#' Drop-in replacement for `xfun::write_utf8()`
#' @keywords internal
write_utf8 <- function(text, con, ...) {
withr::local_options(encoding = "native.enc")
writeLines(enc2utf8(text), con, ..., useBytes = TRUE)
}
14 changes: 6 additions & 8 deletions R/nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,20 @@ find_pos_id_to_keep <- function(pd) {
#' [detected by styler](https://styler.r-lib.org/articles/detect-alignment.html),
#' making stylerignore redundant. See a few illustrative examples below.
#' @details
#' Styling is on by default when you run styler.
#' Styling is on for all lines by default when you run styler.
#'
#' - To mark the start of a sequence where you want to turn styling off, use
#' `# styler: off`.
#' - To mark the end of this sequence, put `# styler: on` in your code. After
#' that line, styler will again format your code.
#' - To ignore an inline statement (i.e. just one line), place `# styler: off`
#' at the end of the line. Note that inline statements cannot contain other
#' comments apart from the marker, i.e. a line like
#' `1 # comment # styler: off` won't be ignored.
#'
#' at the end of the line.
#' To use something else as start and stop markers, set the R options
#' `styler.ignore_start` and
#' `styler.ignore_stop` using [options()]. If you want these
#' settings to persist over multiple R sessions, consider setting them in your
#' R profile, e.g. with `usethis::edit_rprofile()`.
#' `styler.ignore_stop` using [options()]. For styler version > 1.6.2, the
#' option supports character vectors longer than one and the marker are not
#' exactly matched, but using a regular expression, which means you can have
#' multiple marker on one line, e.g. `# nolint start styler: off`.
#' @name stylerignore
#' @examples
#' # as long as the order of the markers is correct, the lines are ignored.
Expand Down
25 changes: 0 additions & 25 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ add_id_and_short <- function(pd) {
#' @importFrom magrittr or
#' @keywords internal
ensure_correct_txt <- function(pd, text) {
ensure_valid_pd(pd)
is_problematic_text <- or(
is_insufficiently_parsed_string(pd),
is_insufficiently_parsed_number(pd)
Expand Down Expand Up @@ -173,30 +172,6 @@ ensure_correct_txt <- function(pd, text) {
arrange_pos_id()
}

#' Ensure that the parse data is valid
#'
#' Test whether all non-terminals have at least one child and throw an error
#' otherwise. As this is check is rather expensive, it is only
#' carried out for configurations we have good reasons to expect problems.
#' @param pd A parse table.
#' @importFrom rlang abort
#' @keywords internal
ensure_valid_pd <- function(pd) {
if (getRversion() < "3.2") {
non_terminals <- pd %>%
filter(terminal == FALSE)
valid_pd <- non_terminals$id %>%
map_lgl(~ .x %in% pd$parent) %>%
all()
if (!valid_pd) {
abort(paste(
"The parse data is not valid and the problem is most likely related",
"to the parser in base R. Please install R >= 3.2 and try again."
))
}
}
TRUE
}

#' Identify strings that were not fully parsed
#'
Expand Down
2 changes: 1 addition & 1 deletion R/roxygen-examples-find.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ identify_start_to_stop_of_roxygen_examples_from_text <- function(text) {
}

identify_start_to_stop_of_roxygen_examples <- function(path) {
content <- xfun::read_utf8(path)
content <- read_utf8_bare(path)
identify_start_to_stop_of_roxygen_examples_from_text(content)
}

Expand Down
3 changes: 2 additions & 1 deletion R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
#' # brace expressions go on new line if part of a pipe, in function call...
#' c(
#' data %>%
#' filter(bar) %>% {
#' filter(bar) %>%
#' {
#' cor(.$col1, .$col2, use = "complete.obs")
#' }
#' )
Expand Down
Loading

0 comments on commit deff5e9

Please sign in to comment.