@theia/notebook 1.53.0-next.5 → 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 (88) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/contributions/notebook-actions-contribution.d.ts +1 -0
  3. package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -1
  4. package/lib/browser/contributions/notebook-actions-contribution.js +31 -5
  5. package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -1
  6. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +2 -0
  7. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -1
  8. package/lib/browser/contributions/notebook-cell-actions-contribution.js +41 -7
  9. package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -1
  10. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts +14 -0
  11. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts.map +1 -0
  12. package/lib/browser/contributions/notebook-status-bar-contribution.js +75 -0
  13. package/lib/browser/contributions/notebook-status-bar-contribution.js.map +1 -0
  14. package/lib/browser/notebook-editor-widget.d.ts.map +1 -1
  15. package/lib/browser/notebook-editor-widget.js +3 -5
  16. package/lib/browser/notebook-editor-widget.js.map +1 -1
  17. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  18. package/lib/browser/notebook-frontend-module.js +3 -0
  19. package/lib/browser/notebook-frontend-module.js.map +1 -1
  20. package/lib/browser/notebook-open-handler.d.ts +3 -2
  21. package/lib/browser/notebook-open-handler.d.ts.map +1 -1
  22. package/lib/browser/notebook-open-handler.js +12 -5
  23. package/lib/browser/notebook-open-handler.js.map +1 -1
  24. package/lib/browser/view/notebook-cell-editor.d.ts +1 -0
  25. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  26. package/lib/browser/view/notebook-cell-editor.js +30 -16
  27. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  28. package/lib/browser/view/notebook-cell-list-view.d.ts +6 -4
  29. package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -1
  30. package/lib/browser/view/notebook-cell-list-view.js +20 -13
  31. package/lib/browser/view/notebook-cell-list-view.js.map +1 -1
  32. package/lib/browser/view-model/notebook-cell-model.d.ts +3 -0
  33. package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -1
  34. package/lib/browser/view-model/notebook-cell-model.js +5 -0
  35. package/lib/browser/view-model/notebook-cell-model.js.map +1 -1
  36. package/package.json +8 -8
  37. package/src/browser/contributions/cell-operations.ts +44 -44
  38. package/src/browser/contributions/notebook-actions-contribution.ts +379 -350
  39. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -487
  40. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  41. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  42. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  43. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  44. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  45. package/src/browser/contributions/notebook-preferences.ts +92 -92
  46. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -0
  47. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  48. package/src/browser/index.ts +27 -27
  49. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  50. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  51. package/src/browser/notebook-editor-widget.tsx +330 -331
  52. package/src/browser/notebook-frontend-module.ts +119 -115
  53. package/src/browser/notebook-open-handler.ts +120 -114
  54. package/src/browser/notebook-output-utils.ts +119 -119
  55. package/src/browser/notebook-renderer-registry.ts +85 -85
  56. package/src/browser/notebook-type-registry.ts +54 -54
  57. package/src/browser/notebook-types.ts +186 -186
  58. package/src/browser/renderers/cell-output-webview.ts +33 -33
  59. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  60. package/src/browser/service/notebook-context-manager.ts +162 -162
  61. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  62. package/src/browser/service/notebook-execution-service.ts +139 -139
  63. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  64. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  65. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  66. package/src/browser/service/notebook-kernel-service.ts +357 -357
  67. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  68. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  69. package/src/browser/service/notebook-options.ts +155 -155
  70. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  71. package/src/browser/service/notebook-service.ts +215 -215
  72. package/src/browser/style/index.css +483 -471
  73. package/src/browser/view/notebook-cell-editor.tsx +263 -247
  74. package/src/browser/view/notebook-cell-list-view.tsx +279 -262
  75. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  76. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  77. package/src/browser/view/notebook-code-cell-view.tsx +350 -350
  78. package/src/browser/view/notebook-find-widget.tsx +335 -335
  79. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  80. package/src/browser/view/notebook-markdown-cell-view.tsx +208 -208
  81. package/src/browser/view/notebook-viewport-service.ts +61 -61
  82. package/src/browser/view-model/notebook-cell-model.ts +473 -466
  83. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  84. package/src/browser/view-model/notebook-model.ts +550 -550
  85. package/src/common/index.ts +18 -18
  86. package/src/common/notebook-common.ts +337 -337
  87. package/src/common/notebook-protocol.ts +35 -35
  88. package/src/common/notebook-range.ts +30 -30
