Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide notebook support for Qute #546

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
"onLanguage:qute-html",
"onLanguage:qute-json",
"onLanguage:qute-yaml",
"onLanguage:qute-txt"
"onLanguage:qute-txt",
"onNotebook:qbook",
"workspaceContains:**/*.qbook"
],
"main": "./dist/extension",
"extensionDependencies": [
Expand Down Expand Up @@ -397,6 +399,18 @@
"scopeName": "grammar.qute",
"path": "./language-support/qute/qute.tmLanguage.json"
}
],
"notebooks": [
{
"id": "qbook",
"type": "qbook",
"displayName": "Qute NoteBook",
"selector": [
{
"filenamePattern": "*.qbook"
}
]
}
]
},
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { initTelemetryService } from './utils/telemetryUtils';
import { getFilePathsFromWorkspace } from './utils/workspaceUtils';
import { WelcomeWebview } from './webviews/WelcomeWebview';
import { createTerminateDebugListener } from './wizards/debugging/terminateProcess';
import { QBookSerializer } from './qute/quteNotebook/qBookSerializer';
import { QBookController } from './qute/quteNotebook/qBookController';

// alias for vscode-java's ExtensionAPI
export type JavaExtensionAPI = any;
Expand Down Expand Up @@ -92,6 +94,7 @@ async function doActivate(context: ExtensionContext) {
});
});

registerQuteNotebook(context);
}

export function deactivate() {
Expand Down Expand Up @@ -155,3 +158,12 @@ async function isQuarkusProject(): Promise<boolean> {
}
return false;
}

function registerQuteNotebook(context: ExtensionContext) {
context.subscriptions.push(
workspace.registerNotebookSerializer(
'qbook', new QBookSerializer(), { transientOutputs: true }
),
new QBookController()
);
}
60 changes: 60 additions & 0 deletions src/qute/quteNotebook/qBookController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as vscode from 'vscode';

export class QBookController {
readonly id = 'test-notebook-renderer-kernel';
public readonly label = 'Sample Notebook Kernel';
readonly supportedLanguages = ['json', 'qute-html', 'qute-yaml', 'qute-json', 'qute-txt'];

private _executionOrder = 0;
private readonly _controller: vscode.NotebookController;

constructor() {

this._controller = vscode.notebooks.createNotebookController(this.id,
'qbook',
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 (let cell of cells) {
this._doExecution(cell);
}
}

private async _doExecution(cell: vscode.NotebookCell): Promise<void> {
const execution = this._controller.createNotebookCellExecution(cell);

const previousIndex = cell.index -1;
const previousCell = cell && cell.notebook.cellAt(previousIndex);
const data = previousCell && previousCell.document.getText();

execution.executionOrder = ++this._executionOrder;
execution.start(Date.now());

try {

const templateContent = cell.document.getText();
const result : string = await vscode.commands.executeCommand('qute.generate', templateContent, data );

execution.replaceOutput([new vscode.NotebookCellOutput([
//vscode.NotebookCellOutputItem.text(cell.document.getText()+ data, "x-application/sample-json-renderer"),
vscode.NotebookCellOutputItem.text(result)
])]);

execution.end(true, Date.now());
} catch (err) {
execution.replaceOutput([new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.error(err)
])]);
execution.end(false, Date.now());
}
}
}
57 changes: 57 additions & 0 deletions src/qute/quteNotebook/qBookSerializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as vscode from 'vscode';
import { TextDecoder, TextEncoder } from "util";

interface RawNotebookData {
cells: RawNotebookCell[]
}

interface RawNotebookCell {
language: string;
value: string;
kind: vscode.NotebookCellKind;
editable?: boolean;
}

export class QBookSerializer implements vscode.NotebookSerializer {
public readonly label: string = 'My Sample Content Serializer';

public async deserializeNotebook(data: Uint8Array, token: vscode.CancellationToken): Promise<vscode.NotebookData> {
var contents = new TextDecoder().decode(data); // convert to String to make JSON object

// 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
));

// Pass read and formatted Notebook Data to VS Code to display Notebook with saved cells
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
let contents: RawNotebookData = { cells: []};

for (const cell of data.cells) {
contents.cells.push({
kind: cell.kind,
language: cell.languageId,
value: cell.value
});
}

// Give a string of all the data to save and VS Code will handle the rest
return new TextEncoder().encode(JSON.stringify(contents));
}
}