From 377fb8b617304133a4d2647fb5c29aa9d291f273 Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 5 Dec 2024 13:40:38 +0100 Subject: [PATCH 01/12] Init setup for "Reload & Preview Manim Cell" command --- package.json | 5 +++++ src/extension.ts | 56 ++++++++-------------------------------------- src/manimCell.ts | 49 +++++++++++++++++++++++----------------- src/previewCode.ts | 54 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 95 insertions(+), 69 deletions(-) diff --git a/package.json b/package.json index 850c8a8f..7ffd0f86 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,11 @@ "title": "Preview active Manim Cell", "category": "Manim Notebook" }, + { + "command": "manim-notebook.reloadAndPreviewManimCell", + "title": "Reload and Preview active Manim Cell", + "category": "Manim Notebook" + }, { "command": "manim-notebook.previewSelection", "title": "Preview selected Manim code", diff --git a/src/extension.ts b/src/extension.ts index fb883c8a..607ea8e8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,8 +2,7 @@ import * as vscode from 'vscode'; import { window } from 'vscode'; import { ManimShell, NoActiveShellError } from './manimShell'; import { ManimCell } from './manimCell'; -import { ManimCellRanges } from './manimCellRanges'; -import { previewCode } from './previewCode'; +import { previewManimCell, reloadAndPreviewManimCell, previewCode } from './previewCode'; import { startScene, exitScene } from './startStopScene'; import { Logger, Window, LogRecorder } from './logger'; @@ -17,6 +16,14 @@ export function activate(context: vscode.ExtensionContext) { previewManimCell(cellCode, startLine); }); + const reloadAndPreviewManimCellCommand = vscode.commands.registerCommand( + 'manim-notebook.reloadAndPreviewManimCell', + (cellCode?: string, startLine?: number) => { + Logger.info("💠 Command requested: Reload & Preview Manim Cell" + + `, startLine=${startLine}`); + reloadAndPreviewManimCell(cellCode, startLine); + }); + const previewSelectionCommand = vscode.commands.registerCommand( 'manim-notebook.previewSelection', () => { Logger.info("💠 Command requested: Preview Selection"); @@ -75,51 +82,6 @@ export function deactivate() { Logger.info("💠 Manim Notebook extension deactivated"); } -/** - * Previews all code inside of a Manim cell. - * - * A Manim cell starts with ## - * - * This can be invoked by either: - * - clicking the code lens (the button above the cell) -> this cell is previewed - * - command pallette -> the 1 cell where the cursor is is previewed - * - * If Manim isn't running, it will be automatically started - * (at the start of the cell which will be previewed: on its starting ## line), - * and then this cell is previewed. - */ -async function previewManimCell(cellCode?: string, startLine?: number) { - let startLineFinal: number | undefined = startLine; - - // User has executed the command via command pallette - if (cellCode === undefined) { - const editor = window.activeTextEditor; - if (!editor) { - Window.showErrorMessage( - 'No opened file found. Place your cursor in a Manim cell.'); - return; - } - const document = editor.document; - - // Get the code of the cell where the cursor is placed - const cursorLine = editor.selection.active.line; - const range = ManimCellRanges.getCellRangeAtLine(document, cursorLine); - if (!range) { - Window.showErrorMessage('Place your cursor in a Manim cell.'); - return; - } - cellCode = document.getText(range); - startLineFinal = range.start.line; - } - - if (startLineFinal === undefined) { - Window.showErrorMessage('Internal error: Line number not found in `previewManimCell()`.'); - return; - } - - await previewCode(cellCode, startLineFinal); -} - /** * Previews the selected code. * diff --git a/src/manimCell.ts b/src/manimCell.ts index 67562fbc..a020dc47 100644 --- a/src/manimCell.ts +++ b/src/manimCell.ts @@ -35,35 +35,42 @@ export class ManimCell implements vscode.CodeLensProvider, vscode.FoldingRangePr } public provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.CodeLens[] { + if (!window.activeTextEditor) { + return []; + } + const codeLenses: vscode.CodeLens[] = []; const ranges = ManimCellRanges.calculateRanges(document); - for (const range of ranges) { - codeLenses.push(new vscode.CodeLens(range)); + for (let range of ranges) { + range = new vscode.Range(range.start, range.end); + const codeLens = new vscode.CodeLens(range); + const codeLensReload = new vscode.CodeLens(range); + + const document = window.activeTextEditor.document; + const cellCode = document.getText(range); + + codeLens.command = { + title: "🥽 Preview Manim Cell", + command: "manim-notebook.previewManimCell", + tooltip: "Preview this Manim Cell inside an interactive Manim environment", + arguments: [cellCode, codeLens.range.start.line] + }; + + codeLensReload.command = { + title: "🥽 Reload & Preview", + command: "manim-notebook.reloadAndPreviewManimCell", + tooltip: "Preview this Manim Cell inside an interactive Manim environment", + arguments: [cellCode, codeLens.range.start.line] + }; + + codeLenses.push(codeLens); + codeLenses.push(codeLensReload); } return codeLenses; } - public resolveCodeLens(codeLens: vscode.CodeLens, token: vscode.CancellationToken): vscode.CodeLens { - if (!window.activeTextEditor) { - return codeLens; - } - - const document = window.activeTextEditor?.document; - const range = new vscode.Range(codeLens.range.start, codeLens.range.end); - const cellCode = document.getText(range); - - codeLens.command = { - title: "▶ Preview Manim Cell", - command: "manim-notebook.previewManimCell", - tooltip: "Preview this Manim Cell inside an interactive Manim environment", - arguments: [cellCode, codeLens.range.start.line] - }; - - return codeLens; - } - public provideFoldingRanges(document: vscode.TextDocument, context: vscode.FoldingContext, token: vscode.CancellationToken): vscode.FoldingRange[] { const ranges = ManimCellRanges.calculateRanges(document); return ranges.map(range => new vscode.FoldingRange(range.start.line, range.end.line)); diff --git a/src/previewCode.ts b/src/previewCode.ts index 0977ed38..4b243bc5 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -2,11 +2,63 @@ import * as vscode from 'vscode'; import { window } from 'vscode'; import { ManimShell } from './manimShell'; import { EventEmitter } from 'events'; -import { Logger } from './logger'; +import { ManimCellRanges } from './manimCellRanges'; +import { Logger, Window } from './logger'; // \x0C: is Ctrl + L, which clears the terminal screen const PREVIEW_COMMAND = `\x0Ccheckpoint_paste()`; +/** + * Previews all code inside of a Manim cell. + * + * A Manim cell starts with `##`. + * + * This can be invoked by either: + * - clicking the code lens (the button above the cell) -> this cell is previewed + * - command pallette -> the 1 cell where the cursor is is previewed + * + * If Manim isn't running, it will be automatically started + * (at the start of the cell which will be previewed: on its starting ## line), + * and then this cell is previewed. + */ +export async function previewManimCell(cellCode?: string, startLine?: number) { + let startLineFinal: number | undefined = startLine; + + // User has executed the command via command pallette + if (cellCode === undefined) { + const editor = window.activeTextEditor; + if (!editor) { + Window.showErrorMessage( + 'No opened file found. Place your cursor in a Manim cell.'); + return; + } + const document = editor.document; + + // Get the code of the cell where the cursor is placed + const cursorLine = editor.selection.active.line; + const range = ManimCellRanges.getCellRangeAtLine(document, cursorLine); + if (!range) { + Window.showErrorMessage('Place your cursor in a Manim cell.'); + return; + } + cellCode = document.getText(range); + startLineFinal = range.start.line; + } + + if (startLineFinal === undefined) { + Window.showErrorMessage('Internal error: Line number not found in `previewManimCell()`.'); + return; + } + + await previewCode(cellCode, startLineFinal); +} + +export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: number) { + Window.showWarningMessage('Reloading and previewing the Manim cell...'); + await previewManimCell(cellCode, startLine); +} + + /** * Interactively previews the given Manim code by means of the * `checkpoint_paste()` method from Manim. From 111141a0fb5b7752456acfcd7029c8982090a939 Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 5 Dec 2024 14:01:08 +0100 Subject: [PATCH 02/12] Use range directly --- src/manimCell.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/manimCell.ts b/src/manimCell.ts index a020dc47..ddae84ee 100644 --- a/src/manimCell.ts +++ b/src/manimCell.ts @@ -54,14 +54,14 @@ export class ManimCell implements vscode.CodeLensProvider, vscode.FoldingRangePr title: "🥽 Preview Manim Cell", command: "manim-notebook.previewManimCell", tooltip: "Preview this Manim Cell inside an interactive Manim environment", - arguments: [cellCode, codeLens.range.start.line] + arguments: [cellCode, range.start.line] }; codeLensReload.command = { title: "🥽 Reload & Preview", command: "manim-notebook.reloadAndPreviewManimCell", tooltip: "Preview this Manim Cell inside an interactive Manim environment", - arguments: [cellCode, codeLens.range.start.line] + arguments: [cellCode, range.start.line] }; codeLenses.push(codeLens); From 43671ecff92f3c00f8000de5ad2125c101e191aa Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 5 Dec 2024 14:01:35 +0100 Subject: [PATCH 03/12] Outsource preview cell args parsing --- src/manimShell.ts | 2 +- src/previewCode.ts | 63 +++++++++++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/manimShell.ts b/src/manimShell.ts index 5ea38886..c4e99621 100644 --- a/src/manimShell.ts +++ b/src/manimShell.ts @@ -503,7 +503,7 @@ export class ManimShell { * A shell that was previously used to run Manim, but has exited from the * Manim session (IPython environment), is considered inactive. */ - private hasActiveShell(): boolean { + public hasActiveShell(): boolean { const hasActiveShell = this.activeShell !== null && this.activeShell.exitStatus === undefined; Logger.debug(`👩‍💻 Has active shell?: ${hasActiveShell}`); diff --git a/src/previewCode.ts b/src/previewCode.ts index 4b243bc5..b9f5f92d 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -8,20 +8,7 @@ import { Logger, Window } from './logger'; // \x0C: is Ctrl + L, which clears the terminal screen const PREVIEW_COMMAND = `\x0Ccheckpoint_paste()`; -/** - * Previews all code inside of a Manim cell. - * - * A Manim cell starts with `##`. - * - * This can be invoked by either: - * - clicking the code lens (the button above the cell) -> this cell is previewed - * - command pallette -> the 1 cell where the cursor is is previewed - * - * If Manim isn't running, it will be automatically started - * (at the start of the cell which will be previewed: on its starting ## line), - * and then this cell is previewed. - */ -export async function previewManimCell(cellCode?: string, startLine?: number) { +function parsePreviewCellArgs(cellCode?: string, startLine?: number) { let startLineFinal: number | undefined = startLine; // User has executed the command via command pallette @@ -46,16 +33,56 @@ export async function previewManimCell(cellCode?: string, startLine?: number) { } if (startLineFinal === undefined) { - Window.showErrorMessage('Internal error: Line number not found in `previewManimCell()`.'); + Window.showErrorMessage( + 'Internal error: Line number not found in `parsePreviewCellArgs()`.'); + return; + } + + return { cellCode, startLineFinal }; +} + +/** + * Previews all code inside of a Manim cell. + * + * A Manim cell starts with `##`. + * + * This can be invoked by either: + * - clicking the code lens (the button above the cell) -> this cell is previewed + * - command pallette -> the 1 cell where the cursor is is previewed + * + * If Manim isn't running, it will be automatically started + * (at the start of the cell which will be previewed: on its starting ## line), + * and then this cell is previewed. + */ +export async function previewManimCell(cellCode?: string, startLine?: number) { + const res = parsePreviewCellArgs(cellCode, startLine); + if (!res) { return; } + const { cellCode: cellCodeFinal, startLineFinal } = res; - await previewCode(cellCode, startLineFinal); + await previewCode(cellCodeFinal, startLineFinal); } export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: number) { - Window.showWarningMessage('Reloading and previewing the Manim cell...'); - await previewManimCell(cellCode, startLine); + console.log("reloadAndPreviewManimCell"); + console.log(startLine); + + const res = parsePreviewCellArgs(cellCode, startLine); + if (!res) { + return; + } + const { cellCode: cellCodeFinal, startLineFinal } = res; + + if (ManimShell.instance.hasActiveShell()) { + const reloadCmd = `reload(${startLineFinal + 1})`; + await ManimShell.instance.executeCommandErrorOnNoActiveSession(reloadCmd); + } else { + Window.showWarningMessage("Not implemented yet"); // TODO + return; + } + + await previewManimCell(cellCodeFinal, startLineFinal); } From 35b53e4f436e0df22b8438700e9c8e0904576116 Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 5 Dec 2024 14:03:52 +0100 Subject: [PATCH 04/12] Rename variables "final" to "parsed" --- src/previewCode.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/previewCode.ts b/src/previewCode.ts index b9f5f92d..bd6126a7 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -9,7 +9,7 @@ import { Logger, Window } from './logger'; const PREVIEW_COMMAND = `\x0Ccheckpoint_paste()`; function parsePreviewCellArgs(cellCode?: string, startLine?: number) { - let startLineFinal: number | undefined = startLine; + let startLineParsed: number | undefined = startLine; // User has executed the command via command pallette if (cellCode === undefined) { @@ -29,16 +29,16 @@ function parsePreviewCellArgs(cellCode?: string, startLine?: number) { return; } cellCode = document.getText(range); - startLineFinal = range.start.line; + startLineParsed = range.start.line; } - if (startLineFinal === undefined) { + if (startLineParsed === undefined) { Window.showErrorMessage( 'Internal error: Line number not found in `parsePreviewCellArgs()`.'); return; } - return { cellCode, startLineFinal }; + return { cellCodeParsed: cellCode, startLineParsed }; } /** @@ -59,9 +59,9 @@ export async function previewManimCell(cellCode?: string, startLine?: number) { if (!res) { return; } - const { cellCode: cellCodeFinal, startLineFinal } = res; + const { cellCodeParsed, startLineParsed } = res; - await previewCode(cellCodeFinal, startLineFinal); + await previewCode(cellCodeParsed, startLineParsed); } export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: number) { @@ -72,17 +72,17 @@ export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: n if (!res) { return; } - const { cellCode: cellCodeFinal, startLineFinal } = res; + const { cellCodeParsed, startLineParsed } = res; if (ManimShell.instance.hasActiveShell()) { - const reloadCmd = `reload(${startLineFinal + 1})`; + const reloadCmd = `reload(${startLineParsed + 1})`; await ManimShell.instance.executeCommandErrorOnNoActiveSession(reloadCmd); } else { Window.showWarningMessage("Not implemented yet"); // TODO return; } - await previewManimCell(cellCodeFinal, startLineFinal); + await previewManimCell(cellCodeParsed, startLineParsed); } From aa189aedb6f3872b6ca5f5f0f4bd2dfe57cef98d Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 5 Dec 2024 14:06:33 +0100 Subject: [PATCH 05/12] Remove unnecessary else branch --- src/previewCode.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/previewCode.ts b/src/previewCode.ts index bd6126a7..38083dfe 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -77,11 +77,7 @@ export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: n if (ManimShell.instance.hasActiveShell()) { const reloadCmd = `reload(${startLineParsed + 1})`; await ManimShell.instance.executeCommandErrorOnNoActiveSession(reloadCmd); - } else { - Window.showWarningMessage("Not implemented yet"); // TODO - return; } - await previewManimCell(cellCodeParsed, startLineParsed); } From 2a14fd93835890363cad019fdfd9cde0056732f4 Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 5 Dec 2024 14:08:08 +0100 Subject: [PATCH 06/12] Improve codelens title & text --- src/manimCell.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/manimCell.ts b/src/manimCell.ts index ddae84ee..97e93bf1 100644 --- a/src/manimCell.ts +++ b/src/manimCell.ts @@ -51,16 +51,16 @@ export class ManimCell implements vscode.CodeLensProvider, vscode.FoldingRangePr const cellCode = document.getText(range); codeLens.command = { - title: "🥽 Preview Manim Cell", + title: "Preview Manim Cell", command: "manim-notebook.previewManimCell", - tooltip: "Preview this Manim Cell inside an interactive Manim environment", + tooltip: "Preview this Manim Cell", arguments: [cellCode, range.start.line] }; codeLensReload.command = { - title: "🥽 Reload & Preview", + title: "Reload & Preview", command: "manim-notebook.reloadAndPreviewManimCell", - tooltip: "Preview this Manim Cell inside an interactive Manim environment", + tooltip: "Reload & Preview this Manim Cell", arguments: [cellCode, range.start.line] }; From c86551e361da487b4e312a5e9dfacd7cc69e650c Mon Sep 17 00:00:00 2001 From: Splines Date: Wed, 11 Dec 2024 12:51:04 +0100 Subject: [PATCH 07/12] Remove unwanted console printouts --- src/previewCode.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/previewCode.ts b/src/previewCode.ts index 38083dfe..8b8cdd2b 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -65,9 +65,6 @@ export async function previewManimCell(cellCode?: string, startLine?: number) { } export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: number) { - console.log("reloadAndPreviewManimCell"); - console.log(startLine); - const res = parsePreviewCellArgs(cellCode, startLine); if (!res) { return; From fc61bd94e9bd0736f0642df3ab7676b202c4f42f Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 12 Dec 2024 12:03:18 +0100 Subject: [PATCH 08/12] Replace wrong import --- src/previewCode.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/previewCode.ts b/src/previewCode.ts index 8b8cdd2b..5a901432 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -2,7 +2,7 @@ import * as vscode from 'vscode'; import { window } from 'vscode'; import { ManimShell } from './manimShell'; import { EventEmitter } from 'events'; -import { ManimCellRanges } from './manimCellRanges'; +import { ManimCellRanges } from './pythonParsing'; import { Logger, Window } from './logger'; // \x0C: is Ctrl + L, which clears the terminal screen From e04577770fe0bb353167f8ab2195981b679c7f32 Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 12 Dec 2024 20:26:58 +0100 Subject: [PATCH 09/12] Fix waiting for IPython reload (add new case) --- src/manimShell.ts | 35 +++++++++++++++++++++++++++++------ src/previewCode.ts | 3 ++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/manimShell.ts b/src/manimShell.ts index 203c84db..eb54a6e1 100644 --- a/src/manimShell.ts +++ b/src/manimShell.ts @@ -152,6 +152,11 @@ export class ManimShell { */ private iPythonCellCount: number = 0; + /** + * TODO + */ + waitForRestartedIPythonInstance = false; + /** * Whether the execution of a new command is locked. This is used to prevent * multiple new scenes from being started at the same time, e.g. when users @@ -199,6 +204,11 @@ export class ManimShell { return ManimShell.#instance; } + public nextTimeWaitForRestartedIPythonInstance() { + this.iPythonCellCount = 0; + this.waitForRestartedIPythonInstance = true; + } + /** * Executes the given command. If no active terminal running Manim is found, * a new terminal is spawned, and a new Manim session is started in it @@ -688,14 +698,27 @@ export class ManimShell { let ipythonMatches = data.match(IPYTHON_CELL_START_REGEX); if (ipythonMatches) { - // Terminal data might include multiple IPython statements, - // so take the highest cell number found. + // Terminal data might include multiple IPython statements const cellNumbers = ipythonMatches.map( match => parseInt(match.match(/\d+/)![0])); - const maxCellNumber = Math.max(...cellNumbers); - this.iPythonCellCount = maxCellNumber; - Logger.debug(`📦 IPython cell ${maxCellNumber} detected`); - this.eventEmitter.emit(ManimShellEvent.IPYTHON_CELL_FINISHED); + + if (this.waitForRestartedIPythonInstance) { + const cellNumber = Math.min(...cellNumbers); + Logger.debug("📦 While waiting for restarted IPython instance:" + + ` cell ${cellNumber} detected`); + if (cellNumber === 1) { + Logger.debug("🔄 Restarted IPython instance detected"); + this.iPythonCellCount = 1; + this.waitForRestartedIPythonInstance = false; + this.eventEmitter.emit(ManimShellEvent.IPYTHON_CELL_FINISHED); + } + } else { + // more frequent case + const cellNumber = Math.max(...cellNumbers); + this.iPythonCellCount = cellNumber; + Logger.debug(`📦 IPython cell ${cellNumber} detected`); + this.eventEmitter.emit(ManimShellEvent.IPYTHON_CELL_FINISHED); + } } if (this.isExecutingCommand && data.match(IPYTHON_MULTILINE_START_REGEX)) { diff --git a/src/previewCode.ts b/src/previewCode.ts index 5a901432..a170e625 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -73,7 +73,8 @@ export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: n if (ManimShell.instance.hasActiveShell()) { const reloadCmd = `reload(${startLineParsed + 1})`; - await ManimShell.instance.executeCommandErrorOnNoActiveSession(reloadCmd); + ManimShell.instance.nextTimeWaitForRestartedIPythonInstance(); + await ManimShell.instance.executeCommandErrorOnNoActiveSession(reloadCmd, true); } await previewManimCell(cellCodeParsed, startLineParsed); } From 881f436a02410319df1a59db7529b9963418c888 Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 12 Dec 2024 20:29:28 +0100 Subject: [PATCH 10/12] Add docstrings for newly added method and flag --- src/manimShell.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/manimShell.ts b/src/manimShell.ts index eb54a6e1..63842857 100644 --- a/src/manimShell.ts +++ b/src/manimShell.ts @@ -153,7 +153,8 @@ export class ManimShell { private iPythonCellCount: number = 0; /** - * TODO + * Whether to wait for a restarted IPython instance, i.e. for an IPython + * cell count of 1. This is used to correctly issue the `reload()` command. */ waitForRestartedIPythonInstance = false; @@ -204,6 +205,11 @@ export class ManimShell { return ManimShell.#instance; } + /** + * Indicates that the next command should wait until a restarted IPython + * instance is detected, i.e. starting with cell 1 again. This is used + * such that we can correctly issue the `reload()` command. + */ public nextTimeWaitForRestartedIPythonInstance() { this.iPythonCellCount = 0; this.waitForRestartedIPythonInstance = true; From 566b75dc7e0d488cabb96438fbd3e91f1b7560f1 Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 12 Dec 2024 21:25:17 +0100 Subject: [PATCH 11/12] Check for locks during reload --- src/manimShell.ts | 55 +++++++++++++++++++++++++++++++--------------- src/previewCode.ts | 2 +- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/manimShell.ts b/src/manimShell.ts index 63842857..baec3f2d 100644 --- a/src/manimShell.ts +++ b/src/manimShell.ts @@ -210,7 +210,11 @@ export class ManimShell { * instance is detected, i.e. starting with cell 1 again. This is used * such that we can correctly issue the `reload()` command. */ - public nextTimeWaitForRestartedIPythonInstance() { + public async nextTimeWaitForRestartedIPythonInstance() { + if (await this.isLocked()) { + return; + } + this.iPythonCellCount = 0; this.waitForRestartedIPythonInstance = true; } @@ -247,6 +251,36 @@ export class ManimShell { command, waitUntilFinished, forceExecute, true, undefined, undefined); } + /** + * Returns whether the command execution is currently locked, i.e. when + * Manim is starting up or another command is currently running. + * + * @param forceExecute see `execCommand()` + * @returns true if the command execution is locked, false otherwise. + */ + private async isLocked(forceExecute = false): Promise { + if (this.lockDuringStartup) { + Window.showWarningMessage("Manim is currently starting. Please wait a moment."); + return true; + } + + if (this.isExecutingCommand) { + // MacOS specific behavior + if (this.shouldLockDuringCommandExecution && !forceExecute) { + Window.showWarningMessage( + `Simultaneous Manim commands are not currently supported on MacOS. ` + + `Please wait for the current operations to finish before initiating ` + + `a new command.`); + return true; + } + + this.sendKeyboardInterrupt(); + await new Promise(resolve => setTimeout(resolve, 500)); + } + + return false; + } + /** * Executes a given command and bundles many different behaviors and options. * @@ -292,27 +326,12 @@ export class ManimShell { return; } - if (this.lockDuringStartup) { - Window.showWarningMessage("Manim is currently starting. Please wait a moment."); - return; - } - if (errorOnNoActiveShell) { this.errorOnNoActiveShell(); } - if (this.isExecutingCommand) { - // MacOS specific behavior - if (this.shouldLockDuringCommandExecution && !forceExecute) { - Window.showWarningMessage( - `Simultaneous Manim commands are not currently supported on MacOS. ` - + `Please wait for the current operations to finish before initiating ` - + `a new command.`); - return; - } - - this.sendKeyboardInterrupt(); - await new Promise(resolve => setTimeout(resolve, 500)); + if (await this.isLocked(forceExecute)) { + return; } this.isExecutingCommand = true; diff --git a/src/previewCode.ts b/src/previewCode.ts index a170e625..5d59735f 100644 --- a/src/previewCode.ts +++ b/src/previewCode.ts @@ -73,7 +73,7 @@ export async function reloadAndPreviewManimCell(cellCode?: string, startLine?: n if (ManimShell.instance.hasActiveShell()) { const reloadCmd = `reload(${startLineParsed + 1})`; - ManimShell.instance.nextTimeWaitForRestartedIPythonInstance(); + await ManimShell.instance.nextTimeWaitForRestartedIPythonInstance(); await ManimShell.instance.executeCommandErrorOnNoActiveSession(reloadCmd, true); } await previewManimCell(cellCodeParsed, startLineParsed); From f69518ebefde7298150352f9ea3d183bdfe65d4a Mon Sep 17 00:00:00 2001 From: Splines Date: Thu, 12 Dec 2024 23:10:39 +0100 Subject: [PATCH 12/12] Clarify docstrings on restarted ipython instance --- src/manimShell.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/manimShell.ts b/src/manimShell.ts index baec3f2d..15e6789c 100644 --- a/src/manimShell.ts +++ b/src/manimShell.ts @@ -154,7 +154,8 @@ export class ManimShell { /** * Whether to wait for a restarted IPython instance, i.e. for an IPython - * cell count of 1. This is used to correctly issue the `reload()` command. + * cell count of 1. This is set to `true` before the `reload()` command is + * issued and set back to `false` after the IPython cell count is 1. */ waitForRestartedIPythonInstance = false; @@ -207,8 +208,8 @@ export class ManimShell { /** * Indicates that the next command should wait until a restarted IPython - * instance is detected, i.e. starting with cell 1 again. This is used - * such that we can correctly issue the `reload()` command. + * instance is detected, i.e. starting with cell 1 again. This should be + * called before the `reload()` command is issued. */ public async nextTimeWaitForRestartedIPythonInstance() { if (await this.isLocked()) {