Skip to content

Commit 67b8681

Browse files
committed
Use RStudio source marker API to display lints
When running under a version of RStudio that supports the source marker API display lints using the Markers pane (this behavior can be disabled via a global option).
1 parent 0378b0b commit 67b8681

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Imports:
1616
stringdist,
1717
testthat,
1818
digest,
19-
igraph
19+
igraph,
20+
rstudioapi (>= 0.2)
2021
License: MIT + file LICENSE
2122
LazyData: true
2223
Suggests:

R/lint.R

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ lint_package <- function(path = NULL, relative_path = TRUE, ...) {
118118

119119
lints <- reorder_lints(lints)
120120
class(lints) <- "lints"
121+
attr(lints, "package_path") <- path
122+
attr(lints, "relative_path") <- relative_path
121123
lints
122124
}
123125

@@ -198,10 +200,45 @@ print.lint <- function(x, ...) {
198200

199201
#' @export
200202
print.lints <- function(x, ...) {
201-
lapply(x, print, ...)
203+
if (getOption("lintr.rstudio_source_markers", TRUE) &&
204+
rstudioapi::hasFun("sourceMarkers"))
205+
rstudio_source_markers(x)
206+
else
207+
lapply(x, print, ...)
202208
invisible(x)
203209
}
204210

211+
rstudio_source_markers <- function(lints) {
212+
213+
# if relative paths were requested then save the package_path
214+
# to be passed along as the basePath to the sourceMarker function
215+
relative_path <- isTRUE(attr(lints, "relative_path"))
216+
if (relative_path)
217+
package_path <- attr(lints, "package_path")
218+
else
219+
package_path <- NULL
220+
221+
# generate the markers
222+
markers <- lapply(lints, function(x) {
223+
marker <- list()
224+
marker$type <- x$type
225+
if (relative_path)
226+
x$filename <- file.path(package_path, x$filename)
227+
marker$file <- x$filename
228+
marker$line <- x$line_number
229+
marker$column <- x$column_number
230+
marker$message <- x$message
231+
marker
232+
})
233+
234+
# request source markers
235+
rstudioapi::callFun("sourceMarkers",
236+
name = "lintr",
237+
markers = markers,
238+
basePath = package_path,
239+
autoSelect = "first")
240+
}
241+
205242
highlight_string <- function(message, column_number = NULL, ranges = NULL) {
206243

207244
maximum <- max(column_number, unlist(ranges))

0 commit comments

Comments
 (0)