mainThreadNotebook.ts 10.3 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';
R
rebornix 已提交
7
import { MainContext, MainThreadNotebookShape, NotebookExtensionDescription, IExtHostContext, ExtHostNotebookShape, ExtHostContext } from '../common/extHost.protocol';
8
import { Disposable } from 'vs/base/common/lifecycle';
R
rebornix 已提交
9
import { URI, UriComponents } from 'vs/base/common/uri';
R
rebornix 已提交
10
import { INotebookService, IMainNotebookController } from 'vs/workbench/contrib/notebook/browser/notebookService';
R
rebornix 已提交
11
import { INotebookTextModel, INotebookMimeTypeSelector, NOTEBOOK_DISPLAY_ORDER, NotebookCellsSplice, NotebookCellOutputsSplice, CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
12
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
13 14
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
15 16
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
R
rebornix 已提交
17

18 19
export class MainThreadNotebookDocument extends Disposable {
	private _textModel: NotebookTextModel;
R
rebornix 已提交
20

21 22
	get textModel() {
		return this._textModel;
R
rebornix 已提交
23
	}
R
rebornix 已提交
24 25

	constructor(
R
rebornix 已提交
26
		private readonly _proxy: ExtHostNotebookShape,
R
rebornix 已提交
27
		public handle: number,
R
rebornix 已提交
28
		public viewType: string,
R
rebornix 已提交
29
		public uri: URI
R
rebornix 已提交
30
	) {
R
rebornix 已提交
31
		super();
32
		this._textModel = new NotebookTextModel(handle, viewType, uri);
R
rebornix 已提交
33
	}
R
rebornix 已提交
34

R
rebornix 已提交
35 36 37
	async deleteCell(uri: URI, index: number): Promise<boolean> {
		let deleteExtHostCell = await this._proxy.$deleteCell(this.viewType, uri, index);
		if (deleteExtHostCell) {
38
			this._textModel.removeCell(index);
R
rebornix 已提交
39 40 41 42
			return true;
		}

		return false;
R
rebornix 已提交
43 44
	}

R
rebornix 已提交
45
	dispose() {
46
		this._textModel.dispose();
R
rebornix 已提交
47 48
		super.dispose();
	}
R
rebornix 已提交
49 50 51 52 53 54 55 56 57
}

@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 已提交
58
		@INotebookService private _notebookService: INotebookService,
59 60 61
		@IConfigurationService private readonly configurationService: IConfigurationService,
		@IEditorService private readonly editorService: IEditorService,

R
rebornix 已提交
62 63 64
	) {
		super();
		this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostNotebook);
R
rebornix 已提交
65 66 67 68
		this.registerListeners();
	}

	registerListeners() {
R
rebornix 已提交
69 70 71
		this._register(this._notebookService.onDidChangeActiveEditor(e => {
			this._proxy.$updateActiveEditor(e.viewType, e.uri);
		}));
R
rebornix 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

		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 已提交
89 90
	}

R
rebornix 已提交
91 92
	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 已提交
93 94
	}

95 96
	async $unregisterNotebookRenderer(handle: number): Promise<void> {
		this._notebookService.unregisterNotebookRenderer(handle);
R
rebornix 已提交
97 98
	}

R
rebornix 已提交
99 100
	async $registerNotebookProvider(extension: NotebookExtensionDescription, viewType: string): Promise<void> {
		let controller = new MainThreadNotebookController(this._proxy, this, viewType);
R
rebornix 已提交
101
		this._notebookProviders.set(viewType, controller);
R
rebornix 已提交
102
		this._notebookService.registerNotebookController(viewType, extension, controller);
R
rebornix 已提交
103
		return;
R
rebornix 已提交
104 105
	}

R
rebornix 已提交
106
	async $unregisterNotebookProvider(viewType: string): Promise<void> {
R
rebornix 已提交
107 108
		this._notebookProviders.delete(viewType);
		this._notebookService.unregisterNotebookProvider(viewType);
R
rebornix 已提交
109 110 111
		return;
	}

R
rebornix 已提交
112 113 114 115 116 117 118
	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 已提交
119 120 121
		return;
	}

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

		if (controller) {
R
rebornix 已提交
126
			controller.updateLanguages(resource, languages);
R
rebornix 已提交
127 128
		}
	}
R
rebornix 已提交
129

R
rebornix 已提交
130 131 132
	async resolveNotebook(viewType: string, uri: URI): Promise<number | undefined> {
		let handle = await this._proxy.$resolveNotebook(viewType, uri);
		return handle;
R
rebornix 已提交
133 134
	}

135
	async $spliceNotebookCells(viewType: string, resource: UriComponents, splices: NotebookCellsSplice[], renderers: number[]): Promise<void> {
136
		let controller = this._notebookProviders.get(viewType);
137
		controller?.spliceNotebookCells(resource, splices, renderers);
138 139
	}

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

