From db564f0d9336066936b7283e1410eb21b2abdc51 Mon Sep 17 00:00:00 2001 From: "Stebbins, Travis [Engineering]" Date: Mon, 9 Dec 2024 16:47:56 -0500 Subject: [PATCH] refactor: Remove erroneous purebook changes --- src/purebook/PurebookController.ts | 136 ----------------------------- src/purebook/purebook.ts | 110 +++++++++++++++++++++-- 2 files changed, 105 insertions(+), 141 deletions(-) delete mode 100644 src/purebook/PurebookController.ts diff --git a/src/purebook/PurebookController.ts b/src/purebook/PurebookController.ts deleted file mode 100644 index d7c77878..00000000 --- a/src/purebook/PurebookController.ts +++ /dev/null @@ -1,136 +0,0 @@ -/** - * Copyright (c) 2024-present, Goldman Sachs - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { - commands, - type NotebookCell, - type NotebookDocument, - NotebookCellOutput, - NotebookCellOutputItem, - type NotebookController, - notebooks, -} from 'vscode'; -import { LEGEND_COMMAND, LEGEND_LANGUAGE_ID } from '../utils/Const'; -import { LegendExecutionResult } from '../results/LegendExecutionResult'; -import { type PlainObject } from '../utils/SerializationUtils'; -import { LegendExecutionResultType } from '../results/LegendExecutionResultType'; - -export class PurebookController { - readonly controllerId = 'legend-book-controller-id'; - readonly notebookType = 'legend-book'; - readonly label = 'Legend Notebook'; - readonly supportedLanguages = [LEGEND_LANGUAGE_ID]; - readonly description = 'Legend Notebook REPL'; - - private readonly _controller: NotebookController; - private _executionOrder = 0; - - constructor() { - this._controller = notebooks.createNotebookController( - this.controllerId, - this.notebookType, - this.label, - ); - - this._controller.supportedLanguages = this.supportedLanguages; - this._controller.supportsExecutionOrder = true; - this._controller.description = this.description; - this._controller.executeHandler = this.executeCells.bind(this); - } - - dispose = (): void => {}; - - executeCells = async ( - cells: NotebookCell[], - _document: NotebookDocument, - controller: NotebookController, - ): Promise => { - await Promise.all(cells.map((cell) => this.executeCell(cell, controller))); - }; - - executeCell = async ( - cell: NotebookCell, - controller: NotebookController, - ): Promise => { - const execution = controller.createNotebookCellExecution(cell); - execution.executionOrder = ++this._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[]; - 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()); - }, - ); - }; -} diff --git a/src/purebook/purebook.ts b/src/purebook/purebook.ts index d2876276..2f3234ef 100644 --- a/src/purebook/purebook.ts +++ b/src/purebook/purebook.ts @@ -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[]; @@ -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 { + await Promise.all(cells.map((cell) => executeCell(cell, controller))); +} + +let executionOrder = 0; + +async function executeCell( + cell: NotebookCell, + controller: NotebookController, +): Promise { + 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[]; + 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 { if (workspace.workspaceFolders) { window