mainThreadNotebook.ts 13.1 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, NotebookCellsSplice, NotebookCellOutputsSplice } 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';
R
rebornix 已提交
12 13
import { ICell, IOutput, INotebook, INotebookMimeTypeSelector, NOTEBOOK_DISPLAY_ORDER } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
R
rebornix 已提交
14 15 16 17 18

export class MainThreadCell implements ICell {
	private _onDidChangeOutputs = new Emitter<void>();
	onDidChangeOutputs: Event<void> = this._onDidChangeOutputs.event;

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

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

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

R
rebornix 已提交
28
	set outputs(newOutputs: IOutput[]) {
R
rebornix 已提交
29 30 31 32
		this._outputs = newOutputs;
		this._onDidChangeOutputs.fire();
	}

R
rebornix 已提交
33 34 35 36 37 38 39 40 41 42 43
	private _isDirty: boolean = false;

	get isDirty() {
		return this._isDirty;
	}

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

44 45
	readonly uri: URI;

R
rebornix 已提交
46
	constructor(
47
		parent: MainThreadNotebookDocument,
R
rebornix 已提交
48 49
		public handle: number,
		public source: string[],
R
rebornix 已提交
50
		public language: string,
R
rebornix 已提交
51 52 53 54
		public cell_type: 'markdown' | 'code',
		outputs: IOutput[]
	) {
		this._outputs = outputs;
55 56 57 58 59 60
		this.uri = URI.from({
			scheme: 'vscode-notebook',
			authority: parent.viewType,
			path: `/cell_${handle}.${cell_type === 'markdown' ? 'md' : 'py'}`,
			query: parent.uri.toString()
		});
R
rebornix 已提交
61
	}
R
rebornix 已提交
62

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

		this._onDidChangeOutputs.fire();
	}

R
rebornix 已提交
71 72 73
	save() {
		this._isDirty = false;
	}
R
rebornix 已提交
74 75
}

R
rebornix 已提交
76 77
export class MainThreadNotebookDocument extends Disposable implements INotebook {
	private readonly _onWillDispose: Emitter<void> = this._register(new Emitter<void>());
R
rebornix 已提交
78
	readonly onWillDispose: Event<void> = this._onWillDispose.event;
R
rebornix 已提交
79 80
	private readonly _onDidChangeCells = new Emitter<void>();
	get onDidChangeCells(): Event<void> { return this._onDidChangeCells.event; }
R
rebornix 已提交
81 82
	private _onDidChangeDirtyState = new Emitter<boolean>();
	onDidChangeDirtyState: Event<boolean> = this._onDidChangeDirtyState.event;
R
rebornix 已提交
83
	private _mapping: Map<number, MainThreadCell> = new Map();
R
rebornix 已提交
84
	private _cellListeners: Map<number, IDisposable> = new Map();
R
rebornix 已提交
85 86 87 88
	cells: MainThreadCell[];
	activeCell: MainThreadCell | undefined;
	languages: string[] = [];
	renderers = new Set<number>();
R
rebornix 已提交
89 90 91 92 93 94 95 96 97 98 99

	private _isDirty: boolean = false;

	get isDirty() {
		return this._isDirty;
	}

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

	constructor(
R
rebornix 已提交
102
		private readonly _proxy: ExtHostNotebookShape,
R
rebornix 已提交
103
		public handle: number,
R
rebornix 已提交
104
		public viewType: string,
R
rebornix 已提交
105
		public uri: URI
R
rebornix 已提交
106
	) {
R
rebornix 已提交
107
		super();
R
rebornix 已提交
108
		this.cells = [];
R
rebornix 已提交
109 110
	}

R
rebornix 已提交
111 112 113 114
	updateLanguages(languages: string[]) {
		this.languages = languages;
	}

115 116 117 118 119 120
	updateRenderers(renderers: number[]) {
		renderers.forEach(render => {
			this.renderers.add(render);
		});
	}

R
rebornix 已提交
121 122 123
	updateActiveCell(handle: number) {
		this.activeCell = this._mapping.get(handle);
	}
R
rebornix 已提交
124

R
rebornix 已提交
125
	async createRawCell(viewType: string, uri: URI, index: number, language: string, type: 'markdown' | 'code'): Promise<MainThreadCell | undefined> {
R
rebornix 已提交
126
		let cell = await this._proxy.$createEmptyCell(viewType, uri, index, language, type);
R
rebornix 已提交
127
		if (cell) {
128
			let mainCell = new MainThreadCell(this, cell.handle, cell.source, cell.language, cell.cell_type, cell.outputs);
R
rebornix 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142
			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 已提交
143 144 145 146 147 148 149 150 151 152 153
	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 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167
	}

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

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

		return ret;
	}

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
	spliceNotebookCells(splices: NotebookCellsSplice[]): void {
		splices.reverse().forEach(splice => {
			let cellDtos = splice[2];
			let newCells = cellDtos.map(cell => {
				let mainCell = new MainThreadCell(this, cell.handle, cell.source, cell.language, cell.cell_type, cell.outputs || []);
				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);
		});

		// @TODO, support incremental insertion/deletion instead of simple list relayout.
		this._onDidChangeCells.fire();
	}

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

R
rebornix 已提交
193 194 195 196
	dispose() {
		this._onWillDispose.fire();
		super.dispose();
	}
R
rebornix 已提交
197 198 199 200 201 202 203 204 205
}

@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 已提交
206 207
		@INotebookService private _notebookService: INotebookService,
		@IConfigurationService private readonly configurationService: IConfigurationService
R
rebornix 已提交
208 209 210
	) {
		super();
		this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostNotebook);
