Skip to content
Open
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
25 changes: 0 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1608,31 +1608,6 @@
"default": true,
"markdownDescription": "Watch the global environment to provide hover, autocompletions, and workspace viewer information. Requires `#r.sessionWatcher#` to be set to `true`."
},
"r.session.objectLengthLimit": {
"type": "integer",
"default": 2000,
"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. Requires `#r.sessionWatcher#` to be set to `true`."
},
"r.session.objectTimeout": {
"type": "integer",
"default": 50,
"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. Requires `#r.sessionWatcher#` to be set to `true`."
},
"r.session.levelOfObjectDetail": {
"type": "string",
"markdownDescription": "How much of the object to show on hover, autocompletion, and in the workspace viewer? Requires `#r.sessionWatcher#` to be set to `true`.",
"default": "Minimal",
"enum": [
"Minimal",
"Normal",
"Detailed"
],
"enumDescriptions": [
"Display literal values and object types only.",
"Display the top level of list content, data frame column values, and example values.",
"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."
]
},
"r.session.emulateRStudioAPI": {
"type": "boolean",
"default": true,
Expand Down
194 changes: 179 additions & 15 deletions sess/R/handlers.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,88 @@
# Handlers for the client Pull Requests (HTTP GET/POST)

capture_str <- function(object, max_level = 0L) {
paste0(utils::capture.output(
utils::str(object,
max.level = max_level,
give.attr = FALSE,
vec.len = 1L
)
), collapse = "\n")
}

try_capture_str <- function(object, max_level = 0L) {
tryCatch(
capture_str(object, max_level),
error = function(e) paste0(class(object), collapse = ", ")
)
}

workspace_child_count <- function(object) {
if (is.environment(object)) {
length(object)
} else if (isS4(object)) {
length(methods::slotNames(object))
} else if (typeof(object) %in% c("list", "pairlist")) {
length(object)
} else {
0L
}
}

get_workspace_data <- function() {
env <- .GlobalEnv
all_names <- ls(env)
all_names <- ls(env, sorted = FALSE)

objs <- lapply(all_names, function(name) {
if (bindingIsActive(name, env)) {
return(list(
class = "active_binding",
type = "active_binding",
length = 0L,
str = "(active-binding)",
has_children = FALSE
))
}

obj <- env[[name]]
list(
obj_class <- class(obj)
obj_type <- typeof(obj)
obj_length <- length(obj)
obj_dim <- dim(obj)
first_class <- if (length(obj_class)) obj_class[[1]] else obj_type
info <- list(
class = class(obj),
type = typeof(obj),
length = length(obj),
# Create a concise string representation
str = paste0(
utils::capture.output(utils::str(obj, max.level = 0, give.attr = FALSE)),
collapse = "\n"
)
type = obj_type,
length = obj_length,
str = if (!is.null(obj_dim)) {
paste0(first_class, ": ", paste(obj_dim, collapse = " x "))
} else if (obj_type == "environment") {
"<environment>"
} else if (obj_type %in% c("closure", "builtin")) {
trimws(try_capture_str(obj))
} else {
paste0(first_class, ", length ", obj_length)
},
has_children = workspace_child_count(obj) > 0L
)

obj_names <- if (is.object(obj)) {
utils::.DollarNames(obj, pattern = "")
} else if (is.recursive(obj)) {
names(obj)
} else {
NULL
}
if (length(obj_names)) {
info$names <- obj_names
}
if (isS4(obj)) {
info$slots <- methods::slotNames(obj)
}
if (!is.null(obj_dim)) {
info$dim <- obj_dim
}
info
})
names(objs) <- all_names

Expand All @@ -26,16 +93,113 @@ get_workspace_data <- function() {
)
}

workspace_object <- function(name, path = list()) {
object <- get(name, envir = .GlobalEnv, inherits = FALSE)
for (selector in path) {
object <- switch(selector$kind,
index = object[[as.integer(selector$value)]],
name = get(selector$value, envir = object, inherits = FALSE),
slot = methods::slot(object, selector$value),
stop("Unknown workspace selector")
)
}
object
}

workspace_child_page_size <- 500L

workspace_child_item <- function(object, str, selector) {
list(
str = str,
class = paste(class(object), collapse = ", "),
type = typeof(object),
has_children = workspace_child_count(object) > 0L,
selector = selector
)
}

workspace_child_label <- function(name, index) {
if (!is.null(name) && !is.na(name) && nzchar(name)) {
paste0("$ ", name)
} else {
paste0("[[", index, "]]")
}
}

get_workspace_children <- function(name, path = list(), start = 1L) {
tryCatch({
object <- workspace_object(name, path)
child_count <- workspace_child_count(object)
if (child_count == 0L) {
return(list(children = I(list()), next_start = NULL))
}

start <- max(1L, as.integer(start))
end <- min(child_count, start + workspace_child_page_size - 1L)
if (start > end) {
return(list(children = I(list()), next_start = NULL))
}

children <- if (is.environment(object)) {
child_names <- ls(object, sorted = FALSE)[seq.int(start, end)]
lapply(child_names, function(child_name) {
if (bindingIsActive(child_name, object)) {
list(
str = paste0("$ ", child_name, ": (active-binding)"),
class = "active_binding",
type = "active_binding",
has_children = FALSE
)
} else {
child <- get(child_name, envir = object, inherits = FALSE)
workspace_child_item(
child,
paste0("$ ", child_name, ": ", trimws(try_capture_str(child))),
list(kind = "name", value = child_name)
)
}
})
} else if (isS4(object)) {
child_names <- methods::slotNames(object)[seq.int(start, end)]
lapply(child_names, function(child_name) {
child <- methods::slot(object, child_name)
workspace_child_item(
child,
paste0("@ ", child_name, ": ", trimws(try_capture_str(child))),
list(kind = "slot", value = child_name)
)
})
} else {
indices <- seq.int(start, end)
child_names <- names(object)
lapply(indices, function(index) {
child <- object[[index]]
child_name <- if (is.null(child_names)) NULL else child_names[[index]]
workspace_child_item(
child,
paste0(
workspace_child_label(child_name, index),
": ",
trimws(try_capture_str(child))
),
list(kind = "index", value = index)
)
})
}

list(
children = I(children),
next_start = if (end < child_count) end + 1L else NULL
)
}, error = function(e) list(children = I(list()), next_start = NULL))
}

handle_hover <- function(expr_str) {
tryCatch(
{
expr <- parse(text = expr_str, keep.source = FALSE)[[1]]
obj <- eval(expr, .GlobalEnv)
str_preview <- paste0(
utils::capture.output(utils::str(obj, max.level = 0, give.attr = FALSE)),
collapse = "\n"
)
list(str = str_preview)
list(str = capture_str(obj))
},
error = function(e) NULL
)
Expand Down Expand Up @@ -77,7 +241,7 @@ handle_complete <- function(expr_str, trigger = NULL) {
}))
}

