Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few more OpenRPC methods for rstudioapi shims #2593

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ class UiFrontendEvent(str, enum.Enum):
# Execute a Positron command
ExecuteCommand = "execute_command"

# Open a workspace
OpenWorkspace = "open_workspace"

# Show a URL in Positron's Viewer pane
ShowUrl = "show_url"


class BusyParams(BaseModel):
"""
Expand Down Expand Up @@ -224,6 +230,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 @@ -256,6 +276,20 @@ class ShowQuestionParams(BaseModel):
)


class ShowDialogParams(BaseModel):
"""
Show a dialog
"""

title: str = Field(
description="The title of the dialog",
)

message: str = Field(
description="The message to display in the dialog",
)


class PromptStateParams(BaseModel):
"""
New state of the primary and secondary prompts
Expand Down Expand Up @@ -300,6 +334,52 @@ 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
"""

path: str = Field(
description="The path for the workspace to be opened",
)

new_window: bool = Field(
description="Should the workspace be opened in a new window?",
)


class ShowUrlParams(BaseModel):
"""
Show a URL in Positron's Viewer pane
"""

url: str = Field(
description="The URL to display",
)


EditorContext.update_forward_refs()

TextDocument.update_forward_refs()
Expand All @@ -316,14 +396,24 @@ class ExecuteCommandParams(BaseModel):

OpenEditorParams.update_forward_refs()

NewDocumentParams.update_forward_refs()

ShowMessageParams.update_forward_refs()

ShowQuestionParams.update_forward_refs()

ShowDialogParams.update_forward_refs()

PromptStateParams.update_forward_refs()

WorkingDirectoryParams.update_forward_refs()

DebugSleepParams.update_forward_refs()

ExecuteCommandParams.update_forward_refs()

ExecuteCodeParams.update_forward_refs()

OpenWorkspaceParams.update_forward_refs()

ShowUrlParams.update_forward_refs()
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
50 changes: 48 additions & 2 deletions src/vs/workbench/api/common/positron/extHostMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

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 { ExtHostModalDialogs } from '../positron/extHostModalDialogs';
import { ExtHostLanguageRuntime } from '../positron/extHostLanguageRuntime';
import { UiFrontendRequest, EditorContext } from 'vs/workbench/services/languageRuntime/common/positronUiComm';
import { JsonRpcErrorCode } from 'vs/workbench/services/languageRuntime/common/positronBaseComm';
import { EndOfLine } from '../extHostTypeConverters';
import { EndOfLine, TextEditorOpenOptions } from '../extHostTypeConverters';

type JsonRpcResponse = JsonRpcResult | JsonRpcError;

Expand All @@ -34,7 +36,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 @@ -71,6 +75,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 @@ -95,6 +109,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 @@ -191,10 +219,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 };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we address #2438, we can pass a Range (i.e. two Position objects) in here in the TextEditorOpenOptions.

this.documents.ensureDocumentData(uri).then(documentData => {
this.editors.showTextDocument(documentData.document, opts);
});

// TODO: Return a document ID
return null;
Comment on lines +260 to +261
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For us to address with #2571

}

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 @@ -252,6 +252,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 @@ -311,6 +330,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 @@ -342,9 +389,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',
LastActiveEditorContext = 'last_active_editor_context'
}
Expand Down
Loading