@theia/notebook 1.53.0-next.4 → 1.53.0-next.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/contributions/cell-operations.d.ts.map +1 -1
  3. package/lib/browser/contributions/cell-operations.js +8 -1
  4. package/lib/browser/contributions/cell-operations.js.map +1 -1
  5. package/lib/browser/contributions/notebook-actions-contribution.d.ts +1 -0
  6. package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -1
  7. package/lib/browser/contributions/notebook-actions-contribution.js +31 -6
  8. package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -1
  9. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +4 -0
  10. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -1
  11. package/lib/browser/contributions/notebook-cell-actions-contribution.js +49 -12
  12. package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -1
  13. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts +14 -0
  14. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts.map +1 -0
  15. package/lib/browser/contributions/notebook-status-bar-contribution.js +75 -0
  16. package/lib/browser/contributions/notebook-status-bar-contribution.js.map +1 -0
  17. package/lib/browser/notebook-editor-widget.d.ts.map +1 -1
  18. package/lib/browser/notebook-editor-widget.js +3 -5
  19. package/lib/browser/notebook-editor-widget.js.map +1 -1
  20. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  21. package/lib/browser/notebook-frontend-module.js +3 -0
  22. package/lib/browser/notebook-frontend-module.js.map +1 -1
  23. package/lib/browser/notebook-open-handler.d.ts +3 -2
  24. package/lib/browser/notebook-open-handler.d.ts.map +1 -1
  25. package/lib/browser/notebook-open-handler.js +12 -5
  26. package/lib/browser/notebook-open-handler.js.map +1 -1
  27. package/lib/browser/service/notebook-options.d.ts +1 -0
  28. package/lib/browser/service/notebook-options.d.ts.map +1 -1
  29. package/lib/browser/service/notebook-options.js +1 -0
  30. package/lib/browser/service/notebook-options.js.map +1 -1
  31. package/lib/browser/service/notebook-service.d.ts +1 -0
  32. package/lib/browser/service/notebook-service.d.ts.map +1 -1
  33. package/lib/browser/service/notebook-service.js +7 -0
  34. package/lib/browser/service/notebook-service.js.map +1 -1
  35. package/lib/browser/view/notebook-cell-editor.d.ts +1 -0
  36. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  37. package/lib/browser/view/notebook-cell-editor.js +30 -16
  38. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  39. package/lib/browser/view/notebook-cell-list-view.d.ts +7 -4
  40. package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -1
  41. package/lib/browser/view/notebook-cell-list-view.js +39 -29
  42. package/lib/browser/view/notebook-cell-list-view.js.map +1 -1
  43. package/lib/browser/view-model/notebook-cell-model.d.ts +3 -0
  44. package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -1
  45. package/lib/browser/view-model/notebook-cell-model.js +5 -0
  46. package/lib/browser/view-model/notebook-cell-model.js.map +1 -1
  47. package/package.json +8 -8
  48. package/src/browser/contributions/cell-operations.ts +44 -39
  49. package/src/browser/contributions/notebook-actions-contribution.ts +379 -351
  50. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -485
  51. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  52. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  53. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  54. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  55. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  56. package/src/browser/contributions/notebook-preferences.ts +92 -92
  57. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -0
  58. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  59. package/src/browser/index.ts +27 -27
  60. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  61. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  62. package/src/browser/notebook-editor-widget.tsx +330 -331
  63. package/src/browser/notebook-frontend-module.ts +119 -115
  64. package/src/browser/notebook-open-handler.ts +120 -114
  65. package/src/browser/notebook-output-utils.ts +119 -119
  66. package/src/browser/notebook-renderer-registry.ts +85 -85
  67. package/src/browser/notebook-type-registry.ts +54 -54
  68. package/src/browser/notebook-types.ts +186 -186
  69. package/src/browser/renderers/cell-output-webview.ts +33 -33
  70. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  71. package/src/browser/service/notebook-context-manager.ts +162 -162
  72. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  73. package/src/browser/service/notebook-execution-service.ts +139 -139
  74. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  75. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  76. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  77. package/src/browser/service/notebook-kernel-service.ts +357 -357
  78. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  79. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  80. package/src/browser/service/notebook-options.ts +155 -154
  81. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  82. package/src/browser/service/notebook-service.ts +215 -209
  83. package/src/browser/style/index.css +483 -467
  84. package/src/browser/view/notebook-cell-editor.tsx +263 -247
  85. package/src/browser/view/notebook-cell-list-view.tsx +279 -259
  86. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  87. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  88. package/src/browser/view/notebook-code-cell-view.tsx +350 -350
  89. package/src/browser/view/notebook-find-widget.tsx +335 -335
  90. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  91. package/src/browser/view/notebook-markdown-cell-view.tsx +208 -208
  92. package/src/browser/view/notebook-viewport-service.ts +61 -61
  93. package/src/browser/view-model/notebook-cell-model.ts +473 -466
  94. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  95. package/src/browser/view-model/notebook-model.ts +550 -550
  96. package/src/common/index.ts +18 -18
  97. package/src/common/notebook-common.ts +337 -337
  98. package/src/common/notebook-protocol.ts +35 -35
  99. package/src/common/notebook-range.ts +30 -30
