extHostNotebook.ts 19.7 KB
Newer Older
R
rebornix 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
R
rebornix 已提交
7
import * as glob from 'vs/base/common/glob';
R
rebornix 已提交
8
import { ExtHostNotebookShape, IMainContext, MainThreadNotebookShape, MainContext, ICellDto, NotebookCellsSplice, NotebookCellOutputsSplice, CellKind, CellOutputKind } from 'vs/workbench/api/common/extHost.protocol';
R
rebornix 已提交
9 10
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { Disposable as VSCodeDisposable } from './extHostTypes';
11
import { URI, UriComponents } from 'vs/base/common/uri';
R
rebornix 已提交
12
import { DisposableStore } from 'vs/base/common/lifecycle';
R
rebornix 已提交
13 14
import { readonly } from 'vs/base/common/errors';
import { Emitter, Event } from 'vs/base/common/event';
R
rebornix 已提交
15
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
R
rebornix 已提交
16
import { INotebookDisplayOrder, parseCellUri, parseCellHandle, ITransformedDisplayOutputDto, IOrderedMimeType, IStreamOutput, IErrorOutput, mimeTypeSupportedByCore, IOutput, sortMimeTypes, diff } from 'vs/workbench/contrib/notebook/common/notebookCommon';
17
import { ISplice } from 'vs/base/common/sequence';
R
rebornix 已提交
18 19 20 21 22

interface ExtHostOutputDisplayOrder {
	defaultOrder: glob.ParsedPattern[];
	userOrder?: glob.ParsedPattern[];
}
R
rebornix 已提交
23 24

export class ExtHostCell implements vscode.NotebookCell {
R
rebornix 已提交
25 26 27

	private static _handlePool: number = 0;
	readonly handle = ExtHostCell._handlePool++;
R
rebornix 已提交
28 29
	public source: string[];
	private _outputs: any[];
30 31
	private _onDidChangeOutputs = new Emitter<ISplice<vscode.CellOutput>[]>();
	onDidChangeOutputs: Event<ISplice<vscode.CellOutput>[]> = this._onDidChangeOutputs.event;
R
rebornix 已提交
32 33
	private _textDocument: vscode.TextDocument | undefined;
	private _initalVersion: number = -1;
34
	private _outputMapping = new Set<vscode.CellOutput>();
R
rebornix 已提交
35 36

	constructor(
R
rebornix 已提交
37
		private _content: string,
R
rebornix 已提交
38
		public cellKind: CellKind,
R
rebornix 已提交
39 40
		public language: string,
		outputs: any[]
R
rebornix 已提交
41
	) {
R
rebornix 已提交
42 43 44 45 46 47 48 49
		this.source = this._content.split(/\r|\n|\r\n/g);
		this._outputs = outputs;
	}

	get outputs() {
		return this._outputs;
	}

R
rebornix 已提交
50
	set outputs(newOutputs: vscode.CellOutput[]) {
51
		let diffs = diff<vscode.CellOutput>(this._outputs || [], newOutputs || [], (a) => {
52 53 54 55 56 57 58 59 60 61 62 63 64
			return this._outputMapping.has(a);
		});

		diffs.forEach(diff => {
			for (let i = diff.start; i < diff.start + diff.deleteCount; i++) {
				this._outputMapping.delete(this._outputs[i]);
			}

			diff.toInsert.forEach(output => {
				this._outputMapping.add(output);
			});
		});

R
rebornix 已提交
65
		this._outputs = newOutputs;
66
		this._onDidChangeOutputs.fire(diffs);
R
rebornix 已提交
67 68 69
	}

	getContent(): string {
R
rebornix 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		if (this._textDocument && this._initalVersion !== this._textDocument?.version) {
			return this._textDocument.getText();
		} else {
			return this.source.join('\n');
		}
	}

	attachTextDocument(document: vscode.TextDocument) {
		this._textDocument = document;
		this._initalVersion = this._textDocument.version;
	}

	detachTextDocument(document: vscode.TextDocument) {
		if (this._textDocument && this._textDocument.version !== this._initalVersion) {
			this.source = this._textDocument.getText().split(/\r|\n|\r\n/g);
		}

		this._textDocument = undefined;
		this._initalVersion = -1;
R
rebornix 已提交
89 90 91
	}
}