if (trigger == "@" && methods::isS4(obj)) {
if (trigger == "@" && isS4(obj)) {
nms <- methods::slotNames(obj)
return(lapply(nms, function(n) {
item <- methods::slot(obj, n)
Expand Down
1 change: 1 addition & 0 deletions sess/R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ dispatch_message <- function(line) {
# Request from vscode → R must reply
handlers <- list(
"workspace" = function(p) get_workspace_data(),
"workspace_children" = function(p) get_workspace_children(p$name, p$path, p$start),
"hover" = function(p) handle_hover(p$expr),
"completion" = function(p) handle_complete(p$expr, p$trigger),
"plot_latest" = function(p) handle_plot_latest(p),
Expand Down
3 changes: 2 additions & 1 deletion src/rTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { extensionContext } from './extension';
import * as util from './util';
import * as selection from './selection';
import { getSelection } from './selection';
import { cleanupSession } from './session';
import { cleanupSession, deferWorkspaceRefresh } from './session';
import { config, delay, getRterm, getCurrentWorkspaceFolder } from './util';
import * as fs from 'fs';
import * as yaml from 'js-yaml';
Expand Down Expand Up @@ -342,6 +342,7 @@ export async function runChunksInTerm(chunks: vscode.Range[]): Promise<void> {
}

export async function runTextInTerm(text: string, execute: boolean = true): Promise<void> {
deferWorkspaceRefresh();
const term = await chooseTerminal();
if (term === undefined) {
return;
Expand Down
59 changes: 50 additions & 9 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ export interface SessionInfo {

export interface GlobalEnv {
[key: string]: {
class: string[];
class: string[] | string;
type: string;
length: number;
str: string;
size?: number;
dim?: number[],
names?: string[],
slots?: string[]
slots?: string[],
has_children?: boolean
}
}

Expand Down Expand Up @@ -85,6 +86,9 @@ export let workspaceFile: string;
const sessions = new Map<string, Session>();
export let activeSession: Session | undefined;
let activeBrowserUri: Uri | undefined;
let workspaceRefreshTimer: NodeJS.Timeout | undefined;
let workspaceRefreshInProgress = false;
let workspaceRefreshPending = false;

interface DataViewColumnDef {
headerName: string;
Expand Down Expand Up @@ -580,15 +584,48 @@ async function updatePlot() {
await globalPlotManager?.showStandardPlot();
}

export function deferWorkspaceRefresh(): void {
if (workspaceRefreshTimer) {
clearTimeout(workspaceRefreshTimer);
workspaceRefreshTimer = undefined;
}
}

function scheduleWorkspaceRefresh(delayMs: number = 500): void {
workspaceRefreshPending = true;
if (workspaceRefreshTimer) {
clearTimeout(workspaceRefreshTimer);
}
workspaceRefreshTimer = setTimeout(() => {
workspaceRefreshTimer = undefined;
void runWorkspaceRefresh();
}, delayMs);
}

async function runWorkspaceRefresh(): Promise<void> {
if (workspaceRefreshInProgress || !workspaceRefreshPending) {
return;
}
workspaceRefreshPending = false;
workspaceRefreshInProgress = true;
try {
await updateWorkspace();
} finally {
workspaceRefreshInProgress = false;
if (workspaceRefreshPending) {
scheduleWorkspaceRefresh();
}
}
}

export async function updateWorkspace() {
if (!globalPipePath) {return;}
const requestedSession = activeSession;
if (!globalPipePath || !requestedSession) {return;}
try {
const response = await sessionRequest({ method: 'workspace' });
if (response) {
if (response && activeSession === requestedSession) {
workspaceData = response as WorkspaceData;
if (activeSession) {
activeSession.workspaceData = workspaceData;
}
requestedSession.workspaceData = workspaceData;
void rWorkspace?.refresh();
console.info('[updateWorkspace] Done');
}
Expand Down Expand Up @@ -1500,13 +1537,15 @@ async function handleNotification(message: Record<string, unknown>, socket: IpcS
if (params.plot_url) {
await globalPlotManager?.showHttpgdPlot(String(params.plot_url));
}
void updateWorkspace();
scheduleWorkspaceRefresh(0);
void watchProcess(rPid).then((v: string) => { void cleanupSession(v); });
break;
}

case 'workspace_updated': {
void updateWorkspace();
if (socket === activeSession?.socket) {
scheduleWorkspaceRefresh();
}
break;
}
case 'help': {
Expand Down Expand Up @@ -1674,6 +1713,8 @@ export async function cleanupSession(pidArg: string): Promise<void> {
session.socket.destroy();
}
if (activeSession === session || pid === pidArg) {
deferWorkspaceRefresh();
workspaceRefreshPending = false;
resetStatusBar();
globalPipePath = undefined;
activeSession = undefined;
Expand Down
Loading
Loading