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

OpenRPC for cursors/ranges rstudioapi shims #2582

Merged
merged 2 commits into from
Apr 2, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ class Selection(BaseModel):
)


class Range(BaseModel):
"""
Selection range
"""

start: Position = Field(
description="Start position of the selection",
)

end: Position = Field(
description="End position of the selection",
)


@enum.unique
class UiBackendRequest(str, enum.Enum):
"""
Expand Down Expand Up @@ -195,6 +209,15 @@ class UiFrontendEvent(str, enum.Enum):
# Execute a Positron command
ExecuteCommand = "execute_command"

# Open a workspace
OpenWorkspace = "open_workspace"

# Set the selections in the editor
SetEditorSelections = "set_editor_selections"

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


class BusyParams(BaseModel):
"""
Expand Down Expand Up @@ -256,6 +279,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 +337,54 @@ class ExecuteCommandParams(BaseModel):
)


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 SetEditorSelectionsParams(BaseModel):
"""
Set the selections in the editor
"""

selections: List[Range] = Field(
description="The selections (really, ranges) to set in the document",
)


class ModifyEditorSelectionsParams(BaseModel):
"""
Modify selections in the editor with a text edit
"""

selections: List[Range] = Field(
description="The selections (really, ranges) to set in the document",
)

values: List[str] = Field(
description="The text values to insert at the selections",
)


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 @@ -308,6 +393,8 @@ class ExecuteCommandParams(BaseModel):

Selection.update_forward_refs()

Range.update_forward_refs()

CallMethodParams.update_forward_refs()

CallMethodRequest.update_forward_refs()
Expand All @@ -320,10 +407,20 @@ class ExecuteCommandParams(BaseModel):

ShowQuestionParams.update_forward_refs()

ShowDialogParams.update_forward_refs()

PromptStateParams.update_forward_refs()

WorkingDirectoryParams.update_forward_refs()

DebugSleepParams.update_forward_refs()

ExecuteCommandParams.update_forward_refs()

OpenWorkspaceParams.update_forward_refs()

SetEditorSelectionsParams.update_forward_refs()

ModifyEditorSelectionsParams.update_forward_refs()

ShowUrlParams.update_forward_refs()
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.72"
"ark": "0.1.73"
}
}
}
63 changes: 63 additions & 0 deletions positron/comms/ui-frontend-openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,51 @@
}
}
},
{
"name": "set_editor_selections",
"summary": "Set the selections in the editor",
"description": "Use this to set the selection ranges/cursor in the editor",
"params": [
{
"name": "selections",
"description": "The selections (really, ranges) to set in the document",
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/range"
}
}
}
]
},
{
"name": "modify_editor_selections",
"summary": "Modify selections in the editor with a text edit",
"description": "Use this to edit a set of selection ranges/cursor in the editor",
"params": [
{
"name": "selections",
"description": "The selections (really, ranges) to set in the document",
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/range"
}
}
},
{
"name": "values",
"description": "The text values to insert at the selections",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
],
"result": {}
},
{
"name": "last_active_editor_context",
"summary": "Context metadata for the last editor",
Expand Down Expand Up @@ -383,6 +428,24 @@
"end",
"text"
]
},
"range": {
"type": "object",
"description": "Selection range",
"properties": {
"start": {
"description": "Start position of the selection",
"$ref": "#/components/schemas/position"
},
"end": {
"description": "End position of the selection",
"$ref": "#/components/schemas/position"
}
},
"required": [
"start",
"end"
]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import { IPositronHelpService } from 'vs/workbench/contrib/positronHelp/browser/
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { IRuntimeClientEvent } from 'vs/workbench/services/languageRuntime/common/languageRuntimeUiClient';
import { URI } from 'vs/base/common/uri';
import { BusyEvent, UiFrontendEvent, OpenEditorEvent, OpenWorkspaceEvent, PromptStateEvent, WorkingDirectoryEvent, ShowMessageEvent, ExecuteCommandEvent } from 'vs/workbench/services/languageRuntime/common/positronUiComm';
import { BusyEvent, UiFrontendEvent, OpenEditorEvent, OpenWorkspaceEvent, PromptStateEvent, WorkingDirectoryEvent, ShowMessageEvent, ExecuteCommandEvent, SetEditorSelectionsEvent } from 'vs/workbench/services/languageRuntime/common/positronUiComm';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditor } from 'vs/editor/common/editorCommon';
import { Selection } from 'vs/editor/common/core/selection';
import { ITextResourceEditorInput } from 'vs/platform/editor/common/editor';
import { IPositronDataExplorerService } from 'vs/workbench/services/positronDataExplorer/browser/interfaces/positronDataExplorerService';
import { ObservableValue } from 'vs/base/common/observableInternal/base';
Expand Down Expand Up @@ -190,6 +192,13 @@ class ExtHostLanguageRuntimeSessionAdapter implements ILanguageRuntimeSession {
// Update busy state
const busy = ev.data as BusyEvent;
this.dynState.busy = busy.busy;
} else if (ev.name === UiFrontendEvent.SetEditorSelections) {
// Set the editor selections
const sel = ev.data as SetEditorSelectionsEvent;
const selections = sel.selections.map(s =>
new Selection(s.start.line, s.start.character, s.end.line, s.end.character));
const editor = this._editorService.activeTextEditorControl as IEditor;
editor.setSelections(selections);
} else if (ev.name === UiFrontendEvent.OpenEditor) {
// Open an editor
const ed = ev.data as OpenEditorEvent;
Expand Down
30 changes: 29 additions & 1 deletion src/vs/workbench/api/common/positron/extHostMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import * as extHostProtocol from './extHost.positron.protocol';
import { ExtHostEditors } from '../extHostTextEditors';
import { ExtHostModalDialogs } from '../positron/extHostModalDialogs';
import { ExtHostWorkspace } from '../extHostWorkspace';
import { UiFrontendRequest, EditorContext } from 'vs/workbench/services/languageRuntime/common/positronUiComm';
import { Range } from 'vs/workbench/api/common/extHostTypes';
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';

Expand Down Expand Up @@ -64,6 +65,18 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
result = await this.lastActiveEditorContext();
break;
}
case UiFrontendRequest.ModifyEditorSelections: {
if (!params ||
!Object.keys(params).includes('selections') ||
!Object.keys(params).includes('values')) {
return newInvalidParamsError(method);
}
const sel = params.selections as UIRange[];
const selections = sel.map(s =>
new Range(s.start.line, s.start.character, s.end.line, s.end.character));
result = await this.modifyEditorLocations(selections, params.values as string[]);
break;
}
case UiFrontendRequest.WorkspaceFolder: {
if (params && Object.keys(params).length > 0) {
return newInvalidParamsError(method);
Expand Down Expand Up @@ -179,6 +192,21 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
};
}