@@ -1,311 +1,311 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2023 TypeFox and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
- /*---------------------------------------------------------------------------------------------
17
- * Copyright (c) Microsoft Corporation. All rights reserved.
18
- * Licensed under the MIT License. See License.txt in the project root for license information.
19
- *--------------------------------------------------------------------------------------------*/
20
-
21
- import { Disposable, DisposableCollection, Emitter, URI, generateUuid } from '@theia/core';
22
- import { inject, injectable } from '@theia/core/shared/inversify';
23
- import { NotebookService } from './notebook-service';
24
- import {
25
- CellEditType, CellExecuteOutputEdit, CellExecuteOutputItemEdit, CellExecutionUpdateType,
26
- CellUri, NotebookCellExecutionState, NotebookCellInternalMetadata
27
- } from '../../common';
28
- import { CellPartialInternalMetadataEditByHandle, CellEditOperation } from '../notebook-types';
29
- import { NotebookModel } from '../view-model/notebook-model';
30
-
31
- export type CellExecuteUpdate = CellExecuteOutputEdit | CellExecuteOutputItemEdit | CellExecutionStateUpdate;
32
-
33
- export interface CellExecutionComplete {
34
- runEndTime?: number;
35
- lastRunSuccess?: boolean;
36
- }
37
-
38
- export interface CellExecutionStateUpdate {
39
- editType: CellExecutionUpdateType.ExecutionState;
40
- executionOrder?: number;
41
- runStartTime?: number;
42
- didPause?: boolean;
43
- isPaused?: boolean;
44
- }
45
-
46
- export enum NotebookExecutionType {
47
- cell,
48
- notebook
49
- }
50
-
51
- export interface NotebookFailStateChangedEvent {
52
- visible: boolean;
53
- notebook: URI;
54
- }
55
-
56
- export interface FailedCellInfo {
57
- cellHandle: number;
58
- disposable: Disposable;
59
- visible: boolean;
60
- }
61
-
62
- @injectable()
63
- export class NotebookExecutionStateService implements Disposable {
64
-
65
- @inject(NotebookService)
66
- protected notebookService: NotebookService;
67
-
68
- protected toDispose: DisposableCollection = new DisposableCollection();
69
-
70
- protected readonly executions = new Map<string, Map<number, CellExecution>>();
71
-
72
- protected readonly onDidChangeExecutionEmitter = new Emitter<CellExecutionStateChangedEvent>();
73
- onDidChangeExecution = this.onDidChangeExecutionEmitter.event;
74
-
75
- protected readonly onDidChangeLastRunFailStateEmitter = new Emitter<NotebookFailStateChangedEvent>();
76
- onDidChangeLastRunFailState = this.onDidChangeLastRunFailStateEmitter.event;
77
-
78
- getOrCreateCellExecution(notebookUri: URI, cellHandle: number): CellExecution {
79
- const notebook = this.notebookService.getNotebookEditorModel(notebookUri);
80
-
81
- if (!notebook) {
82
- throw new Error(`Notebook not found: ${notebookUri.toString()}`);
83
- }
84
-
85
- let execution = this.executions.get(notebookUri.toString())?.get(cellHandle);
86
-
87
- if (!execution) {
88
- execution = this.createNotebookCellExecution(notebook, cellHandle);
89
- if (!this.executions.has(notebookUri.toString())) {
90
- this.executions.set(notebookUri.toString(), new Map());
91
- }
92
- this.executions.get(notebookUri.toString())?.set(cellHandle, execution);
93
- execution.initialize();
94
- this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle, execution));
95
- }
96
-
97
- return execution;
98
-
99
- }
100
-
101
- protected createNotebookCellExecution(notebook: NotebookModel, cellHandle: number): CellExecution {
102
- const notebookUri = notebook.uri;
103
- const execution = new CellExecution(cellHandle, notebook);
104
- execution.toDispose.push(execution.onDidUpdate(() => this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle, execution))));
105
- execution.toDispose.push(execution.onDidComplete(lastRunSuccess => this.onCellExecutionDidComplete(notebookUri, cellHandle, execution, lastRunSuccess)));
106
-
107
- return execution;
108
- }
109
-
110
- protected onCellExecutionDidComplete(notebookUri: URI, cellHandle: number, exe: CellExecution, lastRunSuccess?: boolean): void {
111
- const notebookExecutions = this.executions.get(notebookUri.toString())?.get(cellHandle);
112
- if (!notebookExecutions) {
113
- throw new Error('Notebook Cell Execution not found while trying to complete it');
114
- }
115
-
116
- exe.dispose();
117
- this.executions.get(notebookUri.toString())?.delete(cellHandle);
118
-
119
- this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle));
120
- }
121
-
122
- getCellExecution(cellUri: URI): CellExecution | undefined {
123
- const parsed = CellUri.parse(cellUri);
124
- if (!parsed) {
125
- throw new Error(`Not a cell URI: ${cellUri}`);
126
- }
127
-
128
- return this.executions.get(parsed.notebook.toString())?.get(parsed.handle);
129
- }
130
-
131
- dispose(): void {
132
- this.onDidChangeExecutionEmitter.dispose();
133
- this.onDidChangeLastRunFailStateEmitter.dispose();
134
-
135
- this.executions.forEach(notebookExecutions => notebookExecutions.forEach(execution => execution.dispose()));
136
- }
137
-
138
- }
139
-
140
- export class CellExecution implements Disposable {
141
- protected readonly onDidUpdateEmitter = new Emitter<void>();
142
- readonly onDidUpdate = this.onDidUpdateEmitter.event;
143
-
144
- protected readonly onDidCompleteEmitter = new Emitter<boolean | undefined>();
145
- readonly onDidComplete = this.onDidCompleteEmitter.event;
146
-
147
- toDispose = new DisposableCollection();
148
-
149
- protected _state: NotebookCellExecutionState = NotebookCellExecutionState.Unconfirmed;
150
- get state(): NotebookCellExecutionState {
151
- return this._state;
152
- }
153
-
154
- get notebookURI(): URI {
155
- return this.notebook.uri;
156
- }
157
-
158
- protected _didPause = false;
159
- get didPause(): boolean {
160
- return this._didPause;
161
- }
162
-
163
- protected _isPaused = false;
164
- get isPaused(): boolean {
165
- return this._isPaused;
166
- }
167
-
168
- constructor(
169
- readonly cellHandle: number,
170
- protected readonly notebook: NotebookModel,
171
- ) {
172
- console.debug(`CellExecution#ctor ${this.getCellLog()}`);
173
- }
174
-
175
- initialize(): void {
176
- const startExecuteEdit: CellPartialInternalMetadataEditByHandle = {
177
- editType: CellEditType.PartialInternalMetadata,
178
- handle: this.cellHandle,
179
- internalMetadata: {
180
- executionId: generateUuid(),
181
- runStartTime: undefined,
182
- runEndTime: undefined,
183
- lastRunSuccess: undefined,
184
- executionOrder: undefined,
185
- renderDuration: undefined,
186
- }
187
- };
188
- this.applyCellExecutionEditsToNotebook([startExecuteEdit]);
189
- }
190
-
191
- protected getCellLog(): string {
192
- return `${this.notebookURI.toString()}, ${this.cellHandle}`;
193
- }
194
-
195
- confirm(): void {
196
- this._state = NotebookCellExecutionState.Pending;
197
- this.onDidUpdateEmitter.fire();
198
- }
199
-
200
- update(updates: CellExecuteUpdate[]): void {
201
- if (updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState)) {
202
- this._state = NotebookCellExecutionState.Executing;
203
- }
204
-
205
- if (!this._didPause && updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState && u.didPause)) {
206
- this._didPause = true;
207
- }
208
-
209
- const lastIsPausedUpdate = [...updates].reverse().find(u => u.editType === CellExecutionUpdateType.ExecutionState && typeof u.isPaused === 'boolean');
210
- if (lastIsPausedUpdate) {
211
- this._isPaused = (lastIsPausedUpdate as CellExecutionStateUpdate).isPaused!;
212
- }
213
-
214
- const cellModel = this.notebook.cells.find(c => c.handle === this.cellHandle);
215
- if (!cellModel) {
216
- console.debug(`CellExecution#update, updating cell not in notebook: ${this.notebook.uri.toString()}, ${this.cellHandle}`);
217
- } else {
218
- const edits = updates.map(update => updateToEdit(update, this.cellHandle));
219
- this.applyCellExecutionEditsToNotebook(edits);
220
- }
221
-
222
- if (updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState)) {
223
- this.onDidUpdateEmitter.fire();
224
- }
225
-
226
- }
227
-
228
- complete(completionData: CellExecutionComplete): void {
229
- const cellModel = this.notebook.cells.find(c => c.handle === this.cellHandle);
230
- if (!cellModel) {
231
- console.debug(`CellExecution#complete, completing cell not in notebook: ${this.notebook.uri.toString()}, ${this.cellHandle}`);
232
- } else {
233
- const edit: CellEditOperation = {
234
- editType: CellEditType.PartialInternalMetadata,
235
- handle: this.cellHandle,
236
- internalMetadata: {
237
- lastRunSuccess: completionData.lastRunSuccess,
238
- // eslint-disable-next-line no-null/no-null
239
- runStartTime: this._didPause ? null : cellModel.internalMetadata.runStartTime,
240
- // eslint-disable-next-line no-null/no-null
241
- runEndTime: this._didPause ? null : completionData.runEndTime,
242
- }
243
- };
244
- this.applyCellExecutionEditsToNotebook([edit]);
245
- }
246
-
247
- this.onDidCompleteEmitter.fire(completionData.lastRunSuccess);
248
-
249
- }
250
-
251
- dispose(): void {
252
- this.onDidUpdateEmitter.dispose();
253
- this.onDidCompleteEmitter.dispose();
254
- this.toDispose.dispose();
255
- }
256
-
257
- protected applyCellExecutionEditsToNotebook(edits: CellEditOperation[]): void {
258
- this.notebook.applyEdits(edits, false);
259
- }
260
- }
261
-
262
- export class CellExecutionStateChangedEvent {
263
- readonly type = NotebookExecutionType.cell;
264
- constructor(
265
- readonly notebook: URI,
266
- readonly cellHandle: number,
267
- readonly changed?: CellExecution
268
- ) { }
269
-
270
- affectsCell(cell: URI): boolean {
271
- const parsedUri = CellUri.parse(cell);
272
- return !!parsedUri && this.notebook.isEqual(parsedUri.notebook) && this.cellHandle === parsedUri.handle;
273
- }
274
-
275
- affectsNotebook(notebook: URI): boolean {
276
- return this.notebook.toString() === notebook.toString();
277
- }
278
- }
279
-
280
- export function updateToEdit(update: CellExecuteUpdate, cellHandle: number): CellEditOperation {
281
- if (update.editType === CellExecutionUpdateType.Output) {
282
- return {
283
- editType: CellEditType.Output,
284
- handle: update.cellHandle,
285
- append: update.append,
286
- outputs: update.outputs,
287
- };
288
- } else if (update.editType === CellExecutionUpdateType.OutputItems) {
289
- return {
290
- editType: CellEditType.OutputItems,
291
- items: update.items,
292
- outputId: update.outputId,
293
- append: update.append,
294
- };
295
- } else if (update.editType === CellExecutionUpdateType.ExecutionState) {
296
- const newInternalMetadata: Partial<NotebookCellInternalMetadata> = {};
297
- if (typeof update.executionOrder !== 'undefined') {
298
- newInternalMetadata.executionOrder = update.executionOrder;
299
- }
300
- if (typeof update.runStartTime !== 'undefined') {
301
- newInternalMetadata.runStartTime = update.runStartTime;
302
- }
303
- return {
304
- editType: CellEditType.PartialInternalMetadata,
305
- handle: cellHandle,
306
- internalMetadata: newInternalMetadata
307
- };
308
- }
309
-
310
- throw new Error('Unknown cell update type');
311
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2023 TypeFox and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+ /*---------------------------------------------------------------------------------------------
17
+ * Copyright (c) Microsoft Corporation. All rights reserved.
18
+ * Licensed under the MIT License. See License.txt in the project root for license information.
19
+ *--------------------------------------------------------------------------------------------*/
20
+
21
+ import { Disposable, DisposableCollection, Emitter, URI, generateUuid } from '@theia/core';
22
+ import { inject, injectable } from '@theia/core/shared/inversify';
23
+ import { NotebookService } from './notebook-service';
24
+ import {
25
+ CellEditType, CellExecuteOutputEdit, CellExecuteOutputItemEdit, CellExecutionUpdateType,
26
+ CellUri, NotebookCellExecutionState, NotebookCellInternalMetadata
27
+ } from '../../common';
28
+ import { CellPartialInternalMetadataEditByHandle, CellEditOperation } from '../notebook-types';
29
+ import { NotebookModel } from '../view-model/notebook-model';
30
+
31
+ export type CellExecuteUpdate = CellExecuteOutputEdit | CellExecuteOutputItemEdit | CellExecutionStateUpdate;
32
+
33
+ export interface CellExecutionComplete {
34
+ runEndTime?: number;
35
+ lastRunSuccess?: boolean;
36
+ }
37
+
38
+ export interface CellExecutionStateUpdate {
39
+ editType: CellExecutionUpdateType.ExecutionState;
40
+ executionOrder?: number;
41
+ runStartTime?: number;
42
+ didPause?: boolean;
43
+ isPaused?: boolean;
44
+ }
45
+
46
+ export enum NotebookExecutionType {
47
+ cell,
48
+ notebook
49
+ }
50
+
51
+ export interface NotebookFailStateChangedEvent {
52
+ visible: boolean;
53
+ notebook: URI;
54
+ }
55
+
56
+ export interface FailedCellInfo {
57
+ cellHandle: number;
58
+ disposable: Disposable;
59
+ visible: boolean;
60
+ }
61
+
62
+ @injectable()
63
+ export class NotebookExecutionStateService implements Disposable {
64
+
65
+ @inject(NotebookService)
66
+ protected notebookService: NotebookService;
67
+
68
+ protected toDispose: DisposableCollection = new DisposableCollection();
69
+
70
+ protected readonly executions = new Map<string, Map<number, CellExecution>>();
71
+
72
+ protected readonly onDidChangeExecutionEmitter = new Emitter<CellExecutionStateChangedEvent>();
73
+ onDidChangeExecution = this.onDidChangeExecutionEmitter.event;
74
+
75
+ protected readonly onDidChangeLastRunFailStateEmitter = new Emitter<NotebookFailStateChangedEvent>();
76
+ onDidChangeLastRunFailState = this.onDidChangeLastRunFailStateEmitter.event;
77
+
78
+ getOrCreateCellExecution(notebookUri: URI, cellHandle: number): CellExecution {
79
+ const notebook = this.notebookService.getNotebookEditorModel(notebookUri);
80
+
81
+ if (!notebook) {
82
+ throw new Error(`Notebook not found: ${notebookUri.toString()}`);
83
+ }
84
+
85
+ let execution = this.executions.get(notebookUri.toString())?.get(cellHandle);
86
+
87
+ if (!execution) {
88
+ execution = this.createNotebookCellExecution(notebook, cellHandle);
89
+ if (!this.executions.has(notebookUri.toString())) {
90
+ this.executions.set(notebookUri.toString(), new Map());
91
+ }
92
+ this.executions.get(notebookUri.toString())?.set(cellHandle, execution);
93
+ execution.initialize();
94
+ this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle, execution));
95
+ }
96
+
97
+ return execution;
98
+
99
+ }
100
+
101
+ protected createNotebookCellExecution(notebook: NotebookModel, cellHandle: number): CellExecution {
102
+ const notebookUri = notebook.uri;
103
+ const execution = new CellExecution(cellHandle, notebook);
104
+ execution.toDispose.push(execution.onDidUpdate(() => this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle, execution))));
105
+ execution.toDispose.push(execution.onDidComplete(lastRunSuccess => this.onCellExecutionDidComplete(notebookUri, cellHandle, execution, lastRunSuccess)));
106
+
107
+ return execution;
108
+ }
109
+
110
+ protected onCellExecutionDidComplete(notebookUri: URI, cellHandle: number, exe: CellExecution, lastRunSuccess?: boolean): void {
111
+ const notebookExecutions = this.executions.get(notebookUri.toString())?.get(cellHandle);
112
+ if (!notebookExecutions) {
113
+ throw new Error('Notebook Cell Execution not found while trying to complete it');
114
+ }
115
+
116
+ exe.dispose();
117
+ this.executions.get(notebookUri.toString())?.delete(cellHandle);
118
+
119
+ this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle));
120
+ }
121
+
122
+ getCellExecution(cellUri: URI): CellExecution | undefined {
123
+ const parsed = CellUri.parse(cellUri);
124
+ if (!parsed) {
125
+ throw new Error(`Not a cell URI: ${cellUri}`);
126
+ }
127
+
128
+ return this.executions.get(parsed.notebook.toString())?.get(parsed.handle);
129
+ }
130
+
131
+ dispose(): void {
132
+ this.onDidChangeExecutionEmitter.dispose();
133
+ this.onDidChangeLastRunFailStateEmitter.dispose();
134
+
135
+ this.executions.forEach(notebookExecutions => notebookExecutions.forEach(execution => execution.dispose()));
136
+ }
137
+
138
+ }
139
+
140
+ export class CellExecution implements Disposable {
141
+ protected readonly onDidUpdateEmitter = new Emitter<void>();
142
+ readonly onDidUpdate = this.onDidUpdateEmitter.event;
143
+
144
+ protected readonly onDidCompleteEmitter = new Emitter<boolean | undefined>();
145
+ readonly onDidComplete = this.onDidCompleteEmitter.event;
146
+
147
+ toDispose = new DisposableCollection();
148
+
149
+ protected _state: NotebookCellExecutionState = NotebookCellExecutionState.Unconfirmed;
150
+ get state(): NotebookCellExecutionState {
151
+ return this._state;
152
+ }
153
+
154
+ get notebookURI(): URI {
155
+ return this.notebook.uri;
156
+ }
157
+
158
+ protected _didPause = false;
159
+ get didPause(): boolean {
160
+ return this._didPause;
161
+ }
162
+
163
+ protected _isPaused = false;
164
+ get isPaused(): boolean {
165
+ return this._isPaused;
166
+ }
167
+
168
+ constructor(
169
+ readonly cellHandle: number,
170
+ protected readonly notebook: NotebookModel,
171
+ ) {
172
+ console.debug(`CellExecution#ctor ${this.getCellLog()}`);
173
+ }
174
+
175
+ initialize(): void {
176
+ const startExecuteEdit: CellPartialInternalMetadataEditByHandle = {
177
+ editType: CellEditType.PartialInternalMetadata,
178
+ handle: this.cellHandle,
179
+ internalMetadata: {
180
+ executionId: generateUuid(),
181
+ runStartTime: undefined,
182
+ runEndTime: undefined,
183
+ lastRunSuccess: undefined,
184
+ executionOrder: undefined,
185
+ renderDuration: undefined,
186
+ }
187
+ };
188
+ this.applyCellExecutionEditsToNotebook([startExecuteEdit]);
189
+ }
190
+
191
+ protected getCellLog(): string {
192
+ return `${this.notebookURI.toString()}, ${this.cellHandle}`;
193
+ }
194
+
195
+ confirm(): void {
196
+ this._state = NotebookCellExecutionState.Pending;
197
+ this.onDidUpdateEmitter.fire();
198
+ }
199
+
200
+ update(updates: CellExecuteUpdate[]): void {
201
+ if (updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState)) {
202
+ this._state = NotebookCellExecutionState.Executing;
203
+ }
204
+
205
+ if (!this._didPause && updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState && u.didPause)) {
206
+ this._didPause = true;
207
+ }
208
+
209
+ const lastIsPausedUpdate = [...updates].reverse().find(u => u.editType === CellExecutionUpdateType.ExecutionState && typeof u.isPaused === 'boolean');
210
+ if (lastIsPausedUpdate) {
211
+ this._isPaused = (lastIsPausedUpdate as CellExecutionStateUpdate).isPaused!;
212
+ }
213
+
214
+ const cellModel = this.notebook.cells.find(c => c.handle === this.cellHandle);
215
+ if (!cellModel) {
216
+ console.debug(`CellExecution#update, updating cell not in notebook: ${this.notebook.uri.toString()}, ${this.cellHandle}`);
217
+ } else {
218
+ const edits = updates.map(update => updateToEdit(update, this.cellHandle));
219
+ this.applyCellExecutionEditsToNotebook(edits);
220
+ }
221
+
222
+ if (updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState)) {
223
+ this.onDidUpdateEmitter.fire();
224
+ }
225
+
226
+ }
227
+
228
+ complete(completionData: CellExecutionComplete): void {
229
+ const cellModel = this.notebook.cells.find(c => c.handle === this.cellHandle);
230
+ if (!cellModel) {
231
+ console.debug(`CellExecution#complete, completing cell not in notebook: ${this.notebook.uri.toString()}, ${this.cellHandle}`);
232
+ } else {
233
+ const edit: CellEditOperation = {
234
+ editType: CellEditType.PartialInternalMetadata,
235
+ handle: this.cellHandle,
236
+ internalMetadata: {
237
+ lastRunSuccess: completionData.lastRunSuccess,
238
+ // eslint-disable-next-line no-null/no-null
239
+ runStartTime: this._didPause ? null : cellModel.internalMetadata.runStartTime,
240
+ // eslint-disable-next-line no-null/no-null
241
+ runEndTime: this._didPause ? null : completionData.runEndTime,
242
+ }
243
+ };
244
+ this.applyCellExecutionEditsToNotebook([edit]);
245
+ }
246
+
247
+ this.onDidCompleteEmitter.fire(completionData.lastRunSuccess);
248
+
249
+ }
250
+
251
+ dispose(): void {
252
+ this.onDidUpdateEmitter.dispose();
253
+ this.onDidCompleteEmitter.dispose();
254
+ this.toDispose.dispose();
255
+ }
256
+
257
+ protected applyCellExecutionEditsToNotebook(edits: CellEditOperation[]): void {
258
+ this.notebook.applyEdits(edits, false);
259
+ }
260
+ }
261
+
262
+ export class CellExecutionStateChangedEvent {
263
+ readonly type = NotebookExecutionType.cell;
264
+ constructor(
265
+ readonly notebook: URI,
266
+ readonly cellHandle: number,
267
+ readonly changed?: CellExecution
268
+ ) { }
269
+
270
+ affectsCell(cell: URI): boolean {
271
+ const parsedUri = CellUri.parse(cell);
272
+ return !!parsedUri && this.notebook.isEqual(parsedUri.notebook) && this.cellHandle === parsedUri.handle;
273
+ }
274
+
275
+ affectsNotebook(notebook: URI): boolean {
276
+ return this.notebook.toString() === notebook.toString();
277
+ }
278
+ }
279
+
280
+ export function updateToEdit(update: CellExecuteUpdate, cellHandle: number): CellEditOperation {
281
+ if (update.editType === CellExecutionUpdateType.Output) {
282
+ return {
283
+ editType: CellEditType.Output,
284
+ handle: update.cellHandle,
285
+ append: update.append,
286
+ outputs: update.outputs,
287
+ };
288
+ } else if (update.editType === CellExecutionUpdateType.OutputItems) {
289
+ return {
290
+ editType: CellEditType.OutputItems,
291
+ items: update.items,
292
+ outputId: update.outputId,
293
+ append: update.append,
294
+ };
295
+ } else if (update.editType === CellExecutionUpdateType.ExecutionState) {
296
+ const newInternalMetadata: Partial<NotebookCellInternalMetadata> = {};
297
+ if (typeof update.executionOrder !== 'undefined') {
298
+ newInternalMetadata.executionOrder = update.executionOrder;
299
+ }
300
+ if (typeof update.runStartTime !== 'undefined') {
301
+ newInternalMetadata.runStartTime = update.runStartTime;
302
+ }
303
+ return {
304
+ editType: CellEditType.PartialInternalMetadata,
305
+ handle: cellHandle,
306
+ internalMetadata: newInternalMetadata
307
+ };
308
+ }
309
+
310
+ throw new Error('Unknown cell update type');
311
+ }