Skip to content

Commit

Permalink
refactor: Remove erroneous purebook changes
Browse files Browse the repository at this point in the history
  • Loading branch information
travisstebbins committed Dec 9, 2024
1 parent a0362aa commit db564f0
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 141 deletions.
136 changes: 0 additions & 136 deletions src/purebook/PurebookController.ts

This file was deleted.

110 changes: 105 additions & 5 deletions src/purebook/purebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,25 @@ import {
type CancellationToken,
commands,
type ExtensionContext,
type NotebookCell,
type NotebookDocument,
NotebookCellData,
NotebookCellKind,
NotebookCellOutput,
NotebookCellOutputItem,
type NotebookController,
NotebookData,
notebooks,
type NotebookSerializer,
workspace,
window,
WorkspaceEdit,
Uri,
} from 'vscode';
import { LEGEND_LANGUAGE_ID } from '../utils/Const';
import { PurebookController } from './PurebookController';
import { LEGEND_COMMAND, LEGEND_LANGUAGE_ID } from '../utils/Const';
import type { PlainObject } from '@finos/legend-vscode-extension-dependencies';
import { LegendExecutionResult } from '../results/LegendExecutionResult';
import { LegendExecutionResultType } from '../results/LegendExecutionResultType';

interface RawNotebookCell {
source: string[];
Expand Down Expand Up @@ -84,20 +92,112 @@ class LegendBookSerializer implements NotebookSerializer {
}

export function enableLegendBook(context: ExtensionContext): void {
const controller = new PurebookController();

context.subscriptions.push(
workspace.registerNotebookSerializer(
controller.notebookType,
'legend-book',
new LegendBookSerializer(),
),
);

const controller = notebooks.createNotebookController(
'legend-book-controller-id',
'legend-book',
'Legend Notebook',
executeCells,
);

controller.supportedLanguages = [LEGEND_LANGUAGE_ID];
controller.supportsExecutionOrder = true;
controller.description = 'Legend Notebook REPL';

context.subscriptions.push(controller);

context.subscriptions.push(
commands.registerCommand('legend.createNotebook', createNotebook),
);
}

async function executeCells(
cells: NotebookCell[],
_document: NotebookDocument,
controller: NotebookController,
): Promise<void> {
await Promise.all(cells.map((cell) => executeCell(cell, controller)));
}

let executionOrder = 0;

async function executeCell(
cell: NotebookCell,
controller: NotebookController,
): Promise<void> {
const execution = controller.createNotebookCellExecution(cell);
execution.executionOrder = ++executionOrder;
execution.start(Date.now());

return commands
.executeCommand(
LEGEND_COMMAND,
cell.document.uri.toString(),
0,
'notebook_cell',
'executeCell',
{},
{},
)
.then(
(result) => {
const r = result as PlainObject<LegendExecutionResult>[];
const funcResult = LegendExecutionResult.serialization.fromJson(r[0]!);

if (funcResult.type !== LegendExecutionResultType.SUCCESS) {
execution.replaceOutput([
new NotebookCellOutput([
NotebookCellOutputItem.stdout(funcResult.message!),
NotebookCellOutputItem.stderr(funcResult.logMessage!),
]),
]);
execution.end(false, Date.now());
} else {
try {
let output: NotebookCellOutputItem;
switch (funcResult.messageType) {
case 'text':
output = NotebookCellOutputItem.text(funcResult.message);
break;
case 'json':
output = NotebookCellOutputItem.json(
JSON.parse(funcResult.message),
);
break;
default:
output = NotebookCellOutputItem.stderr(
`Not supported ${funcResult.messageType}`,
);
}

execution.replaceOutput([new NotebookCellOutput([output])]);
execution.end(true, Date.now());
} catch (e) {
execution.replaceOutput([
new NotebookCellOutput([
NotebookCellOutputItem.error(e as Error),
]),
]);
execution.end(false, Date.now());
}
}
},
(error) => {
execution.replaceOutput([
new NotebookCellOutput([NotebookCellOutputItem.error(error)]),
]);

execution.end(false, Date.now());
},
);
}

async function createNotebook(): Promise<void> {
if (workspace.workspaceFolders) {
window
Expand Down

0 comments on commit db564f0

Please sign in to comment.