mainThreadNotebook.ts 13.6 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 { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
7
import { MainContext, MainThreadNotebookShape, NotebookExtensionDescription, IExtHostContext, ExtHostNotebookShape, ExtHostContext } from '../common/extHost.protocol';
R
rebornix 已提交
8
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
R
rebornix 已提交
9
import { URI, UriComponents } from 'vs/base/common/uri';
R
rebornix 已提交
10 11
import { INotebookService, IMainNotebookController } from 'vs/workbench/contrib/notebook/browser/notebookService';
import { Emitter, Event } from 'vs/base/common/event';
12
import { ICell, IOutput, INotebook, INotebookMimeTypeSelector, NOTEBOOK_DISPLAY_ORDER, NotebookCellsSplice, NotebookCellOutputsSplice, CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
13
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
R
rebornix 已提交
14
import { PieceTreeTextBufferFactory, PieceTreeTextBufferBuilder } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder';
R
rebornix 已提交
15 16

export class MainThreadCell implements ICell {
17 18
	private _onDidChangeOutputs = new Emitter<NotebookCellOutputsSplice[]>();
	onDidChangeOutputs: Event<NotebookCellOutputsSplice[]> = this._onDidChangeOutputs.event;
R
rebornix 已提交
19

R
rebornix 已提交
20 21 22
	private _onDidChangeDirtyState = new Emitter<boolean>();
	onDidChangeDirtyState: Event<boolean> = this._onDidChangeDirtyState.event;

R
rebornix 已提交
23 24
	private _outputs: IOutput[];

R
rebornix 已提交
25
	get outputs(): IOutput[] {
R
rebornix 已提交
26 27 28
		return this._outputs;
	}

R
rebornix 已提交
29 30 31 32 33 34 35 36 37 38 39
	private _isDirty: boolean = false;

	get isDirty() {
		return this._isDirty;
	}

	set isDirty(newState: boolean) {
		this._isDirty = newState;
		this._onDidChangeDirtyState.fire(newState);
	}

40 41 42 43 44 45 46 47
	get source() {
		return this._source;
	}

	set source(newValue: string[]) {
		this._source = newValue;
		this._buffer = null;
	}
48

R
rebornix 已提交
49 50
	private _buffer: PieceTreeTextBufferFactory | null = null;

R
rebornix 已提交
51
	constructor(
52
		readonly uri: URI,
R
rebornix 已提交
53
		public handle: number,
54
		private _source: string[],
R
rebornix 已提交
55
		public language: string,
R
rebornix 已提交
56
		public cellKind: CellKind,
R
rebornix 已提交
57 58 59 60
		outputs: IOutput[]
	) {
		this._outputs = outputs;
	}
R
rebornix 已提交
61

62 63 64 65 66
	spliceNotebookCellOutputs(splices: NotebookCellOutputsSplice[]): void {
		splices.reverse().forEach(splice => {
			this.outputs.splice(splice[0], splice[1], ...splice[2]);
		});

67
		this._onDidChangeOutputs.fire(splices);
68 69
	}

R
rebornix 已提交
70 71 72
	save() {
		this._isDirty = false;
	}
R
rebornix 已提交
73 74 75 76 77 78 79 80 81 82 83

	resolveTextBufferFactory(): PieceTreeTextBufferFactory {
		if (this._buffer) {
			return this._buffer;
		}

		let builder = new PieceTreeTextBufferBuilder();
		builder.acceptChunk(this.source.join('\n'));
		this._buffer = builder.finish(true);
		return this._buffer;
	}
R
rebornix 已提交
84 85
}

R
rebornix 已提交
86 87
export class MainThreadNotebookDocument extends Disposable implements INotebook {
	private readonly _onWillDispose: Emitter<void> = this._register(new Emitter<void>());
R
rebornix 已提交
88
	readonly onWillDispose: Event<void> = this._onWillDispose.event;
89 90
	private readonly _onDidChangeCells = new Emitter<NotebookCellsSplice[]>();
	get onDidChangeCells(): Event<NotebookCellsSplice[]> { return this._onDidChangeCells.event; }
R
rebornix 已提交
91 92
	private _onDidChangeDirtyState = new Emitter<boolean>();
	onDidChangeDirtyState: Event<boolean> = this._onDidChangeDirtyState.event;
R
rebornix 已提交
93
	private _mapping: Map<number, MainThreadCell> = new Map();
R
rebornix 已提交
94
	private _cellListeners: Map<number, IDisposable> = new Map();
R
rebornix 已提交
95 96 97 98
	cells: MainThreadCell[];
	activeCell: MainThreadCell | undefined;
	languages: string[] = [];
	renderers = new Set<number>();
R
rebornix 已提交
99 100 101 102 103 104 105 106 107 108 109

	private _isDirty: boolean = false;

	get isDirty() {
		return this._isDirty;
	}

	set isDirty(newState: boolean) {
		this._isDirty = newState;
		this._onDidChangeDirtyState.fire(newState);
	}
R
rebornix 已提交
110 111

	constructor(
R
rebornix 已提交
112
		private readonly _proxy: ExtHostNotebookShape,
R
rebornix 已提交
113
		public handle: number,
R
rebornix 已提交
114
		public viewType: string,
R
rebornix 已提交
115
		public uri: URI
R
rebornix 已提交
116
	) {
R
rebornix 已提交
117
		super();
R
rebornix 已提交
118
		this.cells = [];
R
rebornix 已提交
119 120
	}

R
rebornix 已提交
121 122 123 124
	updateLanguages(languages: string[]) {
		this.languages = languages;
	}

125 126 127 128 129 130
	updateRenderers(renderers: number[]) {
		renderers.forEach(render => {
			this.renderers.add(render);
		});
	}

R
rebornix 已提交
131 132 133
	updateActiveCell(handle: number) {
		this.activeCell = this._mapping.get(handle);
	}
R
rebornix 已提交
134

R
rebornix 已提交
135
	async createRawCell(viewType: string, uri: URI, index: number, language: string, type: CellKind): Promise<MainThreadCell | undefined> {
R
rebornix 已提交
136
		let cell = await this._proxy.$createEmptyCell(viewType, uri, index, language, type);
R
rebornix 已提交
137
		if (cell) {
138
			let mainCell = new MainThreadCell(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs);
R
rebornix 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152
			this._mapping.set(cell.handle, mainCell);
			this.cells.splice(index, 0, mainCell);

			let dirtyStateListener = mainCell.onDidChangeDirtyState((cellState) => {
				this.isDirty = this.isDirty || cellState;
			});

			this._cellListeners.set(cell.handle, dirtyStateListener);
			return mainCell;
		}

		return;
	}

R
rebornix 已提交
153 154 155 156 157 158 159 160 161 162 163
	async deleteCell(uri: URI, index: number): Promise<boolean> {
		let deleteExtHostCell = await this._proxy.$deleteCell(this.viewType, uri, index);
		if (deleteExtHostCell) {
			let cell = this.cells[index];
			this._cellListeners.get(cell.handle)?.dispose();
			this._cellListeners.delete(cell.handle);
			this.cells.splice(index, 1);
			return true;
		}

		return false;
R
rebornix 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177
	}

	async save(): Promise<boolean> {
		let ret = await this._proxy.$saveNotebook(this.viewType, this.uri);

		if (ret) {
			this.cells.forEach((cell) => {
				cell.save();
			});
		}

		return ret;
	}

178 179 180 181
	spliceNotebookCells(splices: NotebookCellsSplice[]): void {
		splices.reverse().forEach(splice => {
			let cellDtos = splice[2];
			let newCells = cellDtos.map(cell => {
182
				let mainCell = new MainThreadCell(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs || []);
183 184 185 186 187 188 189 190 191 192 193
				this._mapping.set(cell.handle, mainCell);
				let dirtyStateListener = mainCell.onDidChangeDirtyState((cellState) => {
					this.isDirty = this.isDirty || cellState;
				});
				this._cellListeners.set(cell.handle, dirtyStateListener);
				return mainCell;
			});

			this.cells.splice(splice[0], splice[1], ...newCells);
		});

194
		this._onDidChangeCells.fire(splices);
195 196 197 198 199 200 201
	}

	spliceNotebookCellOutputs(cellHandle: number, splices: NotebookCellOutputsSplice[]): void {
		let cell = this._mapping.get(cellHandle);
		cell?.spliceNotebookCellOutputs(splices);
	}

R
rebornix 已提交
202 203
	dispose() {
		this._onWillDispose.fire();
R
rebornix 已提交
204
		this._cellListeners.forEach(val => val.dispose());
R
rebornix 已提交
205 206
		super.dispose();
	}
R
rebornix 已提交
207 208 209 210 211 212 213 214 215
}

@extHostNamedCustomer(MainContext.MainThreadNotebook)
export class MainThreadNotebooks extends Disposable implements MainThreadNotebookShape {
	private readonly _notebookProviders = new Map<string, MainThreadNotebookController>();
	private readonly _proxy: ExtHostNotebookShape;

	constructor(
		extHostContext: IExtHostContext,
R
rebornix 已提交
216 217
		@INotebookService private _notebookService: INotebookService,
		@IConfigurationService private readonly configurationService: IConfigurationService
R
rebornix 已提交
218 219 220
	) {
		super();
		this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostNotebook);
R
rebornix 已提交
221 222 223 224
		this.registerListeners();
	}

	registerListeners() {
R
rebornix 已提交
225 226 227
		this._register(this._notebookService.onDidChangeActiveEditor(e => {
			this._proxy.$updateActiveEditor(e.viewType, e.uri);
		}));
R
rebornix 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

		let userOrder = this.configurationService.getValue<string[]>('notebook.displayOrder');
		this._proxy.$acceptDisplayOrder({
			defaultOrder: NOTEBOOK_DISPLAY_ORDER,
			userOrder: userOrder
		});

		this.configurationService.onDidChangeConfiguration(e => {
			if (e.affectedKeys.indexOf('notebook.displayOrder') >= 0) {
				let userOrder = this.configurationService.getValue<string[]>('notebook.displayOrder');

				this._proxy.$acceptDisplayOrder({
					defaultOrder: NOTEBOOK_DISPLAY_ORDER,
					userOrder: userOrder
				});
			}
		});
R
rebornix 已提交
245 246
	}

R
rebornix 已提交
247 248
	async $registerNotebookRenderer(extension: NotebookExtensionDescription, type: string, selectors: INotebookMimeTypeSelector, handle: number, preloads: UriComponents[]): Promise<void> {
		this._notebookService.registerNotebookRenderer(handle, extension, type, selectors, preloads.map(uri => URI.revive(uri)));
R
rebornix 已提交
249 250
	}

251 252
	async $unregisterNotebookRenderer(handle: number): Promise<void> {
		this._notebookService.unregisterNotebookRenderer(handle);
R
rebornix 已提交
253 254
	}

R
rebornix 已提交
255 256
	async $registerNotebookProvider(extension: NotebookExtensionDescription, viewType: string): Promise<void> {
		let controller = new MainThreadNotebookController(this._proxy, this, viewType);
R
rebornix 已提交
257
		this._notebookProviders.set(viewType, controller);
R
rebornix 已提交
258
		this._notebookService.registerNotebookController(viewType, extension, controller);
R
rebornix 已提交
259
		return;
R
rebornix 已提交
260 261
	}

R
rebornix 已提交
262
	async $unregisterNotebookProvider(viewType: string): Promise<void> {
R
rebornix 已提交
263 264
		this._notebookProviders.delete(viewType);
		this._notebookService.unregisterNotebookProvider(viewType);
R
rebornix 已提交
265 266 267
		return;
	}

R
rebornix 已提交
268 269 270 271 272 273 274
	async $createNotebookDocument(handle: number, viewType: string, resource: UriComponents): Promise<void> {
		let controller = this._notebookProviders.get(viewType);

		if (controller) {
			controller.createNotebookDocument(handle, viewType, resource);
		}

R
rebornix 已提交
275 276 277
		return;
	}

R
rebornix 已提交
278
	async $updateNotebookLanguages(viewType: string, resource: UriComponents, languages: string[]): Promise<void> {
R
rebornix 已提交
279 280 281
		let controller = this._notebookProviders.get(viewType);

		if (controller) {
R
rebornix 已提交
282
			controller.updateLanguages(resource, languages);
R
rebornix 已提交
283 284
		}
	}
R
rebornix 已提交
285

R
rebornix 已提交
286 287 288
	async resolveNotebook(viewType: string, uri: URI): Promise<number | undefined> {
		let handle = await this._proxy.$resolveNotebook(viewType, uri);
		return handle;
R
rebornix 已提交
289 290
	}

291
	async $spliceNotebookCells(viewType: string, resource: UriComponents, splices: NotebookCellsSplice[], renderers: number[]): Promise<void> {
292
		let controller = this._notebookProviders.get(viewType);
293
		controller?.spliceNotebookCells(resource, splices, renderers);
294 295
	}

296
	async $spliceNotebookCellOutputs(viewType: string, resource: UriComponents, cellHandle: number, splices: NotebookCellOutputsSplice[], renderers: number[]): Promise<void> {
297
		let controller = this._notebookProviders.get(viewType);
298
		controller?.spliceNotebookCellOutputs(resource, cellHandle, splices, renderers);
299 300
	}

R
rebornix 已提交
301 302
	async executeNotebook(viewType: string, uri: URI): Promise<void> {
		return this._proxy.$executeNotebook(viewType, uri, undefined);
R
rebornix 已提交
303 304 305 306
	}
}

export class MainThreadNotebookController implements IMainNotebookController {
R
rebornix 已提交
307
	private _mapping: Map<string, MainThreadNotebookDocument> = new Map();
R
rebornix 已提交
308 309

	constructor(
R
rebornix 已提交
310 311 312
		private readonly _proxy: ExtHostNotebookShape,
		private _mainThreadNotebook: MainThreadNotebooks,
		private _viewType: string
R
rebornix 已提交
313 314 315 316
	) {
	}

	async resolveNotebook(viewType: string, uri: URI): Promise<INotebook | undefined> {
317
		// TODO: resolve notebook should wait for all notebook document destory operations to finish.
R
rebornix 已提交
318 319 320 321 322 323 324 325 326 327
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());

		if (mainthreadNotebook) {
			return mainthreadNotebook;
		}

		let notebookHandle = await this._mainThreadNotebook.resolveNotebook(viewType, uri);
		if (notebookHandle !== undefined) {
			mainthreadNotebook = this._mapping.get(URI.from(uri).toString());
			return mainthreadNotebook;
R
rebornix 已提交
328
		}
R
rebornix 已提交
329

R
rebornix 已提交
330 331 332
		return undefined;
	}

333
	spliceNotebookCells(resource: UriComponents, splices: NotebookCellsSplice[], renderers: number[]): void {
334
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
335
		mainthreadNotebook?.updateRenderers(renderers);
336 337 338
		mainthreadNotebook?.spliceNotebookCells(splices);
	}

339
	spliceNotebookCellOutputs(resource: UriComponents, cellHandle: number, splices: NotebookCellOutputsSplice[], renderers: number[]): void {
340
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
341
		mainthreadNotebook?.updateRenderers(renderers);
342 343 344
		mainthreadNotebook?.spliceNotebookCellOutputs(cellHandle, splices);
	}

R
rebornix 已提交
345 346 347 348
	async executeNotebook(viewType: string, uri: URI): Promise<void> {
		this._mainThreadNotebook.executeNotebook(viewType, uri);
	}

R
rebornix 已提交
349 350 351 352 353 354 355 356
	// Methods for ExtHost
	async createNotebookDocument(handle: number, viewType: string, resource: UriComponents): Promise<void> {
		let document = new MainThreadNotebookDocument(this._proxy, handle, viewType, URI.revive(resource));
		this._mapping.set(URI.revive(resource).toString(), document);
	}

	updateLanguages(resource: UriComponents, languages: string[]) {
		let document = this._mapping.get(URI.from(resource).toString());
R
rebornix 已提交
357
		document?.updateLanguages(languages);
R
rebornix 已提交
358 359
	}

360 361
	updateNotebookRenderers(resource: UriComponents, renderers: number[]): void {
		let document = this._mapping.get(URI.from(resource).toString());
R
rebornix 已提交
362
		document?.updateRenderers(renderers);
363 364
	}

R
rebornix 已提交
365 366
	updateNotebookActiveCell(uri: URI, cellHandle: number): void {
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());
R
rebornix 已提交
367
		mainthreadNotebook?.updateActiveCell(cellHandle);
R
rebornix 已提交
368 369
	}

