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

Disallow cell URIs with NotebookEdit #169481

Merged
merged 1 commit into from
Dec 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { IExtensionDescription } from 'vs/platform/extensions/common/extensions'
import { FileSystemProviderErrorCode, markAsFileSystemProviderError } from 'vs/platform/files/common/files';
import { RemoteAuthorityResolverErrorCode } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { IRelativePatternDto } from 'vs/workbench/api/common/extHost.protocol';
import { CellEditType, ICellPartialMetadataEdit, IDocumentMetadataEdit, isTextStreamMime } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { CellEditType, CellUri, ICellPartialMetadataEdit, IDocumentMetadataEdit, isTextStreamMime } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { checkProposedApiEnabled } from 'vs/workbench/services/extensions/common/extensions';
import type * as vscode from 'vscode';

Expand Down Expand Up @@ -862,6 +862,10 @@ export class WorkspaceEdit implements vscode.WorkspaceEdit {
edit = editOrTuple;
}
if (NotebookEdit.isNotebookCellEdit(edit)) {
if (uri.scheme === CellUri.scheme) {
Copy link
Member

Choose a reason for hiding this comment

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

@roblourens I don't think this is the best place to do the check. This should either be checked when creating a notebook editor or (better) when actually applying (but not while in "transport" as it is done here).

I think this will be the better place for the scheme-check:

throw new Error('set must be called with a notebook document URI, not a cell URI.');
}

if (edit.newCellMetadata) {
this.replaceNotebookCellMetadata(uri, edit.range.start, edit.newCellMetadata, metadata);
} else if (edit.newNotebookMetadata) {
Expand Down
28 changes: 23 additions & 5 deletions src/vs/workbench/api/test/browser/extHostTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
*--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import * as types from 'vs/workbench/api/common/extHostTypes';
import { CancellationError } from 'vs/base/common/errors';
import { MarshalledId } from 'vs/base/common/marshallingIds';
import { Mimes } from 'vs/base/common/mime';
import { isWindows } from 'vs/base/common/platform';
import { assertType } from 'vs/base/common/types';
import { Mimes } from 'vs/base/common/mime';
import { MarshalledId } from 'vs/base/common/marshallingIds';
import { CancellationError } from 'vs/base/common/errors';
import { URI } from 'vs/base/common/uri';
import * as types from 'vs/workbench/api/common/extHostTypes';
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';

function assertToJSON(a: any, expected: any) {
const raw = JSON.stringify(a);
Expand Down Expand Up @@ -432,6 +433,23 @@ suite('ExtHostTypes', function () {
assert.strictEqual(second.edit.newText, 'Foo');
});

test('WorkspaceEdit - NotebookEdits', () => {
const edit = new types.WorkspaceEdit();
const notebookEdit = types.NotebookEdit.insertCells(0, [new types.NotebookCellData(types.NotebookCellKind.Code, '// hello', 'javascript')]) as types.NotebookEdit;
const notebookUri = URI.parse('/foo/notebook.ipynb');
edit.set(notebookUri, [notebookEdit]);

const cellUri = CellUri.generate(notebookUri, 123);
try {
edit.set(cellUri, [notebookEdit]);
} catch (err) {
assert.ok(err.message.includes('set must be called with a notebook document URI'), err.toString());
return;
}

throw new Error('Expected set to throw with cell URI');
});

test('DocumentLink', () => {
assert.throws(() => new types.DocumentLink(null!, null!));
assert.throws(() => new types.DocumentLink(new types.Range(1, 1, 1, 1), null!));
Expand Down