R
rebornix 已提交
145 146
	async executeNotebook(viewType: string, uri: URI): Promise<void> {
		return this._proxy.$executeNotebook(viewType, uri, undefined);
R
rebornix 已提交
147
	}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

	async $postMessage(handle: number, value: any): Promise<boolean> {

		const activeEditorPane = this.editorService.activeEditorPane as any | undefined;
		if (activeEditorPane?.isNotebookEditor) {
			const notebookEditor = (activeEditorPane as INotebookEditor);

			if (notebookEditor.viewModel?.handle === handle) {
				notebookEditor.postMessage(value);
				return true;
			}
		}

		return false;
	}
R
rebornix 已提交
163 164 165
}

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

	constructor(
R
rebornix 已提交
169 170 171
		private readonly _proxy: ExtHostNotebookShape,
		private _mainThreadNotebook: MainThreadNotebooks,
		private _viewType: string
R
rebornix 已提交
172 173 174
	) {
	}

175
	async resolveNotebook(viewType: string, uri: URI): Promise<NotebookTextModel | undefined> {
176
		// TODO: resolve notebook should wait for all notebook document destory operations to finish.
R
rebornix 已提交
177 178 179
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());

		if (mainthreadNotebook) {
180
			return mainthreadNotebook.textModel;
R
rebornix 已提交
181 182 183 184 185
		}

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

R
rebornix 已提交
189 190 191
		return undefined;
	}

192
	spliceNotebookCells(resource: UriComponents, splices: NotebookCellsSplice[], renderers: number[]): void {
193
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
194 195
		mainthreadNotebook?.textModel.updateRenderers(renderers);
		mainthreadNotebook?.textModel.$spliceNotebookCells(splices);
196 197
	}

198
	spliceNotebookCellOutputs(resource: UriComponents, cellHandle: number, splices: NotebookCellOutputsSplice[], renderers: number[]): void {
199
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
200 201
		mainthreadNotebook?.textModel.updateRenderers(renderers);
		mainthreadNotebook?.textModel.$spliceNotebookCellOutputs(cellHandle, splices);
202 203
	}

R
rebornix 已提交
204 205 206 207
	async executeNotebook(viewType: string, uri: URI): Promise<void> {
		this._mainThreadNotebook.executeNotebook(viewType, uri);
	}

208 209 210 211
	onDidReceiveMessage(uri: UriComponents, message: any): void {
		this._proxy.$onDidReceiveMessage(uri, message);
	}

R
rebornix 已提交
212 213 214 215 216 217 218 219
	// 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());
220
		document?.textModel.updateLanguages(languages);
R
rebornix 已提交
221 222
	}

223 224
	updateNotebookRenderers(resource: UriComponents, renderers: number[]): void {
		let document = this._mapping.get(URI.from(resource).toString());
225
		document?.textModel.updateRenderers(renderers);
226 227
	}

R
rebornix 已提交
228 229
	updateNotebookActiveCell(uri: URI, cellHandle: number): void {
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());
230 231 232
		mainthreadNotebook?.textModel.updateActiveCell(cellHandle);
	}

R
rebornix 已提交
233
	async createRawCell(uri: URI, index: number, language: string, type: CellKind): Promise<NotebookCellTextModel | undefined> {
234 235 236 237 238 239 240
		let cell = await this._proxy.$createEmptyCell(this._viewType, uri, index, language, type);
		if (cell) {
			let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs);
			return mainCell;
		}

		return undefined;
R
rebornix 已提交
241 242
	}

R
rebornix 已提交
243 244 245 246 247 248 249 250 251 252
	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
Rob Lourens 已提交
253
	async executeNotebookActiveCell(uri: URI): Promise<void> {
R
rebornix 已提交
254 255
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());

256
		if (mainthreadNotebook && mainthreadNotebook.textModel.activeCell) {
R
Rob Lourens 已提交
257
			return this._proxy.$executeNotebook(this._viewType, uri, mainthreadNotebook.textModel.activeCell.handle);
R
rebornix 已提交
258 259
		}
	}
R
rebornix 已提交
260

261
	async destoryNotebookDocument(notebook: INotebookTextModel): Promise<void> {
R
rebornix 已提交
262
		let document = this._mapping.get(URI.from(notebook.uri).toString());
R
rebornix 已提交
263

R
rebornix 已提交
264 265 266 267 268 269 270 271
		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 已提交
272 273
		}
	}
274 275 276 277

	async save(uri: URI): Promise<boolean> {
		return this._proxy.$saveNotebook(this._viewType, uri);
	}
R
rebornix 已提交
278
}