From 2696686d0d87621c8cbe08999ae7442fdbd3ebaf Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 12 Jan 2023 14:38:54 -0800 Subject: [PATCH] Adding test liveshare serializer --- notebook-serializer-sample/package.json | 87 ++++++++----------- notebook-serializer-sample/src/controller.ts | 51 ----------- notebook-serializer-sample/src/extension.ts | 30 ++----- notebook-serializer-sample/src/serializer.ts | 22 ++--- .../vscode.proposed.notebookLiveShare.d.ts | 21 +++++ 5 files changed, 72 insertions(+), 139 deletions(-) delete mode 100644 notebook-serializer-sample/src/controller.ts create mode 100644 notebook-serializer-sample/src/vscode.proposed.notebookLiveShare.d.ts diff --git a/notebook-serializer-sample/package.json b/notebook-serializer-sample/package.json index cbf30f81..e3596d50 100644 --- a/notebook-serializer-sample/package.json +++ b/notebook-serializer-sample/package.json @@ -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" + } +} \ No newline at end of file diff --git a/notebook-serializer-sample/src/controller.ts b/notebook-serializer-sample/src/controller.ts deleted file mode 100644 index c087b5c5..00000000 --- a/notebook-serializer-sample/src/controller.ts +++ /dev/null @@ -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 { - 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()); - } - } -} diff --git a/notebook-serializer-sample/src/extension.ts b/notebook-serializer-sample/src/extension.ts index 4fb2dd2f..e816f8e1 100644 --- a/notebook-serializer-sample/src/extension.ts +++ b/notebook-serializer-sample/src/extension.ts @@ -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 + } + ) ); } diff --git a/notebook-serializer-sample/src/serializer.ts b/notebook-serializer-sample/src/serializer.ts index 5b9053ee..1adf5f90 100644 --- a/notebook-serializer-sample/src/serializer.ts +++ b/notebook-serializer-sample/src/serializer.ts @@ -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 { - const contents = new TextDecoder().decode(data); // convert to String - - // Read file contents - let raw: RawNotebookData; - try { - raw = 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); } diff --git a/notebook-serializer-sample/src/vscode.proposed.notebookLiveShare.d.ts b/notebook-serializer-sample/src/vscode.proposed.notebookLiveShare.d.ts new file mode 100644 index 00000000..2642fbba --- /dev/null +++ b/notebook-serializer-sample/src/vscode.proposed.notebookLiveShare.d.ts @@ -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; + } +}