Skip to content

Commit 5990da6

Browse files
olivroyAshesITR
andauthored
Support clickable hyperlinks (#2646)
* lintr now imports cli * Support clickable hyperlinks when they are available * Rename variables + restyle * Remove unneeded space * refactor in `build_line_ref()` * Support path with closing brackets * Update NEWS.md * Update NEWS.md Co-authored-by: AshesITR <[email protected]> * Update methods.R --------- Co-authored-by: AshesITR <[email protected]>
1 parent 8d96145 commit 5990da6

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
## Notes
8989

9090
* All user-facing messages are now prepared using the `{cli}` package (#2418, @IndrajeetPatil). All messages have been reviewed and updated to be more informative and consistent.
91+
* File locations in lints and error messages contain clickable hyperlinks to improve code navigation (#2645, #2588, @olivroy).
9192
* {lintr} now depends on R version 4.0.0. It already does so implicitly due to recursive upstream dependencies requiring this version; we've simply made that dependency explicit and up-front (#2569, @MichaelChirico).
9293

9394
# lintr 3.1.2

R/methods.R

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
#' @export
22
format.lint <- function(x, ..., width = getOption("lintr.format_width")) {
3-
if (requireNamespace("cli", quietly = TRUE)) {
4-
color <- switch(x$type,
5-
warning = cli::col_magenta,
6-
error = cli::col_red,
7-
style = cli::col_blue,
8-
cli::style_bold
9-
)
10-
emph <- cli::style_bold
11-
} else {
12-
# nocov start
13-
color <- identity
14-
emph <- identity
15-
# nocov end
16-
}
3+
color <- switch(x$type,
4+
warning = cli::col_magenta,
5+
error = cli::col_red,
6+
style = cli::col_blue,
7+
cli::style_bold
8+
)
9+
emph <- cli::style_bold
1710

11+
line_ref <- build_line_ref(x)
1812
annotated_msg <- paste0(
19-
emph(
20-
x$filename, ":",
21-
as.character(x$line_number), ":",
22-
as.character(x$column_number), ": ",
23-
sep = ""
24-
),
13+
emph(line_ref, ": "),
2514
color(x$type, ": ", sep = ""),
2615
"[", x$linter, "] ",
2716
emph(x$message)
@@ -40,6 +29,19 @@ format.lint <- function(x, ..., width = getOption("lintr.format_width")) {
4029
)
4130
}
4231

32+
build_line_ref <- function(x) {
33+
line_ref <- paste0(
34+
x$filename, ":",
35+
as.character(x$line_number), ":",
36+
as.character(x$column_number)
37+
)
38+
39+
if (!cli::ansi_has_hyperlink_support()) {
40+
return(line_ref)
41+
}
42+
cli::format_inline("{.path {line_ref}}")
43+
}
44+
4345
#' @export
4446
print.lint <- function(x, ...) {
4547
cat(format(x, ...))

0 commit comments

Comments
 (0)