From fd4b45c3b0b5b9951aec5aa282ba6a35fa1cd5b9 Mon Sep 17 00:00:00 2001 From: Iakov Davydov <671660+idavydov@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:01:16 +0200 Subject: [PATCH 1/2] implement rstudioapi::showPrompt() and rstudioapi::askForPassword() --- R/session/rstudioapi.R | 11 ++++++++++- src/rstudioapi.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/R/session/rstudioapi.R b/R/session/rstudioapi.R index 60ea4358c..ffec79885 100644 --- a/R/session/rstudioapi.R +++ b/R/session/rstudioapi.R @@ -264,7 +264,16 @@ sendToConsole <- function(code, execute = TRUE, echo = TRUE, focus = FALSE) { getConsoleEditorContext <- .vsc_not_yet_implemented sourceMarkers <- .vsc_not_yet_implemented documentClose <- .vsc_not_yet_implemented -showPrompt <- .vsc_not_yet_implemented +showPrompt <- function(title, message, default = NULL) { + response <- rstudioapi_call("show_prompt", title = title, message = message, default = default) + response$response +} + +askForPassword <- function(prompt = "Please enter your password") { + response <- rstudioapi_call("ask_for_password", prompt = prompt) + response$response +} + showQuestion <- .vsc_not_yet_implemented updateDialog <- .vsc_not_yet_implemented openProject <- .vsc_not_yet_implemented diff --git a/src/rstudioapi.ts b/src/rstudioapi.ts index ac4d738e5..42bb449d7 100644 --- a/src/rstudioapi.ts +++ b/src/rstudioapi.ts @@ -41,6 +41,16 @@ export async function dispatchRStudioAPICall(action: string, args: any, sd: stri await writeSuccessResponse(sd); break; } + case 'show_prompt': { + const result = await showPrompt(args.title, args.message, args.default); + await writeResponse(result, sd); + break; + } + case 'ask_for_password': { + const result = await askForPassword(args.prompt); + await writeResponse(result, sd); + break; + } case 'navigate_to_file': { await navigateToFile(args.file, args.line, args.column); await writeSuccessResponse(sd); @@ -164,6 +174,27 @@ export function showDialog(message: string): void { } +export async function showPrompt( + title: string, message: string, defaultValue?: string +): Promise<{ response: string | null }> { + const result = await window.showInputBox({ + title: title, + prompt: message, + value: defaultValue ?? '', + }); + return { response: result ?? null }; +} + +export async function askForPassword( + prompt: string +): Promise<{ response: string | null }> { + const result = await window.showInputBox({ + prompt: prompt, + password: true, + }); + return { response: result ?? null }; +} + export async function navigateToFile(file: string, line: number, column: number): Promise{ const targetDocument = await workspace.openTextDocument(Uri.file(file)); From cf3f64de594af6e9ccdaa2912e76414c9dc2b8bf Mon Sep 17 00:00:00 2001 From: Iakov Davydov <671660+idavydov@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:58:59 +0200 Subject: [PATCH 2/2] set timeout for interactive prompts to 120seconds --- R/session/rstudioapi.R | 4 ++-- R/session/rstudioapi_util.R | 8 ++++++-- R/session/vsc.R | 6 +++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/R/session/rstudioapi.R b/R/session/rstudioapi.R index ffec79885..8de87ba76 100644 --- a/R/session/rstudioapi.R +++ b/R/session/rstudioapi.R @@ -265,12 +265,12 @@ getConsoleEditorContext <- .vsc_not_yet_implemented sourceMarkers <- .vsc_not_yet_implemented documentClose <- .vsc_not_yet_implemented showPrompt <- function(title, message, default = NULL) { - response <- rstudioapi_call("show_prompt", title = title, message = message, default = default) + response <- rstudioapi_call("show_prompt", title = title, message = message, default = default, timeout = 120) response$response } askForPassword <- function(prompt = "Please enter your password") { - response <- rstudioapi_call("ask_for_password", prompt = prompt) + response <- rstudioapi_call("ask_for_password", prompt = prompt, timeout = 120) response$response } diff --git a/R/session/rstudioapi_util.R b/R/session/rstudioapi_util.R index da5fe0ff6..7047e7890 100644 --- a/R/session/rstudioapi_util.R +++ b/R/session/rstudioapi_util.R @@ -1,5 +1,9 @@ -rstudioapi_call <- function(action, ...) { - request_response("rstudioapi", action = action, args = list(...)) +rstudioapi_call <- function(action, ..., timeout = NULL) { + args <- list("rstudioapi", action = action, args = list(...)) + if (!is.null(timeout)) { + args$timeout <- timeout + } + do.call(request_response, args) } rstudioapi_patch_hook <- function(api_env) { diff --git a/R/session/vsc.R b/R/session/vsc.R index 5ab461394..d27347533 100644 --- a/R/session/vsc.R +++ b/R/session/vsc.R @@ -833,14 +833,14 @@ if (rstudioapi_enabled()) { } } - request_response <- function(command, ...) { + request_response <- function(command, ..., timeout = response_timeout) { request(command, ..., sd = dir_session) wait_start <- Sys.time() while (!get_response_lock()) { - if ((Sys.time() - wait_start) > response_timeout) { + if ((Sys.time() - wait_start) > timeout) { stop( "Did not receive a response from VSCode-R API within ", - response_timeout, " seconds." + timeout, " seconds." ) } Sys.sleep(0.1)