Skip to content

Commit 9f920e6

Browse files
authored
Merge pull request #568 from renkun-ken/lintr-load-all
Provide package symbols before lint in diagnostics
2 parents 57c99c9 + e9aa4d0 commit 9f920e6

10 files changed

Lines changed: 40 additions & 11 deletions

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(run)
4+
import(callr)
45
import(xml2)
56
importFrom(R6,R6Class)
7+
importFrom(parallel,detectCores)
68
useDynLib(languageserver)

R/diagnostics.R

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ find_config <- function(filename) {
6666
#'
6767
#' Lint and diagnose problems in a file.
6868
#' @noRd
69-
diagnose_file <- function(uri, is_rmarkdown, content, cache = FALSE) {
69+
diagnose_file <- function(uri, content, is_rmarkdown = FALSE, globals = NULL, cache = FALSE) {
7070
if (length(content) == 0) {
7171
return(list())
7272
}
@@ -84,6 +84,12 @@ diagnose_file <- function(uri, is_rmarkdown, content, cache = FALSE) {
8484
content <- c(content, "")
8585
}
8686

87+
if (length(globals)) {
88+
env_name <- "languageserver:globals"
89+
attach(globals, name = env_name, warn.conflicts = FALSE)
90+
on.exit(detach(env_name, character.only = TRUE))
91+
}
92+
8793
lints <- lintr::lint(path, cache = cache, text = content)
8894
diagnostics <- lapply(lints, diagnostic_from_lint, content = content)
8995
names(diagnostics) <- NULL
@@ -113,12 +119,30 @@ diagnostics_callback <- function(self, uri, version, diagnostics) {
113119
diagnostics_task <- function(self, uri, document, delay = 0) {
114120
version <- document$version
115121
content <- document$content
122+
123+
is_package <- is_package(self$rootPath)
124+
globals <- NULL
125+
126+
if (is_package) {
127+
globals <- new.env(parent = emptyenv())
128+
for (doc in self$workspace$documents$values()) {
129+
if (dirname(path_from_uri(doc$uri)) != file.path(self$rootPath, "R")) next
130+
parse_data <- doc$parse_data
131+
if (is.null(parse_data)) next
132+
for (symbol in parse_data$nonfuncts) {
133+
globals[[symbol]] <- NULL
134+
}
135+
list2env(parse_data$functions, globals)
136+
}
137+
}
138+
116139
create_task(
117140
target = package_call(diagnose_file),
118141
args = list(
119142
uri = uri,
120-
is_rmarkdown = document$is_rmarkdown,
121143
content = content,
144+
is_rmarkdown = document$is_rmarkdown,
145+
globals = globals,
122146
cache = lsp_settings$get("lint_cache")
123147
),
124148
callback = function(result) diagnostics_callback(self, uri, version, result),

R/document.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ is_top_level <- function(arg_env, ...) {
225225
any(vapply(top_level_envs, identical, x = arg_env, FUN.VALUE = logical(1L)))
226226
}
227227

228+
null_function <- local(function() NULL, baseenv())
229+
228230
parser_hooks <- list(
229231
"{" = function(expr, action) {
230232
action$parse(as.list(expr)[-1L])
@@ -362,7 +364,9 @@ parse_expr <- function(content, expr, env, srcref = attr(expr, "srcref")) {
362364

363365
if (type == "function") {
364366
env$functs <- c(env$functs, symbol)
365-
env$formals[[symbol]] <- value[[2L]]
367+
fun <- null_function
368+
formals(fun) <- value[[2L]]
369+
env$functions[[symbol]] <- fun
366370
env$signatures[[symbol]] <- get_signature(symbol, value)
367371
} else {
368372
env$nonfuncts <- c(env$nonfuncts, symbol)
@@ -408,7 +412,7 @@ parse_document <- function(uri, content) {
408412
env$packages <- character()
409413
env$nonfuncts <- character()
410414
env$functs <- character()
411-
env$formals <- list()
415+
env$functions <- list()
412416
env$signatures <- list()
413417
env$definitions <- list()
414418
env$documentation <- list()

R/handlers-general.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ on_initialize <- function(self, id, params) {
4949
#' @noRd
5050
on_initialized <- function(self, params) {
5151
logger$info("on_initialized")
52-
project_root <- self$rootPath
53-
if (length(project_root) && is_package(project_root)) {
52+
if (is_package(self$rootPath)) {
5453
# a bit like devtools::load_all()
5554
self$workspace$load_all(self)
5655
# TODO: result lint result of the package

R/handlers-workspace.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ workspace_did_change_watched_files <- function(self, params) {
4343
# Only non-open documents in a package should be handled here.
4444

4545
project_root <- self$rootPath
46-
if (length(project_root) && is_package(project_root)) {
46+
if (is_package(project_root)) {
4747
source_dir <- file.path(project_root, "R")
4848
for (file_event in params$changes) {
4949
uri <- file_event$uri

R/languagebase.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ LanguageBase <- R6::R6Class("LanguageBase",
158158
},
159159

160160
handle_notification = function(notification) {
161-
# logger$info("receive notification: ", notification)
162161
method <- notification$method
163162
params <- notification$params
164163
if (method %in% names(self$notification_handlers)) {

R/languageserver.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#' @useDynLib languageserver
22
#' @importFrom R6 R6Class
33
#' @import xml2
4+
#' @importFrom parallel detectCores
45
#' @details
56
#' An implementation of the Language Server Protocol for R
67
"_PACKAGE"

R/namespace.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ GlobalEnv <- R6::R6Class("GlobalEnv",
244244
for (doc in self$documents$values()) {
245245
if (!is.null(doc$parse_data)) {
246246
if (funct %in% doc$parse_data$functs) {
247-
return(doc$parse_data$formals[[funct]])
247+
return(formals(doc$parse_data$functions[[funct]]))
248248
}
249249
}
250250
}

R/session.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#' Single R Session for Session Pool
2-
#'
2+
#' @import callr
33
#' @examples
44
#' \dontrun{
55
#' # create a session and bind to a session pool

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ find_package <- function(path = getwd()) {
325325
#' @noRd
326326
is_package <- function(rootPath) {
327327
file <- file.path(rootPath, "DESCRIPTION")
328-
file.exists(file) && !dir.exists(file)
328+
length(file) && file.exists(file) && !dir.exists(file)
329329
}
330330

331331
get_root_path_for_uri <- function(uri, rootPath) {

0 commit comments

Comments
 (0)