Skip to content

Commit 44acce0

Browse files
subhash-arabhiAchal1607
authored andcommitted
Corrected execution counter
1 parent 02ba95e commit 44acce0

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

vscode/src/notebooks/kernel.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { isNbCommandRegistered } from '../commands/utils';
2424
import { nbCommands } from '../commands/commands';
2525
import { createErrorOutput, createOutputItem } from './utils';
2626
import { 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';
2828
import { LanguageClient } from 'vscode-languageclient/node';
2929
import { l10n } from '../localiser';
3030
import { 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

177184
export const notebookKernel = new IJNBKernel();

vscode/src/notebooks/register.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ export const registerNotebookProviders = (context: ExtensionContext) => {
3131
));
3232

3333
context.subscriptions.push(notebookKernel);
34+
context.subscriptions.push(notebookKernel.cleanUpKernel);
3435
}

vscode/src/notebooks/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
IMetadata
3232
} from './types';
3333
import { randomUUID } from 'crypto';
34-
import { isError, isString } from '../utils';
34+
import { isString } from '../utils';
3535
import { mimeTypes } from './constants';
3636

3737
export function base64ToUint8Array(base64: string): Uint8Array {

0 commit comments

Comments
 (0)