Skip to content

Commit 71fab76

Browse files
also recognize lintr ignore markers
1 parent 88595fc commit 71fab76

File tree

8 files changed

+87
-17
lines changed

8 files changed

+87
-17
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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).
7+
48
# styler 1.6.2
59

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

R/nest.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,24 @@ 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
165-
#' `# styler: off`.
165+
#' `# styler: off`. For styler version > 1.6.2, `# nolint` and `#nolint start`
166+
#' also works by default.
166167
#' - To mark the end of this sequence, put `# styler: on` in your code. After
167-
#' that line, styler will again format your code.
168+
#' that line, styler will again format your code. For styler version > 1.6.2,
169+
#' `#nolint end` also works by default.
168170
#' - To ignore an inline statement (i.e. just one line), place `# styler: off`
169171
#' at the end of the line. Note that inline statements cannot contain other
170172
#' comments apart from the marker, i.e. a line like
171-
#' `1 # comment # styler: off` won't be ignored.
173+
#' `1 # comment # styler: off` won't be ignored. For styler version > 1.6.2,
174+
#' `# nolint` and `#nolint start` also works.
172175
#'
173176
#' To use something else as start and stop markers, set the R options
174177
#' `styler.ignore_start` and
175-
#' `styler.ignore_stop` using [options()]. If you want these
178+
#' `styler.ignore_stop` using [options()]. For styler version > 1.6.2, the
179+
#' option supports character vectors longer than one. If you want these
176180
#' settings to persist over multiple R sessions, consider setting them in your
177181
#' R profile, e.g. with `usethis::edit_rprofile()`.
178182
#' @name stylerignore

R/stylerignore.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ 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 <- parse_text %in% option_read("styler.ignore_start")
5858
pd_flat$stylerignore <- rep(FALSE, length(start_candidate))
5959
env_current$any_stylerignore <- any(start_candidate)
6060
if (!env_current$any_stylerignore) {
@@ -64,7 +64,7 @@ add_stylerignore <- function(pd_flat) {
6464
pd_flat_lat_line1 <- lag(pd_flat$line2, default = 0)
6565
on_same_line <- pd_flat$line1 == pd_flat_lat_line1
6666
cumsum_start <- cumsum(start_candidate & !on_same_line)
67-
cumsum_stop <- cumsum(parse_text == option_read("styler.ignore_stop"))
67+
cumsum_stop <- cumsum(parse_text %in% option_read("styler.ignore_stop"))
6868
pd_flat$indicator_off <- cumsum_start + cumsum_stop
6969
is_invalid <- cumsum_start - cumsum_stop < 0 | cumsum_start - cumsum_stop > 1
7070
if (any(is_invalid)) {
@@ -105,7 +105,7 @@ apply_stylerignore <- function(flattened_pd) {
105105
colnames_required_apply_stylerignore <- c(
106106
"pos_id_", "lag_newlines", "lag_spaces", "text", "first_pos_id_in_segment"
107107
)
108-
# cannot rely on flattened_pd$text == option_read("styler.ignore_start")
108+
# cannot rely on flattened_pd$text %in% option_read("styler.ignore_start")
109109
# because if the marker logic is not correct (twice off in a row), we'll
110110
# get it wrong.
111111
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 = "# styler: off",
9-
styler.ignore_stop = "# styler: on",
8+
styler.ignore_start = c("# styler: off", "# nolint", "nolint start"),
9+
styler.ignore_stop = c("# styler: on", "# nolint end"),
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
@@ -103,6 +103,7 @@ LF
103103
LIBS
104104
lifecycle
105105
Ligges
106+
lintr
106107
linux
107108
lorenz
108109
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: 9 additions & 5 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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,63 @@ 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+
})
123+
124+
test_that("works for multiple markers inline", {
125+
withr::local_options(styler.ignore_start = "# noeq", )
126+
expect_equal(
127+
style_text(c(
128+
"1+1",
129+
"1+1# noeq",
130+
"1+1"
131+
)) %>%
132+
as.character(),
133+
c("1 + 1", "1+1# noeq", "1 + 1")
134+
)
135+
})
136+
80137
test_that("works with other markers", {
81138
expect_warning(
82139
withr::with_options(

0 commit comments

Comments
 (0)