mainThreadNotebook.ts 10.5 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, NotebookDocumentMetadata } 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 133 134 135 136 137
	async $updateNotebookMetadata(viewType: string, resource: UriComponents, metadata: NotebookDocumentMetadata | undefined): Promise<void> {
		let controller = this._notebookProviders.get(viewType);

		if (controller) {
			controller.updateNotebookMetadata(resource, metadata);
		}
	}

R
rebornix 已提交
138 139 140
	async resolveNotebook(viewType: string, uri: URI): Promise<number | undefined> {
		let handle = await this._proxy.$resolveNotebook(viewType, uri);
		return handle;
R
rebornix 已提交
141 142
	}

143
	async $spliceNotebookCells(viewType: string, resource: UriComponents, splices: NotebookCellsSplice[], renderers: number[]): Promise<void> {
144
		let controller = this._notebookProviders.get(viewType);
145
		controller?.spliceNotebookCells(resource, splices, renderers);
146 147
	}

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

R
rebornix 已提交
153 154
	async executeNotebook(viewType: string, uri: URI): Promise<void> {
		return this._proxy.$executeNotebook(viewType, uri, undefined);
R
rebornix 已提交
155
	}
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

	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 已提交
171 172 173
}

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

	constructor(
R
rebornix 已提交
177 178 179
		private readonly _proxy: ExtHostNotebookShape,
		private _mainThreadNotebook: MainThreadNotebooks,
		private _viewType: string
R
rebornix 已提交
180 181 182
	) {
	}

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

		if (mainthreadNotebook) {
188
			return mainthreadNotebook.textModel;
R
rebornix 已提交
189 190 191 192 193
		}

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

R
rebornix 已提交
197 198 199
		return undefined;
	}

200
	spliceNotebookCells(resource: UriComponents, splices: NotebookCellsSplice[], renderers: number[]): void {
201
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
202 203
		mainthreadNotebook?.textModel.updateRenderers(renderers);
		mainthreadNotebook?.textModel.$spliceNotebookCells(splices);
204 205
	}

206
	spliceNotebookCellOutputs(resource: UriComponents, cellHandle: number, splices: NotebookCellOutputsSplice[], renderers: number[]): void {
207
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
208 209
		mainthreadNotebook?.textModel.updateRenderers(renderers);
		mainthreadNotebook?.textModel.$spliceNotebookCellOutputs(cellHandle, splices);
210 211
	}

R
rebornix 已提交
212 213 214 215
	async executeNotebook(viewType: string, uri: URI): Promise<void> {
		this._mainThreadNotebook.executeNotebook(viewType, uri);
	}

216 217 218 219
	onDidReceiveMessage(uri: UriComponents, message: any): void {
		this._proxy.$onDidReceiveMessage(uri, message);
	}

R
rebornix 已提交
220 221 222 223 224 225 226 227
	// 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());
228
		document?.textModel.updateLanguages(languages);
R
rebornix 已提交
229 230
	}

R
rebornix 已提交
231 232 233 234 235
	updateNotebookMetadata(resource: UriComponents, metadata: NotebookDocumentMetadata | undefined) {
		let document = this._mapping.get(URI.from(resource).toString());
		document?.textModel.updateNotebookMetadata(metadata);
	}

236 237
	updateNotebookRenderers(resource: UriComponents, renderers: number[]): void {
		let document = this._mapping.get(URI.from(resource).toString());
238
		document?.textModel.updateRenderers(renderers);
239 240
	}

R
rebornix 已提交
241
	async createRawCell(uri: URI, index: number, language: string, type: CellKind): Promise<NotebookCellTextModel | undefined> {
242 243
		let cell = await this._proxy.$createEmptyCell(this._viewType, uri, index, language, type);
		if (cell) {
244
			let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs, cell.metadata);
245 246 247 248
			return mainCell;
		}

		return undefined;
R
rebornix 已提交
249 250
	}

R
rebornix 已提交
251 252 253 254 255 256 257 258 259 260
	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;
	}

261 262 263 264
	async executeNotebookCell(uri: URI, handle: number): Promise<void> {
		return this._proxy.$executeNotebook(this._viewType, uri, handle);
	}

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

R
rebornix 已提交
268 269 270 271 272 273 274 275
		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 已提交
276 277
		}
	}
278 279 280 281

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