92

R
rebornix 已提交
93
export class ExtHostNotebookDocument implements vscode.NotebookDocument, vscode.Disposable {
R
rebornix 已提交
94
	private static _handlePool: number = 0;
R
rebornix 已提交
95 96 97 98
	readonly handle = ExtHostNotebookDocument._handlePool++;

	private _cells: ExtHostCell[] = [];

R
rebornix 已提交
99 100
	private _cellDisposableMapping = new Map<number, DisposableStore>();

R
rebornix 已提交
101 102 103 104 105
	get cells() {
		return this._cells;
	}

	set cells(newCells: ExtHostCell[]) {
106 107 108 109 110 111 112 113
		let diffs = diff<ExtHostCell>(this._cells, newCells, (a) => {
			return this._cellDisposableMapping.has(a.handle);
		});

		diffs.forEach(diff => {
			for (let i = diff.start; i < diff.start + diff.deleteCount; i++) {
				this._cellDisposableMapping.get(this._cells[i].handle)?.clear();
				this._cellDisposableMapping.delete(this._cells[i].handle);
R
rebornix 已提交
114 115
			}

116 117 118
			diff.toInsert.forEach(cell => {
				this._cellDisposableMapping.set(cell.handle, new DisposableStore());
				this._cellDisposableMapping.get(cell.handle)?.add(cell.onDidChangeOutputs((outputDiffs) => {
119
					this.eventuallyUpdateCellOutputs(cell, outputDiffs);
120 121
				}));
			});
R
rebornix 已提交
122
		});
123 124 125

		this._cells = newCells;
		this.eventuallyUpdateCells(diffs);
R
rebornix 已提交
126 127 128 129 130 131 132
	}

	private _languages: string[] = [];

	get languages() {
		return this._languages = [];
	}
R
rebornix 已提交
133

R
rebornix 已提交
134 135 136
	set languages(newLanguages: string[]) {
		this._languages = newLanguages;
		this._proxy.$updateNotebookLanguages(this.viewType, this.uri, this._languages);
R
rebornix 已提交
137
	}
R
rebornix 已提交
138

R
rebornix 已提交
139
	private _displayOrder: vscode.GlobPattern[] = [];
R
rebornix 已提交
140
	private _parsedDisplayOrder: glob.ParsedPattern[] = [];
R
rebornix 已提交
141 142 143 144 145 146 147

	get displayOrder() {
		return this._displayOrder;
	}

	set displayOrder(newOrder: vscode.GlobPattern[]) {
		this._displayOrder = newOrder;
R
rebornix 已提交
148 149 150 151 152
		this._parsedDisplayOrder = newOrder.map(pattern => glob.parse(pattern));
	}

