From f706f4c07db74a800546b261cf795961e77eefee Mon Sep 17 00:00:00 2001 From: mike-tasca Date: Sun, 3 Mar 2024 15:08:32 +0100 Subject: [PATCH] FIX: Detect when a Notebook was closed during execution --- commands.txt | 16 +++++++++++++++- src/extension.ts | 39 +++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/commands.txt b/commands.txt index a35ba48..d6d2973 100644 --- a/commands.txt +++ b/commands.txt @@ -71,4 +71,18 @@ fi # chmod it: chmod +x ./scriptt.sh # run it: -./scriptt.sh \ No newline at end of file +./scriptt.sh + + + + + +#######################TODO: + + - Check why SyntaxErrors Range dont ALWAYS rebuild (ongoing) + - DONE Fix bug: when you close terminal on execution_explicit (or implicit), it gets stuck + - Fix bug: "with f: open" syntax propagates weirdly (it should NOT count the f as Output ...) + - Feature: Setting to remove Codelenses + - Feature: Less bright colors + - Feature: Color less often while writing (this is complicated...) + - Small typo in Readme \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 680955c..2aea5f6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -107,12 +107,18 @@ async function executeCodeInKernel(code: string, kernel: Kernel, output_channel: return result; } +enum IWExecutionResult { + Succeeded = 'Succeeded', + Failed = 'Failed', + NotebookClodsed = 'NotebookClodsed' +} + async function executeCodeInInteractiveWindow( text: string, notebook: NotebookDocument, output: OutputChannel | null, -): Promise { - // Currently returns True or False, depending if the code was executed successfully or not. TODO: Rethink if you need something more... +): Promise { + // Currently returns an IWExecutionResult. TODO: Rethink if you need something more... await vscode.commands.executeCommand('jupyter.execSelectionInteractive', text); // OTHER THINGS I TRIED: @@ -136,27 +142,30 @@ async function executeCodeInInteractiveWindow( if (!cell) { window.showErrorMessage('Reactive Jupyter: Failed to execute the code in the Interactive Window: No matching cell was identified'); - return false; + return IWExecutionResult.NotebookClodsed; } let cell_state = CellState.Undefined; await new Promise((resolve) => setTimeout(resolve, 250)); for (let i = 0; i > -1; i++) { + if (!notebook || notebook.isClosed || cell.notebook.isClosed) { + return IWExecutionResult.NotebookClodsed; + } cell_state = getCellState(cell); if (cell_state === CellState.Success) { if (has_error_mime(cell)) { - return false; + return IWExecutionResult.Failed; } - return true; + return IWExecutionResult.Succeeded; } else if (cell_state === CellState.Error) { - return false; + return IWExecutionResult.Failed; } await new Promise((resolve) => setTimeout(resolve, 250)); } window.showErrorMessage('Reactive Jupyter: Failed to execute the code in the Interactive Window: The cell did not finish executing'); - return false; + return IWExecutionResult.Failed; } @@ -235,7 +244,11 @@ async function safeExecuteCodeInInteractiveWindow( let [notebook, kernel] = notebookAndKernel; updateKernelState(globals, editor, KernelState.explicit_execution_started); const result = await executeCodeInInteractiveWindow(command, notebook, output); - if (return_to_initial_state) { updateKernelState(globals, editor, expected_initial_state); } + if (result == IWExecutionResult.NotebookClodsed) { + window.showErrorMessage("Reactive Jupyter: Lost Connection to this editor's Notebook. Please initialize the extension with the command 'Initialize Reactive Jupyter' or the CodeLens at the top"); + updateKernelState(globals, editor, KernelState.initializable_messaged); + } + else if (return_to_initial_state) { updateKernelState(globals, editor, expected_initial_state); } return result; } @@ -270,7 +283,7 @@ async function queueComputation( } let res = await safeExecuteCodeInInteractiveWindow(range.text, activeTextEditor, output, globals); - if (!res) { break; } + if ( res != IWExecutionResult.Succeeded ) { break; } const update_result = await safeExecuteCodeInKernel(getSyncRangeCommand(range), activeTextEditor, output, globals); if (!update_result) { @@ -289,11 +302,9 @@ async function queueComputation( } } const update_result = await safeExecuteCodeInKernel(getUnlockCommand(), activeTextEditor, output, globals); - if (!update_result) { + if (getKernelState(globals, activeTextEditor) == KernelState.extension_available && !update_result) { vscode.window.showErrorMessage('Reactive Jupyter: Failed to unlock the Python kernel: ' + update_result); } - else { - } } @@ -416,8 +427,8 @@ const kernelStateTransitions: Map = new Map([ [KernelState.kernel_available, [KernelState.instantialization_started, KernelState.extension_available].concat(kernelIinitialStates)], [KernelState.instantialization_started, [KernelState.extension_available].concat(kernelIinitialStates)], [KernelState.extension_available, [KernelState.explicit_execution_started, KernelState.implicit_execution_started].concat(kernelIinitialStates)], - [KernelState.explicit_execution_started, [KernelState.extension_available]], - [KernelState.implicit_execution_started, [KernelState.extension_available]], + [KernelState.explicit_execution_started, [KernelState.extension_available].concat(kernelIinitialStates)], + [KernelState.implicit_execution_started, [KernelState.extension_available].concat(kernelIinitialStates)], ]); function updateKernelState(globals: Map, editor: TextEditor, newState_: string) {