Skip to content

Commit 0777866

Browse files
new width= parameter for output methods (#2369)
* new width= parameter for output methods * workaround for GHA * delint
1 parent 6d2b327 commit 0777866

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* `unnecessary_lambda_linter` is extended to encourage vectorized comparisons where possible, e.g. `sapply(x, sum) > 0` instead of `sapply(x, function(x) sum(x) > 0)` (part of #884, @MichaelChirico). Toggle this behavior with argument `allow_comparison`.
3131
* `backport_linter()` is slightly faster by moving expensive computations outside the linting function (#2339, #2348, @AshesITR and @MichaelChirico).
3232
* `Linter()` has a new argument `linter_level` (default `NA`). This is used by `lint()` to more efficiently check for expression levels than the idiom `if (!is_lint_level(...)) { return(list()) }` (#2351, @AshesITR).
33+
* `format()` and `print()` methods for `lint` and `lints` classes get a new option `width` to control the printing width of lint messages (#1884, @MichaelChirico). The default is controlled by a new option `lintr.format_width`; if unset, no wrapping occurs (matching earlier behavior).
3334

3435
### New linters
3536

R/methods.R

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#' @export
2-
format.lint <- function(x, ...) {
2+
format.lint <- function(x, ..., width = getOption("lintr.format_width")) {
33
if (requireNamespace("cli", quietly = TRUE)) {
44
color <- switch(x$type,
55
warning = cli::col_magenta,
@@ -15,7 +15,7 @@ format.lint <- function(x, ...) {
1515
# nocov end
1616
}
1717

18-
paste0(
18+
annotated_msg <- paste0(
1919
emph(
2020
x$filename, ":",
2121
as.character(x$line_number), ":",
@@ -24,7 +24,15 @@ format.lint <- function(x, ...) {
2424
),
2525
color(x$type, ": ", sep = ""),
2626
"[", x$linter, "] ",
27-
emph(x$message), "\n",
27+
emph(x$message)
28+
)
29+
30+
if (!is.null(width)) {
31+
annotated_msg <- paste(strwrap(annotated_msg, exdent = 4L, width = width), collapse = "\n")
32+
}
33+
34+
paste0(
35+
annotated_msg, "\n",
2836
# swap tabs for spaces for #528 (sorry Richard Hendricks)
2937
chartr("\t", " ", x$line), "\n",
3038
highlight_string(x$message, x$column_number, x$ranges),
@@ -34,7 +42,7 @@ format.lint <- function(x, ...) {
3442

3543
#' @export
3644
print.lint <- function(x, ...) {
37-
cat(format(x))
45+
cat(format(x, ...))
3846
invisible(x)
3947
}
4048

@@ -68,8 +76,8 @@ markdown <- function(x, info, ...) {
6876
}
6977

7078
#' @export
71-
format.lints <- function(x, ...) {
72-
paste(vapply(x, format, character(1L)), collapse = "\n")
79+
format.lints <- function(x, ..., width = getOption("lintr.format_width")) {
80+
paste(vapply(x, format, character(1L), width = width), collapse = "\n")
7381
}
7482

7583
#' @export

tests/testthat/test-methods.R

+40
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,43 @@ test_that("as.data.table.list is _not_ dispatched directly", {
157157
lints <- lint(text = "a = 1", linters = assignment_linter())
158158
expect_identical(nrow(data.table::as.data.table(lints)), 1L)
159159
})
160+
161+
local({
162+
# avoid impact of CLI mark-up on strwrap output.
163+
# (testthat, or cli, already do so, but force it explicitly here for emphasis)
164+
withr::local_options(c(cli.num_colors = 0L))
165+
# force "default" print method even on GHA
166+
withr::local_envvar(c(GITHUB_ACTIONS = NA))
167+
168+
test_linter <- make_linter_from_xpath("*[1]", lint_message = "The quick brown fox jumps over the lazy dog.")
169+
170+
lints <- lint(text = "a", linters = test_linter())
171+
lint <- lints[[1L]]
172+
173+
wrapped_strings <- c(
174+
"[test_linter]\n The\n quick\n brown\n fox\n jumps\n over\n the\n lazy\n dog.",
175+
"[test_linter]\n The quick brown\n fox jumps over\n the lazy dog.",
176+
"[test_linter] The\n quick brown fox jumps over the lazy\n dog.",
177+
"[test_linter] The quick brown fox jumps over the lazy dog."
178+
)
179+
180+
patrick::with_parameters_test_that(
181+
"format.lint, format.lints, print.lint, print.lints support optional message wrapping",
182+
{
183+
expect_match(format(lint, width = width), wrapped_string, fixed = TRUE)
184+
expect_match(format(lints, width = width), wrapped_string, fixed = TRUE)
185+
expect_output(print(lint, width = width), wrapped_string, fixed = TRUE)
186+
expect_output(print(lints, width = width), wrapped_string, fixed = TRUE)
187+
188+
withr::with_options(c(lintr.format_width = width), {
189+
expect_match(format(lint), wrapped_string, fixed = TRUE)
190+
expect_match(format(lints), wrapped_string, fixed = TRUE)
191+
expect_output(print(lint), wrapped_string, fixed = TRUE)
192+
expect_output(print(lints), wrapped_string, fixed = TRUE)
193+
})
194+
},
195+
.test_name = c(10L, 20L, 40L, 80L),
196+
width = c(10L, 20L, 40L, 80L),
197+
wrapped_string = wrapped_strings
198+
)
199+
})

0 commit comments

Comments
 (0)