Skip to content

Commit 44985b4

Browse files
author
Fred Wu
committed
update workspace scan using later package
1 parent 2b4acfe commit 44985b4

2 files changed

Lines changed: 54 additions & 41 deletions

File tree

R/session/vsc.R

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -690,17 +690,19 @@ inspect_env <- function(env, cache) {
690690
info_str <- try_capture_str(obj, 0)
691691
}
692692
info$str <- scalar(trimws(info_str))
693-
obj_names <- if (is.object(obj)) {
694-
.DollarNames(obj, pattern = "")
695-
} else if (is.recursive(obj)) {
696-
names(obj)
697-
} else {
698-
NULL
699-
}
693+
}
700694

701-
if (length(obj_names)) {
702-
info$names <- obj_names
703-
}
695+
# Always get names for autocomplete (fast, even for large objects)
696+
obj_names <- if (is.object(obj)) {
697+
.DollarNames(obj, pattern = "")
698+
} else if (is.recursive(obj)) {
699+
names(obj)
700+
} else {
701+
NULL
702+
}
703+
704+
if (length(obj_names)) {
705+
info$names <- obj_names
704706
}
705707

706708
if (isS4(obj)) {
@@ -726,7 +728,16 @@ workspace_file <- file.path(dir_session, "workspace.json")
726728
workspace_lock_file <- file.path(dir_session, "workspace.lock")
727729
file.create(workspace_lock_file, showWarnings = FALSE)
728730

729-
update_workspace <- function(...) {
731+
# Deferred workspace scanning - scan when R is idle, not immediately after command
732+
pending_workspace_scan <- NULL
733+
workspace_scan_delay <- getOption("vsc.workspace_scan_delay", 0.2) # 200ms default
734+
use_later <- requireNamespace("later", quietly = TRUE)
735+
if (!use_later) {
736+
message("Install package `later` for improved terminal responsiveness: install.packages('later')")
737+
}
738+
739+
do_workspace_scan <- function() {
740+
pending_workspace_scan <<- NULL
730741
tryCatch({
731742
data <- list(
732743
search = search()[-1],
@@ -736,6 +747,20 @@ update_workspace <- function(...) {
736747
jsonlite::write_json(data, workspace_file, force = TRUE, pretty = FALSE)
737748
cat(get_timestamp(), file = workspace_lock_file)
738749
}, error = message)
750+
}
751+
752+
update_workspace <- function(...) {
753+
if (use_later) {
754+
# Cancel any pending scan (debounce)
755+
if (!is.null(pending_workspace_scan)) {
756+
tryCatch(later::later_cancel(pending_workspace_scan), error = function(e) NULL)
757+
}
758+
# Schedule scan after delay (runs when R is idle)
759+
pending_workspace_scan <<- later::later(do_workspace_scan, delay = workspace_scan_delay)
760+
} else {
761+
# Fallback: immediate scan if 'later' not available
762+
do_workspace_scan()
763+
}
739764
TRUE
740765
}
741766
update_workspace()

package-lock.json

Lines changed: 18 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)