R
rebornix 已提交
211 212 213 214
		this.registerListeners();
	}

	registerListeners() {
R
rebornix 已提交
215 216 217
		this._register(this._notebookService.onDidChangeActiveEditor(e => {
			this._proxy.$updateActiveEditor(e.viewType, e.uri);
		}));
R
rebornix 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

		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 已提交
235 236
	}

237 238
	async $registerNotebookRenderer(extension: NotebookExtensionDescription, selectors: INotebookMimeTypeSelector, handle: number, preloads: UriComponents[]): Promise<void> {
		this._notebookService.registerNotebookRenderer(handle, extension, selectors, preloads.map(uri => URI.revive(uri)));
R
rebornix 已提交
239 240
	}

241 242
	async $unregisterNotebookRenderer(handle: number): Promise<void> {
		this._notebookService.unregisterNotebookRenderer(handle);
R
rebornix 已提交
243 244
	}

R
rebornix 已提交
245 246
	async $registerNotebookProvider(extension: NotebookExtensionDescription, viewType: string): Promise<void> {
		let controller = new MainThreadNotebookController(this._proxy, this, viewType);
R
rebornix 已提交
247
		this._notebookProviders.set(viewType, controller);
R
rebornix 已提交
248
		this._notebookService.registerNotebookController(viewType, extension, controller);
R
rebornix 已提交
249
		return;
R
rebornix 已提交
250 251
	}

R
rebornix 已提交
252
	async $unregisterNotebookProvider(viewType: string): Promise<void> {
R
rebornix 已提交
253 254
		this._notebookProviders.delete(viewType);
		this._notebookService.unregisterNotebookProvider(viewType);
R
rebornix 已提交
255 256 257
		return;
	}

R
rebornix 已提交
258 259 260 261 262 263 264
	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 已提交
265 266 267
		return;
	}

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

		if (controller) {
R
rebornix 已提交
272
			controller.updateLanguages(resource, languages);
R
rebornix 已提交
273 274
		}
	}
R
rebornix 已提交
275

R
rebornix 已提交
276 277 278
	async resolveNotebook(viewType: string, uri: URI): Promise<number | undefined> {
		let handle = await this._proxy.$resolveNotebook(viewType, uri);
		return handle;
R
rebornix 已提交
279 280
	}

281 282 283 284 285 286 287 288 289 290
	async $spliceNotebookCells(viewType: string, resource: UriComponents, splices: NotebookCellsSplice[]): Promise<void> {
		let controller = this._notebookProviders.get(viewType);
		controller?.spliceNotebookCells(resource, splices);
	}

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

R
rebornix 已提交
291 292
	async executeNotebook(viewType: string, uri: URI): Promise<void> {
		return this._proxy.$executeNotebook(viewType, uri, undefined);
R
rebornix 已提交
293 294 295 296
	}
}

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

	constructor(
R
rebornix 已提交
300 301 302
		private readonly _proxy: ExtHostNotebookShape,
		private _mainThreadNotebook: MainThreadNotebooks,
		private _viewType: string
R
rebornix 已提交
303 304 305 306
	) {
	}

	async resolveNotebook(viewType: string, uri: URI): Promise<INotebook | undefined> {
307
		// TODO: resolve notebook should wait for all notebook document destory operations to finish.
R
rebornix 已提交
308 309 310 311 312 313 314 315 316 317
		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 已提交
318
		}
R
rebornix 已提交
319

R
rebornix 已提交
320 321 322
		return undefined;
	}

323 324 325 326 327 328 329 330 331 332
	spliceNotebookCells(resource: UriComponents, splices: NotebookCellsSplice[]): void {
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
		mainthreadNotebook?.spliceNotebookCells(splices);
	}

	spliceNotebookCellOutputs(resource: UriComponents, cellHandle: number, splices: NotebookCellOutputsSplice[]): void {
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
		mainthreadNotebook?.spliceNotebookCellOutputs(cellHandle, splices);
	}

R
rebornix 已提交
333 334 335 336
	async executeNotebook(viewType: string, uri: URI): Promise<void> {
		this._mainThreadNotebook.executeNotebook(viewType, uri);
	}

R
rebornix 已提交
337 338 339 340 341 342 343 344
	// 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 已提交
345
		document?.updateLanguages(languages);
R
rebornix 已提交
346 347
	}

348 349
	updateNotebookRenderers(resource: UriComponents, renderers: number[]): void {
		let document = this._mapping.get(URI.from(resource).toString());
R
rebornix 已提交
350
		document?.updateRenderers(renderers);
351 352
	}

R
rebornix 已提交
353 354
	updateNotebookActiveCell(uri: URI, cellHandle: number): void {
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());
R
rebornix 已提交
355
		mainthreadNotebook?.updateActiveCell(cellHandle);
R
rebornix 已提交
356 357
	}

R
rebornix 已提交
358 359
	async createRawCell(uri: URI, index: number, language: string, type: 'markdown' | 'code'): Promise<ICell | undefined> {
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());
R
rebornix 已提交
360
		return mainthreadNotebook?.createRawCell(this._viewType, uri, index, language, type);
R
rebornix 已提交
361 362
	}

R
rebornix 已提交
363 364 365 366 367 368 369 370 371 372
	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 已提交
373 374 375 376
	executeNotebookActiveCell(uri: URI): void {
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());

		if (mainthreadNotebook && mainthreadNotebook.activeCell) {
R
rebornix 已提交
377
			this._proxy.$executeNotebook(this._viewType, uri, mainthreadNotebook.activeCell.handle);
R
rebornix 已提交
378 379
		}
	}
R
rebornix 已提交
380

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

R
rebornix 已提交
384 385 386 387 388 389 390 391
		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 已提交
392 393
		}
	}
R
rebornix 已提交
394
}