	get parsedDisplayOrder() {
		return this._parsedDisplayOrder;
R
rebornix 已提交
153 154
	}

R
rebornix 已提交
155
	constructor(
R
rebornix 已提交
156
		private readonly _proxy: MainThreadNotebookShape,
R
rebornix 已提交
157
		public viewType: string,
R
rebornix 已提交
158 159
		public uri: URI,
		public renderingHandler: ExtHostNotebookOutputRenderingHandler
R
rebornix 已提交
160
	) {
R
rebornix 已提交
161

R
rebornix 已提交
162
	}
R
rebornix 已提交
163 164 165
	dispose() {
		this._cellDisposableMapping.forEach(cell => cell.dispose());
	}
R
rebornix 已提交
166

R
rebornix 已提交
167
	get fileName() { return this.uri.fsPath; }
R
rebornix 已提交
168

R
rebornix 已提交
169 170
	get isDirty() { return false; }

171
	eventuallyUpdateCells(diffs: ISplice<ExtHostCell>[]) {
172 173 174 175 176 177 178
		let renderers = new Set<number>();
		let diffDtos: NotebookCellsSplice[] = [];

		diffDtos = diffs.map(diff => {
			let inserts = diff.toInsert;

			let cellDtos = inserts.map(cell => {
R
rebornix 已提交
179 180 181 182
				let outputs: IOutput[] = [];
				if (cell.outputs.length) {
					outputs = cell.outputs.map(output => {
						if (output.outputKind === CellOutputKind.Rich) {
183
							const ret = this.transformMimeTypes(cell, output);
184

185 186
							if (ret.orderedMimeTypes[ret.pickedMimeTypeIndex].isResolved) {
								renderers.add(ret.orderedMimeTypes[ret.pickedMimeTypeIndex].rendererId!);
187
							}
188 189
							return ret;
						} else {
R
rebornix 已提交
190
							return output as IStreamOutput | IErrorOutput;
191 192 193 194 195
						}
					});
				}

				return {
196 197 198
					handle: cell.handle,
					source: cell.source,
					language: cell.language,
R
rebornix 已提交
199
					cellKind: cell.cellKind,
200 201 202 203 204 205 206 207 208 209 210 211 212
					outputs: outputs,
					isDirty: false
				};
			});

			return [diff.start, diff.deleteCount, cellDtos];
		});

		this._proxy.$spliceNotebookCells(
			this.viewType,
			this.uri,
			diffDtos,
			Array.from(renderers)
213
		);
R
rebornix 已提交
214
	}
R
rebornix 已提交
215

216 217 218 219 220
	eventuallyUpdateCellOutputs(cell: ExtHostCell, diffs: ISplice<vscode.CellOutput>[]) {
		let renderers = new Set<number>();
		let outputDtos: NotebookCellOutputsSplice[] = diffs.map(diff => {
			let outputs = diff.toInsert;

221
			let transformedOutputs = outputs.map(output => {
R
rebornix 已提交
222
				if (output.outputKind === CellOutputKind.Rich) {
223
					const ret = this.transformMimeTypes(cell, output);
224

225 226
					if (ret.orderedMimeTypes[ret.pickedMimeTypeIndex].isResolved) {
						renderers.add(ret.orderedMimeTypes[ret.pickedMimeTypeIndex].rendererId!);
227
					}
228 229 230
					return ret;
				} else {
					return output as IStreamOutput | IErrorOutput;
231 232 233
				}
			});

234
			return [diff.start, diff.deleteCount, transformedOutputs];
235 236 237 238 239
		});

		this._proxy.$spliceNotebookCellOutputs(this.viewType, this.uri, cell.handle, outputDtos, Array.from(renderers));
	}

R
rebornix 已提交
240
	insertCell(index: number, cell: ExtHostCell) {
R
rebornix 已提交
241
		this.cells.splice(index, 0, cell);
R
rebornix 已提交
242 243 244 245 246 247 248

		if (!this._cellDisposableMapping.has(cell.handle)) {
			this._cellDisposableMapping.set(cell.handle, new DisposableStore());
		}

		let store = this._cellDisposableMapping.get(cell.handle)!;

249
		store.add(cell.onDidChangeOutputs((diffs) => {
250
			this.eventuallyUpdateCellOutputs(cell, diffs);
R
rebornix 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
		}));
	}

	deleteCell(index: number): boolean {
		if (index >= this.cells.length) {
			return false;
		}

		let cell = this.cells[index];
		this._cellDisposableMapping.get(cell.handle)?.dispose();
		this._cellDisposableMapping.delete(cell.handle);

		this.cells.splice(index, 1);
		return true;
	}

267 268 269 270

	transformMimeTypes(cell: ExtHostCell, output: vscode.CellDisplayOutput): ITransformedDisplayOutputDto {
		let mimeTypes = Object.keys(output.data);

R
rebornix 已提交
271
		// TODO@rebornix, the document display order might be assigned a bit later. We need to postpone sending the outputs to the core side.
R
rebornix 已提交
272 273
		let coreDisplayOrder = this.renderingHandler.outputDisplayOrder;
		const sorted = sortMimeTypes(mimeTypes, coreDisplayOrder?.userOrder || [], this._parsedDisplayOrder, coreDisplayOrder?.defaultOrder || []);
274 275 276 277 278 279 280

		let orderMimeTypes: IOrderedMimeType[] = [];

		sorted.forEach(mimeType => {
			let handlers = this.renderingHandler.findBestMatchedRenderer(mimeType);

			if (handlers.length) {
R
rebornix 已提交
281
				let renderedOutput = handlers[0].render(this, cell, output, mimeType);
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

				orderMimeTypes.push({
					mimeType: mimeType,
					isResolved: true,
					rendererId: handlers[0].handle,
					output: renderedOutput
				});

				for (let i = 1; i < handlers.length; i++) {
					orderMimeTypes.push({
						mimeType: mimeType,
						isResolved: false,
						rendererId: handlers[i].handle
					});
				}

				if (mimeTypeSupportedByCore(mimeType)) {
					orderMimeTypes.push({
						mimeType: mimeType,
						isResolved: false,
						rendererId: -1
					});
				}
			} else {
				orderMimeTypes.push({
					mimeType: mimeType,
					isResolved: false
				});
			}
		});

		return {
R
rebornix 已提交
314
			outputKind: output.outputKind,
315 316 317 318 319 320
			data: output.data,
			orderedMimeTypes: orderMimeTypes,
			pickedMimeTypeIndex: 0
		};
	}

R
rebornix 已提交
321
	getCell(cellHandle: number) {
R
rebornix 已提交
322 323
		return this.cells.find(cell => cell.handle === cellHandle);
	}
R
rebornix 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

