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

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 (78) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/index.d.ts +1 -0
  3. package/lib/browser/index.d.ts.map +1 -1
  4. package/lib/browser/index.js +1 -0
  5. package/lib/browser/index.js.map +1 -1
  6. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  7. package/lib/browser/notebook-frontend-module.js +2 -0
  8. package/lib/browser/notebook-frontend-module.js.map +1 -1
  9. package/lib/browser/service/notebook-cell-editor-service.d.ts +16 -0
  10. package/lib/browser/service/notebook-cell-editor-service.d.ts.map +1 -0
  11. package/lib/browser/service/notebook-cell-editor-service.js +53 -0
  12. package/lib/browser/service/notebook-cell-editor-service.js.map +1 -0
  13. package/lib/browser/view/notebook-cell-editor.d.ts +2 -0
  14. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  15. package/lib/browser/view/notebook-cell-editor.js +12 -1
  16. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  17. package/lib/browser/view/notebook-code-cell-view.d.ts +2 -0
  18. package/lib/browser/view/notebook-code-cell-view.d.ts.map +1 -1
  19. package/lib/browser/view/notebook-code-cell-view.js +6 -1
  20. package/lib/browser/view/notebook-code-cell-view.js.map +1 -1
  21. package/lib/browser/view/notebook-markdown-cell-view.d.ts +2 -0
  22. package/lib/browser/view/notebook-markdown-cell-view.d.ts.map +1 -1
  23. package/lib/browser/view/notebook-markdown-cell-view.js +8 -3
  24. package/lib/browser/view/notebook-markdown-cell-view.js.map +1 -1
  25. package/package.json +7 -7
  26. package/src/browser/contributions/cell-operations.ts +44 -44
  27. package/src/browser/contributions/notebook-actions-contribution.ts +379 -379
  28. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -525
  29. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  30. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  31. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  32. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  33. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  34. package/src/browser/contributions/notebook-preferences.ts +92 -92
  35. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -77
  36. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  37. package/src/browser/index.ts +28 -27
  38. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  39. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  40. package/src/browser/notebook-editor-widget.tsx +330 -330
  41. package/src/browser/notebook-frontend-module.ts +121 -119
  42. package/src/browser/notebook-open-handler.ts +120 -120
  43. package/src/browser/notebook-output-utils.ts +119 -119
  44. package/src/browser/notebook-renderer-registry.ts +85 -85
  45. package/src/browser/notebook-type-registry.ts +54 -54
  46. package/src/browser/notebook-types.ts +186 -186
  47. package/src/browser/renderers/cell-output-webview.ts +33 -33
  48. package/src/browser/service/notebook-cell-editor-service.ts +56 -0
  49. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  50. package/src/browser/service/notebook-context-manager.ts +162 -162
  51. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  52. package/src/browser/service/notebook-execution-service.ts +139 -139
  53. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  54. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  55. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  56. package/src/browser/service/notebook-kernel-service.ts +357 -357
  57. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  58. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  59. package/src/browser/service/notebook-options.ts +155 -155
  60. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  61. package/src/browser/service/notebook-service.ts +215 -215
  62. package/src/browser/style/index.css +484 -483
  63. package/src/browser/view/notebook-cell-editor.tsx +276 -263
  64. package/src/browser/view/notebook-cell-list-view.tsx +279 -279
  65. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  66. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  67. package/src/browser/view/notebook-code-cell-view.tsx +355 -350
  68. package/src/browser/view/notebook-find-widget.tsx +335 -335
  69. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  70. package/src/browser/view/notebook-markdown-cell-view.tsx +215 -208
  71. package/src/browser/view/notebook-viewport-service.ts +61 -61
  72. package/src/browser/view-model/notebook-cell-model.ts +473 -473
  73. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  74. package/src/browser/view-model/notebook-model.ts +550 -550
  75. package/src/common/index.ts +18 -18
  76. package/src/common/notebook-common.ts +337 -337
  77. package/src/common/notebook-protocol.ts +35 -35
  78. 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
+ }