Adding test liveshare serializer

This commit is contained in:
Matt Bierner
2023-01-12 14:38:54 -08:00
parent 88f689af76
commit 2696686d0d
5 changed files with 72 additions and 139 deletions

View File

@ -1,53 +1,38 @@
{
"name": "notebook-serializer-sample",
"displayName": "notebook-serializer-sample",
"description": "Notebook using Serializer API sample",
"publisher": "vscode-samples",
"version": "0.0.1",
"engines": {
"vscode": "^1.74.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onNotebook:test-notebook-serializer",
"onCommand:notebook-serializer-sample.createJsonNotebook"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "notebook-serializer-sample.createJsonNotebook",
"title": "Create JSON Notebook"
}
"name": "notebook-serializer-sample",
"displayName": "notebook-serializer-sample",
"description": "Notebook using Serializer API sample",
"publisher": "vscode-samples",
"version": "0.0.1",
"engines": {
"vscode": "^1.74.0"
},
"enabledApiProposals": [
"notebookLiveShare"
],
"notebooks": [
{
"type": "test-notebook-serializer",
"displayName": "Sample Notebook",
"selector": [
{
"filenamePattern": "*.sample-json-notebook"
}
]
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -b",
"lint": "eslint src --ext ts",
"watch": "tsc -b --watch"
},
"devDependencies": {
"@types/mocha": "^8.2.2",
"@types/node": "14.x",
"@types/vscode": "^1.74.0",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"eslint": "^7.27.0",
"mocha": "^10.2.0",
"typescript": "^4.3.2"
}
}
"categories": [
"Other"
],
"activationEvents": [
"onFileSystem:file",
"onNotebook:liveshare-jupyter-notebook",
"onNotebookSerializer:liveshare-jupyter-notebook"
],
"main": "./out/extension.js",
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -b",
"lint": "eslint src --ext ts",
"watch": "tsc -b --watch"
},
"devDependencies": {
"@types/mocha": "^8.2.2",
"@types/node": "14.x",
"@types/vscode": "^1.74.0",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"eslint": "^7.27.0",
"mocha": "^10.2.0",
"typescript": "^4.3.2"
}
}

View File

@ -1,51 +0,0 @@
import * as vscode from 'vscode';
export class SampleKernel {
private readonly _id = 'test-notebook-serializer-kernel';
private readonly _label = 'Sample Notebook Kernel';
private readonly _supportedLanguages = ['json'];
private _executionOrder = 0;
private readonly _controller: vscode.NotebookController;
constructor() {
this._controller = vscode.notebooks.createNotebookController(this._id,
'test-notebook-serializer',
this._label);
this._controller.supportedLanguages = this._supportedLanguages;
this._controller.supportsExecutionOrder = true;
this._controller.executeHandler = this._executeAll.bind(this);
}
dispose(): void {
this._controller.dispose();
}
private _executeAll(cells: vscode.NotebookCell[], _notebook: vscode.NotebookDocument, _controller: vscode.NotebookController): void {
for (const cell of cells) {
this._doExecution(cell);
}
}
private async _doExecution(cell: vscode.NotebookCell): Promise<void> {
const execution = this._controller.createNotebookCellExecution(cell);
execution.executionOrder = ++this._executionOrder;
execution.start(Date.now());
try {
execution.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.json(JSON.parse(cell.document.getText()))
])]);
execution.end(true, Date.now());
} catch (err) {
execution.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.error(err as Error)
])]);
execution.end(false, Date.now());
}
}
}

View File

@ -1,33 +1,17 @@
import * as vscode from 'vscode';
import { SampleKernel } from './controller';
import { SampleContentSerializer } from './serializer';
const NOTEBOOK_TYPE = 'test-notebook-serializer';
const NOTEBOOK_TYPE = 'liveshare-jupyter-notebook';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('notebook-serializer-sample.createJsonNotebook', async () => {
const language = 'json';
const defaultValue = `{ "hello_world": 123 }`;
const cell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, defaultValue, language);
const data = new vscode.NotebookData([cell]);
data.metadata = {
custom: {
cells: [],
metadata: {
orig_nbformat: 4
},
nbformat: 4,
nbformat_minor: 2
}
};
const doc = await vscode.workspace.openNotebookDocument(NOTEBOOK_TYPE, data);
await vscode.window.showNotebookDocument(doc);
}));
context.subscriptions.push(
vscode.workspace.registerNotebookSerializer(
NOTEBOOK_TYPE, new SampleContentSerializer(), { transientOutputs: true }
),
new SampleKernel()
NOTEBOOK_TYPE, new SampleContentSerializer(), { transientOutputs: true }, {
displayName: 'Liveshare test',
filenamePattern: ['*.ipynb'],
exclusive: true
}
)
);
}

View File

@ -21,22 +21,16 @@ export class SampleContentSerializer implements vscode.NotebookSerializer {
public readonly label: string = 'My Sample Content Serializer';
public async deserializeNotebook(data: Uint8Array, token: vscode.CancellationToken): Promise<vscode.NotebookData> {
const contents = new TextDecoder().decode(data); // convert to String
// Read file contents
let raw: RawNotebookData;
try {
raw = <RawNotebookData>JSON.parse(contents);
} catch {
raw = { cells: [] };
}
console.log('Invoking my serializer');
// Create array of Notebook cells for the VS Code API from file contents
const cells = raw.cells.map(item => new vscode.NotebookCellData(
item.kind,
item.value,
item.language
));
const cells: vscode.NotebookCellData[] = [
new vscode.NotebookCellData(
vscode.NotebookCellKind.Markup,
'Hi from my test extension',
'markdown'
)
];
return new vscode.NotebookData(cells);
}

View File

@ -0,0 +1,21 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
declare module 'vscode' {
// https://github.com/microsoft/vscode/issues/106744
export interface NotebookRegistrationData {
readonly displayName: string;
readonly filenamePattern: ReadonlyArray<(GlobPattern | { readonly include: GlobPattern; readonly exclude: GlobPattern })>;
readonly exclusive?: boolean;
}
export namespace workspace {
// SPECIAL overload with NotebookRegistrationData
export function registerNotebookSerializer(notebookType: string, serializer: NotebookSerializer, options?: NotebookDocumentContentOptions, registration?: NotebookRegistrationData): Disposable;
}
}