	attachCellTextDocument(cellHandle: number, textDocument: vscode.TextDocument) {
		let cell = this.cells.find(cell => cell.handle === cellHandle);

		if (cell) {
			cell.attachTextDocument(textDocument);
		}
	}

	detachCellTextDocument(cellHandle: number, textDocument: vscode.TextDocument) {
		let cell = this.cells.find(cell => cell.handle === cellHandle);

		if (cell) {
			cell.detachTextDocument(textDocument);
		}
	}
R
rebornix 已提交
340 341
}

R
rebornix 已提交
342
export class ExtHostNotebookEditor implements vscode.NotebookEditor, vscode.Disposable {
R
rebornix 已提交
343
	private _viewColumn: vscode.ViewColumn | undefined;
R
rebornix 已提交
344
	private _disposableStore = new DisposableStore();
R
rebornix 已提交
345 346

	constructor(
R
rebornix 已提交
347
		viewType: string,
R
rebornix 已提交
348 349 350
		readonly id: string,
		public uri: URI,
		public document: ExtHostNotebookDocument,
R
rebornix 已提交
351
		private _documentsAndEditors: ExtHostDocumentsAndEditors
R
rebornix 已提交
352
	) {
R
rebornix 已提交
353
		this._disposableStore.add(this._documentsAndEditors.onDidAddDocuments(documents => {
R
rebornix 已提交
354 355
			for (const data of documents) {
				let textDocument = data.document;
R
rebornix 已提交
356
				let parsedCellUri = parseCellUri(textDocument.uri);
R
rebornix 已提交
357

R
rebornix 已提交
358 359 360
				if (!parsedCellUri) {
					continue;
				}
R
rebornix 已提交
361

R
rebornix 已提交
362 363 364
				let notebookUri = parsedCellUri.notebook;
				let cellFsPath = textDocument.uri.fsPath;

R
rebornix 已提交
365
				const cellHandle = parseCellHandle(cellFsPath);
R
rebornix 已提交
366

R
rebornix 已提交
367
				if (cellHandle !== undefined) {
R
rebornix 已提交
368 369
					if (this.document.uri.fsPath === notebookUri.fsPath) {
						document.attachCellTextDocument(Number(cellHandle), textDocument);
R
rebornix 已提交
370 371 372
					}
				}
			}
R
rebornix 已提交
373
		}));
R
rebornix 已提交
374

R
rebornix 已提交
375
		this._disposableStore.add(this._documentsAndEditors.onDidRemoveDocuments(documents => {
R
rebornix 已提交
376 377
			for (const data of documents) {
				let textDocument = data.document;
R
rebornix 已提交
378
				let parsedCellUri = parseCellUri(textDocument.uri);
R
rebornix 已提交
379

R
rebornix 已提交
380 381 382
				if (!parsedCellUri) {
					continue;
				}
R
rebornix 已提交
383

R
rebornix 已提交
384 385 386
				let notebookUri = parsedCellUri.notebook;
				let cellFsPath = textDocument.uri.fsPath;

R
rebornix 已提交
387
				const cellHandle = parseCellHandle(cellFsPath);
R
rebornix 已提交
388

R
rebornix 已提交
389
				if (cellHandle !== undefined) {
R
rebornix 已提交
390 391
					if (this.document.uri.fsPath === notebookUri.fsPath) {
						document.detachCellTextDocument(Number(cellHandle), textDocument);
R
rebornix 已提交
392 393 394
					}
				}
			}
R
rebornix 已提交
395 396 397 398 399
		}));
	}