async modifyEditorLocations(locations: Range[], values: string[]): Promise<null> {
const editor = this.editors.getActiveTextEditor();
if (!editor) {
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this throw an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so (???) because similarly to here we don't know that there is an active text editor currently. Maybe when we address #2571 we should also look at what gets returned when there is no active editor.

If you have no active editor in RStudio, it uses the console, which are not handling at all yet. Dealing with this for ranges is analogous to us eventually handling id here.

}

editor.edit(editBuilder => {
locations.map((location, i) => {
editBuilder.replace(location, values[i]);
});
});

return null;
}

async workspaceFolder(): Promise<string | null> {
const folders = this.workspace.getWorkspaceFolders();
if (folders && folders.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { Disposable } from 'vs/base/common/lifecycle';
import { Event } from 'vs/base/common/event';
import { IRuntimeClientInstance } from 'vs/workbench/services/languageRuntime/common/languageRuntimeClientInstance';
import { BusyEvent, ClearConsoleEvent, UiFrontendEvent, OpenEditorEvent, OpenWorkspaceEvent, PositronUiComm, PromptStateEvent, ShowMessageEvent, WorkingDirectoryEvent, ExecuteCommandEvent, ShowUrlEvent } from './positronUiComm';
import { BusyEvent, ClearConsoleEvent, UiFrontendEvent, OpenEditorEvent, OpenWorkspaceEvent, PositronUiComm, PromptStateEvent, ShowMessageEvent, WorkingDirectoryEvent, ExecuteCommandEvent, ShowUrlEvent, SetEditorSelectionsEvent } from './positronUiComm';


/**
Expand Down Expand Up @@ -63,6 +63,7 @@ export class UiClientInstance extends Disposable {
/** Emitters for events forwarded from the UI comm */
onDidBusy: Event<BusyEvent>;
onDidClearConsole: Event<ClearConsoleEvent>;
onDidSetEditorSelections: Event<SetEditorSelectionsEvent>;
onDidOpenEditor: Event<OpenEditorEvent>;
onDidOpenWorkspace: Event<OpenWorkspaceEvent>;
onDidShowMessage: Event<ShowMessageEvent>;
Expand All @@ -86,6 +87,7 @@ export class UiClientInstance extends Disposable {
this._comm = new PositronUiComm(this._client);
this.onDidBusy = this._comm.onDidBusy;
this.onDidClearConsole = this._comm.onDidClearConsole;
this.onDidSetEditorSelections = this._comm.onDidSetEditorSelections;
this.onDidOpenEditor = this._comm.onDidOpenEditor;
this.onDidOpenWorkspace = this._comm.onDidOpenWorkspace;
this.onDidShowMessage = this._comm.onDidShowMessage;
Expand Down
Loading
Loading