Skip to content

Commit 8ee83f2

Browse files
juliasilgelionel-
andauthored
Implement executeCommand for rstudioapi as OpenRPC contract (#256)
* Implement `executeCommand` for rstudioapi as OpenRPC contract * We can of course do more * We can use the new `executeCode` for other bits of rstudioapi as well * Use `ps_show_message()` * Use braces * Update crates/ark/src/modules/rstudio/commands.R Co-authored-by: Lionel Henry <[email protected]> * Remove newly untrue comment * Bump ark to 0.1.61 --------- Co-authored-by: Lionel Henry <[email protected]>
1 parent 90a2c17 commit 8ee83f2

File tree

8 files changed

+63
-4
lines changed

8 files changed

+63
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/amalthea/src/comm/ui_comm.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ pub struct DebugSleepParams {
148148
pub ms: f64,
149149
}
150150

151+
/// Parameters for the ExecuteCommand method.
152+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
153+
pub struct ExecuteCommandParams {
154+
/// The command to execute
155+
pub command: String,
156+
}
157+
151158
/**
152159
* Backend RPC request types for the ui comm
153160
*/
@@ -187,6 +194,13 @@ pub enum UiFrontendRequest {
187194
#[serde(rename = "debug_sleep")]
188195
DebugSleep(DebugSleepParams),
189196

197+
/// Execute a Positron command
198+
///
199+
/// Use this to execute a Positron command from the backend (like from a
200+
/// runtime)
201+
#[serde(rename = "execute_command")]
202+
ExecuteCommand(ExecuteCommandParams),
203+
190204
/// Context metadata for the last editor
191205
///
192206
/// Returns metadata such as file path for the last editor selected by the
@@ -205,6 +219,9 @@ pub enum UiFrontendReply {
205219
/// Reply for the debug_sleep method (no result)
206220
DebugSleepReply(),
207221

222+
/// Reply for the execute_command method (no result)
223+
ExecuteCommandReply(),
224+
208225
/// Editor metadata
209226
LastActiveEditorContextReply(Option<EditorContext>),
210227

@@ -256,6 +273,7 @@ pub fn ui_frontend_reply_from_value(
256273
) -> anyhow::Result<UiFrontendReply> {
257274
match request {
258275
UiFrontendRequest::DebugSleep(_) => Ok(UiFrontendReply::DebugSleepReply()),
276+
UiFrontendRequest::ExecuteCommand(_) => Ok(UiFrontendReply::ExecuteCommandReply()),
259277
UiFrontendRequest::LastActiveEditorContext => Ok(UiFrontendReply::LastActiveEditorContextReply(serde_json::from_value(reply)?)),
260278
}
261279
}

crates/ark/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ark"
3-
version = "0.1.60"
3+
version = "0.1.61"
44
edition = "2021"
55
rust-version = "1.75.0"
66
description = """

crates/ark/src/lsp/show_message.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use stdext::unwrap;
1515
use crate::interface::RMain;
1616

1717
/// Shows a message in the Positron frontend
18-
///
19-
/// Test helper for `R_ShowMessage()` support
2018
#[harp::register]
2119
pub unsafe extern "C" fn ps_show_message(message: SEXP) -> anyhow::Result<SEXP> {
2220
// Convert message to a string

crates/ark/src/modules/positron/frontend-methods.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
.ps.Call("ps_ui_last_active_editor_context")
1111
}
1212

13+
#' @export
14+
.ps.ui.executeCommand <- function(command) {
15+
.ps.Call("ps_ui_execute_command", command)
16+
}
17+
18+
#' @export
19+
.ps.ui.showMessage <- function(message) {
20+
.ps.Call("ps_show_message", message)
21+
}
22+
1323
#' @export
1424
.ps.ui.debugSleep <- function(ms) {
1525
# stopifnot(is.numeric(ms) && length(ms) == 1 && !is.na(ms))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#' @export
2+
.rs.api.executeCommand <- function(commandId, quiet = FALSE) {
3+
commandId <- switch(
4+
commandId,
5+
"activateConsole" = "workbench.action.positronConsole.focusConsole",
6+
"activateTerminal" = "workbench.action.terminal.focus",
7+
"saveAllSourceDocs" = "workbench.action.files.saveAll",
8+
{
9+
if (!quiet) {
10+
.ps.ui.showMessage(paste0("The command '", commandId, "' does not exist."))
11+
}
12+
return()
13+
}
14+
)
15+
.ps.ui.executeCommand(commandId)
16+
}

crates/ark/src/modules/rstudio/document-api.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ convert_position <- function(ps_pos) {
4646
)
4747
)
4848
}
49+
50+
#' @export
51+
.rs.api.documentSaveAll <- function() {
52+
invisible(.ps.ui.executeCommand("workbench.action.files.saveAll"))
53+
}

crates/ark/src/ui/methods.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
use amalthea::comm::ui_comm::DebugSleepParams;
9+
use amalthea::comm::ui_comm::ExecuteCommandParams;
910
use amalthea::comm::ui_comm::UiFrontendRequest;
1011
use harp::object::RObject;
1112
use libr::SEXP;
@@ -19,6 +20,17 @@ pub unsafe extern "C" fn ps_ui_last_active_editor_context() -> anyhow::Result<SE
1920
Ok(out.sexp)
2021
}
2122

23+
#[harp::register]
24+
pub unsafe extern "C" fn ps_ui_execute_command(command: SEXP) -> anyhow::Result<SEXP> {
25+
let params = ExecuteCommandParams {
26+
command: RObject::view(command).try_into()?,
27+
};
28+
29+
let main = RMain::get();
30+
let out = main.call_frontend_method(UiFrontendRequest::ExecuteCommand(params))?;
31+
Ok(out.sexp)
32+
}
33+
2234
#[harp::register]
2335
pub unsafe extern "C" fn ps_ui_debug_sleep(ms: SEXP) -> anyhow::Result<SEXP> {
2436
let params = DebugSleepParams {

0 commit comments

Comments
 (0)