R
rebornix 已提交
370
	async createRawCell(uri: URI, index: number, language: string, type: CellKind): Promise<ICell | undefined> {
R
rebornix 已提交
371
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());
R
rebornix 已提交
372
		return mainthreadNotebook?.createRawCell(this._viewType, uri, index, language, type);
R
rebornix 已提交
373 374
	}

R
rebornix 已提交
375 376 377 378 379 380 381 382 383 384
	async deleteCell(uri: URI, index: number): Promise<boolean> {
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());

		if (mainthreadNotebook) {
			return mainthreadNotebook.deleteCell(uri, index);
		}

		return false;
	}

R
rebornix 已提交
385 386 387 388
	executeNotebookActiveCell(uri: URI): void {
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());

		if (mainthreadNotebook && mainthreadNotebook.activeCell) {
R
rebornix 已提交
389
			this._proxy.$executeNotebook(this._viewType, uri, mainthreadNotebook.activeCell.handle);
R
rebornix 已提交
390 391
		}
	}
R
rebornix 已提交
392

393
	async destoryNotebookDocument(notebook: INotebook): Promise<void> {
R
rebornix 已提交
394
		let document = this._mapping.get(URI.from(notebook.uri).toString());
R
rebornix 已提交
395

R
rebornix 已提交
396 397 398 399 400 401 402 403
		if (!document) {
			return;
		}

		let removeFromExtHost = await this._proxy.$destoryNotebookDocument(this._viewType, notebook.uri);
		if (removeFromExtHost) {
			document.dispose();
			this._mapping.delete(URI.from(notebook.uri).toString());
R
rebornix 已提交
404 405
		}
	}
R
rebornix 已提交
406
}