@theia/notebook 1.41.0

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 (188) hide show
  1. package/README.md +30 -0
  2. package/lib/browser/contributions/notebook-actions-contribution.d.ts +27 -0
  3. package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -0
  4. package/lib/browser/contributions/notebook-actions-contribution.js +172 -0
  5. package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -0
  6. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +30 -0
  7. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -0
  8. package/lib/browser/contributions/notebook-cell-actions-contribution.js +200 -0
  9. package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -0
  10. package/lib/browser/contributions/notebook-color-contribution.d.ts +6 -0
  11. package/lib/browser/contributions/notebook-color-contribution.d.ts.map +1 -0
  12. package/lib/browser/contributions/notebook-color-contribution.js +252 -0
  13. package/lib/browser/contributions/notebook-color-contribution.js.map +1 -0
  14. package/lib/browser/contributions/notebook-context-keys.d.ts +38 -0
  15. package/lib/browser/contributions/notebook-context-keys.d.ts.map +1 -0
  16. package/lib/browser/contributions/notebook-context-keys.js +95 -0
  17. package/lib/browser/contributions/notebook-context-keys.js.map +1 -0
  18. package/lib/browser/index.d.ts +11 -0
  19. package/lib/browser/index.d.ts.map +1 -0
  20. package/lib/browser/index.js +38 -0
  21. package/lib/browser/index.js.map +1 -0
  22. package/lib/browser/notebook-cell-resource-resolver.d.ts +20 -0
  23. package/lib/browser/notebook-cell-resource-resolver.d.ts.map +1 -0
  24. package/lib/browser/notebook-cell-resource-resolver.js +75 -0
  25. package/lib/browser/notebook-cell-resource-resolver.js.map +1 -0
  26. package/lib/browser/notebook-editor-widget-factory.d.ts +17 -0
  27. package/lib/browser/notebook-editor-widget-factory.d.ts.map +1 -0
  28. package/lib/browser/notebook-editor-widget-factory.js +78 -0
  29. package/lib/browser/notebook-editor-widget-factory.js.map +1 -0
  30. package/lib/browser/notebook-editor-widget.d.ts +49 -0
  31. package/lib/browser/notebook-editor-widget.d.ts.map +1 -0
  32. package/lib/browser/notebook-editor-widget.js +150 -0
  33. package/lib/browser/notebook-editor-widget.js.map +1 -0
  34. package/lib/browser/notebook-frontend-module.d.ts +5 -0
  35. package/lib/browser/notebook-frontend-module.d.ts.map +1 -0
  36. package/lib/browser/notebook-frontend-module.js +81 -0
  37. package/lib/browser/notebook-frontend-module.js.map +1 -0
  38. package/lib/browser/notebook-open-handler.d.ts +18 -0
  39. package/lib/browser/notebook-open-handler.d.ts.map +1 -0
  40. package/lib/browser/notebook-open-handler.js +94 -0
  41. package/lib/browser/notebook-open-handler.js.map +1 -0
  42. package/lib/browser/notebook-renderer-registry.d.ts +17 -0
  43. package/lib/browser/notebook-renderer-registry.d.ts.map +1 -0
  44. package/lib/browser/notebook-renderer-registry.js +63 -0
  45. package/lib/browser/notebook-renderer-registry.js.map +1 -0
  46. package/lib/browser/notebook-type-registry.d.ts +7 -0
  47. package/lib/browser/notebook-type-registry.d.ts.map +1 -0
  48. package/lib/browser/notebook-type-registry.js +42 -0
  49. package/lib/browser/notebook-type-registry.js.map +1 -0
  50. package/lib/browser/renderers/cell-output-webview.d.ts +12 -0
  51. package/lib/browser/renderers/cell-output-webview.d.ts.map +1 -0
  52. package/lib/browser/renderers/cell-output-webview.js +20 -0
  53. package/lib/browser/renderers/cell-output-webview.js.map +1 -0
  54. package/lib/browser/service/notebook-cell-context-manager.d.ts +16 -0
  55. package/lib/browser/service/notebook-cell-context-manager.d.ts.map +1 -0
  56. package/lib/browser/service/notebook-cell-context-manager.js +83 -0
  57. package/lib/browser/service/notebook-cell-context-manager.js.map +1 -0
  58. package/lib/browser/service/notebook-editor-service.d.ts +22 -0
  59. package/lib/browser/service/notebook-editor-service.d.ts.map +1 -0
  60. package/lib/browser/service/notebook-editor-service.js +95 -0
  61. package/lib/browser/service/notebook-editor-service.js.map +1 -0
  62. package/lib/browser/service/notebook-execution-service.d.ts +25 -0
  63. package/lib/browser/service/notebook-execution-service.d.ts.map +1 -0
  64. package/lib/browser/service/notebook-execution-service.js +140 -0
  65. package/lib/browser/service/notebook-execution-service.js.map +1 -0
  66. package/lib/browser/service/notebook-execution-state-service.d.ts +94 -0
  67. package/lib/browser/service/notebook-execution-state-service.d.ts.map +1 -0
  68. package/lib/browser/service/notebook-execution-state-service.js +250 -0
  69. package/lib/browser/service/notebook-execution-state-service.js.map +1 -0
  70. package/lib/browser/service/notebook-kernel-history-service.d.ts +23 -0
  71. package/lib/browser/service/notebook-kernel-history-service.d.ts.map +1 -0
  72. package/lib/browser/service/notebook-kernel-history-service.js +109 -0
  73. package/lib/browser/service/notebook-kernel-history-service.js.map +1 -0
  74. package/lib/browser/service/notebook-kernel-quick-pick-service.d.ts +65 -0
  75. package/lib/browser/service/notebook-kernel-quick-pick-service.d.ts.map +1 -0
  76. package/lib/browser/service/notebook-kernel-quick-pick-service.js +430 -0
  77. package/lib/browser/service/notebook-kernel-quick-pick-service.js.map +1 -0
  78. package/lib/browser/service/notebook-kernel-service.d.ts +112 -0
  79. package/lib/browser/service/notebook-kernel-service.d.ts.map +1 -0
  80. package/lib/browser/service/notebook-kernel-service.js +274 -0
  81. package/lib/browser/service/notebook-kernel-service.js.map +1 -0
  82. package/lib/browser/service/notebook-model-resolver-service.d.ts +26 -0
  83. package/lib/browser/service/notebook-model-resolver-service.d.ts.map +1 -0
  84. package/lib/browser/service/notebook-model-resolver-service.js +139 -0
  85. package/lib/browser/service/notebook-model-resolver-service.js.map +1 -0
  86. package/lib/browser/service/notebook-renderer-messaging-service.d.ts +30 -0
  87. package/lib/browser/service/notebook-renderer-messaging-service.d.ts.map +1 -0
  88. package/lib/browser/service/notebook-renderer-messaging-service.js +94 -0
  89. package/lib/browser/service/notebook-renderer-messaging-service.js.map +1 -0
  90. package/lib/browser/service/notebook-service.d.ts +64 -0
  91. package/lib/browser/service/notebook-service.d.ts.map +1 -0
  92. package/lib/browser/service/notebook-service.js +162 -0
  93. package/lib/browser/service/notebook-service.js.map +1 -0
  94. package/lib/browser/view/notebook-cell-editor.d.ts +26 -0
  95. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -0
  96. package/lib/browser/view/notebook-cell-editor.js +79 -0
  97. package/lib/browser/view/notebook-cell-editor.js.map +1 -0
  98. package/lib/browser/view/notebook-cell-list-view.d.ts +43 -0
  99. package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -0
  100. package/lib/browser/view/notebook-cell-list-view.js +111 -0
  101. package/lib/browser/view/notebook-cell-list-view.js.map +1 -0
  102. package/lib/browser/view/notebook-cell-toolbar-factory.d.ts +25 -0
  103. package/lib/browser/view/notebook-cell-toolbar-factory.d.ts.map +1 -0
  104. package/lib/browser/view/notebook-cell-toolbar-factory.js +93 -0
  105. package/lib/browser/view/notebook-cell-toolbar-factory.js.map +1 -0
  106. package/lib/browser/view/notebook-cell-toolbar.d.ts +25 -0
  107. package/lib/browser/view/notebook-cell-toolbar.d.ts.map +1 -0
  108. package/lib/browser/view/notebook-cell-toolbar.js +50 -0
  109. package/lib/browser/view/notebook-cell-toolbar.js.map +1 -0
  110. package/lib/browser/view/notebook-code-cell-view.d.ts +47 -0
  111. package/lib/browser/view/notebook-code-cell-view.d.ts.map +1 -0
  112. package/lib/browser/view/notebook-code-cell-view.js +166 -0
  113. package/lib/browser/view/notebook-code-cell-view.js.map +1 -0
  114. package/lib/browser/view/notebook-main-toolbar.d.ts +31 -0
  115. package/lib/browser/view/notebook-main-toolbar.d.ts.map +1 -0
  116. package/lib/browser/view/notebook-main-toolbar.js +118 -0
  117. package/lib/browser/view/notebook-main-toolbar.js.map +1 -0
  118. package/lib/browser/view/notebook-markdown-cell-view.d.ts +12 -0
  119. package/lib/browser/view/notebook-markdown-cell-view.d.ts.map +1 -0
  120. package/lib/browser/view/notebook-markdown-cell-view.js +70 -0
  121. package/lib/browser/view/notebook-markdown-cell-view.js.map +1 -0
  122. package/lib/browser/view-model/notebook-cell-model.d.ts +74 -0
  123. package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -0
  124. package/lib/browser/view-model/notebook-cell-model.js +226 -0
  125. package/lib/browser/view-model/notebook-cell-model.js.map +1 -0
  126. package/lib/browser/view-model/notebook-cell-output-model.d.ts +19 -0
  127. package/lib/browser/view-model/notebook-cell-output-model.d.ts.map +1 -0
  128. package/lib/browser/view-model/notebook-cell-output-model.js +61 -0
  129. package/lib/browser/view-model/notebook-cell-output-model.js.map +1 -0
  130. package/lib/browser/view-model/notebook-model.d.ts +59 -0
  131. package/lib/browser/view-model/notebook-model.d.ts.map +1 -0
  132. package/lib/browser/view-model/notebook-model.js +315 -0
  133. package/lib/browser/view-model/notebook-model.js.map +1 -0
  134. package/lib/common/index.d.ts +3 -0
  135. package/lib/common/index.d.ts.map +1 -0
  136. package/lib/common/index.js +30 -0
  137. package/lib/common/index.js.map +1 -0
  138. package/lib/common/notebook-common.d.ts +344 -0
  139. package/lib/common/notebook-common.d.ts.map +1 -0
  140. package/lib/common/notebook-common.js +112 -0
  141. package/lib/common/notebook-common.js.map +1 -0
  142. package/lib/common/notebook-protocol.d.ts +21 -0
  143. package/lib/common/notebook-protocol.d.ts.map +1 -0
  144. package/lib/common/notebook-protocol.js +18 -0
  145. package/lib/common/notebook-protocol.js.map +1 -0
  146. package/lib/common/notebook-range.d.ts +14 -0
  147. package/lib/common/notebook-range.d.ts.map +1 -0
  148. package/lib/common/notebook-range.js +18 -0
  149. package/lib/common/notebook-range.js.map +1 -0
  150. package/package.json +52 -0
  151. package/src/browser/contributions/notebook-actions-contribution.ts +171 -0
  152. package/src/browser/contributions/notebook-cell-actions-contribution.ts +204 -0
  153. package/src/browser/contributions/notebook-color-contribution.ts +268 -0
  154. package/src/browser/contributions/notebook-context-keys.ts +99 -0
  155. package/src/browser/index.ts +26 -0
  156. package/src/browser/notebook-cell-resource-resolver.ts +79 -0
  157. package/src/browser/notebook-editor-widget-factory.ts +70 -0
  158. package/src/browser/notebook-editor-widget.tsx +157 -0
  159. package/src/browser/notebook-frontend-module.ts +95 -0
  160. package/src/browser/notebook-open-handler.ts +87 -0
  161. package/src/browser/notebook-renderer-registry.ts +62 -0
  162. package/src/browser/notebook-type-registry.ts +30 -0
  163. package/src/browser/renderers/cell-output-webview.ts +32 -0
  164. package/src/browser/service/notebook-cell-context-manager.ts +70 -0
  165. package/src/browser/service/notebook-editor-service.ts +86 -0
  166. package/src/browser/service/notebook-execution-service.ts +139 -0
  167. package/src/browser/service/notebook-execution-state-service.ts +322 -0
  168. package/src/browser/service/notebook-kernel-history-service.ts +109 -0
  169. package/src/browser/service/notebook-kernel-quick-pick-service.ts +494 -0
  170. package/src/browser/service/notebook-kernel-service.ts +357 -0
  171. package/src/browser/service/notebook-model-resolver-service.ts +141 -0
  172. package/src/browser/service/notebook-renderer-messaging-service.ts +111 -0
  173. package/src/browser/service/notebook-service.ts +178 -0
  174. package/src/browser/style/index.css +236 -0
  175. package/src/browser/view/notebook-cell-editor.tsx +97 -0
  176. package/src/browser/view/notebook-cell-list-view.tsx +171 -0
  177. package/src/browser/view/notebook-cell-toolbar-factory.tsx +91 -0
  178. package/src/browser/view/notebook-cell-toolbar.tsx +70 -0
  179. package/src/browser/view/notebook-code-cell-view.tsx +190 -0
  180. package/src/browser/view/notebook-main-toolbar.tsx +115 -0
  181. package/src/browser/view/notebook-markdown-cell-view.tsx +73 -0
  182. package/src/browser/view-model/notebook-cell-model.ts +271 -0
  183. package/src/browser/view-model/notebook-cell-output-model.ts +69 -0
  184. package/src/browser/view-model/notebook-model.ts +372 -0
  185. package/src/common/index.ts +18 -0
  186. package/src/common/notebook-common.ts +462 -0
  187. package/src/common/notebook-protocol.ts +35 -0
  188. package/src/common/notebook-range.ts +30 -0
