Skip to content

Commit

Permalink
Add a few more OpenRPC methods for rstudioapi shims (#2593)
Browse files Browse the repository at this point in the history
* Add `execute_code` OpenRPC method

* Add `new_document` OpenRPC method

* Bump ark to 0.1.75

* Bump ark to 0.1.76 on request from @DavisVaughan

---------

Signed-off-by: Julia Silge <[email protected]>
  • Loading branch information
juliasilge authored Apr 3, 2024
1 parent 831da2a commit 4f9f147
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ class OpenEditorParams(BaseModel):
)


class NewDocumentParams(BaseModel):
"""
Create a new document with text contents
"""

contents: str = Field(
description="Document contents",
)

language_id: str = Field(
description="Language identifier",
)


class ShowMessageParams(BaseModel):
"""
Show a message
Expand Down Expand Up @@ -337,6 +351,28 @@ class ExecuteCommandParams(BaseModel):
)


class ExecuteCodeParams(BaseModel):
"""
Execute code in a Positron runtime
"""

language_id: str = Field(
description="The language ID of the code to execute",
)

code: str = Field(
description="The code to execute",
)

focus: bool = Field(
description="Whether to focus the runtime's console",
)

allow_incomplete: bool = Field(
description="Whether to bypass runtime code completeness checks",
)


class OpenWorkspaceParams(BaseModel):
"""
Open a workspace
Expand Down Expand Up @@ -403,6 +439,8 @@ class ShowUrlParams(BaseModel):

OpenEditorParams.update_forward_refs()

NewDocumentParams.update_forward_refs()

ShowMessageParams.update_forward_refs()

ShowQuestionParams.update_forward_refs()
Expand All @@ -417,6 +455,8 @@ class ShowUrlParams(BaseModel):

ExecuteCommandParams.update_forward_refs()

ExecuteCodeParams.update_forward_refs()

OpenWorkspaceParams.update_forward_refs()

