Skip to content

Commit f447c1e

Browse files
Don't allow multiple since regex is already enough flexible
1 parent 71fab76 commit f447c1e

File tree

7 files changed

+54
-77
lines changed

7 files changed

+54
-77
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ VignetteBuilder:
4646
Encoding: UTF-8
4747
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate",
4848
"pkgapi::api_roclet"))
49-
RoxygenNote: 7.1.1.9001
49+
RoxygenNote: 7.1.2
5050
Collate:
5151
'addins.R'
5252
'communicate.R'

NEWS.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
# styler 1.6.2.9000 (Development version)
22

33

4-
* multiple stylerignore patterns can be specified at once and lintr markers
5-
`# nolint`, `# nolint start` and `# nolint end` are now by default recognized
6-
in addition to `# styler: off` and `# styler: on` (#849).
4+
* stylerignore markers are now interpreted as regular expressions instead of
5+
comments that must match exactly. This allows to specify multiple markers in
6+
one regular expression for `styler.ignore_start` and `styler.ignore_stop`,
7+
e.g. to use markers for lintr and styler on the same line, you can use
8+
`options(styler.ignore_start = "nolint start|styler: off"`:
9+
10+
```r
11+
# nolint start, styler: off
12+
1 +1
13+
# nolint end
14+
# styler: on
15+
```
16+
As a consequence of this approach, the defaults for `styler.ignore_start` and
17+
`styler.ignore_stop` omit the `#` (#849).
18+
719

820
# styler 1.6.2
921

R/nest.R

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,17 @@ find_pos_id_to_keep <- function(pd) {
162162
#' 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
165-
#' `# styler: off`. For styler version > 1.6.2, `# nolint` and `#nolint start`
166-
#' also works by default.
165+
#' `# styler: off`.
167166
#' - To mark the end of this sequence, put `# styler: on` in your code. After
168-
#' that line, styler will again format your code. For styler version > 1.6.2,
169-
#' `#nolint end` also works by default.
167+
#' that line, styler will again format your code.
170168
#' - To ignore an inline statement (i.e. just one line), place `# styler: off`
171-
#' at the end of the line. Note that inline statements cannot contain other
172-
#' comments apart from the marker, i.e. a line like
173-
#' `1 # comment # styler: off` won't be ignored. For styler version > 1.6.2,
174-
#' `# nolint` and `#nolint start` also works.
175-
#'
169+
#' at the end of the line.
176170
#' To use something else as start and stop markers, set the R options
177171
#' `styler.ignore_start` and
178172
#' `styler.ignore_stop` using [options()]. For styler version > 1.6.2, the
179-
#' option supports character vectors longer than one. If you want these
180-
#' settings to persist over multiple R sessions, consider setting them in your
181-
#' R profile, e.g. with `usethis::edit_rprofile()`.
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`.
182176
#' @name stylerignore
183177
#' @examples
184178
#' # 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
@@ -54,7 +54,9 @@ env_add_stylerignore <- function(pd_flat) {
5454
#' @keywords internal
5555
add_stylerignore <- function(pd_flat) {
5656
parse_text <- trimws(pd_flat$text)
57-
start_candidate <- parse_text %in% 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 %in% 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)) {
@@ -105,7 +110,7 @@ apply_stylerignore <- function(flattened_pd) {
105110
colnames_required_apply_stylerignore <- c(
106111
"pos_id_", "lag_newlines", "lag_spaces", "text", "first_pos_id_in_segment"
107112
)
108-
# cannot rely on flattened_pd$text %in% option_read("styler.ignore_start")
113+
# cannot rely on flattened_pd$text == option_read("styler.ignore_start")
109114
# because if the marker logic is not correct (twice off in a row), we'll
110115
# get it wrong.
111116
to_ignore <- flattened_pd$stylerignore == TRUE

R/zzz.R

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

man/stylerignore.Rd

Lines changed: 7 additions & 13 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: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,49 +77,6 @@ test_that("works with other markers", {
7777
)
7878
})
7979

80-
test_that("works with multiple markers", {
81-
expect_equal(
82-
withr::with_options(
83-
list(
84-
styler.ignore_start = c("# startignore", "#lintstart"),
85-
styler.ignore_stop = "# xxx"
86-
),
87-
{
88-
style_text(c(
89-
"1+1",
90-
"#lintstart",
91-
"1+1",
92-
"# xxx",
93-
"1+1"
94-
)) %>%
95-
as.character()
96-
}
97-
),
98-
c("1 + 1", "#lintstart", "1+1", "# xxx", "1 + 1")
99-
)
100-
})
101-
102-
test_that("works with multiple markers", {
103-
expect_equal(
104-
withr::with_options(
105-
list(
106-
styler.ignore_start = "# startignore",
107-
styler.ignore_stop = c("# xxx", "# lintstop")
108-
),
109-
{
110-
style_text(c(
111-
"1+1",
112-
"# startignore",
113-
"1+1",
114-
"# lintstop",
115-
"1+1"
116-
)) %>%
117-
as.character()
118-
}
119-
),
120-
c("1 + 1", "# startignore", "1+1", "# lintstop", "1 + 1")
121-
)
122-
})
12380

12481
test_that("works for multiple markers inline", {
12582
withr::local_options(styler.ignore_start = "# noeq", )
@@ -134,6 +91,21 @@ test_that("works for multiple markers inline", {
13491
)
13592
})
13693

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+
137109
test_that("works with other markers", {
138110
expect_warning(
139111
withr::with_options(

0 commit comments

Comments
 (0)