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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { VSBuffer } from 'vs/base/common/buffer';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Emitter, Event } from 'vs/base/common/event';
import { IRelativePattern } from 'vs/base/common/glob';
import { hash } from 'vs/base/common/hash';
import { DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { ResourceMap } from 'vs/base/common/map';
import { MarshalledId } from 'vs/base/common/marshallingIds';
import { isFalsyOrWhitespace } from 'vs/base/common/strings';
import { assertIsDefined } from 'vs/base/common/types';
import { URI, UriComponents } from 'vs/base/common/uri';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { Cache } from 'vs/workbench/api/common/cache';
import { ExtHostNotebookShape, IMainContext, IModelAddedData, INotebookCellStatusBarListDto, INotebookDocumentsAndEditorsDelta, INotebookDocumentShowOptions, INotebookEditorAddData, MainContext, MainThreadNotebookDocumentsShape, MainThreadNotebookEditorsShape, MainThreadNotebookShape, NotebookDataDto } from 'vs/workbench/api/common/extHost.protocol';
import { ApiCommand, ApiCommandArgument, ApiCommandResult, CommandsConverter, ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
import { IExtensionStoragePaths } from 'vs/workbench/api/common/extHostStoragePaths';
import * as typeConverters from 'vs/workbench/api/common/extHostTypeConverters';
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
import { INotebookExclusiveDocumentFilter, INotebookContributionData } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { SerializableObjectWithBuffers } from 'vs/workbench/services/extensions/common/proxyIdentifier';
import type * as vscode from 'vscode';
import { ExtHostCell, ExtHostNotebookDocument } from './extHostNotebookDocument';
import { ExtHostNotebookEditor } from './extHostNotebookEditor';
type NotebookContentProviderData = {
readonly provider: vscode.NotebookContentProvider;
readonly extension: IExtensionDescription;
};
export class ExtHostNotebookController implements ExtHostNotebookShape {
private static _notebookStatusBarItemProviderHandlePool: number = 0;
private readonly _notebookProxy: MainThreadNotebookShape;
private readonly _notebookDocumentsProxy: MainThreadNotebookDocumentsShape;
private readonly _notebookEditorsProxy: MainThreadNotebookEditorsShape;
private readonly _notebookContentProviders = new Map<string, NotebookContentProviderData>();
private readonly _notebookStatusBarItemProviders = new Map<number, vscode.NotebookCellStatusBarItemProvider>();
private readonly _documents = new ResourceMap<ExtHostNotebookDocument>();
private readonly _editors = new Map<string, ExtHostNotebookEditor>();
private readonly _commandsConverter: CommandsConverter;
private readonly _onDidChangeActiveNotebookEditor = new Emitter<vscode.NotebookEditor | undefined>();
readonly onDidChangeActiveNotebookEditor = this._onDidChangeActiveNotebookEditor.event;
private _activeNotebookEditor: ExtHostNotebookEditor | undefined;
get activeNotebookEditor(): vscode.NotebookEditor | undefined {
return this._activeNotebookEditor?.apiEditor;
}
private _visibleNotebookEditors: ExtHostNotebookEditor[] = [];
get visibleNotebookEditors(): vscode.NotebookEditor[] {
return this._visibleNotebookEditors.map(editor => editor.apiEditor);
}
private _onDidOpenNotebookDocument = new Emitter<vscode.NotebookDocument>();
onDidOpenNotebookDocument: Event<vscode.NotebookDocument> = this._onDidOpenNotebookDocument.event;
private _onDidCloseNotebookDocument = new Emitter<vscode.NotebookDocument>();
onDidCloseNotebookDocument: Event<vscode.NotebookDocument> = this._onDidCloseNotebookDocument.event;
private _onDidChangeVisibleNotebookEditors = new Emitter<vscode.NotebookEditor[]>();
onDidChangeVisibleNotebookEditors = this._onDidChangeVisibleNotebookEditors.event;
private _statusBarCache = new Cache<IDisposable>('NotebookCellStatusBarCache');
constructor(
mainContext: IMainContext,
commands: ExtHostCommands,
private _textDocumentsAndEditors: ExtHostDocumentsAndEditors,
private _textDocuments: ExtHostDocuments,
private readonly _extensionStoragePaths: IExtensionStoragePaths,
) {
this._notebookProxy = mainContext.getProxy(MainContext.MainThreadNotebook);
this._notebookDocumentsProxy = mainContext.getProxy(MainContext.MainThreadNotebookDocuments);
this._notebookEditorsProxy = mainContext.getProxy(MainContext.MainThreadNotebookEditors);
this._commandsConverter = commands.converter;
commands.registerArgumentProcessor({
// Serialized INotebookCellActionContext
processArgument: (arg) => {
if (arg && arg.$mid === MarshalledId.NotebookCellActionContext) {
const notebookUri = arg.notebookEditor?.notebookUri;
const cellHandle = arg.cell.handle;
const data = this._documents.get(notebookUri);
const cell = data?.getCell(cellHandle);
if (cell) {
return cell.apiCell;
}
}
return arg;
}
});
ExtHostNotebookController._registerApiCommands(commands);
}
getEditorById(editorId: string): ExtHostNotebookEditor {
const editor = this._editors.get(editorId);
if (!editor) {
throw new Error(`unknown text editor: ${editorId}. known editors: ${[...this._editors.keys()]} `);
}
return editor;
}
getIdByEditor(editor: vscode.NotebookEditor): string | undefined {
for (const [id, candidate] of this._editors) {
if (candidate.apiEditor === editor) {
return id;
}
}
return undefined;
}
get notebookDocuments() {
return [...this._documents.values()];
}
getNotebookDocument(uri: URI, relaxed: true): ExtHostNotebookDocument | undefined;
getNotebookDocument(uri: URI): ExtHostNotebookDocument;
getNotebookDocument(uri: URI, relaxed?: true): ExtHostNotebookDocument | undefined {
const result = this._documents.get(uri);
if (!result && !relaxed) {
throw new Error(`NO notebook document for '${uri}'`);
}
return result;
}
private _getProviderData(viewType: string): NotebookContentProviderData {
const result = this._notebookContentProviders.get(viewType);
if (!result) {
throw new Error(`NO provider for '${viewType}'`);
}
return result;
}
registerNotebookContentProvider(
extension: IExtensionDescription,
viewType: string,
provider: vscode.NotebookContentProvider,
options?: vscode.NotebookDocumentContentOptions,
registration?: vscode.NotebookRegistrationData
): vscode.Disposable {
if (isFalsyOrWhitespace(viewType)) {
throw new Error(`viewType cannot be empty or just whitespace`);
}
if (this._notebookContentProviders.has(viewType)) {
throw new Error(`Notebook provider for '${viewType}' already registered`);
}
this._notebookContentProviders.set(viewType, { extension, provider });
let listener: IDisposable | undefined;
if (provider.onDidChangeNotebookContentOptions) {
listener = provider.onDidChangeNotebookContentOptions(() => {
const internalOptions = typeConverters.NotebookDocumentContentOptions.from(provider.options);
this._notebookProxy.$updateNotebookProviderOptions(viewType, internalOptions);
});
}
this._notebookProxy.$registerNotebookProvider(
{ id: extension.identifier, location: extension.extensionLocation },
viewType,
typeConverters.NotebookDocumentContentOptions.from(options),
ExtHostNotebookController._convertNotebookRegistrationData(extension, registration)
);
return new extHostTypes.Disposable(() => {
listener?.dispose();
this._notebookContentProviders.delete(viewType);
this._notebookProxy.$unregisterNotebookProvider(viewType);
});
}
private static _convertNotebookRegistrationData(extension: IExtensionDescription, registration: vscode.NotebookRegistrationData | undefined): INotebookContributionData | undefined {
if (!registration) {
return;
}
const viewOptionsFilenamePattern = registration.filenamePattern
.map(pattern => typeConverters.NotebookExclusiveDocumentPattern.from(pattern))
.filter(pattern => pattern !== undefined) as (string | IRelativePattern | INotebookExclusiveDocumentFilter)[];
if (registration.filenamePattern && !viewOptionsFilenamePattern) {
console.warn(`Notebook content provider view options file name pattern is invalid ${registration.filenamePattern}`);
return undefined;
}
return {
extension: extension.identifier,
providerDisplayName: extension.displayName || extension.name,
displayName: registration.displayName,
filenamePattern: viewOptionsFilenamePattern,
exclusive: registration.exclusive || false
};
}
registerNotebookCellStatusBarItemProvider(extension: IExtensionDescription, notebookType: string, provider: vscode.NotebookCellStatusBarItemProvider) {
const handle = ExtHostNotebookController._notebookStatusBarItemProviderHandlePool++;
const eventHandle = typeof provider.onDidChangeCellStatusBarItems === 'function' ? ExtHostNotebookController._notebookStatusBarItemProviderHandlePool++ : undefined;
this._notebookStatusBarItemProviders.set(handle, provider);
this._notebookProxy.$registerNotebookCellStatusBarItemProvider(handle, eventHandle, notebookType);
let subscription: vscode.Disposable | undefined;
if (eventHandle !== undefined) {
subscription = provider.onDidChangeCellStatusBarItems!(_ => this._notebookProxy.$emitCellStatusBarEvent(eventHandle));
}
return new extHostTypes.Disposable(() => {
this._notebookStatusBarItemProviders.delete(handle);
this._notebookProxy.$unregisterNotebookCellStatusBarItemProvider(handle, eventHandle);
if (subscription) {
subscription.dispose();
}
});
}
async createNotebookDocument(options: { viewType: string; content?: vscode.NotebookData }): Promise<URI> {
const canonicalUri = await this._notebookDocumentsProxy.$tryCreateNotebook({
viewType: options.viewType,
content: options.content && typeConverters.NotebookData.from(options.content)
});
return URI.revive(canonicalUri);
}
async openNotebookDocument(uri: URI): Promise<vscode.NotebookDocument> {
const cached = this._documents.get(uri);
if (cached) {
return cached.apiNotebook;
}
const canonicalUri = await this._notebookDocumentsProxy.$tryOpenNotebook(uri);
const document = this._documents.get(URI.revive(canonicalUri));
return assertIsDefined(document?.apiNotebook);
}
async showNotebookDocument(notebookOrUri: vscode.NotebookDocument | URI, options?: vscode.NotebookDocumentShowOptions): Promise<vscode.NotebookEditor> {
if (URI.isUri(notebookOrUri)) {
notebookOrUri = await this.openNotebookDocument(notebookOrUri);
}
let resolvedOptions: INotebookDocumentShowOptions;
if (typeof options === 'object') {
resolvedOptions = {
position: typeConverters.ViewColumn.from(options.viewColumn),
preserveFocus: options.preserveFocus,
selections: options.selections && options.selections.map(typeConverters.NotebookRange.from),
pinned: typeof options.preview === 'boolean' ? !options.preview : undefined
};
} else {
resolvedOptions = {
preserveFocus: false
};
}
const editorId = await this._notebookEditorsProxy.$tryShowNotebookDocument(notebookOrUri.uri, notebookOrUri.notebookType, resolvedOptions);
const editor = editorId && this._editors.get(editorId)?.apiEditor;
if (editor) {
return editor;
}
if (editorId) {
throw new Error(`Could NOT open editor for "${notebookOrUri.uri.toString()}" because another editor opened in the meantime.`);
} else {
throw new Error(`Could NOT open editor for "${notebookOrUri.uri.toString()}".`);
}
}
async $provideNotebookCellStatusBarItems(handle: number, uri: UriComponents, index: number, token: CancellationToken): Promise<INotebookCellStatusBarListDto | undefined> {
const provider = this._notebookStatusBarItemProviders.get(handle);
const revivedUri = URI.revive(uri);
const document = this._documents.get(revivedUri);
if (!document || !provider) {
return;
}
const cell = document.getCellFromIndex(index);
if (!cell) {
return;
}
const result = await provider.provideCellStatusBarItems(cell.apiCell, token);
if (!result) {
return undefined;
}
const disposables = new DisposableStore();
const cacheId = this._statusBarCache.add([disposables]);
const resultArr = Array.isArray(result) ? result : [result];
const items = resultArr.map(item => typeConverters.NotebookStatusBarItem.from(item, this._commandsConverter, disposables));
return {
cacheId,
items
};
}
$releaseNotebookCellStatusBarItems(cacheId: number): void {
this._statusBarCache.delete(cacheId);
}
// --- serialize/deserialize
private _handlePool = 0;
private readonly _notebookSerializer = new Map<number, vscode.NotebookSerializer>();
registerNotebookSerializer(extension: IExtensionDescription, viewType: string, serializer: vscode.NotebookSerializer, options?: vscode.NotebookDocumentContentOptions, registration?: vscode.NotebookRegistrationData): vscode.Disposable {
if (isFalsyOrWhitespace(viewType)) {
throw new Error(`viewType cannot be empty or just whitespace`);
}
const handle = this._handlePool++;
this._notebookSerializer.set(handle, serializer);
this._notebookProxy.$registerNotebookSerializer(
handle,
{ id: extension.identifier, location: extension.extensionLocation },
viewType,
typeConverters.NotebookDocumentContentOptions.from(options),
ExtHostNotebookController._convertNotebookRegistrationData(extension, registration)
);
return toDisposable(() => {
this._notebookProxy.$unregisterNotebookSerializer(handle);
});
}
async $dataToNotebook(handle: number, bytes: VSBuffer, token: CancellationToken): Promise<SerializableObjectWithBuffers<NotebookDataDto>> {
const serializer = this._notebookSerializer.get(handle);
if (!serializer) {
throw new Error('NO serializer found');
}
const data = await serializer.deserializeNotebook(bytes.buffer, token);
return new SerializableObjectWithBuffers(typeConverters.NotebookData.from(data));
}
async $notebookToData(handle: number, data: SerializableObjectWithBuffers<NotebookDataDto>, token: CancellationToken): Promise<VSBuffer> {
const serializer = this._notebookSerializer.get(handle);
if (!serializer) {
throw new Error('NO serializer found');
}
const bytes = await serializer.serializeNotebook(typeConverters.NotebookData.to(data.value), token);
return VSBuffer.wrap(bytes);
}
// --- open, save, saveAs, backup
async $openNotebook(viewType: string, uri: UriComponents, backupId: string | undefined, untitledDocumentData: VSBuffer | undefined, token: CancellationToken): Promise<SerializableObjectWithBuffers<NotebookDataDto>> {
const { provider } = this._getProviderData(viewType);
const data = await provider.openNotebook(URI.revive(uri), { backupId, untitledDocumentData: untitledDocumentData?.buffer }, token);
return new SerializableObjectWithBuffers({
metadata: data.metadata ?? Object.create(null),
cells: data.cells.map(typeConverters.NotebookCellData.from),
});
}
async $saveNotebook(viewType: string, uri: UriComponents, token: CancellationToken): Promise<boolean> {
const document = this.getNotebookDocument(URI.revive(uri));
const { provider } = this._getProviderData(viewType);
await provider.saveNotebook(document.apiNotebook, token);
return true;
}
async $saveNotebookAs(viewType: string, uri: UriComponents, target: UriComponents, token: CancellationToken): Promise<boolean> {
const document = this.getNotebookDocument(URI.revive(uri));
const { provider } = this._getProviderData(viewType);
await provider.saveNotebookAs(URI.revive(target), document.apiNotebook, token);
return true;
}
private _backupIdPool: number = 0;
async $backupNotebook(viewType: string, uri: UriComponents, cancellation: CancellationToken): Promise<string> {
const document = this.getNotebookDocument(URI.revive(uri));
const provider = this._getProviderData(viewType);
const storagePath = this._extensionStoragePaths.workspaceValue(provider.extension) ?? this._extensionStoragePaths.globalValue(provider.extension);
const fileName = String(hash([document.uri.toString(), this._backupIdPool++]));
const backupUri = URI.joinPath(storagePath, fileName);
const backup = await provider.provider.backupNotebook(document.apiNotebook, { destination: backupUri }, cancellation);
document.updateBackup(backup);
return backup.id;
}
private _createExtHostEditor(document: ExtHostNotebookDocument, editorId: string, data: INotebookEditorAddData) {
if (this._editors.has(editorId)) {
throw new Error(`editor with id ALREADY EXSIST: ${editorId}`);
}
const editor = new ExtHostNotebookEditor(
editorId,
this._notebookEditorsProxy,
document,
data.visibleRanges.map(typeConverters.NotebookRange.to),
data.selections.map(typeConverters.NotebookRange.to),
typeof data.viewColumn === 'number' ? typeConverters.ViewColumn.to(data.viewColumn) : undefined
);
this._editors.set(editorId, editor);
}
$acceptDocumentAndEditorsDelta(delta: SerializableObjectWithBuffers<INotebookDocumentsAndEditorsDelta>): void {
if (delta.value.removedDocuments) {
for (const uri of delta.value.removedDocuments) {
const revivedUri = URI.revive(uri);
const document = this._documents.get(revivedUri);
if (document) {
document.dispose();
this._documents.delete(revivedUri);
this._textDocumentsAndEditors.$acceptDocumentsAndEditorsDelta({ removedDocuments: document.apiNotebook.getCells().map(cell => cell.document.uri) });
this._onDidCloseNotebookDocument.fire(document.apiNotebook);
}
for (const editor of this._editors.values()) {
if (editor.notebookData.uri.toString() === revivedUri.toString()) {
this._editors.delete(editor.id);
}
}
}
}
if (delta.value.addedDocuments) {
const addedCellDocuments: IModelAddedData[] = [];
for (const modelData of delta.value.addedDocuments) {
const uri = URI.revive(modelData.uri);
if (this._documents.has(uri)) {
throw new Error(`adding EXISTING notebook ${uri} `);
}
const document = new ExtHostNotebookDocument(
this._notebookDocumentsProxy,
this._textDocumentsAndEditors,
this._textDocuments,
uri,
modelData
);
// add cell document as vscode.TextDocument
addedCellDocuments.push(...modelData.cells.map(cell => ExtHostCell.asModelAddData(document.apiNotebook, cell)));
this._documents.get(uri)?.dispose();
this._documents.set(uri, document);
this._textDocumentsAndEditors.$acceptDocumentsAndEditorsDelta({ addedDocuments: addedCellDocuments });
this._onDidOpenNotebookDocument.fire(document.apiNotebook);
}
}
if (delta.value.addedEditors) {
for (const editorModelData of delta.value.addedEditors) {
if (this._editors.has(editorModelData.id)) {
return;
}
const revivedUri = URI.revive(editorModelData.documentUri);
const document = this._documents.get(revivedUri);
if (document) {
this._createExtHostEditor(document, editorModelData.id, editorModelData);
}
}
}
const removedEditors: ExtHostNotebookEditor[] = [];
if (delta.value.removedEditors) {
for (const editorid of delta.value.removedEditors) {
const editor = this._editors.get(editorid);
if (editor) {
this._editors.delete(editorid);
if (this._activeNotebookEditor?.id === editor.id) {
this._activeNotebookEditor = undefined;
}
removedEditors.push(editor);
}
}
}
if (delta.value.visibleEditors) {
this._visibleNotebookEditors = delta.value.visibleEditors.map(id => this._editors.get(id)!).filter(editor => !!editor) as ExtHostNotebookEditor[];
const visibleEditorsSet = new Set<string>();
this._visibleNotebookEditors.forEach(editor => visibleEditorsSet.add(editor.id));
for (const editor of this._editors.values()) {
const newValue = visibleEditorsSet.has(editor.id);
editor._acceptVisibility(newValue);
}
this._visibleNotebookEditors = [...this._editors.values()].map(e => e).filter(e => e.visible);
this._onDidChangeVisibleNotebookEditors.fire(this.visibleNotebookEditors);
}
if (delta.value.newActiveEditor === null) {
// clear active notebook as current active editor is non-notebook editor
this._activeNotebookEditor = undefined;
} else if (delta.value.newActiveEditor) {
this._activeNotebookEditor = this._editors.get(delta.value.newActiveEditor);
}
if (delta.value.newActiveEditor !== undefined) {
this._onDidChangeActiveNotebookEditor.fire(this._activeNotebookEditor?.apiEditor);
}
}
private static _registerApiCommands(extHostCommands: ExtHostCommands) {
const notebookTypeArg = ApiCommandArgument.String.with('notebookType', 'A notebook type');
const commandDataToNotebook = new ApiCommand(
'vscode.executeDataToNotebook', '_executeDataToNotebook', 'Invoke notebook serializer',
[notebookTypeArg, new ApiCommandArgument<Uint8Array, VSBuffer>('data', 'Bytes to convert to data', v => v instanceof Uint8Array, v => VSBuffer.wrap(v))],
new ApiCommandResult<SerializableObjectWithBuffers<NotebookDataDto>, vscode.NotebookData>('Notebook Data', data => typeConverters.NotebookData.to(data.value))
);
const commandNotebookToData = new ApiCommand(
'vscode.executeNotebookToData', '_executeNotebookToData', 'Invoke notebook serializer',
[notebookTypeArg, new ApiCommandArgument<vscode.NotebookData, SerializableObjectWithBuffers<NotebookDataDto>>('NotebookData', 'Notebook data to convert to bytes', v => true, v => new SerializableObjectWithBuffers(typeConverters.NotebookData.from(v)))],
new ApiCommandResult<VSBuffer, Uint8Array>('Bytes', dto => dto.buffer)
);
extHostCommands.registerApiCommand(commandDataToNotebook);
extHostCommands.registerApiCommand(commandNotebookToData);
}
}
|