@@ -7,13 +7,15 @@ import { Emitter } from 'vs/base/common/event';
7
7
import { combinedDisposable , Disposable , IDisposable } from 'vs/base/common/lifecycle' ;
8
8
import { ResourceMap } from 'vs/base/common/map' ;
9
9
import { isEqual } from 'vs/base/common/resources' ;
10
+ import { withNullAsUndefined } from 'vs/base/common/types' ;
10
11
import { URI } from 'vs/base/common/uri' ;
11
12
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
12
13
import { ILogService } from 'vs/platform/log/common/log' ;
13
14
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel' ;
14
15
import { CellEditType , CellUri , ICellEditOperation , NotebookCellExecutionState , NotebookCellInternalMetadata , NotebookTextModelWillAddRemoveEvent } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
15
16
import { CellExecutionUpdateType , INotebookExecutionService } from 'vs/workbench/contrib/notebook/common/notebookExecutionService' ;
16
17
import { ICellExecuteUpdate , ICellExecutionComplete , ICellExecutionStateChangedEvent , ICellExecutionStateUpdate , IFailedCellInfo , INotebookCellExecution , INotebookExecutionStateService , INotebookFailStateChangedEvent } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService' ;
18
+ import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService' ;
17
19
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService' ;
18
20
19
21
export class NotebookExecutionStateService extends Disposable implements INotebookExecutionStateService {
@@ -68,11 +70,16 @@ export class NotebookExecutionStateService extends Disposable implements INotebo
68
70
return undefined ;
69
71
}
70
72
71
- getCellExecutionStatesForNotebook ( notebook : URI ) : INotebookCellExecution [ ] {
73
+ getCellExecutionsForNotebook ( notebook : URI ) : INotebookCellExecution [ ] {
72
74
const exeMap = this . _executions . get ( notebook ) ;
73
75
return exeMap ? Array . from ( exeMap . values ( ) ) : [ ] ;
74
76
}
75
77
78
+ getCellExecutionsByHandleForNotebook ( notebook : URI ) : Map < number , INotebookCellExecution > | undefined {
79
+ const exeMap = this . _executions . get ( notebook ) ;
80
+ return withNullAsUndefined ( exeMap ) ;
81
+ }
82
+
76
83
private _onCellExecutionDidChange ( notebookUri : URI , cellHandle : number , exe : CellExecution ) : void {
77
84
this . _onDidChangeCellExecution . fire ( new NotebookExecutionEvent ( notebookUri , cellHandle , exe ) ) ;
78
85
}
@@ -244,6 +251,7 @@ class NotebookExecutionListeners extends Disposable {
244
251
constructor (
245
252
notebook : URI ,
246
253
@INotebookService private readonly _notebookService : INotebookService ,
254
+ @INotebookKernelService private readonly _notebookKernelService : INotebookKernelService ,
247
255
@INotebookExecutionService private readonly _notebookExecutionService : INotebookExecutionService ,
248
256
@INotebookExecutionStateService private readonly _notebookExecutionStateService : INotebookExecutionStateService ,
249
257
@ILogService private readonly _logService : ILogService ,
@@ -263,7 +271,7 @@ class NotebookExecutionListeners extends Disposable {
263
271
264
272
private cancelAll ( ) : void {
265
273
this . _logService . debug ( `NotebookExecutionListeners#cancelAll` ) ;
266
- const exes = this . _notebookExecutionStateService . getCellExecutionStatesForNotebook ( this . _notebookModel . uri ) ;
274
+ const exes = this . _notebookExecutionStateService . getCellExecutionsForNotebook ( this . _notebookModel . uri ) ;
267
275
this . _notebookExecutionService . cancelNotebookCellHandles ( this . _notebookModel , exes . map ( exe => exe . cellHandle ) ) ;
268
276
}
269
277
@@ -273,25 +281,34 @@ class NotebookExecutionListeners extends Disposable {
273
281
}
274
282
275
283
private onWillAddRemoveCells ( e : NotebookTextModelWillAddRemoveEvent ) : void {
276
- const notebookExes = this . _notebookExecutionStateService . getCellExecutionStatesForNotebook ( this . _notebookModel . uri ) ;
277
- const handles = new Set ( notebookExes . map ( exe => exe . cellHandle ) ) ;
278
- const myDeletedHandles = new Set < number > ( ) ;
279
- e . rawEvent . changes . forEach ( ( [ start , deleteCount ] ) => {
280
- if ( deleteCount ) {
281
- const deletedHandles = this . _notebookModel . cells . slice ( start , start + deleteCount ) . map ( c => c . handle ) ;
282
- deletedHandles . forEach ( h => {
283
- if ( handles . has ( h ) ) {
284
- myDeletedHandles . add ( h ) ;
285
- }
286
- } ) ;
287
- }
288
-
289
- return false ;
290
- } ) ;
284
+ const notebookExes = this . _notebookExecutionStateService . getCellExecutionsByHandleForNotebook ( this . _notebookModel . uri ) ;
285
+
286
+ const executingDeletedHandles = new Set < number > ( ) ;
287
+ const pendingDeletedHandles = new Set < number > ( ) ;
288
+ if ( notebookExes ) {
289
+ e . rawEvent . changes . forEach ( ( [ start , deleteCount ] ) => {
290
+ if ( deleteCount ) {
291
+ const deletedHandles = this . _notebookModel . cells . slice ( start , start + deleteCount ) . map ( c => c . handle ) ;
292
+ deletedHandles . forEach ( h => {
293
+ const exe = notebookExes . get ( h ) ;
294
+ if ( exe ?. state === NotebookCellExecutionState . Executing ) {
295
+ executingDeletedHandles . add ( h ) ;
296
+ } else if ( exe ) {
297
+ pendingDeletedHandles . add ( h ) ;
298
+ }
299
+ } ) ;
300
+ }
301
+ } ) ;
302
+ }
291
303
292
- if ( myDeletedHandles . size ) {
293
- this . _logService . debug ( `NotebookExecution#onWillAddRemoveCells, ${ JSON . stringify ( [ ...myDeletedHandles ] ) } ` ) ;
294
- this . _notebookExecutionService . cancelNotebookCellHandles ( this . _notebookModel , myDeletedHandles ) ;
304
+ if ( executingDeletedHandles . size || pendingDeletedHandles . size ) {
305
+ const kernel = this . _notebookKernelService . getSelectedOrSuggestedKernel ( this . _notebookModel ) ;
306
+ if ( kernel ) {
307
+ const implementsInterrupt = kernel . implementsInterrupt ;
308
+ const handlesToCancel = implementsInterrupt ? [ ...executingDeletedHandles ] : [ ...executingDeletedHandles , ...pendingDeletedHandles ] ;
309
+ this . _logService . debug ( `NotebookExecution#onWillAddRemoveCells, ${ JSON . stringify ( [ ...handlesToCancel ] ) } ` ) ;
310
+ kernel . cancelNotebookCellExecution ( this . _notebookModel . uri , handlesToCancel ) ;
311
+ }
295
312
}
296
313
}
297
314
}
0 commit comments