@@ -0,0 +1,322 @@
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, Emitter, URI } 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, CellPartialInternalMetadataEditByHandle, NotebookCellExecutionState, CellEditOperation, NotebookCellInternalMetadata
27
+ } from '../../common';
28
+ import { NotebookModel } from '../view-model/notebook-model';
29
+ import { v4 } from 'uuid';
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 interface ICellExecutionStateUpdate {
47
+ editType: CellExecutionUpdateType.ExecutionState;
48
+ executionOrder?: number;
49
+ runStartTime?: number;
50
+ didPause?: boolean;
51
+ isPaused?: boolean;
52
+ }
53
+
54
+ export interface ICellExecutionStateUpdate {
55
+ editType: CellExecutionUpdateType.ExecutionState;
56
+ executionOrder?: number;
57
+ runStartTime?: number;
58
+ didPause?: boolean;
59
+ isPaused?: boolean;
60
+ }
61
+
62
+ export interface ICellExecutionComplete {
63
+ runEndTime?: number;
64
+ lastRunSuccess?: boolean;
65
+ }
66
+ export enum NotebookExecutionType {
67
+ cell,
68
+ notebook
69
+ }
70
+
71
+ export interface NotebookFailStateChangedEvent {
72
+ visible: boolean;
73
+ notebook: URI;
74
+ }
75
+
76
+ export interface FailedCellInfo {
77
+ cellHandle: number;
78
+ disposable: Disposable;
79
+ visible: boolean;
80
+ }
81
+
82
+ @injectable()
83
+ export class NotebookExecutionStateService implements Disposable {
84
+
85
+ @inject(NotebookService)
86
+ protected notebookService: NotebookService;
87
+
88
+ protected readonly executions = new Map<string, CellExecution>();
89
+
90
+ private readonly onDidChangeExecutionEmitter = new Emitter<CellExecutionStateChangedEvent>();
91
+ onDidChangeExecution = this.onDidChangeExecutionEmitter.event;
92
+
93
+ private readonly onDidChangeLastRunFailStateEmitter = new Emitter<NotebookFailStateChangedEvent>();
94
+ onDidChangeLastRunFailState = this.onDidChangeLastRunFailStateEmitter.event;
95
+
96
+ createCellExecution(notebookUri: URI, cellHandle: number): CellExecution {
97
+ const notebook = this.notebookService.getNotebookEditorModel(notebookUri);
98
+
99
+ if (!notebook) {
100
+ throw new Error(`Notebook not found: ${notebookUri.toString()}`);
101
+ }
102
+
103
+ let execution = this.executions.get(`${notebookUri}/${cellHandle}`);
104
+
105
+ if (!execution) {
106
+ execution = this.createNotebookCellExecution(notebook, cellHandle);
107
+ this.executions.set(`${notebookUri}/${cellHandle}`, execution);
108
+ execution.initialize();
109
+ this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle, execution));
110
+ }
111
+
112
+ return execution;
113
+
114
+ }
115
+
116
+ private createNotebookCellExecution(notebook: NotebookModel, cellHandle: number): CellExecution {
117
+ const notebookUri = notebook.uri;
118
+ const execution = new CellExecution(cellHandle, notebook);
119
+ execution.onDidUpdate(() => this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle, execution)));
120
+ execution.onDidComplete(lastRunSuccess => this.onCellExecutionDidComplete(notebookUri, cellHandle, execution, lastRunSuccess));
121
+
122
+ return execution;
123
+ }
124
+
125
+ private onCellExecutionDidComplete(notebookUri: URI, cellHandle: number, exe: CellExecution, lastRunSuccess?: boolean): void {
126
+ const notebookExecutions = this.executions.get(`${notebookUri}/${cellHandle}`);
127
+ if (!notebookExecutions) {
128
+ return;
129
+ }
130
+
131
+ exe.dispose();
132
+ this.executions.delete(`${notebookUri}/${cellHandle}`);
133
+
134
+ this.onDidChangeExecutionEmitter.fire(new CellExecutionStateChangedEvent(notebookUri, cellHandle));
135
+ }
136
+
137
+ getCellExecution(cellUri: URI): CellExecution | undefined {
138
+ const parsed = CellUri.parse(cellUri);
139
+ if (!parsed) {
140
+ throw new Error(`Not a cell URI: ${cellUri}`);
141
+ }
142
+
143
+ return this.executions.get(`${parsed.notebook.toString()}/${parsed.handle}`);
144
+ }
145
+
146
+ dispose(): void {
147
+ this.onDidChangeExecutionEmitter.dispose();
148
+ this.onDidChangeLastRunFailStateEmitter.dispose();
149
+
150
+ this.executions.forEach(cellExecution => cellExecution.dispose());
151
+ }
152
+
153
+ }
154
+
155
+ export class CellExecution implements Disposable {
156
+ private readonly onDidUpdateEmitter = new Emitter<void>();
157
+ readonly onDidUpdate = this.onDidUpdateEmitter.event;
158
+
159
+ private readonly onDidCompleteEmitter = new Emitter<boolean | undefined>();
160
+ readonly onDidComplete = this.onDidCompleteEmitter.event;
161
+
162
+ private _state: NotebookCellExecutionState = NotebookCellExecutionState.Unconfirmed;
163
+ get state(): NotebookCellExecutionState {
164
+ return this._state;
165
+ }
166
+
167
+ get notebookURI(): URI {
168
+ return this.notebook.uri;
169
+ }
170
+
171
+ private _didPause = false;
172
+ get didPause(): boolean {
173
+ return this._didPause;
174
+ }
175
+
176
+ private _isPaused = false;
177
+ get isPaused(): boolean {
178
+ return this._isPaused;
179
+ }
180
+
181
+ constructor(
182
+ readonly cellHandle: number,
183
+ private readonly notebook: NotebookModel,
184
+ ) {
185
+ console.debug(`CellExecution#ctor ${this.getCellLog()}`);
186
+ }
187
+
188
+ initialize(): void {
189
+ const startExecuteEdit: CellPartialInternalMetadataEditByHandle = {
190
+ editType: CellEditType.PartialInternalMetadata,
191
+ handle: this.cellHandle,
192
+ internalMetadata: {
193
+ executionId: v4(),
194
+ runStartTime: undefined,
195
+ runEndTime: undefined,
196
+ lastRunSuccess: undefined,
197
+ executionOrder: undefined,
198
+ renderDuration: undefined,
199
+ }
200
+ };
201
+ this.applyExecutionEdits([startExecuteEdit]);
202
+ }
203
+
204
+ private getCellLog(): string {
205
+ return `${this.notebookURI.toString()}, ${this.cellHandle}`;
206
+ }
207
+
208
+ confirm(): void {
209
+ this._state = NotebookCellExecutionState.Pending;
210
+ this.onDidUpdateEmitter.fire();
211
+ }
212
+
213
+ update(updates: CellExecuteUpdate[]): void {
214
+ if (updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState)) {
215
+ this._state = NotebookCellExecutionState.Executing;
216
+ }
217
+
218
+ if (!this._didPause && updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState && u.didPause)) {
219
+ this._didPause = true;
220
+ }
221
+
222
+ const lastIsPausedUpdate = [...updates].reverse().find(u => u.editType === CellExecutionUpdateType.ExecutionState && typeof u.isPaused === 'boolean');
223
+ if (lastIsPausedUpdate) {
224
+ this._isPaused = (lastIsPausedUpdate as ICellExecutionStateUpdate).isPaused!;
225
+ }
226
+
227
+ const cellModel = this.notebook.cells.find(c => c.handle === this.cellHandle);
228
+ if (!cellModel) {
229
+ console.debug(`CellExecution#update, updating cell not in notebook: ${this.notebook.uri.toString()}, ${this.cellHandle}`);
230
+ } else {
231
+ const edits = updates.map(update => updateToEdit(update, this.cellHandle));
232
+ this.applyExecutionEdits(edits);
233
+ }
234
+
235
+ if (updates.some(u => u.editType === CellExecutionUpdateType.ExecutionState)) {
236
+ this.onDidUpdateEmitter.fire();
237
+ }
238
+
239
+ }
240
+
241
+ complete(completionData: CellExecutionComplete): void {
242
+ const cellModel = this.notebook.cells.find(c => c.handle === this.cellHandle);
243
+ if (!cellModel) {
244
+ console.debug(`CellExecution#complete, completing cell not in notebook: ${this.notebook.uri.toString()}, ${this.cellHandle}`);
245
+ } else {
246
+ const edit: CellEditOperation = {
247
+ editType: CellEditType.PartialInternalMetadata,
248
+ handle: this.cellHandle,
249
+ internalMetadata: {
250
+ lastRunSuccess: completionData.lastRunSuccess,
251
+ // eslint-disable-next-line no-null/no-null
252
+ runStartTime: this._didPause ? null : cellModel.internalMetadata.runStartTime,
253
+ // eslint-disable-next-line no-null/no-null
254
+ runEndTime: this._didPause ? null : completionData.runEndTime,
255
+ }
256
+ };
257
+ this.applyExecutionEdits([edit]);
258
+ }
259
+
260
+ this.onDidCompleteEmitter.fire(completionData.lastRunSuccess);
261
+
262
+ }
263
+
264
+ dispose(): void {
265
+ this.onDidUpdateEmitter.dispose();
266
+ this.onDidCompleteEmitter.dispose();
267
+ }
268
+
269
+ private applyExecutionEdits(edits: CellEditOperation[]): void {
270
+ this.notebook.applyEdits(edits, false);
271
+ }
272
+ }
273
+
274
+ export class CellExecutionStateChangedEvent {
275
+ readonly type = NotebookExecutionType.cell;
276
+ constructor(
277
+ readonly notebook: URI,
278
+ readonly cellHandle: number,
279
+ readonly changed?: CellExecution
280
+ ) { }
281
+
282
+ affectsCell(cell: URI): boolean {
283
+ const parsedUri = CellUri.parse(cell);
284
+ return !!parsedUri && this.notebook.isEqual(parsedUri.notebook) && this.cellHandle === parsedUri.handle;
285
+ }
286
+
287
+ affectsNotebook(notebook: URI): boolean {
288
+ return this.notebook.toString() === notebook.toString();
289
+ }
290
+ }
291
+
292
+ export function updateToEdit(update: CellExecuteUpdate, cellHandle: number): CellEditOperation {
293
+ if (update.editType === CellExecutionUpdateType.Output) {
294
+ return {
295
+ editType: CellEditType.Output,
296
+ handle: update.cellHandle,
297
+ append: update.append,
298
+ outputs: update.outputs,
299
+ };
300
+ } else if (update.editType === CellExecutionUpdateType.OutputItems) {
301
+ return {
302
+ editType: CellEditType.OutputItems,
303
+ items: update.items,
304
+ append: update.append,
305
+ };
306
+ } else if (update.editType === CellExecutionUpdateType.ExecutionState) {
307
+ const newInternalMetadata: Partial<NotebookCellInternalMetadata> = {};
308
+ if (typeof update.executionOrder !== 'undefined') {
309
+ newInternalMetadata.executionOrder = update.executionOrder;
310
+ }
311
+ if (typeof update.runStartTime !== 'undefined') {
312
+ newInternalMetadata.runStartTime = update.runStartTime;
313
+ }
314
+ return {
315
+ editType: CellEditType.PartialInternalMetadata,
316
+ handle: cellHandle,
317
+ internalMetadata: newInternalMetadata
318
+ };
319
+ }
320
+
321
+ throw new Error('Unknown cell update type');
322
+ }
@@ -0,0 +1,109 @@
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 { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
22
+ import { StorageService } from '@theia/core/lib/browser';
23
+ import { NotebookKernel, NotebookTextModelLike, NotebookKernelService } from './notebook-kernel-service';
24
+ import { Disposable } from '@theia/core';
25
+
26
+ interface KernelsList {
27
+ [viewType: string]: string[];
28
+ }
29
+
30
+ interface MostRecentKernelsResult {
31
+ selected?: NotebookKernel,
32
+ all: NotebookKernel[]
33
+ }
34
+
35
+ const MAX_KERNELS_IN_HISTORY = 5;
36
+
37
+ @injectable()
38
+ export class NotebookKernelHistoryService implements Disposable {
39
+
40
+ @inject(StorageService)
41
+ protected storageService: StorageService;
42
+
43
+ @inject(NotebookKernelService)
44
+ protected notebookKernelService: NotebookKernelService;
45
+
46
+ declare serviceBrand: undefined;
47
+
48
+ private static STORAGE_KEY = 'notebook.kernelHistory';
49
+ private mostRecentKernelsMap: KernelsList = {};
50
+
51
+ @postConstruct()
52
+ protected init(): void {
53
+ this.loadState();
54
+ }
55
+
56
+ getKernels(notebook: NotebookTextModelLike): MostRecentKernelsResult {
57
+ const allAvailableKernels = this.notebookKernelService.getMatchingKernel(notebook);
58
+ const allKernels = allAvailableKernels.all;
59
+ const selectedKernel = allAvailableKernels.selected;
60
+ // We will suggest the only kernel
61
+ const suggested = allAvailableKernels.all.length === 1 ? allAvailableKernels.all[0] : undefined;
62
+ const mostRecentKernelIds = this.mostRecentKernelsMap[notebook.viewType] ? this.mostRecentKernelsMap[notebook.viewType].map(kernel => kernel[1]) : [];
63
+ const all = mostRecentKernelIds.map(kernelId => allKernels.find(kernel => kernel.id === kernelId)).filter(kernel => !!kernel) as NotebookKernel[];
64
+
65
+ return {
66
+ selected: selectedKernel ?? suggested,
67
+ all
68
+ };
69
+ }
70
+
71
+ addMostRecentKernel(kernel: NotebookKernel): void {
72
+ const viewType = kernel.viewType;
73
+ const recentKernels = this.mostRecentKernelsMap[viewType] ?? [kernel.id];
74
+
75
+ if (recentKernels.length > MAX_KERNELS_IN_HISTORY) {
76
+ recentKernels.splice(MAX_KERNELS_IN_HISTORY);
77
+ }
78
+
79
+ this.mostRecentKernelsMap[viewType] = recentKernels;
80
+ this.saveState();
81
+ }
82
+
83
+ private saveState(): void {
84
+ let notEmpty = false;
85
+ for (const [_, kernels] of Object.entries(this.mostRecentKernelsMap)) {
86
+ notEmpty = notEmpty || Object.entries(kernels).length > 0;
87
+ }
88
+
89
+ this.storageService.setData(NotebookKernelHistoryService.STORAGE_KEY, notEmpty ? this.mostRecentKernelsMap : undefined);
90
+ }
91
+
92
+ private async loadState(): Promise<void> {
93
+ const kernelMap = await this.storageService.getData(NotebookKernelHistoryService.STORAGE_KEY);
94
+ if (kernelMap) {
95
+ this.mostRecentKernelsMap = kernelMap as KernelsList;
96
+ } else {
97
+ this.mostRecentKernelsMap = {};
98
+ }
99
+ }
100
+
101
+ clear(): void {
102
+ this.mostRecentKernelsMap = {};
103
+ this.saveState();
104
+ }
105
+
106
+ dispose(): void {
107
+
108
+ }
109
+ }