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
11 changes: 10 additions & 1 deletion R/session/rstudioapi.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, timeout = 120)
response$response
}

askForPassword <- function(prompt = "Please enter your password") {
response <- rstudioapi_call("ask_for_password", prompt = prompt, timeout = 120)
response$response
}

showQuestion <- .vsc_not_yet_implemented
updateDialog <- .vsc_not_yet_implemented
openProject <- .vsc_not_yet_implemented
Expand Down
8 changes: 6 additions & 2 deletions R/session/rstudioapi_util.R
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions R/session/vsc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions src/rstudioapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<void>{

const targetDocument = await workspace.openTextDocument(Uri.file(file));
Expand Down