SetEditorSelectionsParams.update_forward_refs()
Expand Down
2 changes: 1 addition & 1 deletion extensions/positron-r/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@
},
"positron": {
"binaryDependencies": {
"ark": "0.1.74"
"ark": "0.1.76"
}
}
}
58 changes: 58 additions & 0 deletions positron/comms/ui-frontend-openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@
}
]
},
{
"name": "new_document",
"summary": "Create a new document with text contents",
"description": "Use this to create a new document with the given language ID and text contents",
"params": [
{
"name": "contents",
"description": "Document contents",
"schema": {
"type": "string"
}
},
{
"name": "language_id",
"description": "Language identifier",
"schema": {
"type": "string"
}
}
],
"result": {}
},
{
"name": "show_message",
"summary": "Show a message",
Expand Down Expand Up @@ -194,6 +216,42 @@
}
]
},
{
"name": "execute_code",
"summary": "Execute code in a Positron runtime",
"description": "Use this to execute code in a Positron runtime",
"params": [
{
"name": "language_id",
"description": "The language ID of the code to execute",
"schema": {
"type": "string"
}
},
{
"name": "code",
"description": "The code to execute",
"schema": {
"type": "string"
}
},
{
"name": "focus",
"description": "Whether to focus the runtime's console",
"schema": {
"type": "boolean"
}
},
{
"name": "allow_incomplete",
"description": "Whether to bypass runtime code completeness checks",
"schema": {
"type": "boolean"
}
}
],
"result": {}
},
{
"name": "open_workspace",
"summary": "Open a workspace",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as extHostTypes from 'vs/workbench/api/common/positron/extHostTypes.pos
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
import { ExtHostPreviewPanels } from 'vs/workbench/api/common/positron/extHostPreviewPanels';
import { ExtHostModalDialogs } from 'vs/workbench/api/common/positron/extHostModalDialogs';
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
import { ExtHostContext } from 'vs/workbench/api/common/extHost.protocol';
import { IExtHostWorkspace } from 'vs/workbench/api/common/extHostWorkspace';
import { ExtHostWebviews } from 'vs/workbench/api/common/extHostWebview';
Expand Down Expand Up @@ -57,12 +58,12 @@ export function createPositronApiFactoryAndRegisterActors(accessor: ServicesAcce
const extHostLanguageFeatures: ExtHostLanguageFeatures =
rpcProtocol.getRaw(ExtHostContext.ExtHostLanguageFeatures);
const extHostEditors: ExtHostEditors = rpcProtocol.getRaw(ExtHostContext.ExtHostEditors);

const extHostDocuments: ExtHostDocuments = rpcProtocol.getRaw(ExtHostContext.ExtHostDocuments);
const extHostLanguageRuntime = rpcProtocol.set(ExtHostPositronContext.ExtHostLanguageRuntime, new ExtHostLanguageRuntime(rpcProtocol));
const extHostPreviewPanels = rpcProtocol.set(ExtHostPositronContext.ExtHostPreviewPanel, new ExtHostPreviewPanels(rpcProtocol, extHostWebviews, extHostWorkspace));
const extHostModalDialogs = rpcProtocol.set(ExtHostPositronContext.ExtHostModalDialogs, new ExtHostModalDialogs(rpcProtocol));
const extHostConsoleService = rpcProtocol.set(ExtHostPositronContext.ExtHostConsoleService, new ExtHostConsoleService(rpcProtocol, extHostLogService));
const extHostMethods = rpcProtocol.set(ExtHostPositronContext.ExtHostMethods, new ExtHostMethods(rpcProtocol, extHostEditors, extHostModalDialogs, extHostWorkspace));
const extHostMethods = rpcProtocol.set(ExtHostPositronContext.ExtHostMethods, new ExtHostMethods(rpcProtocol, extHostEditors, extHostDocuments, extHostModalDialogs, extHostLanguageRuntime, extHostWorkspace));

return function (extension: IExtensionDescription, extensionInfo: IExtensionRegistries, configProvider: ExtHostConfigProvider): typeof positron {

Expand Down
52 changes: 49 additions & 3 deletions src/vs/workbench/api/common/positron/extHostMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

import * as extHostProtocol from './extHost.positron.protocol';
import { ExtHostEditors } from '../extHostTextEditors';
import { ExtHostModalDialogs } from '../positron/extHostModalDialogs';
import { ExtHostDocuments } from '../extHostDocuments';
import { ExtHostWorkspace } from '../extHostWorkspace';
import { Range } from 'vs/workbench/api/common/extHostTypes';
import { ExtHostModalDialogs } from '../positron/extHostModalDialogs';
import { ExtHostLanguageRuntime } from '../positron/extHostLanguageRuntime';
import { UiFrontendRequest, EditorContext, Range as UIRange } from 'vs/workbench/services/languageRuntime/common/positronUiComm';
import { JsonRpcErrorCode } from 'vs/workbench/services/languageRuntime/common/positronBaseComm';
import { EndOfLine } from '../extHostTypeConverters';
import { Range } from 'vs/workbench/api/common/extHostTypes';
import { EndOfLine, TextEditorOpenOptions } from '../extHostTypeConverters';

type JsonRpcResponse = JsonRpcResult | JsonRpcError;

Expand All @@ -35,7 +37,9 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
constructor(
_mainContext: extHostProtocol.IMainPositronContext,
private readonly editors: ExtHostEditors,
private readonly documents: ExtHostDocuments,
private readonly dialogs: ExtHostModalDialogs,
private readonly runtime: ExtHostLanguageRuntime,
private readonly workspace: ExtHostWorkspace
) {
}
Expand Down Expand Up @@ -84,6 +88,16 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
result = await this.workspaceFolder();
break;
}
case UiFrontendRequest.NewDocument: {
if (!params ||
!Object.keys(params).includes('contents') ||
!Object.keys(params).includes('language_id')) {
return newInvalidParamsError(method);
}
result = await this.createDocument(params.contents as string,
params.language_id as string);
break;
}
case UiFrontendRequest.ShowQuestion: {
if (!params ||
!Object.keys(params).includes('title') ||
Expand All @@ -108,6 +122,20 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
params.message as string);
break;
}
case UiFrontendRequest.ExecuteCode: {
if (!params ||
!Object.keys(params).includes('language_id') ||
!Object.keys(params).includes('code') ||
!Object.keys(params).includes('focus') ||
!Object.keys(params).includes('allow_incomplete')) {
return newInvalidParamsError(method);
}
result = await this.executeCode(params.language_id as string,
params.code as string,
params.focus as boolean,
params.allow_incomplete as boolean);
break;
}
case UiFrontendRequest.DebugSleep: {
if (!params || !Object.keys(params).includes('ms')) {
return newInvalidParamsError(method);
Expand Down Expand Up @@ -219,10 +247,28 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
return this.dialogs.showSimpleModalDialogMessage(title, message);
}

async createDocument(contents: string, languageId: string): Promise<null> {

const uri = await this.documents.createDocumentData({
content: contents, language: languageId
});
const opts: TextEditorOpenOptions = { preview: true };
this.documents.ensureDocumentData(uri).then(documentData => {
this.editors.showTextDocument(documentData.document, opts);
});

// TODO: Return a document ID
return null;
}

async showQuestion(title: string, message: string, okButtonTitle: string, cancelButtonTitle: string): Promise<boolean> {
return this.dialogs.showSimpleModalDialogPrompt(title, message, okButtonTitle, cancelButtonTitle);
}

async executeCode(languageId: string, code: string, focus: boolean, allowIncomplete?: boolean): Promise<boolean> {
return this.runtime.executeCode(languageId, code, focus, allowIncomplete);
}

async debugSleep(ms: number): Promise<null> {
await delay(ms);
return null;
Expand Down
49 changes: 49 additions & 0 deletions src/vs/workbench/services/languageRuntime/common/positronUiComm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ export interface ShowUrlEvent {

}

/**
* Request: Create a new document with text contents
*
* Use this to create a new document with the given language ID and text
* contents
*/
export interface NewDocumentRequest {
/**
* Document contents
*/
contents: string;

/**
* Language identifier
*/
language_id: string;

}

/**
* Request: Show a question
*
Expand Down Expand Up @@ -338,6 +357,34 @@ export interface DebugSleepRequest {

}

/**
* Request: Execute code in a Positron runtime
*
* Use this to execute code in a Positron runtime
*/
export interface ExecuteCodeRequest {
/**
* The language ID of the code to execute
*/
language_id: string;

/**
* The code to execute
*/
code: string;

/**
* Whether to focus the runtime's console
*/
focus: boolean;

/**
* Whether to bypass runtime code completeness checks
*/
allow_incomplete: boolean;

}

/**
* Request: Path to the workspace folder
*
Expand Down Expand Up @@ -388,9 +435,11 @@ export enum UiFrontendEvent {
}

export enum UiFrontendRequest {
NewDocument = 'new_document',
ShowQuestion = 'show_question',
ShowDialog = 'show_dialog',
DebugSleep = 'debug_sleep',
ExecuteCode = 'execute_code',
WorkspaceFolder = 'workspace_folder',
ModifyEditorSelections = 'modify_editor_selections',
LastActiveEditorContext = 'last_active_editor_context'
Expand Down

0 comments on commit 4f9f147

Please sign in to comment.