Skip to content

Commit cb41a6b

Browse files
authored
Support functions in View() (#8262)
Addresses #2945. Companion PR for posit-dev/ark#848. The main change needed on the frontend side is to detect non-file schemes in the `OpenEditor` event handler of the UI comm. This way we preserve `ark:` URIs, which allows Ark to request the frontend to open documents via its own vdoc provider. I also improved vdoc provider errors to be more user friendly. The error is mentioned in the virtual editor and the error message is logged. ### Release Notes <!-- Optionally, replace `N/A` with text to be included in the next release notes. The `N/A` bullets are ignored. If you refer to one or more Positron issues, these issues are used to collect information about the feature or bugfix, such as the relevant language pack as determined by Github labels of type `lang: `. The note will automatically be tagged with the language. These notes are typically filled by the Positron team. If you are an external contributor, you may ignore this section. --> #### New Features - `View()` now supports function objects (#2945). It takes you directly to the original source, if the function has source references, or to a virtual document containing the deparsed function code otherwise. - Functions are now also viewable from the Variables pane. Click on the "view" button on the right hand side of a variable entry to view its source definition in an editor. #### Bug Fixes - N/A ### QA Notes See companion PR: posit-dev/ark#848. <!-- Positron team members: please add relevant e2e test tags, so the tests can be run when you open this pull request. - Instructions: https://github.com/posit-dev/positron/blob/main/test/e2e/README.md#pull-requests-and-test-tags - Available tags: https://github.com/posit-dev/positron/blob/main/test/e2e/infra/test-runner/test-tags.ts --> <!-- Add additional information for QA on how to validate the change, paying special attention to the level of risk, adjacent areas that could be affected by the change, and any important contextual information not present in the linked issues. --> @:variables
1 parent 93d2f68 commit cb41a6b

File tree

10 files changed

+33
-16
lines changed

10 files changed

+33
-16
lines changed

extensions/positron-r/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@
815815
},
816816
"positron": {
817817
"binaryDependencies": {
818-
"ark": "0.1.193"
818+
"ark": "0.1.194"
819819
},
820820
"minimumRVersion": "4.2.0",
821821
"minimumRenvVersion": "1.0.9"

extensions/positron-r/src/virtual-documents.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as vscode from 'vscode';
77
import { RequestType } from 'vscode-languageclient';
88
import { LanguageClient } from 'vscode-languageclient/node';
9+
import { LOGGER } from './extension';
910

1011
interface VirtualDocumentParams {
1112
path: string;
@@ -27,6 +28,11 @@ export class VirtualDocumentProvider implements vscode.TextDocumentContentProvid
2728
path: uri.path,
2829
};
2930

30-
return await this._client.sendRequest(VIRTUAL_DOCUMENT_REQUEST_TYPE, params, token);
31+
try {
32+
return await this._client.sendRequest(VIRTUAL_DOCUMENT_REQUEST_TYPE, params, token);
33+
} catch (err) {
34+
LOGGER.warn(`Failed to provide document for URI '${uri}': ${err}`);
35+
return 'Error: This document does not exist';
36+
}
3137
}
3238
}

positron/comms/variables-backend-openrpc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@
186186
"schema": {
187187
"description": "The ID of the viewer that was opened.",
188188
"type": "string"
189-
}
189+
},
190+
"required": false
190191
}
191192
}
192193
],

src/vs/workbench/api/browser/positron/mainThreadLanguageRuntime.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,15 @@ class ExtHostLanguageRuntimeSessionAdapter extends Disposable implements ILangua
230230
} else if (ev.name === UiFrontendEvent.OpenEditor) {
231231
// Open an editor
232232
const ed = ev.data as OpenEditorEvent;
233+
234+
let file = URI.parse(ed.file);
235+
if (!file.scheme) {
236+
// If the URI doesn't have a scheme, assume it's a file URI
237+
file = URI.file(ed.file);
238+
}
239+
233240
const editor: ITextResourceEditorInput = {
234-
resource: URI.file(ed.file),
241+
resource: file,
235242
options: { selection: { startLineNumber: ed.line, startColumn: ed.column } }
236243
};
237244
this._editorService.openEditor(editor);

src/vs/workbench/api/common/positron/extHostMethods.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,14 @@ export class ExtHostMethods implements extHostProtocol.ExtHostMethodsShape {
281281
}
282282

283283
async createDocument(contents: string, languageId: string): Promise<null> {
284-
285284
const uri = await this.documents.createDocumentData({
286-
content: contents, language: languageId
285+
content: contents,
286+
language: languageId,
287287
});
288+
const documentData = await this.documents.ensureDocumentData(uri);
289+
288290
const opts: TextEditorOpenOptions = { preview: true };
289-
this.documents.ensureDocumentData(uri).then(documentData => {
290-
this.editors.showTextDocument(documentData.document, opts);
291-
});
291+
await this.editors.showTextDocument(documentData.document, opts);
292292

293293
// TODO: Return a document ID
294294
return null;

src/vs/workbench/contrib/positronVariables/browser/components/variableItem.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ export const VariableItem = (props: VariableItemProps) => {
137137
));
138138
}
139139

140-
// If a binding was returned, save the binding between the viewer and the variable item.
140+
// If a binding was returned, save the binding between the viewer and the
141+
// variable item. Note that it's valid for backends to not return any ID
142+
// if no comm was open. This happens with Ark when the user views a
143+
// function object: a virtual document is opened in an editor but is not
144+
// managed by a comm.
141145
if (viewerId) {
142146
explorerService.setInstanceForVar(viewerId, item.id);
143147
}

src/vs/workbench/services/languageRuntime/common/languageRuntimeVariablesClient.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ export class PositronVariable {
7676
/**
7777
* Requests that the language runtime open a viewer for this variable.
7878
*
79-
* @returns The ID of the viewer that was opened.
79+
* @returns The ID of the viewer that was opened, if any.
8080
*/
81-
async view(): Promise<string> {
81+
async view(): Promise<string | undefined> {
8282
const path = this.parentKeys.concat(this.data.access_key);
8383
return this._comm.view(path);
8484
}
@@ -268,4 +268,3 @@ export class VariablesClientInstance extends Disposable {
268268
}));
269269
}
270270
}
271-

src/vs/workbench/services/languageRuntime/common/positronVariablesComm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export class PositronVariablesComm extends PositronBaseComm {
415415
*
416416
* @returns The ID of the viewer that was opened.
417417
*/
418-
view(path: Array<string>): Promise<string> {
418+
view(path: Array<string>): Promise<string | undefined> {
419419
return super.performRpc('view', ['path'], [path]);
420420
}
421421

src/vs/workbench/services/positronVariables/common/classes/variableItem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export class VariableItem implements IVariableItem {
324324
/**
325325
* Requests that a viewer be opened for this variable.
326326
*/
327-
async view(): Promise<string> {
327+
async view(): Promise<string | undefined> {
328328
return this._variable.view();
329329
}
330330

src/vs/workbench/services/positronVariables/common/interfaces/variableItem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@ export interface IVariableItem {
8585
/**
8686
* Requests that a data viewer be opened for this variable.
8787
*/
88-
view(): Promise<string>;
88+
view(): Promise<string | undefined>;
8989
}

0 commit comments

Comments
 (0)