Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lint text if supported #284

Merged
merged 15 commits into from
Apr 29, 2021
24 changes: 16 additions & 8 deletions R/diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,35 @@ diagnose_file <- function(uri, content) {
}

path <- path_from_uri(uri)

if (length(content) == 1) {
content <- c(content, "")
}

linter_file <- find_config(path)
linters <- NULL
lint_text_support <- "text" %in% names(formals(lintr::lint))
renkun-ken marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this for the version of lintr specified in DESCRIPTION?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we could safely assume lintr(text=). I'll remove those checking.


if (is.null(linter_file)) {
linters <- getOption("languageserver.default_linters", NULL)
} else {
linters <- NULL
} else if (!lint_text_support) {
op <- options(lintr.linter_file = linter_file)
on.exit(options(op))
}

if (length(content) == 1) {
content <- c(content, "")
lint_cache <- getOption("languageserver.lint_cache", TRUE)
lints <- if (lint_text_support) {
lintr::lint(path, linters = linters, cache = lint_cache, text = content)
} else {
text <- paste0(content, collapse = "\n")
lintr::lint(text, linters = linters, cache = lint_cache)
}

text <- paste0(content, collapse = "\n")
diagnostics <- lapply(
lintr::lint(text, linters = linters), diagnostic_from_lint, content = content)
diagnostics <- lapply(lints, diagnostic_from_lint, content = content)
names(diagnostics) <- NULL
diagnostics
}


diagnostics_callback <- function(self, uri, version, diagnostics) {
if (is.null(diagnostics) || !self$workspace$documents$has(uri)) return(NULL)
logger$info("diagnostics_callback called:", list(uri = uri, version = version))
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/test-lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,65 @@ test_that("lintr works", {

expect_equal(client$diagnostics$size(), 1)
expect_equal(client$diagnostics$get(data$uri), data$diagnostics)
expect_equal(data$diagnostics[[1]]$source, "assignment_linter")
expect_equal(data$diagnostics[[1]]$message, "Use <-, not =, for assignment.")
})

test_that("lintr config file works", {
skip_on_cran()

dir <- tempdir()
lintr_file <- file.path(dir, ".lintr")
on.exit(unlink(lintr_file))

writeLines("linters: with_defaults()", lintr_file)

client <- language_client(working_dir = dir, diagnostics = TRUE)

temp_file <- withr::local_tempfile(tmpdir = dir, fileext = ".R")
writeLines("a=1", temp_file)

client %>% did_open(temp_file)
data <- client %>% wait_for("textDocument/publishDiagnostics")

expect_equal(client$diagnostics$size(), 1)
expect_equal(client$diagnostics$get(data$uri), data$diagnostics)
expect_length(data$diagnostics, 2)
expect_setequal(vapply(data$diagnostics, "[[", character(1), "source"),
c("assignment_linter", "infix_spaces_linter"))


writeLines("linters: with_defaults(assignment_linter=NULL)", lintr_file)

client <- language_client(working_dir = dir, diagnostics = TRUE)

temp_file <- withr::local_tempfile(tmpdir = dir, fileext = ".R")
writeLines("a=1", temp_file)

client %>% did_open(temp_file)
data <- client %>% wait_for("textDocument/publishDiagnostics")

expect_equal(client$diagnostics$size(), 1)
expect_equal(client$diagnostics$get(data$uri), data$diagnostics)
expect_length(data$diagnostics, 1)
expect_setequal(vapply(data$diagnostics, "[[", character(1), "source"),
c("infix_spaces_linter"))

writeLines("linters: list()", lintr_file)

client <- language_client(working_dir = dir, diagnostics = TRUE)

temp_file <- withr::local_tempfile(tmpdir = dir, fileext = ".R")
writeLines("a=1", temp_file)

client %>% did_open(temp_file)
data <- client %>% wait_for("textDocument/publishDiagnostics")

expect_equal(client$diagnostics$size(), 1)
expect_equal(client$diagnostics$get(data$uri), data$diagnostics)
expect_length(data$diagnostics, 0)
})

test_that("lintr is disabled", {
skip_on_cran()
client <- language_client(diagnostics = FALSE)
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-unicode-path.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Test Unicode path")

test_that("Works with unicode path", {
skip_on_cran()
if (.Platform$OS.type == "windows") {
Expand Down