From 8cdcef346a515f388c0fcae19b2609fd93ba304e Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Thu, 25 Jun 2020 11:10:38 +0800 Subject: [PATCH 01/13] Use lint text if supported --- R/diagnostics.R | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/R/diagnostics.R b/R/diagnostics.R index ecc759c9..90cf686f 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -66,7 +66,7 @@ find_config <- function(filename) { #' #' Lint and diagnose problems in a file. #' @keywords internal -diagnose_file <- function(uri, content) { +diagnose_file <- function(uri, content, wd) { if (length(content) == 0) { return(list()) } @@ -79,22 +79,33 @@ 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)) + 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, "") - } + old_wd <- setwd(wd) + on.exit(setwd(old_wd)) text <- paste0(content, collapse = "\n") - diagnostics <- lapply( - lintr::lint(text, linters = linters), diagnostic_from_lint, content = content) + lints <- if (lint_text_support) { + lintr::lint(path, linters = linters, text = text) + } else { + lintr::lint(text, linters = linters) + } + + diagnostics <- lapply(lints, diagnostic_from_lint, content = content) names(diagnostics) <- NULL diagnostics } @@ -121,7 +132,7 @@ diagnostics_task <- function(self, uri, document, delay = 0) { content <- document$content create_task( target = package_call(diagnose_file), - args = list(uri = uri, content = content), + args = list(uri = uri, content = content, wd = self$rootPath), callback = function(result) diagnostics_callback(self, uri, version, result), error = function(e) { logger$info("diagnostics_task:", e) From d113be5aaa1659270f8679d0233f3f155bd784b3 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Thu, 25 Jun 2020 11:20:33 +0800 Subject: [PATCH 02/13] Use rootPath instead of wd --- R/diagnostics.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/diagnostics.R b/R/diagnostics.R index 90cf686f..3c6c0999 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -66,7 +66,7 @@ find_config <- function(filename) { #' #' Lint and diagnose problems in a file. #' @keywords internal -diagnose_file <- function(uri, content, wd) { +diagnose_file <- function(uri, content, rootPath) { if (length(content) == 0) { return(list()) } @@ -95,7 +95,7 @@ diagnose_file <- function(uri, content, wd) { on.exit(options(op)) } - old_wd <- setwd(wd) + old_wd <- setwd(rootPath) on.exit(setwd(old_wd)) text <- paste0(content, collapse = "\n") @@ -132,7 +132,7 @@ diagnostics_task <- function(self, uri, document, delay = 0) { content <- document$content create_task( target = package_call(diagnose_file), - args = list(uri = uri, content = content, wd = self$rootPath), + args = list(uri = uri, content = content, rootPath = self$rootPath), callback = function(result) diagnostics_callback(self, uri, version, result), error = function(e) { logger$info("diagnostics_task:", e) From 902565a38dbcf2cb3c8ebd267ae8d8c60b6c13e2 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Sat, 27 Jun 2020 08:45:36 +0800 Subject: [PATCH 03/13] Remove whitespaces --- R/diagnostics.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/diagnostics.R b/R/diagnostics.R index 3c6c0999..55d1fdda 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -104,7 +104,7 @@ diagnose_file <- function(uri, content, rootPath) { } else { lintr::lint(text, linters = linters) } - + diagnostics <- lapply(lints, diagnostic_from_lint, content = content) names(diagnostics) <- NULL diagnostics From fa9d8390e5c2a53d45c6c1b60d9ceb335202ce0a Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Mon, 29 Mar 2021 15:24:18 +0800 Subject: [PATCH 04/13] Update lint call --- R/diagnostics.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/diagnostics.R b/R/diagnostics.R index f3eb5645..40862fe3 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -98,10 +98,10 @@ diagnose_file <- function(uri, content, rootPath) { old_wd <- setwd(rootPath) on.exit(setwd(old_wd)) - text <- paste0(content, collapse = "\n") lints <- if (lint_text_support) { - lintr::lint(path, linters = linters, text = text) + lintr::lint(path, linters = linters, text = content) } else { + text <- paste0(content, collapse = "\n") lintr::lint(text, linters = linters) } From 447aaf957f15a6883eb5636b6239ff320fc47982 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Mon, 29 Mar 2021 18:18:56 +0800 Subject: [PATCH 05/13] Enable lint cache by default --- R/diagnostics.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/diagnostics.R b/R/diagnostics.R index 40862fe3..19c47926 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -98,11 +98,12 @@ diagnose_file <- function(uri, content, rootPath) { old_wd <- setwd(rootPath) on.exit(setwd(old_wd)) + lint_cache <- getOption("languageserver.lint_cache", TRUE) lints <- if (lint_text_support) { - lintr::lint(path, linters = linters, text = content) + lintr::lint(path, linters = linters, cache = lint_cache, text = content) } else { text <- paste0(content, collapse = "\n") - lintr::lint(text, linters = linters) + lintr::lint(text, linters = linters, cache = lint_cache) } diagnostics <- lapply(lints, diagnostic_from_lint, content = content) @@ -110,7 +111,6 @@ diagnose_file <- function(uri, content, rootPath) { 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)) From 5d066cc45315eb8c8dbc46882defbc41c6b36fe5 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Tue, 30 Mar 2021 18:24:39 +0800 Subject: [PATCH 06/13] Remove context() --- tests/testthat/test-unicode-path.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/testthat/test-unicode-path.R b/tests/testthat/test-unicode-path.R index 9f90e8ad..9636abf9 100644 --- a/tests/testthat/test-unicode-path.R +++ b/tests/testthat/test-unicode-path.R @@ -1,5 +1,3 @@ -context("Test Unicode path") - test_that("Works with unicode path", { skip_on_cran() if (.Platform$OS.type == "windows") { From 0adcf6ce826123e46d50559fde3bf19cb0f3f212 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Tue, 30 Mar 2021 18:27:21 +0800 Subject: [PATCH 07/13] Use Remotes --- DESCRIPTION | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index 1eb0b0a1..c85ccc67 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -46,6 +46,8 @@ Suggests: testthat (>= 2.1.0), withr (>= 2.3.0), rmarkdown (>= 2.0) +Remotes: + jimhester/lintr#503 ByteCompile: yes Encoding: UTF-8 LazyData: true From c51e96ece24bc9b98a1edac83c0f28223f76a6f1 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Tue, 30 Mar 2021 18:44:38 +0800 Subject: [PATCH 08/13] Remove rootPath from diagnose_file --- R/diagnostics.R | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/R/diagnostics.R b/R/diagnostics.R index 19c47926..2b192e93 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -66,7 +66,7 @@ find_config <- function(filename) { #' #' Lint and diagnose problems in a file. #' @keywords internal -diagnose_file <- function(uri, content, rootPath) { +diagnose_file <- function(uri, content) { if (length(content) == 0) { return(list()) } @@ -95,9 +95,6 @@ diagnose_file <- function(uri, content, rootPath) { on.exit(options(op)) } - old_wd <- setwd(rootPath) - on.exit(setwd(old_wd)) - lint_cache <- getOption("languageserver.lint_cache", TRUE) lints <- if (lint_text_support) { lintr::lint(path, linters = linters, cache = lint_cache, text = content) @@ -132,7 +129,7 @@ diagnostics_task <- function(self, uri, document, delay = 0) { content <- document$content create_task( target = package_call(diagnose_file), - args = list(uri = uri, content = content, rootPath = self$rootPath), + args = list(uri = uri, content = content), callback = function(result) diagnostics_callback(self, uri, version, result), error = function(e) { logger$info("diagnostics_task:", e) From ceb2688263bc238a705fccf06e189ba33b780eae Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Sun, 4 Apr 2021 15:45:03 +0800 Subject: [PATCH 09/13] Remove remotes --- DESCRIPTION | 2 -- 1 file changed, 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c85ccc67..1eb0b0a1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -46,8 +46,6 @@ Suggests: testthat (>= 2.1.0), withr (>= 2.3.0), rmarkdown (>= 2.0) -Remotes: - jimhester/lintr#503 ByteCompile: yes Encoding: UTF-8 LazyData: true From e05b691a7acdb73089877fb9e303f43c8428e02d Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Sun, 4 Apr 2021 16:29:18 +0800 Subject: [PATCH 10/13] Add test cases --- tests/testthat/test-lintr.R | 56 +++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/testthat/test-lintr.R b/tests/testthat/test-lintr.R index d2262220..4f72d466 100644 --- a/tests/testthat/test-lintr.R +++ b/tests/testthat/test-lintr.R @@ -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) From 146a37cbe76d5c6240196c8a90150b607653e989 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Wed, 7 Apr 2021 08:57:29 +0800 Subject: [PATCH 11/13] Add note --- R/diagnostics.R | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/R/diagnostics.R b/R/diagnostics.R index 2b192e93..554914a4 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -86,6 +86,10 @@ diagnose_file <- function(uri, content) { linter_file <- find_config(path) linters <- NULL + + # In the upcoming lintr 3.0.0, lint(text=) is supported so that + # we could supply both filename and text and the text will be + # regarded as the content of the file. lint_text_support <- "text" %in% names(formals(lintr::lint)) if (is.null(linter_file)) { From 4d42f4a63d79784340172a484cd6447db6ecbe97 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Thu, 29 Apr 2021 13:36:57 +0800 Subject: [PATCH 12/13] Add remotes --- DESCRIPTION | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 73f28e0b..650b08ee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,7 @@ Imports: desc (>= 1.2.0), fs (>= 1.3.1), jsonlite (>= 1.6), - lintr (>= 2.0.0), + lintr (>= 2.0.1.9000), parallel, R6 (>= 2.4.1), repr (>= 1.1.0), @@ -46,6 +46,8 @@ Suggests: testthat (>= 2.1.0), withr (>= 2.3.0), rmarkdown (>= 2.0) +Remotes: + jimhester/lintr ByteCompile: yes Encoding: UTF-8 NeedsCompilation: yes From f4cf4ce923e3b7986c2bfaac15cac9bd12a2d093 Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Thu, 29 Apr 2021 14:59:18 +0800 Subject: [PATCH 13/13] Remove lint(text=) checking --- R/diagnostics.R | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/R/diagnostics.R b/R/diagnostics.R index 554914a4..b45227c1 100644 --- a/R/diagnostics.R +++ b/R/diagnostics.R @@ -84,28 +84,15 @@ diagnose_file <- function(uri, content) { content <- c(content, "") } - linter_file <- find_config(path) linters <- NULL - # In the upcoming lintr 3.0.0, lint(text=) is supported so that - # we could supply both filename and text and the text will be - # regarded as the content of the file. - lint_text_support <- "text" %in% names(formals(lintr::lint)) - + linter_file <- find_config(path) if (is.null(linter_file)) { linters <- getOption("languageserver.default_linters", NULL) - } else if (!lint_text_support) { - op <- options(lintr.linter_file = linter_file) - on.exit(options(op)) } 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) - } + lints <- lintr::lint(path, linters = linters, cache = lint_cache, text = content) diagnostics <- lapply(lints, diagnostic_from_lint, content = content) names(diagnostics) <- NULL