Skip to content

Commit

Permalink
Support clickable hyperlinks (#2646)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
olivroy and AshesITR authored Aug 9, 2024
1 parent 8d96145 commit 5990da6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
## Notes

* 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.
* File locations in lints and error messages contain clickable hyperlinks to improve code navigation (#2645, #2588, @olivroy).
* {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).

# lintr 3.1.2
Expand Down
42 changes: 22 additions & 20 deletions R/methods.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
#' @export
format.lint <- function(x, ..., width = getOption("lintr.format_width")) {
if (requireNamespace("cli", quietly = TRUE)) {
color <- switch(x$type,
warning = cli::col_magenta,
error = cli::col_red,
style = cli::col_blue,
cli::style_bold
)
emph <- cli::style_bold
} else {
# nocov start
color <- identity
emph <- identity
# nocov end
}
color <- switch(x$type,
warning = cli::col_magenta,
error = cli::col_red,
style = cli::col_blue,
cli::style_bold
)
emph <- cli::style_bold

line_ref <- build_line_ref(x)
annotated_msg <- paste0(
emph(
x$filename, ":",
as.character(x$line_number), ":",
as.character(x$column_number), ": ",
sep = ""
),
emph(line_ref, ": "),
color(x$type, ": ", sep = ""),
"[", x$linter, "] ",
emph(x$message)
Expand All @@ -40,6 +29,19 @@ format.lint <- function(x, ..., width = getOption("lintr.format_width")) {
)
}

build_line_ref <- function(x) {
line_ref <- paste0(
x$filename, ":",
as.character(x$line_number), ":",
as.character(x$column_number)
)

if (!cli::ansi_has_hyperlink_support()) {
return(line_ref)
}
cli::format_inline("{.path {line_ref}}")
}

#' @export
print.lint <- function(x, ...) {
cat(format(x, ...))
Expand Down

0 comments on commit 5990da6

Please sign in to comment.