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

Load documents referenced in source() calls #382

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions R/document.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ parse_expr <- function(content, expr, env, level = 0L, srcref = attr(expr, "srcr
if (!(pkg %in% env$packages)) {
env$packages <- c(env$packages, pkg)
}
} else if (f %in% "source") {
call <- match.call(base::source, as.call(e))
if (!is.character(call$file)) next
if (!is.null(call$local) && !identical(call$local, FALSE)) next
env$sources <- c(env$sources, call$file)
}
}
env
Expand All @@ -316,6 +321,7 @@ parse_document <- function(uri, content) {
parse_env <- function() {
env <- new.env(parent = .GlobalEnv)
env$packages <- character()
env$sources <- character()
env$nonfuncts <- character()
env$functs <- character()
env$formals <- list()
Expand Down Expand Up @@ -370,6 +376,18 @@ parse_callback <- function(self, uri, version, parse_data) {
}
}
}

for (source_file in parse_data$sources) {
source_path <- fs::path_abs(source_file, self$rootPath)
if (!file.exists(source_path)) next
source_uri <- path_to_uri(source_path)
if (self$workspace$documents$has(source_uri)) next
source_doc <- Document$new(source_uri, NULL, stringi::stri_read_lines(source_path))
self$workspace$documents$set(source_uri, source_doc)
self$text_sync(source_uri, document = source_doc, parse = TRUE)
}

NULL
}

parse_task <- function(self, uri, document, delay = 0) {
Expand Down
20 changes: 19 additions & 1 deletion R/handlers-textsync.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,26 @@ text_document_did_close <- function(self, params) {
diagnostics_callback(self, uri, NULL, list())
}

# do not remove document if sourced by other open documents
is_sourced <- FALSE
for (doc_uri in self$workspace$documents$keys()) {
if (doc_uri == uri) next
doc <- self$workspace$documents$get(doc_uri)
for (source_file in doc$parse_data$sources) {
source_path <- fs::path_abs(source_file, self$rootPath)
source_uri <- path_to_uri(source_path)
if (source_uri == uri) {
is_sourced <- TRUE
break
}
}
if (is_sourced) {
break
}
}

# do not remove document in package
if (!(is_package(self$rootPath) && is_from_workspace)) {
if (!(is_package(self$rootPath) && is_from_workspace) && !is_sourced) {
diagnostics_callback(self, uri, NULL, list())
self$workspace$documents$remove(uri)
self$workspace$update_loaded_packages()
Expand Down