	dispose() {
		this._disposableStore.dispose();
R
rebornix 已提交
400 401
	}

R
rebornix 已提交
402
	createCell(content: string, language: string, type: CellKind, outputs: vscode.CellOutput[]): vscode.NotebookCell {
R
rebornix 已提交
403 404 405 406 407 408 409 410 411 412
		let cell = new ExtHostCell(content, type, language, outputs);
		return cell;
	}

	get viewColumn(): vscode.ViewColumn | undefined {
		return this._viewColumn;
	}

	set viewColumn(value) {
		throw readonly('viewColumn');
R
rebornix 已提交
413 414 415
	}
}

R
rebornix 已提交
416
export class ExtHostNotebookOutputRenderer {
R
rebornix 已提交
417 418 419
	private static _handlePool: number = 0;
	readonly handle = ExtHostNotebookOutputRenderer._handlePool++;

R
rebornix 已提交
420
	constructor(
R
rebornix 已提交
421 422 423
		public type: string,
		public filter: vscode.NotebookOutputSelector,
		public renderer: vscode.NotebookOutputRenderer
R
rebornix 已提交
424 425 426 427
	) {

	}

R
rebornix 已提交
428 429 430
	matches(mimeType: string): boolean {
		if (this.filter.subTypes) {
			if (this.filter.subTypes.indexOf(mimeType) >= 0) {
R
rebornix 已提交
431 432 433 434 435 436
				return true;
			}
		}
		return false;
	}

R
rebornix 已提交
437 438
	render(document: ExtHostNotebookDocument, cell: ExtHostCell, output: vscode.CellOutput, mimeType: string): string {
		let html = this.renderer.render(document, cell, output, mimeType);
R
rebornix 已提交
439

440
		return html;
R
rebornix 已提交
441 442 443 444
	}
}

export interface ExtHostNotebookOutputRenderingHandler {
R
rebornix 已提交
445
	outputDisplayOrder: ExtHostOutputDisplayOrder | undefined;
R
rebornix 已提交
446
	findBestMatchedRenderer(mimeType: string): ExtHostNotebookOutputRenderer[];
R
rebornix 已提交
447 448 449
}

