diff --git a/NEWS.md b/NEWS.md index 68ef13f8d..d2508f02d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/methods.R b/R/methods.R index 869c17ef1..7a8fe3cc1 100644 --- a/R/methods.R +++ b/R/methods.R @@ -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) @@ -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, ...))