-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
notebook.contribution.ts
793 lines (710 loc) · 32.5 KB
/
notebook.contribution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Schemas } from 'vs/base/common/network';
import { IDisposable, Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { parse } from 'vs/base/common/marshalling';
import { isEqual } from 'vs/base/common/resources';
import { assertType } from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import { format } from 'vs/base/common/jsonFormatter';
import { applyEdits } from 'vs/base/common/jsonEdit';
import { ITextModel, ITextBufferFactory, DefaultEndOfLine, ITextBuffer } from 'vs/editor/common/model';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
import { ITextModelContentProvider, ITextModelService } from 'vs/editor/common/services/resolverService';
import * as nls from 'vs/nls';
import { Extensions, IConfigurationPropertySchema, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { Registry } from 'vs/platform/registry/common/platform';
import { EditorPaneDescriptor, IEditorPaneRegistry } from 'vs/workbench/browser/editor';
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { IEditorSerializer, IEditorFactoryRegistry, EditorExtensions } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { NotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookEditor';
import { isCompositeNotebookEditorInput, NotebookEditorInput, NotebookEditorInputOptions } from 'vs/workbench/contrib/notebook/common/notebookEditorInput';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { NotebookService } from 'vs/workbench/contrib/notebook/browser/notebookServiceImpl';
import { CellKind, CellToolbarLocation, CellToolbarVisibility, CellUri, DisplayOrderKey, UndoRedoPerCell, IResolvedNotebookEditorModel, NotebookDocumentBackupData, NotebookTextDiffEditorPreview, NotebookWorkingCopyTypeIdentifier, ShowCellStatusBar, CompactView, FocusIndicator, InsertToolbarLocation, GlobalToolbar, ConsolidatedOutputButton, ShowFoldingControls, DragAndDropEnabled, NotebookCellEditorOptionsCustomizations, ConsolidatedRunButton, TextOutputLineLimit, GlobalToolbarShowLabel, IOutputItemDto } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
import { INotebookEditorModelResolverService } from 'vs/workbench/contrib/notebook/common/notebookEditorModelResolverService';
import { NotebookDiffEditorInput } from 'vs/workbench/contrib/notebook/browser/notebookDiffEditorInput';
import { NotebookTextDiffEditor } from 'vs/workbench/contrib/notebook/browser/diff/notebookTextDiffEditor';
import { INotebookEditorWorkerService } from 'vs/workbench/contrib/notebook/common/services/notebookWorkerService';
import { NotebookEditorWorkerServiceImpl } from 'vs/workbench/contrib/notebook/common/services/notebookWorkerServiceImpl';
import { INotebookCellStatusBarService } from 'vs/workbench/contrib/notebook/common/notebookCellStatusBarService';
import { NotebookCellStatusBarService } from 'vs/workbench/contrib/notebook/browser/notebookCellStatusBarServiceImpl';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/notebookEditorService';
import { NotebookEditorWidgetService } from 'vs/workbench/contrib/notebook/browser/notebookEditorServiceImpl';
import { IJSONContributionRegistry, Extensions as JSONExtensions } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema';
import { Event } from 'vs/base/common/event';
import { getFormatedMetadataJSON } from 'vs/workbench/contrib/notebook/browser/diff/diffElementViewModel';
import { NotebookModelResolverServiceImpl } from 'vs/workbench/contrib/notebook/common/notebookEditorModelResolverServiceImpl';
import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { NotebookKernelService } from 'vs/workbench/contrib/notebook/browser/notebookKernelServiceImpl';
import { IWorkingCopyIdentifier } from 'vs/workbench/services/workingCopy/common/workingCopy';
import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { IWorkingCopyEditorService } from 'vs/workbench/services/workingCopy/common/workingCopyEditorService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ILabelService } from 'vs/platform/label/common/label';
import { IWorkingCopyBackupService } from 'vs/workbench/services/workingCopy/common/workingCopyBackup';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { NotebookRendererMessagingService } from 'vs/workbench/contrib/notebook/browser/notebookRendererMessagingServiceImpl';
import { INotebookRendererMessagingService } from 'vs/workbench/contrib/notebook/common/notebookRendererMessagingService';
// Editor Controller
import 'vs/workbench/contrib/notebook/browser/controller/coreActions';
import 'vs/workbench/contrib/notebook/browser/controller/insertCellActions';
import 'vs/workbench/contrib/notebook/browser/controller/executeActions';
import 'vs/workbench/contrib/notebook/browser/controller/layoutActions';
import 'vs/workbench/contrib/notebook/browser/controller/editActions';
import 'vs/workbench/contrib/notebook/browser/controller/apiActions';
// Editor Contribution
import 'vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard';
import 'vs/workbench/contrib/notebook/browser/contrib/find/findController';
import 'vs/workbench/contrib/notebook/browser/contrib/fold/folding';
import 'vs/workbench/contrib/notebook/browser/contrib/format/formatting';
import 'vs/workbench/contrib/notebook/browser/contrib/gettingStarted/notebookGettingStarted';
import 'vs/workbench/contrib/notebook/browser/contrib/layout/layoutActions';
import 'vs/workbench/contrib/notebook/browser/contrib/marker/markerProvider';
import 'vs/workbench/contrib/notebook/browser/contrib/navigation/arrow';
import 'vs/workbench/contrib/notebook/browser/contrib/outline/notebookOutline';
import 'vs/workbench/contrib/notebook/browser/contrib/profile/notebookProfile';
import 'vs/workbench/contrib/notebook/browser/contrib/cellStatusBar/statusBarProviders';
import 'vs/workbench/contrib/notebook/browser/contrib/cellStatusBar/contributedStatusBarItemController';
import 'vs/workbench/contrib/notebook/browser/contrib/cellStatusBar/executionStatusBarItemController';
import 'vs/workbench/contrib/notebook/browser/contrib/editorStatusBar/editorStatusBar';
import 'vs/workbench/contrib/notebook/browser/contrib/undoRedo/notebookUndoRedo';
import 'vs/workbench/contrib/notebook/browser/contrib/cellCommands/cellCommands';
import 'vs/workbench/contrib/notebook/browser/contrib/viewportCustomMarkdown/viewportCustomMarkdown';
import 'vs/workbench/contrib/notebook/browser/contrib/troubleshoot/layout';
import 'vs/workbench/contrib/notebook/browser/contrib/codeRenderer/codeRenderer';
import 'vs/workbench/contrib/notebook/browser/contrib/breakpoints/notebookBreakpoints';
// Diff Editor Contribution
import 'vs/workbench/contrib/notebook/browser/diff/notebookDiffActions';
// Output renderers registration
import 'vs/workbench/contrib/notebook/browser/view/output/transforms/richTransform';
import { editorOptionsRegistry } from 'vs/editor/common/config/editorOptions';
import { NotebookExecutionService } from 'vs/workbench/contrib/notebook/browser/notebookExecutionServiceImpl';
import { INotebookExecutionService } from 'vs/workbench/contrib/notebook/common/notebookExecutionService';
import { INotebookKeymapService } from 'vs/workbench/contrib/notebook/common/notebookKeymapService';
import { NotebookKeymapService } from 'vs/workbench/contrib/notebook/browser/notebookKeymapServiceImpl';
/*--------------------------------------------------------------------------------------------- */
Registry.as<IEditorPaneRegistry>(EditorExtensions.EditorPane).registerEditorPane(
EditorPaneDescriptor.create(
NotebookEditor,
NotebookEditor.ID,
'Notebook Editor'
),
[
new SyncDescriptor(NotebookEditorInput)
]
);
Registry.as<IEditorPaneRegistry>(EditorExtensions.EditorPane).registerEditorPane(
EditorPaneDescriptor.create(
NotebookTextDiffEditor,
NotebookTextDiffEditor.ID,
'Notebook Diff Editor'
),
[
new SyncDescriptor(NotebookDiffEditorInput)
]
);
class NotebookDiffEditorSerializer implements IEditorSerializer {
canSerialize(): boolean {
return true;
}
serialize(input: EditorInput): string {
assertType(input instanceof NotebookDiffEditorInput);
return JSON.stringify({
resource: input.resource,
originalResource: input.original.resource,
name: input.getName(),
originalName: input.original.getName(),
textDiffName: input.getName(),
viewType: input.viewType,
});
}
deserialize(instantiationService: IInstantiationService, raw: string) {
type Data = { resource: URI, originalResource: URI, name: string, originalName: string, viewType: string, textDiffName: string | undefined, group: number; };
const data = <Data>parse(raw);
if (!data) {
return undefined;
}
const { resource, originalResource, name, viewType } = data;
if (!data || !URI.isUri(resource) || !URI.isUri(originalResource) || typeof name !== 'string' || typeof viewType !== 'string') {
return undefined;
}
const input = NotebookDiffEditorInput.create(instantiationService, resource, name, undefined, originalResource, viewType);
return input;
}
static canResolveBackup(editorInput: EditorInput, backupResource: URI): boolean {
return false;
}
}
type SerializedNotebookEditorData = { resource: URI, viewType: string, options?: NotebookEditorInputOptions };
class NotebookEditorSerializer implements IEditorSerializer {
canSerialize(): boolean {
return true;
}
serialize(input: EditorInput): string {
assertType(input instanceof NotebookEditorInput);
const data: SerializedNotebookEditorData = {
resource: input.resource,
viewType: input.viewType,
options: input.options
};
return JSON.stringify(data);
}
deserialize(instantiationService: IInstantiationService, raw: string) {
const data = <SerializedNotebookEditorData>parse(raw);
if (!data) {
return undefined;
}
const { resource, viewType, options } = data;
if (!data || !URI.isUri(resource) || typeof viewType !== 'string') {
return undefined;
}
const input = NotebookEditorInput.create(instantiationService, resource, viewType, options);
return input;
}
}
Registry.as<IEditorFactoryRegistry>(EditorExtensions.EditorFactory).registerEditorSerializer(
NotebookEditorInput.ID,
NotebookEditorSerializer
);
Registry.as<IEditorFactoryRegistry>(EditorExtensions.EditorFactory).registerEditorSerializer(
NotebookDiffEditorInput.ID,
NotebookDiffEditorSerializer
);
export class NotebookContribution extends Disposable implements IWorkbenchContribution {
constructor(
@IUndoRedoService undoRedoService: IUndoRedoService,
@IConfigurationService configurationService: IConfigurationService,
) {
super();
const undoRedoPerCell = configurationService.getValue<boolean>(UndoRedoPerCell);
this._register(undoRedoService.registerUriComparisonKeyComputer(CellUri.scheme, {
getComparisonKey: (uri: URI): string => {
if (undoRedoPerCell) {
return uri.toString();
}
return NotebookContribution._getCellUndoRedoComparisonKey(uri);
}
}));
}
private static _getCellUndoRedoComparisonKey(uri: URI) {
const data = CellUri.parse(uri);
if (!data) {
return uri.toString();
}
return data.notebook.toString();
}
}
class CellContentProvider implements ITextModelContentProvider {
private readonly _registration: IDisposable;
constructor(
@ITextModelService textModelService: ITextModelService,
@IModelService private readonly _modelService: IModelService,
@IModeService private readonly _modeService: IModeService,
@INotebookEditorModelResolverService private readonly _notebookModelResolverService: INotebookEditorModelResolverService,
) {
this._registration = textModelService.registerTextModelContentProvider(CellUri.scheme, this);
}
dispose(): void {
this._registration.dispose();
}
async provideTextContent(resource: URI): Promise<ITextModel | null> {
const existing = this._modelService.getModel(resource);
if (existing) {
return existing;
}
const data = CellUri.parse(resource);
// const data = parseCellUri(resource);
if (!data) {
return null;
}
const ref = await this._notebookModelResolverService.resolve(data.notebook);
let result: ITextModel | null = null;
for (const cell of ref.object.notebook.cells) {
if (cell.uri.toString() === resource.toString()) {
const bufferFactory: ITextBufferFactory = {
create: (defaultEOL) => {
const newEOL = (defaultEOL === DefaultEndOfLine.CRLF ? '\r\n' : '\n');
(cell.textBuffer as ITextBuffer).setEOL(newEOL);
return { textBuffer: cell.textBuffer as ITextBuffer, disposable: Disposable.None };
},
getFirstLineText: (limit: number) => {
return cell.textBuffer.getLineContent(1).substr(0, limit);
}
};
const languageId = this._modeService.getModeIdForLanguageName(cell.language);
const languageSelection = languageId ? this._modeService.create(languageId) : (cell.cellKind === CellKind.Markup ? this._modeService.create('markdown') : this._modeService.createByFilepathOrFirstLine(resource, cell.textBuffer.getLineContent(1)));
result = this._modelService.createModel(
bufferFactory,
languageSelection,
resource
);
break;
}
}
if (result) {
const once = Event.any(result.onWillDispose, ref.object.notebook.onWillDispose)(() => {
once.dispose();
ref.dispose();
});
}
return result;
}
}
class CellInfoContentProvider {
private readonly _disposables: IDisposable[] = [];
constructor(
@ITextModelService textModelService: ITextModelService,
@IModelService private readonly _modelService: IModelService,
@IModeService private readonly _modeService: IModeService,
@ILabelService private readonly _labelService: ILabelService,
@INotebookEditorModelResolverService private readonly _notebookModelResolverService: INotebookEditorModelResolverService,
) {
this._disposables.push(textModelService.registerTextModelContentProvider(Schemas.vscodeNotebookCellMetadata, {
provideTextContent: this.provideMetadataTextContent.bind(this)
}));
this._disposables.push(textModelService.registerTextModelContentProvider(Schemas.vscodeNotebookCellOutput, {
provideTextContent: this.provideOutputTextContent.bind(this)
}));
this._disposables.push(this._labelService.registerFormatter({
scheme: Schemas.vscodeNotebookCellMetadata,
formatting: {
label: '${path} (metadata)',
separator: '/'
}
}));
this._disposables.push(this._labelService.registerFormatter({
scheme: Schemas.vscodeNotebookCellOutput,
formatting: {
label: '${path} (output)',
separator: '/'
}
}));
}
dispose(): void {
this._disposables.forEach(d => d.dispose());
}
async provideMetadataTextContent(resource: URI): Promise<ITextModel | null> {
const existing = this._modelService.getModel(resource);
if (existing) {
return existing;
}
const data = CellUri.parseCellUri(resource, Schemas.vscodeNotebookCellMetadata);
if (!data) {
return null;
}
const ref = await this._notebookModelResolverService.resolve(data.notebook);
let result: ITextModel | null = null;
const mode = this._modeService.create('json');
for (const cell of ref.object.notebook.cells) {
if (cell.handle === data.handle) {
const metadataSource = getFormatedMetadataJSON(ref.object.notebook, cell.metadata, cell.language);
result = this._modelService.createModel(
metadataSource,
mode,
resource
);
break;
}
}
if (result) {
const once = result.onWillDispose(() => {
once.dispose();
ref.dispose();
});
}
return result;
}
private _getStreamOutputData(outputs: IOutputItemDto[]) {
if (!outputs.length) {
return null;
}
const first = outputs[0];
const mime = first.mime;
const sameStream = !outputs.find(op => op.mime !== mime);
if (sameStream) {
return outputs.map(opit => opit.data.toString()).join('');
} else {
return null;
}
}
async provideOutputTextContent(resource: URI): Promise<ITextModel | null> {
const existing = this._modelService.getModel(resource);
if (existing) {
return existing;
}
const data = CellUri.parseCellUri(resource, Schemas.vscodeNotebookCellOutput);
if (!data) {
return null;
}
const ref = await this._notebookModelResolverService.resolve(data.notebook);
let result: ITextModel | null = null;
const mode = this._modeService.create('json');
for (const cell of ref.object.notebook.cells) {
if (cell.handle === data.handle) {
if (cell.outputs.length === 1) {
// single output
const streamOutputData = this._getStreamOutputData(cell.outputs[0].outputs);
if (streamOutputData) {
result = this._modelService.createModel(
streamOutputData,
this._modeService.create('plaintext'),
resource
);
break;
}
}
const content = JSON.stringify(cell.outputs.map(output => ({
metadata: output.metadata,
outputItems: output.outputs.map(opit => ({
mimeType: opit.mime,
data: opit.data.toString()
}))
})));
const edits = format(content, undefined, {});
const outputSource = applyEdits(content, edits);
result = this._modelService.createModel(
outputSource,
mode,
resource
);
break;
}
}
if (result) {
const once = result.onWillDispose(() => {
once.dispose();
ref.dispose();
});
}
return result;
}
}
class RegisterSchemasContribution extends Disposable implements IWorkbenchContribution {
constructor() {
super();
this.registerMetadataSchemas();
}
private registerMetadataSchemas(): void {
const jsonRegistry = Registry.as<IJSONContributionRegistry>(JSONExtensions.JSONContribution);
const metadataSchema: IJSONSchema = {
properties: {
['language']: {
type: 'string',
description: 'The language for the cell'
},
['inputCollapsed']: {
type: 'boolean',
description: `Whether a code cell's editor is collapsed`
},
['outputCollapsed']: {
type: 'boolean',
description: `Whether a code cell's outputs are collapsed`
}
},
// patternProperties: allSettings.patternProperties,
additionalProperties: true,
allowTrailingCommas: true,
allowComments: true
};
jsonRegistry.registerSchema('vscode://schemas/notebook/cellmetadata', metadataSchema);
}
}
class NotebookEditorManager implements IWorkbenchContribution {
private readonly _disposables = new DisposableStore();
constructor(
@IEditorService private readonly _editorService: IEditorService,
@INotebookEditorModelResolverService private readonly _notebookEditorModelService: INotebookEditorModelResolverService,
@INotebookService notebookService: INotebookService,
@IEditorGroupsService editorGroups: IEditorGroupsService,
) {
// OPEN notebook editor for models that have turned dirty without being visible in an editor
type E = IResolvedNotebookEditorModel;
this._disposables.add(Event.debounce<E, E[]>(
this._notebookEditorModelService.onDidChangeDirty,
(last, current) => !last ? [current] : [...last, current],
100
)(this._openMissingDirtyNotebookEditors, this));
// CLOSE notebook editor for models that have no more serializer
this._disposables.add(notebookService.onWillRemoveViewType(e => {
for (const group of editorGroups.groups) {
const staleInputs = group.editors.filter(input => input instanceof NotebookEditorInput && input.viewType === e);
group.closeEditors(staleInputs);
}
}));
}
dispose(): void {
this._disposables.dispose();
}
private _openMissingDirtyNotebookEditors(models: IResolvedNotebookEditorModel[]): void {
const result: IResourceEditorInput[] = [];
for (let model of models) {
if (model.isDirty() && !this._editorService.isOpened({ resource: model.resource, typeId: NotebookEditorInput.ID, editorId: model.viewType }) && model.resource.scheme !== Schemas.vscodeInteractive) {
result.push({
resource: model.resource,
options: { inactive: true, preserveFocus: true, pinned: true, override: model.viewType }
});
}
}
if (result.length > 0) {
this._editorService.openEditors(result);
}
}
}
class SimpleNotebookWorkingCopyEditorHandler extends Disposable implements IWorkbenchContribution {
constructor(
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IWorkingCopyEditorService private readonly _workingCopyEditorService: IWorkingCopyEditorService,
@IExtensionService private readonly _extensionService: IExtensionService
) {
super();
this._installHandler();
}
private async _installHandler(): Promise<void> {
await this._extensionService.whenInstalledExtensionsRegistered();
this._register(this._workingCopyEditorService.registerHandler({
handles: workingCopy => typeof this._getViewType(workingCopy) === 'string',
isOpen: (workingCopy, editor) => editor instanceof NotebookEditorInput && editor.viewType === this._getViewType(workingCopy) && isEqual(workingCopy.resource, editor.resource),
createEditor: workingCopy => NotebookEditorInput.create(this._instantiationService, workingCopy.resource, this._getViewType(workingCopy)!)
}));
}
private _getViewType(workingCopy: IWorkingCopyIdentifier): string | undefined {
return NotebookWorkingCopyTypeIdentifier.parse(workingCopy.typeId);
}
}
class ComplexNotebookWorkingCopyEditorHandler extends Disposable implements IWorkbenchContribution {
constructor(
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IWorkingCopyEditorService private readonly _workingCopyEditorService: IWorkingCopyEditorService,
@IExtensionService private readonly _extensionService: IExtensionService,
@IWorkingCopyBackupService private readonly _workingCopyBackupService: IWorkingCopyBackupService
) {
super();
this._installHandler();
}
private async _installHandler(): Promise<void> {
await this._extensionService.whenInstalledExtensionsRegistered();
this._register(this._workingCopyEditorService.registerHandler({
handles: workingCopy => workingCopy.resource.scheme === Schemas.vscodeNotebook,
isOpen: (workingCopy, editor) => {
if (isCompositeNotebookEditorInput(editor)) {
return !!editor.editorInputs.find(input => isEqual(URI.from({ scheme: Schemas.vscodeNotebook, path: input.resource.toString() }), workingCopy.resource));
}
return editor instanceof NotebookEditorInput && isEqual(URI.from({ scheme: Schemas.vscodeNotebook, path: editor.resource.toString() }), workingCopy.resource);
},
createEditor: async workingCopy => {
// TODO this is really bad and should adopt the `typeId`
// for backups instead of storing that information in the
// backup.
// But since complex notebooks are deprecated, not worth
// pushing for it and should eventually delete this code
// entirely.
const backup = await this._workingCopyBackupService.resolve<NotebookDocumentBackupData>(workingCopy);
if (!backup?.meta) {
throw new Error(`No backup found for Notebook editor: ${workingCopy.resource}`);
}
return NotebookEditorInput.create(this._instantiationService, workingCopy.resource, backup.meta.viewType, { startDirty: true });
}
}));
}
}
const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchContributionsRegistry.registerWorkbenchContribution(NotebookContribution, LifecyclePhase.Starting);
workbenchContributionsRegistry.registerWorkbenchContribution(CellContentProvider, LifecyclePhase.Starting);
workbenchContributionsRegistry.registerWorkbenchContribution(CellInfoContentProvider, LifecyclePhase.Starting);
workbenchContributionsRegistry.registerWorkbenchContribution(RegisterSchemasContribution, LifecyclePhase.Starting);
workbenchContributionsRegistry.registerWorkbenchContribution(NotebookEditorManager, LifecyclePhase.Ready);
workbenchContributionsRegistry.registerWorkbenchContribution(SimpleNotebookWorkingCopyEditorHandler, LifecyclePhase.Ready);
workbenchContributionsRegistry.registerWorkbenchContribution(ComplexNotebookWorkingCopyEditorHandler, LifecyclePhase.Ready);
registerSingleton(INotebookService, NotebookService);
registerSingleton(INotebookEditorWorkerService, NotebookEditorWorkerServiceImpl);
registerSingleton(INotebookEditorModelResolverService, NotebookModelResolverServiceImpl, true);
registerSingleton(INotebookCellStatusBarService, NotebookCellStatusBarService, true);
registerSingleton(INotebookEditorService, NotebookEditorWidgetService, true);
registerSingleton(INotebookKernelService, NotebookKernelService, true);
registerSingleton(INotebookExecutionService, NotebookExecutionService, true);
registerSingleton(INotebookRendererMessagingService, NotebookRendererMessagingService, true);
registerSingleton(INotebookKeymapService, NotebookKeymapService, true);
const schemas: IJSONSchemaMap = {};
function isConfigurationPropertySchema(x: IConfigurationPropertySchema | { [path: string]: IConfigurationPropertySchema; }): x is IConfigurationPropertySchema {
return (typeof x.type !== 'undefined' || typeof x.anyOf !== 'undefined');
}
for (const editorOption of editorOptionsRegistry) {
const schema = editorOption.schema;
if (schema) {
if (isConfigurationPropertySchema(schema)) {
schemas[`editor.${editorOption.name}`] = schema;
} else {
for (let key in schema) {
if (Object.hasOwnProperty.call(schema, key)) {
schemas[key] = schema[key];
}
}
}
}
}
const editorOptionsCustomizationSchema: IConfigurationPropertySchema = {
description: nls.localize('notebook.editorOptions.experimentalCustomization', 'Settings for code editors used in notebooks. This can be used to customize most editor.* settings.'),
default: {},
allOf: [
{
properties: schemas,
}
// , {
// patternProperties: {
// '^\\[.*\\]$': {
// type: 'object',
// default: {},
// properties: schemas
// }
// }
// }
],
tags: ['notebookLayout']
};
const configurationRegistry = Registry.as<IConfigurationRegistry>(Extensions.Configuration);
configurationRegistry.registerConfiguration({
id: 'notebook',
order: 100,
title: nls.localize('notebookConfigurationTitle', "Notebook"),
type: 'object',
properties: {
[DisplayOrderKey]: {
description: nls.localize('notebook.displayOrder.description', "Priority list for output mime types"),
type: ['array'],
items: {
type: 'string'
},
default: []
},
[CellToolbarLocation]: {
description: nls.localize('notebook.cellToolbarLocation.description', "Where the cell toolbar should be shown, or whether it should be hidden."),
type: 'object',
additionalProperties: {
markdownDescription: nls.localize('notebook.cellToolbarLocation.viewType', "Configure the cell toolbar position for for specific file types"),
type: 'string',
enum: ['left', 'right', 'hidden']
},
default: {
'default': 'right'
},
tags: ['notebookLayout']
},
[ShowCellStatusBar]: {
description: nls.localize('notebook.showCellStatusbar.description', "Whether the cell status bar should be shown."),
type: 'string',
enum: ['hidden', 'visible', 'visibleAfterExecute'],
enumDescriptions: [
nls.localize('notebook.showCellStatusbar.hidden.description', "The cell Status bar is always hidden."),
nls.localize('notebook.showCellStatusbar.visible.description', "The cell Status bar is always visible."),
nls.localize('notebook.showCellStatusbar.visibleAfterExecute.description', "The cell Status bar is hidden until the cell has executed. Then it becomes visible to show the execution status.")],
default: 'visible',
tags: ['notebookLayout']
},
[NotebookTextDiffEditorPreview]: {
description: nls.localize('notebook.diff.enablePreview.description', "Whether to use the enhanced text diff editor for notebook."),
type: 'boolean',
default: true,
tags: ['notebookLayout']
},
[CellToolbarVisibility]: {
markdownDescription: nls.localize('notebook.cellToolbarVisibility.description', "Whether the cell toolbar should appear on hover or click."),
type: 'string',
enum: ['hover', 'click'],
default: 'click',
tags: ['notebookLayout']
},
[UndoRedoPerCell]: {
description: nls.localize('notebook.undoRedoPerCell.description', "Whether to use separate undo/redo stack for each cell."),
type: 'boolean',
default: true,
tags: ['notebookLayout']
},
[CompactView]: {
description: nls.localize('notebook.compactView.description', "Control whether the notebook editor should be rendered in a compact form. For example, when turned on, it will decrease the left margin width."),
type: 'boolean',
default: true,
tags: ['notebookLayout']
},
[FocusIndicator]: {
description: nls.localize('notebook.focusIndicator.description', "Controls where the focus indicator is rendered, either along the cell borders or on the left gutter"),
type: 'string',
enum: ['border', 'gutter'],
default: 'gutter',
tags: ['notebookLayout']
},
[InsertToolbarLocation]: {
description: nls.localize('notebook.insertToolbarPosition.description', "Control where the insert cell actions should appear."),
type: 'string',
enum: ['betweenCells', 'notebookToolbar', 'both', 'hidden'],
enumDescriptions: [
nls.localize('insertToolbarLocation.betweenCells', "A toolbar that appears on hover between cells."),
nls.localize('insertToolbarLocation.notebookToolbar', "The toolbar at the top of the notebook editor."),
nls.localize('insertToolbarLocation.both', "Both toolbars."),
nls.localize('insertToolbarLocation.hidden', "The insert actions don't appear anywhere."),
],
default: 'both',
tags: ['notebookLayout']
},
[GlobalToolbar]: {
description: nls.localize('notebook.globalToolbar.description', "Control whether to render a global toolbar inside the notebook editor."),
type: 'boolean',
default: true,
tags: ['notebookLayout']
},
[ConsolidatedOutputButton]: {
description: nls.localize('notebook.consolidatedOutputButton.description', "Control whether outputs action should be rendered in the output toolbar."),
type: 'boolean',
default: true,
tags: ['notebookLayout']
},
[ShowFoldingControls]: {
description: nls.localize('notebook.showFoldingControls.description', "Controls when the Markdown header folding arrow is shown."),
type: 'string',
enum: ['always', 'mouseover'],
enumDescriptions: [
nls.localize('showFoldingControls.always', "The folding controls are always visible."),
nls.localize('showFoldingControls.mouseover', "The folding controls are visible only on mouseover."),
],
default: 'mouseover',
tags: ['notebookLayout']
},
[DragAndDropEnabled]: {
description: nls.localize('notebook.dragAndDrop.description', "Control whether the notebook editor should allow moving cells through drag and drop."),
type: 'boolean',
default: true,
tags: ['notebookLayout']
},
[ConsolidatedRunButton]: {
description: nls.localize('notebook.consolidatedRunButton.description', "Control whether extra actions are shown in a dropdown next to the run button."),
type: 'boolean',
default: false,
tags: ['notebookLayout']
},
[GlobalToolbarShowLabel]: {
description: nls.localize('notebook.globalToolbarShowLabel', "Control whether the actions on the notebook toolbar should render label or not."),
type: 'boolean',
default: true,
tags: ['notebookLayout']
},
[TextOutputLineLimit]: {
description: nls.localize('notebook.textOutputLineLimit', "Control how many lines of text in a text output is rendered."),
type: 'number',
default: 30,
tags: ['notebookLayout']
},
[NotebookCellEditorOptionsCustomizations]: editorOptionsCustomizationSchema
}
});