export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostNotebookOutputRenderingHandler {
R
rebornix 已提交
450 451
	private static _handlePool: number = 0;

R
rebornix 已提交
452 453
	private readonly _proxy: MainThreadNotebookShape;
	private readonly _notebookProviders = new Map<string, { readonly provider: vscode.NotebookProvider, readonly extension: IExtensionDescription }>();
R
rebornix 已提交
454
	private readonly _documents = new Map<string, ExtHostNotebookDocument>();
R
rebornix 已提交
455
	private readonly _editors = new Map<string, ExtHostNotebookEditor>();
R
rebornix 已提交
456
	private readonly _notebookOutputRenderers = new Map<number, ExtHostNotebookOutputRenderer>();
R
rebornix 已提交
457
	private _outputDisplayOrder: ExtHostOutputDisplayOrder | undefined;
R
rebornix 已提交
458

R
rebornix 已提交
459 460
	get outputDisplayOrder(): ExtHostOutputDisplayOrder | undefined {
		return this._outputDisplayOrder;
R
rebornix 已提交
461 462
	}

R
rebornix 已提交
463 464 465 466 467 468
	private _activeNotebookDocument: ExtHostNotebookDocument | undefined;

	get activeNotebookDocument() {
		return this._activeNotebookDocument;
	}

R
rebornix 已提交
469 470 471 472
	constructor(mainContext: IMainContext, private _documentsAndEditors: ExtHostDocumentsAndEditors) {
		this._proxy = mainContext.getProxy(MainContext.MainThreadNotebook);
	}

R
rebornix 已提交
473
	registerNotebookOutputRenderer(
R
rebornix 已提交
474
		type: string,
R
rebornix 已提交
475
		extension: IExtensionDescription,
R
rebornix 已提交
476
		filter: vscode.NotebookOutputSelector,
R
rebornix 已提交
477
		renderer: vscode.NotebookOutputRenderer
R
rebornix 已提交
478
	): vscode.Disposable {
R
rebornix 已提交
479
		let extHostRenderer = new ExtHostNotebookOutputRenderer(type, filter, renderer);
R
rebornix 已提交
480
		this._notebookOutputRenderers.set(extHostRenderer.handle, extHostRenderer);
R
rebornix 已提交
481
		this._proxy.$registerNotebookRenderer({ id: extension.identifier, location: extension.extensionLocation }, type, filter, extHostRenderer.handle, renderer.preloads || []);
R
rebornix 已提交
482
		return new VSCodeDisposable(() => {
R
rebornix 已提交
483 484 485
			this._notebookOutputRenderers.delete(extHostRenderer.handle);
			this._proxy.$unregisterNotebookRenderer(extHostRenderer.handle);
		});
R
rebornix 已提交
486 487
	}

R
rebornix 已提交
488 489
	findBestMatchedRenderer(mimeType: string): ExtHostNotebookOutputRenderer[] {
		let matches: ExtHostNotebookOutputRenderer[] = [];
R
rebornix 已提交
490
		for (let renderer of this._notebookOutputRenderers) {
R
rebornix 已提交
491
			if (renderer[1].matches(mimeType)) {
R
rebornix 已提交
492
				matches.push(renderer[1]);
R
rebornix 已提交
493 494 495
			}
		}

R
rebornix 已提交
496
		return matches;
R
rebornix 已提交
497 498
	}

R
rebornix 已提交
499
	registerNotebookProvider(
R
rebornix 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
		extension: IExtensionDescription,
		viewType: string,
		provider: vscode.NotebookProvider,
	): vscode.Disposable {

		if (this._notebookProviders.has(viewType)) {
			throw new Error(`Notebook provider for '${viewType}' already registered`);
		}

		this._notebookProviders.set(viewType, { extension, provider });
		this._proxy.$registerNotebookProvider({ id: extension.identifier, location: extension.extensionLocation }, viewType);
		return new VSCodeDisposable(() => {
			this._notebookProviders.delete(viewType);
			this._proxy.$unregisterNotebookProvider(viewType);
		});
	}

517
	async $resolveNotebook(viewType: string, uri: UriComponents): Promise<number | undefined> {
R
rebornix 已提交
518 519 520
		let provider = this._notebookProviders.get(viewType);

		if (provider) {
R
rebornix 已提交
521
			if (!this._documents.has(URI.revive(uri).toString())) {
R
rebornix 已提交
522
				let document = new ExtHostNotebookDocument(this._proxy, viewType, URI.revive(uri), this);
R
rebornix 已提交
523 524
				await this._proxy.$createNotebookDocument(
					document.handle,
R
rebornix 已提交
525
					viewType,
R
rebornix 已提交
526 527 528 529
					uri
				);

				this._documents.set(URI.revive(uri).toString(), document);
R
rebornix 已提交
530
			}
R
rebornix 已提交
531

R
rebornix 已提交
532 533 534
			let editor = new ExtHostNotebookEditor(
				viewType,
				`${ExtHostNotebookController._handlePool++}`,
535
				URI.revive(uri),
R
rebornix 已提交
536 537 538
				this._documents.get(URI.revive(uri).toString())!,
				this._documentsAndEditors
			);
R
rebornix 已提交
539 540

			this._editors.set(URI.revive(uri).toString(), editor);
R
rebornix 已提交
541
			await provider.provider.resolveNotebook(editor);
542
			// await editor.document.$updateCells();
R
rebornix 已提交
543
			return editor.document.handle;
R
rebornix 已提交
544 545 546 547 548
		}

		return Promise.resolve(undefined);
	}

R
rebornix 已提交
549
	async $executeNotebook(viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void> {
R
rebornix 已提交
550 551
		let provider = this._notebookProviders.get(viewType);

R
rebornix 已提交
552 553
		if (!provider) {
			return;
R
rebornix 已提交
554
		}
R
rebornix 已提交
555

R
rebornix 已提交
556
		let document = this._documents.get(URI.revive(uri).toString());
R
rebornix 已提交
557

R
rebornix 已提交
558 559
		if (!document) {
			return;
R
rebornix 已提交
560
		}
R
rebornix 已提交
561 562 563

		let cell = cellHandle !== undefined ? document.getCell(cellHandle) : undefined;
		return provider.provider.executeCell(document!, cell);
R
rebornix 已提交
564 565
	}

R
rebornix 已提交
566
	async $createEmptyCell(viewType: string, uri: URI, index: number, language: string, type: CellKind): Promise<ICellDto | undefined> {
R
rebornix 已提交
567 568 569 570 571 572 573
		let provider = this._notebookProviders.get(viewType);

		if (provider) {
			let editor = this._editors.get(URI.revive(uri).toString());
			let document = this._documents.get(URI.revive(uri).toString());

			let rawCell = editor?.createCell('', language, type, []) as ExtHostCell;
R
rebornix 已提交
574
			document?.insertCell(index, rawCell!);
R
rebornix 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

			let allDocuments = this._documentsAndEditors.allDocuments();
			for (let i = 0; i < allDocuments.length; i++) {
				let textDocument = allDocuments[i].document;
				let parsedCellUri = parseCellUri(textDocument.uri);

				if (!parsedCellUri) {
					continue;
				}

				let notebookUri = parsedCellUri.notebook;
				let cellFsPath = textDocument.uri.fsPath;
				const cellHandle = parseCellHandle(cellFsPath);

				if (cellHandle !== undefined) {
					if (uri.fsPath === notebookUri.fsPath && Number(cellHandle) === rawCell.handle) {
						rawCell.attachTextDocument(textDocument);
					}

				}
			}


R
rebornix 已提交
598 599 600 601
			return {
				handle: rawCell.handle,
				source: rawCell.source,
				language: rawCell.language,
R
rebornix 已提交
602
				cellKind: rawCell.cellKind,
603
				outputs: []
R
rebornix 已提交
604 605 606 607 608 609
			};
		}

		return;
	}

610
	async $deleteCell(viewType: string, uri: UriComponents, index: number): Promise<boolean> {
R
rebornix 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
		let provider = this._notebookProviders.get(viewType);

		if (provider) {
			let document = this._documents.get(URI.revive(uri).toString());

			if (document) {
				return document.deleteCell(index);
			}

			return false;
		}

		return false;
	}

626
	async $saveNotebook(viewType: string, uri: UriComponents): Promise<boolean> {
R
rebornix 已提交
627 628 629 630 631 632 633 634 635 636
		let provider = this._notebookProviders.get(viewType);
		let document = this._documents.get(URI.revive(uri).toString());

		if (provider && document) {
			return await provider.provider.save(document);
		}

		return false;
	}

637
	async $updateActiveEditor(viewType: string, uri: UriComponents): Promise<void> {
R
rebornix 已提交
638 639 640 641 642 643 644 645 646
		let document = this._documents.get(URI.revive(uri).toString());

		if (document) {
			this._activeNotebookDocument = document;
		} else {
			this._activeNotebookDocument = undefined;
		}
	}

647
	async $destoryNotebookDocument(viewType: string, uri: UriComponents): Promise<boolean> {
648 649 650 651 652 653
		let provider = this._notebookProviders.get(viewType);

		if (provider) {
			let document = this._documents.get(URI.revive(uri).toString());

			if (document) {
R
rebornix 已提交
654
				document.dispose();
655 656 657 658 659 660
				this._documents.delete(URI.revive(uri).toString());
			}

			let editor = this._editors.get(URI.revive(uri).toString());

			if (editor) {
R
rebornix 已提交
661
				editor.dispose();
662 663 664 665 666 667 668 669 670
				this._editors.delete(URI.revive(uri).toString());
			}

			return true;
		}

		return false;
	}

R
rebornix 已提交
671 672 673 674 675 676 677 678
	$acceptDisplayOrder(displayOrder: INotebookDisplayOrder): void {
		let parsedDefaultDisplayOrder = displayOrder.defaultOrder.map(pattern => glob.parse(pattern));
		let parsedUserPattern = displayOrder.userOrder?.map(pattern => glob.parse(pattern));
		this._outputDisplayOrder = {
			defaultOrder: parsedDefaultDisplayOrder,
			userOrder: parsedUserPattern
		};
	}
R
rebornix 已提交
679
}