Skip to content

Commit 29801b7

Browse files
renkun-kenElianHugh
authored andcommitted
Add r.session.levelOfObjectDetail=Normal for max.level=1 (REditorSupport#815)
* Add r.session.levelOfObjectDetail=Normal for max.level=1 * Add object timeout * Update capture_str
1 parent 1b7a569 commit 29801b7

2 files changed

Lines changed: 55 additions & 33 deletions

File tree

R/session/vsc.R

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ load_settings <- function() {
1515
return(FALSE)
1616
}
1717

18+
setting <- function(x, ...) {
19+
switch(EXPR = x, ..., x)
20+
}
21+
1822
mapping <- quote(list(
1923
vsc.use_httpgd = plot$useHttpgd,
2024
vsc.show_object_size = workspaceViewer$showObjectSize,
2125
vsc.rstudioapi = session$emulateRStudioAPI,
22-
vsc.str.max.level = session$levelOfObjectDetail,
26+
vsc.str.max.level = setting(session$levelOfObjectDetail, Minimal = 0, Normal = 1, Detailed = 2),
2327
vsc.object_length_limit = session$objectLengthLimit,
28+
vsc.object_timeout = session$objectTimeout,
2429
vsc.globalenv = session$watchGlobalEnvironment,
25-
vsc.plot = session$viewers$viewColumn$plot,
26-
vsc.browser = session$viewers$viewColumn$browser,
27-
vsc.viewer = session$viewers$viewColumn$viewer,
28-
vsc.page_viewer = session$viewers$viewColumn$pageViewer,
29-
vsc.view = session$viewers$viewColumn$view,
30-
vsc.helpPanel = session$viewers$viewColumn$helpPanel
30+
vsc.plot = setting(session$viewers$viewColumn$plot, Disable = FALSE),
31+
vsc.browser = setting(session$viewers$viewColumn$browser, Disable = FALSE),
32+
vsc.viewer = setting(session$viewers$viewColumn$viewer, Disable = FALSE),
33+
vsc.page_viewer = setting(session$viewers$viewColumn$pageViewer, Disable = FALSE),
34+
vsc.view = setting(session$viewers$viewColumn$view, Disable = FALSE),
35+
vsc.helpPanel = setting(session$viewers$viewColumn$helpPanel, Disable = FALSE)
3136
))
3237

3338
vsc_settings <- tryCatch(jsonlite::read_json(settings_file), error = function(e) {
@@ -41,21 +46,7 @@ load_settings <- function() {
4146
ops <- eval(mapping, vsc_settings)
4247

4348
# exclude options set by user on startup
44-
ops <- ops[!(names(ops) %in% user_options)]
45-
46-
# translate VS Code setting values to R option values
47-
r_options <- lapply(ops, function(x) {
48-
if (is.character(x) && length(x) == 1) {
49-
switch(EXPR = x,
50-
"Disable" = FALSE,
51-
"Minimal" = 0,
52-
"Detailed" = 2,
53-
x
54-
)
55-
} else {
56-
x
57-
}
58-
})
49+
r_options <- ops[!(names(ops) %in% user_options)]
5950

6051
options(r_options)
6152
}
@@ -88,15 +79,27 @@ request <- function(command, ...) {
8879
cat(get_timestamp(), file = request_lock_file)
8980
}
9081

82+
try_catch_timeout <- function(expr, timeout = Inf, ...) {
83+
expr <- substitute(expr)
84+
envir <- parent.frame()
85+
setTimeLimit(timeout, transient = TRUE)
86+
on.exit(setTimeLimit())
87+
tryCatch(eval(expr, envir), ...)
88+
}
89+
9190
capture_str <- function(object, max.level = getOption("vsc.str.max.level", 0)) {
91+
paste0(utils::capture.output(
92+
utils::str(object,
93+
max.level = max.level,
94+
give.attr = FALSE,
95+
vec.len = 1
96+
)
97+
), collapse = "\n")
98+
}
99+
100+
try_capture_str <- function(object, max.level = getOption("vsc.str.max.level", 0)) {
92101
tryCatch(
93-
paste0(utils::capture.output(
94-
utils::str(object,
95-
max.level = max.level,
96-
give.attr = FALSE,
97-
vec.len = 1
98-
)
99-
), collapse = "\n"),
102+
capture_str(object, max.level = max.level),
100103
error = function(e) {
101104
paste0(class(object), collapse = ", ")
102105
}
@@ -135,6 +138,7 @@ inspect_env <- function(env, cache) {
135138
is_active <- rlang::env_binding_are_active(env, all_names)
136139
show_object_size <- getOption("vsc.show_object_size", FALSE)
137140
object_length_limit <- getOption("vsc.object_length_limit", 2000)
141+
object_timeout <- getOption("vsc.object_timeout", 50) / 1000
138142
str_max_level <- getOption("vsc.str.max.level", 0)
139143
objs <- lapply(all_names, function(name) {
140144
if (is_promise[[name]]) {
@@ -174,9 +178,20 @@ inspect_env <- function(env, cache) {
174178
}
175179

176180
if (length(obj) > object_length_limit) {
177-
info$str <- scalar(trimws(capture_str(obj, 0)))
181+
info$str <- scalar(trimws(try_capture_str(obj, 0)))
178182
} else {
179-
info$str <- scalar(trimws(capture_str(obj, str_max_level)))
183+
info_str <- NULL
184+
if (str_max_level > 0) {
185+
info_str <- try_catch_timeout(
186+
capture_str(obj, str_max_level),
187+
timeout = object_timeout,
188+
error = function(e) NULL
189+
)
190+
}
191+
if (is.null(info_str)) {
192+
info_str <- try_capture_str(obj, 0)
193+
}
194+
info$str <- scalar(trimws(info_str))
180195
obj_names <- if (is.object(obj)) {
181196
.DollarNames(obj, pattern = "")
182197
} else if (is.recursive(obj)) {
@@ -401,7 +416,7 @@ if (show_view) {
401416
type = typeof(obj),
402417
length = length(obj),
403418
size = as.integer(object.size(obj)),
404-
value = trimws(capture_str(obj, 0)),
419+
value = trimws(try_capture_str(obj, 0)),
405420
stringsAsFactors = FALSE,
406421
check.names = FALSE
407422
)

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1403,17 +1403,24 @@
14031403
"default": 2000,
14041404
"markdownDescription": "The upper limit of object length to show object details in workspace viewer and provide session symbol completion. Decrease this value if you experience significant delay after executing R commands caused by large global objects with many elements. Changes the option `vsc.object_length_limit` in R. Requires `#r.sessionWatcher#` to be set to `true`."
14051405
},
1406+
"r.session.objectTimeout": {
1407+
"type": "integer",
1408+
"default": 50,
1409+
"markdownDescription": "The maximum number of milliseconds to get information of a single object in the global environment. Decrease this value if you experience significant delay after executing R commands caused by large global objects with many elements. Changes the option `vsc.object_timeout` in R. Requires `#r.sessionWatcher#` to be set to `true`."
1410+
},
14061411
"r.session.levelOfObjectDetail": {
14071412
"type": "string",
14081413
"markdownDescription": "How much of the object to show on hover, autocompletion, and in the workspace viewer? Changes the option `vsc.str.max.level` in R. Requires `#r.sessionWatcher#` to be set to `true`.",
14091414
"default": "Minimal",
14101415
"enum": [
14111416
"Minimal",
1417+
"Normal",
14121418
"Detailed"
14131419
],
14141420
"enumDescriptions": [
14151421
"Display literal values and object types only.",
1416-
"Display list content, data frame column values, and example values."
1422+
"Display the top level of list content, data frame column values, and example values.",
1423+
"Display the top two levels of list content, data frame column values, and example values. This option may cause notable delay after each user input in the terminal."
14171424
]
14181425
},
14191426
"r.session.emulateRStudioAPI": {

0 commit comments

Comments
 (0)