@@ -1,479 +1,479 @@
1
-
2
- // *****************************************************************************
3
- // Copyright (C) 2023 TypeFox and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
- /*---------------------------------------------------------------------------------------------
18
- * Copyright (c) Microsoft Corporation. All rights reserved.
19
- * Licensed under the MIT License. See License.txt in the project root for license information.
20
- *--------------------------------------------------------------------------------------------*/
21
- import { ArrayUtils, CommandService, DisposableCollection, Event, nls, QuickInputButton, QuickInputService, QuickPickInput, QuickPickItem, URI, } from '@theia/core';
22
- import { inject, injectable } from '@theia/core/shared/inversify';
23
- import { NotebookKernelService, NotebookKernel, NotebookKernelMatchResult, SourceCommand } from './notebook-kernel-service';
24
- import { NotebookModel } from '../view-model/notebook-model';
25
- import { NotebookEditorWidget } from '../notebook-editor-widget';
26
- import { codicon, OpenerService } from '@theia/core/lib/browser';
27
- import { NotebookKernelHistoryService } from './notebook-kernel-history-service';
28
- import { NotebookCommand, NotebookModelResource } from '../../common';
29
- import debounce = require('@theia/core/shared/lodash.debounce');
30
-
31
- export const JUPYTER_EXTENSION_ID = 'ms-toolsai.jupyter';
32
-
33
- type KernelPick = QuickPickItem & { kernel: NotebookKernel };
34
- function isKernelPick(item: QuickPickInput<QuickPickItem>): item is KernelPick {
35
- return 'kernel' in item;
36
- }
37
- type GroupedKernelsPick = QuickPickItem & { kernels: NotebookKernel[]; source: string };
38
- function isGroupedKernelsPick(item: QuickPickInput<QuickPickItem>): item is GroupedKernelsPick {
39
- return 'kernels' in item;
40
- }
41
- type SourcePick = QuickPickItem & { action: SourceCommand };
42
- function isSourcePick(item: QuickPickInput<QuickPickItem>): item is SourcePick {
43
- return 'action' in item;
44
- }
45
- type InstallExtensionPick = QuickPickItem & { extensionIds: string[] };
46
-
47
- type KernelSourceQuickPickItem = QuickPickItem & { command: NotebookCommand; documentation?: string };
48
- function isKernelSourceQuickPickItem(item: QuickPickItem): item is KernelSourceQuickPickItem {
49
- return 'command' in item;
50
- }
51
-
52
- function supportAutoRun(item: QuickPickInput<KernelQuickPickItem>): item is QuickPickItem {
53
- return 'autoRun' in item && !!item.autoRun;
54
- }
55
-
56
- type KernelQuickPickItem = (QuickPickItem & { autoRun?: boolean }) | InstallExtensionPick | KernelPick | GroupedKernelsPick | SourcePick | KernelSourceQuickPickItem;
57
-
58
- const KERNEL_PICKER_UPDATE_DEBOUNCE = 200;
59
-
60
- export type KernelQuickPickContext =
61
- { id: string; extension: string } |
62
- { notebookEditorId: string } |
63
- { id: string; extension: string; notebookEditorId: string } |
64
- { ui?: boolean; notebookEditor?: NotebookEditorWidget };
65
-
66
- function toKernelQuickPick(kernel: NotebookKernel, selected: NotebookKernel | undefined): KernelPick {
67
- const res: KernelPick = {
68
- kernel,
69
- label: kernel.label,
70
- description: kernel.description,
71
- detail: kernel.detail
72
- };
73
- if (kernel.id === selected?.id) {
74
- if (!res.description) {
75
- res.description = nls.localizeByDefault('Currently Selected');
76
- } else {
77
- res.description = nls.localizeByDefault('{0} - Currently Selected', res.description);
78
- }
79
- }
80
- return res;
81
- }
82
-
83
- @injectable()
84
- export class NotebookKernelQuickPickService {
85
-
86
- @inject(NotebookKernelService)
87
- protected readonly notebookKernelService: NotebookKernelService;
88
- @inject(QuickInputService)
89
- protected readonly quickInputService: QuickInputService;
90
- @inject(CommandService)
91
- protected readonly commandService: CommandService;
92
-
93
- @inject(OpenerService)
94
- protected openerService: OpenerService;
95
-
96
- @inject(NotebookKernelHistoryService)
97
- protected notebookKernelHistoryService: NotebookKernelHistoryService;
98
-
99
- async showQuickPick(editor: NotebookModel, wantedId?: string, skipAutoRun?: boolean): Promise<boolean> {
100
- const notebook = editor;
101
- const matchResult = this.getMatchingResult(notebook);
102
- const { selected, all } = matchResult;
103
-
104
- let newKernel: NotebookKernel | undefined;
105
- if (wantedId) {
106
- for (const candidate of all) {
107
- if (candidate.id === wantedId) {
108
- newKernel = candidate;
109
- break;
110
- }
111
- }
112
- if (!newKernel) {
113
- console.warn(`wanted kernel DOES NOT EXIST, wanted: ${wantedId}, all: ${all.map(k => k.id)}`);
114
- return false;
115
- }
116
- }
117
-
118
- if (newKernel) {
119
- this.selectKernel(notebook, newKernel);
120
- return true;
121
- }
122
-
123
- const quickPick = this.quickInputService.createQuickPick<KernelQuickPickItem>();
124
- const quickPickItems = this.getKernelPickerQuickPickItems(matchResult);
125
-
126
- if (quickPickItems.length === 1 && supportAutoRun(quickPickItems[0]) && !skipAutoRun) {
127
- return this.handleQuickPick(editor, quickPickItems[0], quickPickItems as KernelQuickPickItem[]);
128
- }
129
-
130
- quickPick.items = quickPickItems;
131
- quickPick.canSelectMany = false;
132
- quickPick.placeholder = selected
133
- ? nls.localizeByDefault("Change kernel for '{0}'", 'current') // TODO get label for current notebook from a label provider
134
- : nls.localizeByDefault("Select kernel for '{0}'", 'current');
135
-
136
- quickPick.busy = this.notebookKernelService.getKernelDetectionTasks(notebook).length > 0;
137
-
138
- const kernelDetectionTaskListener = this.notebookKernelService.onDidChangeKernelDetectionTasks(() => {
139
- quickPick.busy = this.notebookKernelService.getKernelDetectionTasks(notebook).length > 0;
140
- });
141
-
142
- const kernelChangeEventListener = debounce(
143
- Event.any(
144
- this.notebookKernelService.onDidChangeSourceActions,
145
- this.notebookKernelService.onDidAddKernel,
146
- this.notebookKernelService.onDidRemoveKernel,
147
- this.notebookKernelService.onDidChangeNotebookAffinity
148
- ),
149
- KERNEL_PICKER_UPDATE_DEBOUNCE
150
- )(async () => {
151
- // reset quick pick progress
152
- quickPick.busy = false;
153
-
154
- const currentActiveItems = quickPick.activeItems;
155
- const newMatchResult = this.getMatchingResult(notebook);
156
- const newQuickPickItems = this.getKernelPickerQuickPickItems(newMatchResult);
157
- quickPick.keepScrollPosition = true;
158
-
159
- // recalculate active items
160
- const activeItems: KernelQuickPickItem[] = [];
161
- for (const item of currentActiveItems) {
162
- if (isKernelPick(item)) {
163
- const kernelId = item.kernel.id;
164
- const sameItem = newQuickPickItems.find(pi => isKernelPick(pi) && pi.kernel.id === kernelId) as KernelPick | undefined;
165
- if (sameItem) {
166
- activeItems.push(sameItem);
167
- }
168
- } else if (isSourcePick(item)) {
169
- const sameItem = newQuickPickItems.find(pi => isSourcePick(pi) && pi.action.command.id === item.action.command.id) as SourcePick | undefined;
170
- if (sameItem) {
171
- activeItems.push(sameItem);
172
- }
173
- }
174
- }
175
-
176
- quickPick.items = newQuickPickItems;
177
- quickPick.activeItems = activeItems;
178
- }, this);
179
-
180
- const pick = await new Promise<{ selected: KernelQuickPickItem | undefined; items: KernelQuickPickItem[] }>((resolve, reject) => {
181
- quickPick.onDidAccept(() => {
182
- const item = quickPick.selectedItems[0];
183
- if (item) {
184
- resolve({ selected: item, items: quickPick.items as KernelQuickPickItem[] });
185
- } else {
186
- resolve({ selected: undefined, items: quickPick.items as KernelQuickPickItem[] });
187
- }
188
-
189
- quickPick.hide();
190
- });
191
-
192
- quickPick.onDidHide(() => {
193
- kernelDetectionTaskListener.dispose();
194
- kernelChangeEventListener?.dispose();
195
- quickPick.dispose();
196
- resolve({ selected: undefined, items: quickPick.items as KernelQuickPickItem[] });
197
- });
198
- quickPick.show();
199
- });
200
-
201
- if (pick.selected) {
202
- return this.handleQuickPick(editor, pick.selected, pick.items);
203
- }
204
-
205
- return false;
206
- }
207
-
208
- protected getKernelPickerQuickPickItems(matchResult: NotebookKernelMatchResult): QuickPickInput<KernelQuickPickItem>[] {
209
- const quickPickItems: QuickPickInput<KernelQuickPickItem>[] = [];
210
-
211
- if (matchResult.selected) {
212
- const kernelItem = toKernelQuickPick(matchResult.selected, matchResult.selected);
213
- quickPickItems.push(kernelItem);
214
- }
215
-
216
- // TODO use suggested here when kernel affinity is implemented. For now though show all kernels
217
- matchResult.all.filter(kernel => kernel.id !== matchResult.selected?.id).map(kernel => toKernelQuickPick(kernel, matchResult.selected))
218
- .forEach(kernel => {
219
- quickPickItems.push(kernel);
220
- });
221
-
222
- const shouldAutoRun = quickPickItems.length === 0;
223
-
224
- if (quickPickItems.length > 0) {
225
- quickPickItems.push({
226
- type: 'separator'
227
- });
228
- }
229
-
230
- // select another kernel quick pick
231
- quickPickItems.push({
232
- id: 'selectAnother',
233
- label: nls.localizeByDefault('Select Another Kernel...'),
234
- autoRun: shouldAutoRun
235
- });
236
-
237
- return quickPickItems;
238
- }
239
-
240
- protected selectKernel(notebook: NotebookModel, kernel: NotebookKernel): void {
241
- const currentInfo = this.notebookKernelService.getMatchingKernel(notebook);
242
- if (currentInfo.selected) {
243
- // there is already a selected kernel
244
- this.notebookKernelHistoryService.addMostRecentKernel(currentInfo.selected);
245
- }
246
- this.notebookKernelService.selectKernelForNotebook(kernel, notebook);
247
- this.notebookKernelHistoryService.addMostRecentKernel(kernel);
248
- }
249
-
250
- protected getMatchingResult(notebook: NotebookModel): NotebookKernelMatchResult {
251
- const { selected, all } = this.notebookKernelHistoryService.getKernels(notebook);
252
- const matchingResult = this.notebookKernelService.getMatchingKernel(notebook);
253
- return {
254
- selected: selected,
255
- all: matchingResult.all,
256
- suggestions: all,
257
- hidden: []
258
- };
259
- }
260
-
261
- protected async handleQuickPick(editor: NotebookModel, pick: KernelQuickPickItem, items: KernelQuickPickItem[]): Promise<boolean> {
262
- if (pick.id === 'selectAnother') {
263
- return this.displaySelectAnotherQuickPick(editor, items.length === 1 && items[0] === pick);
264
- }
265
-
266
- if (isKernelPick(pick)) {
267
- const newKernel = pick.kernel;
268
- this.selectKernel(editor, newKernel);
269
- return true;
270
- }
271
-
272
- if (isSourcePick(pick)) {
273
- // selected explicitly, it should trigger the execution?
274
- pick.action.run(this.commandService);
275
- }
276
-
277
- return true;
278
- }
279
-
280
- protected async displaySelectAnotherQuickPick(editor: NotebookModel, kernelListEmpty: boolean): Promise<boolean> {
281
- const notebook: NotebookModel = editor;
282
- const disposables = new DisposableCollection();
283
- const quickPick = this.quickInputService.createQuickPick<KernelQuickPickItem>();
284
- const quickPickItem = await new Promise<KernelQuickPickItem | QuickInputButton | undefined>(resolve => {
285
- // select from kernel sources
286
- quickPick.title = kernelListEmpty ? nls.localizeByDefault('Select Kernel') : nls.localizeByDefault('Select Another Kernel');
287
- quickPick.placeholder = nls.localizeByDefault('Type to choose a kernel source');
288
- quickPick.busy = true;
289
- // quickPick.buttons = [this.quickInputService.backButton];
290
- quickPick.show();
291
-
292
- disposables.push(quickPick.onDidTriggerButton(button => {
293
- if (button === this.quickInputService.backButton) {
294
- resolve(button);
295
- }
296
- }));
297
- quickPick.onDidTriggerItemButton(async e => {
298
-
299
- if (isKernelSourceQuickPickItem(e.item) && e.item.documentation !== undefined) {
300
- const uri: URI | undefined = this.isUri(e.item.documentation) ? new URI(e.item.documentation) : await this.commandService.executeCommand(e.item.documentation);
301
- if (uri) {
302
- (await this.openerService.getOpener(uri, { openExternal: true })).open(uri, { openExternal: true });
303
- }
304
- }
305
- });
306
- disposables.push(quickPick.onDidAccept(async () => {
307
- resolve(quickPick.selectedItems[0]);
308
- }));
309
- disposables.push(quickPick.onDidHide(() => {
310
- resolve(undefined);
311
- }));
312
-
313
- this.calculateKernelSources(editor).then(quickPickItems => {
314
- quickPick.items = quickPickItems;
315
- if (quickPick.items.length > 0) {
316
- quickPick.busy = false;
317
- }
318
- });
319
-
320
- debounce(
321
- Event.any(
322
- this.notebookKernelService.onDidChangeSourceActions,
323
- this.notebookKernelService.onDidAddKernel,
324
- this.notebookKernelService.onDidRemoveKernel
325
- ),
326
- KERNEL_PICKER_UPDATE_DEBOUNCE,
327
- )(async () => {
328
- quickPick.busy = true;
329
- const quickPickItems = await this.calculateKernelSources(editor);
330
- quickPick.items = quickPickItems;
331
- quickPick.busy = false;
332
- });
333
- });
334
-
335
- quickPick.hide();
336
- disposables.dispose();
337
-
338
- if (quickPickItem === this.quickInputService.backButton) {
339
- return this.showQuickPick(editor, undefined, true);
340
- }
341
-
342
- if (quickPickItem) {
343
- const selectedKernelPickItem = quickPickItem as KernelQuickPickItem;
344
- if (isKernelSourceQuickPickItem(selectedKernelPickItem)) {
345
- try {
346
- const selectedKernelId = await this.executeCommand<string>(notebook, selectedKernelPickItem.command);
347
- if (selectedKernelId) {
348
- const { all } = this.getMatchingResult(notebook);
349
- const notebookKernel = all.find(kernel => kernel.id === `ms-toolsai.jupyter/${selectedKernelId}`);
350
- if (notebookKernel) {
351
- this.selectKernel(notebook, notebookKernel);
352
- return true;
353
- }
354
- return true;
355
- } else {
356
- return this.displaySelectAnotherQuickPick(editor, false);
357
- }
358
- } catch (ex) {
359
- console.error('Failed to select notebook kernel', ex);
360
- return false;
361
- }
362
- } else if (isKernelPick(selectedKernelPickItem)) {
363
- this.selectKernel(notebook, selectedKernelPickItem.kernel);
364
- return true;
365
- } else if (isGroupedKernelsPick(selectedKernelPickItem)) {
366
- await this.selectOneKernel(notebook, selectedKernelPickItem.source, selectedKernelPickItem.kernels);
367
- return true;
368
- } else if (isSourcePick(selectedKernelPickItem)) {
369
- // selected explicitly, it should trigger the execution?
370
- try {
371
- await selectedKernelPickItem.action.run(this.commandService);
372
- return true;
373
- } catch (ex) {
374
- console.error('Failed to select notebook kernel', ex);
375
- return false;
376
- }
377
- }
378
- // } else if (isSearchMarketplacePick(selectedKernelPickItem)) {
379
- // await this.showKernelExtension(
380
- // this.paneCompositePartService,
381
- // this.extensionWorkbenchService,
382
- // this.extensionService,
383
- // editor.textModel.viewType,
384
- // []
385
- // );
386
- // return true;
387
- // } else if (isInstallExtensionPick(selectedKernelPickItem)) {
388
- // await this.showKernelExtension(
389
- // this.paneCompositePartService,
390
- // this.extensionWorkbenchService,
391
- // this.extensionService,
392
- // editor.textModel.viewType,
393
- // selectedKernelPickItem.extensionIds,
394
- // );
395
- // return true;
396
- // }
397
- }
398
-
399
- return false;
400
- }
401
-
402
- protected isUri(value: string): boolean {
403
- return /^(?<scheme>\w[\w\d+.-]*):/.test(value);
404
- }
405
-
406
- protected async calculateKernelSources(editor: NotebookModel): Promise<QuickPickInput<KernelQuickPickItem>[]> {
407
- const notebook: NotebookModel = editor;
408
-
409
- const actions = await this.notebookKernelService.getKernelSourceActionsFromProviders(notebook);
410
- const matchResult = this.getMatchingResult(notebook);
411
-
412
- const others = matchResult.all.filter(item => item.extensionId !== JUPYTER_EXTENSION_ID);
413
- const quickPickItems: QuickPickInput<KernelQuickPickItem>[] = [];
414
-
415
- // group controllers by extension
416
- for (const group of ArrayUtils.groupBy(others, (a, b) => a.extensionId === b.extensionId ? 0 : 1)) {
417
- const source = group[0].extensionId;
418
- if (group.length > 1) {
419
- quickPickItems.push({
420
- label: source,
421
- kernels: group
422
- });
423
- } else {
424
- quickPickItems.push({
425
- label: group[0].label,
426
- kernel: group[0]
427
- });
428
- }
429
- }
430
-
431
- const validActions = actions.filter(action => action.command);
432
-
433
- quickPickItems.push(...validActions.map(action => {
434
- const buttons = action.documentation ? [{
435
- iconClass: codicon('info'),
436
- tooltip: nls.localizeByDefault('Learn More'),
437
- }] : [];
438
- return {
439
- id: typeof action.command! === 'string' ? action.command! : action.command!.id,
440
- label: action.label,
441
- description: action.description,
442
- command: action.command,
443
- documentation: action.documentation,
444
- buttons
445
- };
446
- }));
447
-
448
- return quickPickItems;
449
- }
450
-
451
- protected async selectOneKernel(notebook: NotebookModel, source: string, kernels: NotebookKernel[]): Promise<void> {
452
- const quickPickItems: QuickPickInput<KernelPick>[] = kernels.map(kernel => toKernelQuickPick(kernel, undefined));
453
- const quickPick = this.quickInputService.createQuickPick<KernelQuickPickItem>();
454
- quickPick.items = quickPickItems;
455
- quickPick.canSelectMany = false;
456
-
457
- quickPick.title = nls.localizeByDefault('Select Kernel from {0}', source);
458
-
459
- quickPick.onDidAccept(async () => {
460
- if (quickPick.selectedItems && quickPick.selectedItems.length > 0 && isKernelPick(quickPick.selectedItems[0])) {
461
- this.selectKernel(notebook, quickPick.selectedItems[0].kernel);
462
- }
463
-
464
- quickPick.hide();
465
- quickPick.dispose();
466
- });
467
-
468
- quickPick.onDidHide(() => {
469
- quickPick.dispose();
470
- });
471
-
472
- quickPick.show();
473
- }
474
-
475
- protected async executeCommand<T>(notebook: NotebookModel, command: NotebookCommand): Promise<T | undefined | void> {
476
- const args = (command.arguments || []).concat([NotebookModelResource.create(notebook.uri)]);
477
- return this.commandService.executeCommand(command.id, ...args);
478
- }
479
- }
1
+
2
+ // *****************************************************************************
3
+ // Copyright (C) 2023 TypeFox and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ /*---------------------------------------------------------------------------------------------
18
+ * Copyright (c) Microsoft Corporation. All rights reserved.
19
+ * Licensed under the MIT License. See License.txt in the project root for license information.
20
+ *--------------------------------------------------------------------------------------------*/
21
+ import { ArrayUtils, CommandService, DisposableCollection, Event, nls, QuickInputButton, QuickInputService, QuickPickInput, QuickPickItem, URI, } from '@theia/core';
22
+ import { inject, injectable } from '@theia/core/shared/inversify';
23
+ import { NotebookKernelService, NotebookKernel, NotebookKernelMatchResult, SourceCommand } from './notebook-kernel-service';
24
+ import { NotebookModel } from '../view-model/notebook-model';
25
+ import { NotebookEditorWidget } from '../notebook-editor-widget';
26
+ import { codicon, OpenerService } from '@theia/core/lib/browser';
27
+ import { NotebookKernelHistoryService } from './notebook-kernel-history-service';
28
+ import { NotebookCommand, NotebookModelResource } from '../../common';
29
+ import debounce = require('@theia/core/shared/lodash.debounce');
30
+
31
+ export const JUPYTER_EXTENSION_ID = 'ms-toolsai.jupyter';
32
+
33
+ type KernelPick = QuickPickItem & { kernel: NotebookKernel };
34
+ function isKernelPick(item: QuickPickInput<QuickPickItem>): item is KernelPick {
35
+ return 'kernel' in item;
36
+ }
37
+ type GroupedKernelsPick = QuickPickItem & { kernels: NotebookKernel[]; source: string };
38
+ function isGroupedKernelsPick(item: QuickPickInput<QuickPickItem>): item is GroupedKernelsPick {
39
+ return 'kernels' in item;
40
+ }
41
+ type SourcePick = QuickPickItem & { action: SourceCommand };
42
+ function isSourcePick(item: QuickPickInput<QuickPickItem>): item is SourcePick {
43
+ return 'action' in item;
44
+ }
45
+ type InstallExtensionPick = QuickPickItem & { extensionIds: string[] };
46
+
47
+ type KernelSourceQuickPickItem = QuickPickItem & { command: NotebookCommand; documentation?: string };
48
+ function isKernelSourceQuickPickItem(item: QuickPickItem): item is KernelSourceQuickPickItem {
49
+ return 'command' in item;
50
+ }
51
+
52
+ function supportAutoRun(item: QuickPickInput<KernelQuickPickItem>): item is QuickPickItem {
53
+ return 'autoRun' in item && !!item.autoRun;
54
+ }
55
+
56
+ type KernelQuickPickItem = (QuickPickItem & { autoRun?: boolean }) | InstallExtensionPick | KernelPick | GroupedKernelsPick | SourcePick | KernelSourceQuickPickItem;
57
+
58
+ const KERNEL_PICKER_UPDATE_DEBOUNCE = 200;
59
+
60
+ export type KernelQuickPickContext =
61
+ { id: string; extension: string } |
62
+ { notebookEditorId: string } |
63
+ { id: string; extension: string; notebookEditorId: string } |
64
+ { ui?: boolean; notebookEditor?: NotebookEditorWidget };
65
+
66
+ function toKernelQuickPick(kernel: NotebookKernel, selected: NotebookKernel | undefined): KernelPick {
67
+ const res: KernelPick = {
68
+ kernel,
69
+ label: kernel.label,
70
+ description: kernel.description,
71
+ detail: kernel.detail
72
+ };
73
+ if (kernel.id === selected?.id) {
74
+ if (!res.description) {
75
+ res.description = nls.localizeByDefault('Currently Selected');
76
+ } else {
77
+ res.description = nls.localizeByDefault('{0} - Currently Selected', res.description);
78
+ }
79
+ }
80
+ return res;
81
+ }
82
+
83
+ @injectable()
84
+ export class NotebookKernelQuickPickService {
85
+
86
+ @inject(NotebookKernelService)
87
+ protected readonly notebookKernelService: NotebookKernelService;
88
+ @inject(QuickInputService)
89
+ protected readonly quickInputService: QuickInputService;
90
+ @inject(CommandService)
91
+ protected readonly commandService: CommandService;
92
+
93
+ @inject(OpenerService)
94
+ protected openerService: OpenerService;
95
+
96
+ @inject(NotebookKernelHistoryService)
97
+ protected notebookKernelHistoryService: NotebookKernelHistoryService;
98
+
99
+ async showQuickPick(editor: NotebookModel, wantedId?: string, skipAutoRun?: boolean): Promise<boolean> {
100
+ const notebook = editor;
101
+ const matchResult = this.getMatchingResult(notebook);
102
+ const { selected, all } = matchResult;
103
+
104
+ let newKernel: NotebookKernel | undefined;
105
+ if (wantedId) {
106
+ for (const candidate of all) {
107
+ if (candidate.id === wantedId) {
108
+ newKernel = candidate;
109
+ break;
110
+ }
111
+ }
112
+ if (!newKernel) {
113
+ console.warn(`wanted kernel DOES NOT EXIST, wanted: ${wantedId}, all: ${all.map(k => k.id)}`);
114
+ return false;
115
+ }
116
+ }
117
+
118
+ if (newKernel) {
119
+ this.selectKernel(notebook, newKernel);
120
+ return true;
121
+ }
122
+
123
+ const quickPick = this.quickInputService.createQuickPick<KernelQuickPickItem>();
124
+ const quickPickItems = this.getKernelPickerQuickPickItems(matchResult);
125
+
126
+ if (quickPickItems.length === 1 && supportAutoRun(quickPickItems[0]) && !skipAutoRun) {
127
+ return this.handleQuickPick(editor, quickPickItems[0], quickPickItems as KernelQuickPickItem[]);
128
+ }
129
+
130
+ quickPick.items = quickPickItems;
131
+ quickPick.canSelectMany = false;
132
+ quickPick.placeholder = selected
133
+ ? nls.localizeByDefault("Change kernel for '{0}'", 'current') // TODO get label for current notebook from a label provider
134
+ : nls.localizeByDefault("Select kernel for '{0}'", 'current');
135
+
136
+ quickPick.busy = this.notebookKernelService.getKernelDetectionTasks(notebook).length > 0;
137
+
138
+ const kernelDetectionTaskListener = this.notebookKernelService.onDidChangeKernelDetectionTasks(() => {
139
+ quickPick.busy = this.notebookKernelService.getKernelDetectionTasks(notebook).length > 0;
140
+ });
141
+
142
+ const kernelChangeEventListener = debounce(
143
+ Event.any(
144
+ this.notebookKernelService.onDidChangeSourceActions,
145
+ this.notebookKernelService.onDidAddKernel,
146
+ this.notebookKernelService.onDidRemoveKernel,
147
+ this.notebookKernelService.onDidChangeNotebookAffinity
148
+ ),
149
+ KERNEL_PICKER_UPDATE_DEBOUNCE
150
+ )(async () => {
151
+ // reset quick pick progress
152
+ quickPick.busy = false;
153
+
154
+ const currentActiveItems = quickPick.activeItems;
155
+ const newMatchResult = this.getMatchingResult(notebook);
156
+ const newQuickPickItems = this.getKernelPickerQuickPickItems(newMatchResult);
157
+ quickPick.keepScrollPosition = true;
158
+
159
+ // recalculate active items
160
+ const activeItems: KernelQuickPickItem[] = [];
161
+ for (const item of currentActiveItems) {
162
+ if (isKernelPick(item)) {
163
+ const kernelId = item.kernel.id;
164
+ const sameItem = newQuickPickItems.find(pi => isKernelPick(pi) && pi.kernel.id === kernelId) as KernelPick | undefined;
165
+ if (sameItem) {
166
+ activeItems.push(sameItem);
167
+ }
168
+ } else if (isSourcePick(item)) {
169
+ const sameItem = newQuickPickItems.find(pi => isSourcePick(pi) && pi.action.command.id === item.action.command.id) as SourcePick | undefined;
170
+ if (sameItem) {
171
+ activeItems.push(sameItem);
172
+ }
173
+ }
174
+ }
175
+
176
+ quickPick.items = newQuickPickItems;
177
+ quickPick.activeItems = activeItems;
178
+ }, this);
179
+
180
+ const pick = await new Promise<{ selected: KernelQuickPickItem | undefined; items: KernelQuickPickItem[] }>((resolve, reject) => {
181
+ quickPick.onDidAccept(() => {
182
+ const item = quickPick.selectedItems[0];
183
+ if (item) {
184
+ resolve({ selected: item, items: quickPick.items as KernelQuickPickItem[] });
185
+ } else {
186
+ resolve({ selected: undefined, items: quickPick.items as KernelQuickPickItem[] });
187
+ }
188
+
189
+ quickPick.hide();
190
+ });
191
+
192
+ quickPick.onDidHide(() => {
193
+ kernelDetectionTaskListener.dispose();
194
+ kernelChangeEventListener?.dispose();
195
+ quickPick.dispose();
196
+ resolve({ selected: undefined, items: quickPick.items as KernelQuickPickItem[] });
197
+ });
198
+ quickPick.show();
199
+ });
200
+
201
+ if (pick.selected) {
202
+ return this.handleQuickPick(editor, pick.selected, pick.items);
203
+ }
204
+
205
+ return false;
206
+ }
207
+
208
+ protected getKernelPickerQuickPickItems(matchResult: NotebookKernelMatchResult): QuickPickInput<KernelQuickPickItem>[] {
209
+ const quickPickItems: QuickPickInput<KernelQuickPickItem>[] = [];
210
+
211
+ if (matchResult.selected) {
212
+ const kernelItem = toKernelQuickPick(matchResult.selected, matchResult.selected);
213
+ quickPickItems.push(kernelItem);
214
+ }
215
+
216
+ // TODO use suggested here when kernel affinity is implemented. For now though show all kernels
217
+ matchResult.all.filter(kernel => kernel.id !== matchResult.selected?.id).map(kernel => toKernelQuickPick(kernel, matchResult.selected))
218
+ .forEach(kernel => {
219
+ quickPickItems.push(kernel);
220
+ });
221
+
222
+ const shouldAutoRun = quickPickItems.length === 0;
223
+
224
+ if (quickPickItems.length > 0) {
225
+ quickPickItems.push({
226
+ type: 'separator'
227
+ });
228
+ }
229
+
230
+ // select another kernel quick pick
231
+ quickPickItems.push({
232
+ id: 'selectAnother',
233
+ label: nls.localizeByDefault('Select Another Kernel...'),
234
+ autoRun: shouldAutoRun
235
+ });
236
+
237
+ return quickPickItems;
238
+ }
239
+
240
+ protected selectKernel(notebook: NotebookModel, kernel: NotebookKernel): void {
241
+ const currentInfo = this.notebookKernelService.getMatchingKernel(notebook);
242
+ if (currentInfo.selected) {
243
+ // there is already a selected kernel
244
+ this.notebookKernelHistoryService.addMostRecentKernel(currentInfo.selected);
245
+ }
246
+ this.notebookKernelService.selectKernelForNotebook(kernel, notebook);
247
+ this.notebookKernelHistoryService.addMostRecentKernel(kernel);
248
+ }
249
+
250
+ protected getMatchingResult(notebook: NotebookModel): NotebookKernelMatchResult {
251
+ const { selected, all } = this.notebookKernelHistoryService.getKernels(notebook);
252
+ const matchingResult = this.notebookKernelService.getMatchingKernel(notebook);
253
+ return {
254
+ selected: selected,
255
+ all: matchingResult.all,
256
+ suggestions: all,
257
+ hidden: []
258
+ };
259
+ }
260
+
261
+ protected async handleQuickPick(editor: NotebookModel, pick: KernelQuickPickItem, items: KernelQuickPickItem[]): Promise<boolean> {
262
+ if (pick.id === 'selectAnother') {
263
+ return this.displaySelectAnotherQuickPick(editor, items.length === 1 && items[0] === pick);
264
+ }
265
+
266
+ if (isKernelPick(pick)) {
267
+ const newKernel = pick.kernel;
268
+ this.selectKernel(editor, newKernel);
269
+ return true;
270
+ }
271
+
272
+ if (isSourcePick(pick)) {
273
+ // selected explicitly, it should trigger the execution?
274
+ pick.action.run(this.commandService);
275
+ }
276
+
277
+ return true;
278
+ }
279
+
280
+ protected async displaySelectAnotherQuickPick(editor: NotebookModel, kernelListEmpty: boolean): Promise<boolean> {
281
+ const notebook: NotebookModel = editor;
282
+ const disposables = new DisposableCollection();
283
+ const quickPick = this.quickInputService.createQuickPick<KernelQuickPickItem>();
284
+ const quickPickItem = await new Promise<KernelQuickPickItem | QuickInputButton | undefined>(resolve => {
285
+ // select from kernel sources
286
+ quickPick.title = kernelListEmpty ? nls.localizeByDefault('Select Kernel') : nls.localizeByDefault('Select Another Kernel');
287
+ quickPick.placeholder = nls.localizeByDefault('Type to choose a kernel source');
288
+ quickPick.busy = true;
289
+ // quickPick.buttons = [this.quickInputService.backButton];
290
+ quickPick.show();
291
+
292
+ disposables.push(quickPick.onDidTriggerButton(button => {
293
+ if (button === this.quickInputService.backButton) {
294
+ resolve(button);
295
+ }
296
+ }));
297
+ quickPick.onDidTriggerItemButton(async e => {
298
+
299
+ if (isKernelSourceQuickPickItem(e.item) && e.item.documentation !== undefined) {
300
+ const uri: URI | undefined = this.isUri(e.item.documentation) ? new URI(e.item.documentation) : await this.commandService.executeCommand(e.item.documentation);
301
+ if (uri) {
302
+ (await this.openerService.getOpener(uri, { openExternal: true })).open(uri, { openExternal: true });
303
+ }
304
+ }
305
+ });
306
+ disposables.push(quickPick.onDidAccept(async () => {
307
+ resolve(quickPick.selectedItems[0]);
308
+ }));
309
+ disposables.push(quickPick.onDidHide(() => {
310
+ resolve(undefined);
311
+ }));
312
+
313
+ this.calculateKernelSources(editor).then(quickPickItems => {
314
+ quickPick.items = quickPickItems;
315
+ if (quickPick.items.length > 0) {
316
+ quickPick.busy = false;
317
+ }
318
+ });
319
+
320
+ debounce(
321
+ Event.any(
322
+ this.notebookKernelService.onDidChangeSourceActions,
323
+ this.notebookKernelService.onDidAddKernel,
324
+ this.notebookKernelService.onDidRemoveKernel
325
+ ),
326
+ KERNEL_PICKER_UPDATE_DEBOUNCE,
327
+ )(async () => {
328
+ quickPick.busy = true;
329
+ const quickPickItems = await this.calculateKernelSources(editor);
330
+ quickPick.items = quickPickItems;
331
+ quickPick.busy = false;
332
+ });
333
+ });
334
+
335
+ quickPick.hide();
336
+ disposables.dispose();
337
+
338
+ if (quickPickItem === this.quickInputService.backButton) {
339
+ return this.showQuickPick(editor, undefined, true);
340
+ }
341
+
342
+ if (quickPickItem) {
343
+ const selectedKernelPickItem = quickPickItem as KernelQuickPickItem;
344
+ if (isKernelSourceQuickPickItem(selectedKernelPickItem)) {
345
+ try {
346
+ const selectedKernelId = await this.executeCommand<string>(notebook, selectedKernelPickItem.command);
347
+ if (selectedKernelId) {
348
+ const { all } = this.getMatchingResult(notebook);
349
+ const notebookKernel = all.find(kernel => kernel.id === `ms-toolsai.jupyter/${selectedKernelId}`);
350
+ if (notebookKernel) {
351
+ this.selectKernel(notebook, notebookKernel);
352
+ return true;
353
+ }
354
+ return true;
355
+ } else {
356
+ return this.displaySelectAnotherQuickPick(editor, false);
357
+ }
358
+ } catch (ex) {
359
+ console.error('Failed to select notebook kernel', ex);
360
+ return false;
361
+ }
362
+ } else if (isKernelPick(selectedKernelPickItem)) {
363
+ this.selectKernel(notebook, selectedKernelPickItem.kernel);
364
+ return true;
365
+ } else if (isGroupedKernelsPick(selectedKernelPickItem)) {
366
+ await this.selectOneKernel(notebook, selectedKernelPickItem.source, selectedKernelPickItem.kernels);
367
+ return true;
368
+ } else if (isSourcePick(selectedKernelPickItem)) {
369
+ // selected explicitly, it should trigger the execution?
370
+ try {
371
+ await selectedKernelPickItem.action.run(this.commandService);
372
+ return true;
373
+ } catch (ex) {
374
+ console.error('Failed to select notebook kernel', ex);
375
+ return false;
376
+ }
377
+ }
378
+ // } else if (isSearchMarketplacePick(selectedKernelPickItem)) {
379
+ // await this.showKernelExtension(
380
+ // this.paneCompositePartService,
381
+ // this.extensionWorkbenchService,
382
+ // this.extensionService,
383
+ // editor.textModel.viewType,
384
+ // []
385
+ // );
386
+ // return true;
387
+ // } else if (isInstallExtensionPick(selectedKernelPickItem)) {
388
+ // await this.showKernelExtension(
389
+ // this.paneCompositePartService,
390
+ // this.extensionWorkbenchService,
391
+ // this.extensionService,
392
+ // editor.textModel.viewType,
393
+ // selectedKernelPickItem.extensionIds,
394
+ // );
395
+ // return true;
396
+ // }
397
+ }
398
+
399
+ return false;
400
+ }
401
+
402
+ protected isUri(value: string): boolean {
403
+ return /^(?<scheme>\w[\w\d+.-]*):/.test(value);
404
+ }
405
+
406
+ protected async calculateKernelSources(editor: NotebookModel): Promise<QuickPickInput<KernelQuickPickItem>[]> {
407
+ const notebook: NotebookModel = editor;
408
+
409
+ const actions = await this.notebookKernelService.getKernelSourceActionsFromProviders(notebook);
410
+ const matchResult = this.getMatchingResult(notebook);
411
+
412
+ const others = matchResult.all.filter(item => item.extensionId !== JUPYTER_EXTENSION_ID);
413
+ const quickPickItems: QuickPickInput<KernelQuickPickItem>[] = [];
414
+
415
+ // group controllers by extension
416
+ for (const group of ArrayUtils.groupBy(others, (a, b) => a.extensionId === b.extensionId ? 0 : 1)) {
417
+ const source = group[0].extensionId;
418
+ if (group.length > 1) {
419
+ quickPickItems.push({
420
+ label: source,
421
+ kernels: group
422
+ });
423
+ } else {
424
+ quickPickItems.push({
425
+ label: group[0].label,
426
+ kernel: group[0]
427
+ });
428
+ }
429
+ }
430
+
431
+ const validActions = actions.filter(action => action.command);
432
+
433
+ quickPickItems.push(...validActions.map(action => {
434
+ const buttons = action.documentation ? [{
435
+ iconClass: codicon('info'),
436
+ tooltip: nls.localizeByDefault('Learn More'),
437
+ }] : [];
438
+ return {
439
+ id: typeof action.command! === 'string' ? action.command! : action.command!.id,
440
+ label: action.label,
441
+ description: action.description,
442
+ command: action.command,
443
+ documentation: action.documentation,
444
+ buttons
445
+ };
446
+ }));
447
+
448
+ return quickPickItems;
449
+ }
450
+
451
+ protected async selectOneKernel(notebook: NotebookModel, source: string, kernels: NotebookKernel[]): Promise<void> {
452
+ const quickPickItems: QuickPickInput<KernelPick>[] = kernels.map(kernel => toKernelQuickPick(kernel, undefined));
453
+ const quickPick = this.quickInputService.createQuickPick<KernelQuickPickItem>();
454
+ quickPick.items = quickPickItems;
455
+ quickPick.canSelectMany = false;
456
+
457
+ quickPick.title = nls.localizeByDefault('Select Kernel from {0}', source);
458
+
459
+ quickPick.onDidAccept(async () => {
460
+ if (quickPick.selectedItems && quickPick.selectedItems.length > 0 && isKernelPick(quickPick.selectedItems[0])) {
461
+ this.selectKernel(notebook, quickPick.selectedItems[0].kernel);
462
+ }
463
+
464
+ quickPick.hide();
465
+ quickPick.dispose();
466
+ });
467
+
468
+ quickPick.onDidHide(() => {
469
+ quickPick.dispose();
470
+ });
471
+
472
+ quickPick.show();
473
+ }
474
+
475
+ protected async executeCommand<T>(notebook: NotebookModel, command: NotebookCommand): Promise<T | undefined | void> {
476
+ const args = (command.arguments || []).concat([NotebookModelResource.create(notebook.uri)]);
477
+ return this.commandService.executeCommand(command.id, ...args);
478
+ }
479
+ }