Skip to content

Commit

Permalink
Use RStudio source marker API to display lints
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jjallaire committed Jan 23, 2015
1 parent 0378b0b commit 67b8681
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Imports:
stringdist,
testthat,
digest,
igraph
igraph,
rstudioapi (>= 0.2)
License: MIT + file LICENSE
LazyData: true
Suggests:
Expand Down
39 changes: 38 additions & 1 deletion R/lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ lint_package <- function(path = NULL, relative_path = TRUE, ...) {

lints <- reorder_lints(lints)
class(lints) <- "lints"
attr(lints, "package_path") <- path
attr(lints, "relative_path") <- relative_path
lints
}

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

#' @export
print.lints <- function(x, ...) {
lapply(x, print, ...)
if (getOption("lintr.rstudio_source_markers", TRUE) &&
rstudioapi::hasFun("sourceMarkers"))
rstudio_source_markers(x)
else
lapply(x, print, ...)
invisible(x)
}

rstudio_source_markers <- function(lints) {

# if relative paths were requested then save the package_path
# to be passed along as the basePath to the sourceMarker function
relative_path <- isTRUE(attr(lints, "relative_path"))
if (relative_path)
package_path <- attr(lints, "package_path")
else
package_path <- NULL

# generate the markers
markers <- lapply(lints, function(x) {
marker <- list()
marker$type <- x$type
if (relative_path)
x$filename <- file.path(package_path, x$filename)
marker$file <- x$filename
marker$line <- x$line_number
marker$column <- x$column_number
marker$message <- x$message
marker
})

# request source markers
rstudioapi::callFun("sourceMarkers",
name = "lintr",
markers = markers,
basePath = package_path,
autoSelect = "first")
}

highlight_string <- function(message, column_number = NULL, ranges = NULL) {

maximum <- max(column_number, unlist(ranges))
Expand Down

0 comments on commit 67b8681

Please sign in to comment.