@@ -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) {
113119diagnostics_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 ),
0 commit comments