Skip to content

Commit 52cef76

Browse files
Merge pull request #849 from lorenzwalthert/multiple-markers
Also recognize lintr ignore markers
2 parents 6023284 + eaf2864 commit 52cef76

File tree

8 files changed

+70
-23
lines changed

8 files changed

+70
-23
lines changed

NEWS.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
* Remove dependency on {xfun} (#866).
1616
* use pre-commit via GitHub Actions (#872).
1717

18+
* stylerignore markers are now interpreted as regular expressions instead of
19+
comments that must match exactly. This allows to specify multiple markers in
20+
one regular expression for `styler.ignore_start` and `styler.ignore_stop`,
21+
e.g. to use markers for lintr and styler on the same line, you can use
22+
`options(styler.ignore_start = "nolint start|styler: off"`:
23+
24+
```r
25+
# nolint start, styler: off
26+
1 +1
27+
# nolint end
28+
# styler: on
29+
```
30+
As a consequence of this approach, the defaults for `styler.ignore_start` and
31+
`styler.ignore_stop` omit the `#` (#849).
32+
33+
1834
# styler 1.6.2
1935

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

R/nest.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,22 +159,20 @@ find_pos_id_to_keep <- function(pd) {
159159
#' [detected by styler](https://styler.r-lib.org/articles/detect-alignment.html),
160160
#' making stylerignore redundant. See a few illustrative examples below.
161161
#' @details
162-
#' Styling is on by default when you run styler.
162+
#' Styling is on for all lines by default when you run styler.
163163
#'
164164
#' - To mark the start of a sequence where you want to turn styling off, use
165165
#' `# styler: off`.
166166
#' - To mark the end of this sequence, put `# styler: on` in your code. After
167167
#' that line, styler will again format your code.
168168
#' - To ignore an inline statement (i.e. just one line), place `# styler: off`
169-
#' at the end of the line. Note that inline statements cannot contain other
170-
#' comments apart from the marker, i.e. a line like
171-
#' `1 # comment # styler: off` won't be ignored.
172-
#'
169+
#' at the end of the line.
173170
#' To use something else as start and stop markers, set the R options
174171
#' `styler.ignore_start` and
175-
#' `styler.ignore_stop` using [options()]. If you want these
176-
#' settings to persist over multiple R sessions, consider setting them in your
177-
#' R profile, e.g. with `usethis::edit_rprofile()`.
172+
#' `styler.ignore_stop` using [options()]. For styler version > 1.6.2, the
173+
#' option supports character vectors longer than one and the marker are not
174+
#' exactly matched, but using a regular expression, which means you can have
175+
#' multiple marker on one line, e.g. `# nolint start styler: off`.
178176
#' @name stylerignore
179177
#' @examples
180178
#' # as long as the order of the markers is correct, the lines are ignored.

R/stylerignore.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ env_add_stylerignore <- function(pd_flat) {
4848
#'
4949
#' See examples in [stylerignore]. Note that you should reuse the stylerignore
5050
#' column to compute switch points or similar and not a plain
51-
#' `pd$text == option_read("styler.ignore_start")` because that will fail to
51+
#' `pd$text %in% option_read("styler.ignore_start")` because that will fail to
5252
#' give correct switch points in the case stylerignore sequences are invalid.
5353
#' @param pd_flat A parse table.
5454
#' @keywords internal
5555
add_stylerignore <- function(pd_flat) {
5656
parse_text <- trimws(pd_flat$text)
57-
start_candidate <- parse_text == option_read("styler.ignore_start")
57+
start_candidate <- grepl(
58+
option_read("styler.ignore_start"), parse_text
59+
) & pd_flat$token == "COMMENT"
5860
pd_flat$stylerignore <- rep(FALSE, length(start_candidate))
5961
env_current$any_stylerignore <- any(start_candidate)
6062
if (!env_current$any_stylerignore) {
@@ -64,7 +66,10 @@ add_stylerignore <- function(pd_flat) {
6466
pd_flat_lat_line1 <- lag(pd_flat$line2, default = 0)
6567
on_same_line <- pd_flat$line1 == pd_flat_lat_line1
6668
cumsum_start <- cumsum(start_candidate & !on_same_line)
67-
cumsum_stop <- cumsum(parse_text == option_read("styler.ignore_stop"))
69+
cumsum_stop <- cumsum(
70+
grepl(option_read("styler.ignore_stop"), parse_text) &
71+
pd_flat$token == "COMMENT"
72+
)
6873
pd_flat$indicator_off <- cumsum_start + cumsum_stop
6974
is_invalid <- cumsum_start - cumsum_stop < 0 | cumsum_start - cumsum_stop > 1
7075
if (any(is_invalid)) {

R/zzz.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
styler.cache_root = NULL,
66
styler.cache_name = styler_version,
77
styler.colored_print.vertical = TRUE,
8-
styler.ignore_start = "# styler: off",
9-
styler.ignore_stop = "# styler: on",
8+
styler.ignore_start = "styler: off",
9+
styler.ignore_stop = "styler: on",
1010
styler.quiet = FALSE,
1111
styler.test_dir_writable = TRUE
1212
)

inst/WORDLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ LF
105105
LIBS
106106
lifecycle
107107
Ligges
108+
lintr
108109
linux
109110
lorenz
110111
lorenzwalthert

man/add_stylerignore.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.

man/stylerignore.Rd

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

tests/testthat/test-stylerignore.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,35 @@ test_that("works with other markers", {
7777
)
7878
})
7979

80+
81+
test_that("works for multiple markers inline", {
82+
withr::local_options(styler.ignore_start = "# noeq", )
83+
expect_equal(
84+
style_text(c(
85+
"1+1",
86+
"1+1# noeq",
87+
"1+1"
88+
)) %>%
89+
as.character(),
90+
c("1 + 1", "1+1# noeq", "1 + 1")
91+
)
92+
})
93+
94+
95+
test_that("works for multiple markers inline on one line", {
96+
withr::local_options(styler.ignore_start = "nolint start|styler: off")
97+
expect_equal(
98+
style_text(c(
99+
"1+1",
100+
"1+1# nolint start styler: off",
101+
"1+1"
102+
)) %>%
103+
as.character(),
104+
c("1 + 1", "1+1# nolint start styler: off", "1 + 1")
105+
)
106+
})
107+
108+
80109
test_that("works with other markers", {
81110
expect_warning(
82111
withr::with_options(

0 commit comments

Comments
 (0)