mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
Rename to "-sample"
This commit is contained in:
51
notebook-serializer-sample/src/controller.ts
Normal file
51
notebook-serializer-sample/src/controller.ts
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
33
notebook-serializer-sample/src/extension.ts
Normal file
33
notebook-serializer-sample/src/extension.ts
Normal 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()
|
||||
);
|
||||
}
|
||||
58
notebook-serializer-sample/src/serializer.ts
Normal file
58
notebook-serializer-sample/src/serializer.ts
Normal 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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user