Enable VS Code extension APIs for getting opened notebook documents (#18043)

This commit is contained in:
Cory Rivera
2022-01-11 12:57:55 -08:00
committed by GitHub
parent 7940814e8d
commit df2934a80c
3 changed files with 81 additions and 15 deletions

View File

@@ -0,0 +1,45 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import type * as azdata from 'azdata';
import { VSCodeNotebookDocument } from 'sql/workbench/api/common/vscodeNotebookDocument';
import { functionalityNotSupportedError } from 'sql/base/common/locConstants';
export class VSCodeNotebookEditor implements vscode.NotebookEditor {
private readonly _document: vscode.NotebookDocument;
constructor(editor: azdata.nb.NotebookEditor) {
this._document = new VSCodeNotebookDocument(editor.document);
}
public get document(): vscode.NotebookDocument {
return this._document;
}
public get selections(): vscode.NotebookRange[] {
throw new Error(functionalityNotSupportedError);
}
public get visibleRanges(): vscode.NotebookRange[] {
throw new Error(functionalityNotSupportedError);
}
public get viewColumn(): vscode.ViewColumn | undefined {
throw new Error(functionalityNotSupportedError);
}
public revealRange(range: vscode.NotebookRange, revealType?: vscode.NotebookEditorRevealType): void {
throw new Error(functionalityNotSupportedError);
}
public edit(callback: (editBuilder: vscode.NotebookEditorEdit) => void): Promise<boolean> {
return Promise.reject(functionalityNotSupportedError);
}
public setDecorations(decorationType: vscode.NotebookEditorDecorationType, range: vscode.NotebookRange): void {
throw new Error(functionalityNotSupportedError);
}
}

View File

@@ -15,6 +15,7 @@ import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
import { convertToVSCodeNotebookCell } from 'sql/workbench/api/common/vscodeExecuteProvider';
import { VSCodeNotebookDocument } from 'sql/workbench/api/common/vscodeNotebookDocument';
import { URI } from 'vs/base/common/uri';
import { VSCodeNotebookEditor } from 'sql/workbench/api/common/vscodeNotebookEditor';
class MockNotebookSerializer implements vscode.NotebookSerializer {
deserializeNotebook(content: Uint8Array, token: vscode.CancellationToken): vscode.NotebookData | Thenable<vscode.NotebookData> {
@@ -366,6 +367,17 @@ suite('Notebook Serializer', () => {
validateCellRange: () => undefined
};
function validateDocsMatch(actualDoc: vscode.NotebookDocument, expectedDoc: vscode.NotebookDocument): void {
assert.deepStrictEqual(actualDoc.uri, expectedDoc.uri);
assert.strictEqual(actualDoc.notebookType, expectedDoc.notebookType);
assert.strictEqual(actualDoc.version, expectedDoc.version);
assert.strictEqual(actualDoc.isDirty, expectedDoc.isDirty);
assert.strictEqual(actualDoc.isUntitled, expectedDoc.isUntitled);
assert.strictEqual(actualDoc.isClosed, expectedDoc.isClosed);
assert.deepStrictEqual(actualDoc.metadata, expectedDoc.metadata);
assert.strictEqual(actualDoc.cellCount, expectedDoc.cellCount);
}
test('Convert ADS NotebookDocument into VS Code NotebookDocument', async () => {
let expectedDoc: vscode.NotebookDocument = {
get uri() { return testDoc.uri; },
@@ -382,14 +394,7 @@ suite('Notebook Serializer', () => {
};
let actualDoc = new VSCodeNotebookDocument(testDoc);
assert.deepStrictEqual(actualDoc.uri, expectedDoc.uri);
assert.strictEqual(actualDoc.notebookType, expectedDoc.notebookType);
assert.strictEqual(actualDoc.version, expectedDoc.version);
assert.strictEqual(actualDoc.isDirty, expectedDoc.isDirty);
assert.strictEqual(actualDoc.isUntitled, expectedDoc.isUntitled);
assert.strictEqual(actualDoc.isClosed, expectedDoc.isClosed);
assert.deepStrictEqual(actualDoc.metadata, expectedDoc.metadata);
assert.strictEqual(actualDoc.cellCount, expectedDoc.cellCount);
validateDocsMatch(actualDoc, expectedDoc);
});
// Have to validate cell fields manually since one of the NotebookCell fields is a function pointer,
@@ -440,6 +445,23 @@ suite('Notebook Serializer', () => {
secondCell = vsDoc.cellAt(10);
validateCellMatches(secondCell, expectedCells[1]);
});
test('VS Code NotebookEditor functionality', async () => {
let editor = <azdata.nb.NotebookEditor>{ document: testDoc };
let vscodeEditor = new VSCodeNotebookEditor(editor);
let expectedDoc = new VSCodeNotebookDocument(testDoc);
validateDocsMatch(vscodeEditor.document, expectedDoc);
// We only need the document field for VSCodeNotebookEditor, so the other
// fields should be non-functional
assert.throws(() => vscodeEditor.selections);
assert.throws(() => vscodeEditor.visibleRanges);
assert.throws(() => vscodeEditor.viewColumn);
assert.throws(() => vscodeEditor.revealRange(undefined));
assert.throws(() => vscodeEditor.setDecorations(undefined, undefined));
await assert.rejects(() => vscodeEditor.edit(() => undefined));
});
});
suite('Notebook Controller', () => {