Rename to "-sample"

This commit is contained in:
Rob Lourens
2023-01-10 16:13:47 -08:00
parent d7b731e7b0
commit 5ebba6b65c
13 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,51 @@
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

@ -0,0 +1,33 @@
import * as vscode from 'vscode';
import { SampleKernel } from './controller';
import { SampleContentSerializer } from './serializer';
const NOTEBOOK_TYPE = 'test-notebook-serializer';
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()
);
}

View File

@ -0,0 +1,58 @@
import * as vscode from 'vscode';
import { TextDecoder, TextEncoder } from 'util';
/**
* An ultra-minimal sample provider that lets the user type in JSON, and then
* outputs JSON cells. The outputs are transient and not saved to notebook file on disk.
*/
interface RawNotebookData {
cells: RawNotebookCell[]
}
interface RawNotebookCell {
language: string;
value: string;
kind: vscode.NotebookCellKind;
editable?: boolean;
}
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: [] };
}
// 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
));
return new vscode.NotebookData(cells);
}
public async serializeNotebook(data: vscode.NotebookData, token: vscode.CancellationToken): Promise<Uint8Array> {
// Map the Notebook data into the format we want to save the Notebook data as
const contents: RawNotebookData = { cells: [] };
for (const cell of data.cells) {
contents.cells.push({
kind: cell.kind,
language: cell.languageId,
value: cell.value
});
}
return new TextEncoder().encode(JSON.stringify(contents));
}
}