@@ -24,7 +24,7 @@ import { isNbCommandRegistered } from '../commands/utils';
2424import { nbCommands } from '../commands/commands' ;
2525import { createErrorOutput , createOutputItem } from './utils' ;
2626import { NotebookCellExecutionResult } from '../lsp/protocol' ;
27- import { NotebookCell , NotebookController , NotebookDocument , Disposable , notebooks , commands , NotebookCellOutput } from 'vscode' ;
27+ import { NotebookCell , NotebookController , NotebookDocument , Disposable , notebooks , commands , NotebookCellOutput , workspace } from 'vscode' ;
2828import { LanguageClient } from 'vscode-languageclient/node' ;
2929import { l10n } from '../localiser' ;
3030import { ijnbConstants , ipynbConstants , supportLanguages } from './constants' ;
@@ -86,6 +86,7 @@ export class IJNBKernel implements Disposable {
8686 const cellId = cell . document . uri . toString ( ) ;
8787 const sourceCode = cell . document . getText ( ) ;
8888 const codeCellExecution = new CodeCellExecution ( controller . id , notebookId , cell ) ;
89+ this . getIncementedExecutionCounter ( notebookId ) ;
8990 try {
9091 this . cellControllerIdMap . set ( cellId , codeCellExecution ) ;
9192 const client : LanguageClient = await globalState . getClientPromise ( ) . client ;
@@ -141,15 +142,15 @@ export class IJNBKernel implements Disposable {
141142
142143 private handleUnkownLanguageTypeExecution = async ( notebookId : string , cell : NotebookCell , controller : NotebookController ) => {
143144 const exec = controller . createNotebookCellExecution ( cell ) ;
144- exec . executionOrder = this . getExecutionCounterAndIncrement ( notebookId ) ;
145+ exec . executionOrder = this . getIncementedExecutionCounter ( notebookId ) ;
145146 exec . start ( Date . now ( ) ) ;
146147 await exec . replaceOutput ( createErrorOutput ( new Error ( `Doesn't support ${ cell . document . languageId } execution` ) ) ) ;
147- exec . end ( true , Date . now ( ) ) ;
148+ exec . end ( false , Date . now ( ) ) ;
148149 }
149150
150- private getExecutionCounterAndIncrement = ( notebookId : string ) => {
151- const next = IJNBKernel . executionCounter . get ( notebookId ) ?? 1 ;
152- IJNBKernel . executionCounter . set ( notebookId , next + 1 ) ;
151+ private getIncementedExecutionCounter = ( notebookId : string ) => {
152+ const next = ( IJNBKernel . executionCounter . get ( notebookId ) ?? 0 ) + 1 ;
153+ IJNBKernel . executionCounter . set ( notebookId , next ) ;
153154 return next ;
154155 }
155156
@@ -160,18 +161,24 @@ export class IJNBKernel implements Disposable {
160161 private handleMarkdownCellExecution = async ( notebookId : string , cell : NotebookCell , controller : NotebookController ) => {
161162 const exec = controller . createNotebookCellExecution ( cell ) ;
162163 const mimeType = 'text/markdown' ;
163- exec . executionOrder = this . getExecutionCounterAndIncrement ( notebookId ) ;
164+ exec . executionOrder = this . getIncementedExecutionCounter ( notebookId ) ;
164165 try {
165166 exec . start ( Date . now ( ) ) ;
166167 await exec . replaceOutput ( [
167168 new NotebookCellOutput ( [ createOutputItem ( cell . document . getText ( ) , mimeType ) ] ) ,
168169 ] ) ;
170+ exec . end ( true , Date . now ( ) ) ;
169171 } catch ( error ) {
170172 await exec . replaceOutput ( createErrorOutput ( error as Error ) ) ;
171- } finally {
172- exec . end ( true , Date . now ( ) ) ;
173+ exec . end ( false , Date . now ( ) ) ;
173174 }
174175 }
176+
177+ cleanUpKernel = workspace . onDidCloseNotebookDocument ( doc => {
178+ if ( doc . notebookType === ijnbConstants . NOTEBOOK_TYPE ) {
179+ IJNBKernel . executionCounter . delete ( doc . uri . toString ( ) ) ;
180+ }
181+ } ) ;
175182}
176183
177184export const notebookKernel = new IJNBKernel ( ) ;
0 commit comments