mainThreadNotebook.ts 13.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';
10
import { INotebookService, IMainNotebookController } from 'vs/workbench/contrib/notebook/common/notebookService';
R
rebornix 已提交
11
import { INotebookTextModel, INotebookMimeTypeSelector, NOTEBOOK_DISPLAY_ORDER, NotebookCellOutputsSplice, NotebookDocumentMetadata, NotebookCellMetadata, ICellEditOperation, ACCESSIBLE_NOTEBOOK_DISPLAY_ORDER, CellEditType, CellKind, INotebookKernelInfo } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
12
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
13
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
14 15
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
16
import { CancellationToken } from 'vs/base/common/cancellation';
R
rebornix 已提交
17
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
R
rebornix 已提交
18 19
import { IRelativePattern } from 'vs/base/common/glob';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
R
rebornix 已提交
20

21 22
export class MainThreadNotebookDocument extends Disposable {
	private _textModel: NotebookTextModel;
R
rebornix 已提交
23

24 25
	get textModel() {
		return this._textModel;
R
rebornix 已提交
26
	}
R
rebornix 已提交
27 28

	constructor(
R
rebornix 已提交
29
		private readonly _proxy: ExtHostNotebookShape,
R
rebornix 已提交
30
		public handle: number,
R
rebornix 已提交
31
		public viewType: string,
R
rebornix 已提交
32
		public uri: URI
R
rebornix 已提交
33
	) {
R
rebornix 已提交
34
		super();
35
		this._textModel = new NotebookTextModel(handle, viewType, uri);
R
rebornix 已提交
36 37 38
		this._register(this._textModel.onDidModelChange(e => {
			this._proxy.$acceptModelChanged(this.uri, e);
		}));
R
rebornix 已提交
39 40 41 42
		this._register(this._textModel.onDidSelectionChange(e => {
			const selectionsChange = e ? { selections: e } : null;
			this._proxy.$acceptEditorPropertiesChanged(uri, { selections: selectionsChange });
		}));
R
rebornix 已提交
43
	}
R
rebornix 已提交
44

R
rebornix 已提交
45 46 47
	applyEdit(modelVersionId: number, edits: ICellEditOperation[]): boolean {
		return this._textModel.applyEdit(modelVersionId, edits);
	}
R
rebornix 已提交
48

R
rebornix 已提交
49 50
	updateRenderers(renderers: number[]) {
		this._textModel.updateRenderers(renderers);
R
rebornix 已提交
51 52
	}

R
rebornix 已提交
53
	dispose() {
54
		this._textModel.dispose();
R
rebornix 已提交
55 56
		super.dispose();
	}
R
rebornix 已提交
57 58 59 60 61
}

@extHostNamedCustomer(MainContext.MainThreadNotebook)
export class MainThreadNotebooks extends Disposable implements MainThreadNotebookShape {
	private readonly _notebookProviders = new Map<string, MainThreadNotebookController>();
R
rebornix 已提交
62
	private readonly _notebookKernels = new Map<string, MainThreadNotebookKernel>();
R
rebornix 已提交
63 64 65 66
	private readonly _proxy: ExtHostNotebookShape;

	constructor(
		extHostContext: IExtHostContext,
R
rebornix 已提交
67
		@INotebookService private _notebookService: INotebookService,
68 69
		@IConfigurationService private readonly configurationService: IConfigurationService,
		@IEditorService private readonly editorService: IEditorService,
R
rebornix 已提交
70
		@IAccessibilityService private readonly accessibilityService: IAccessibilityService
71

R
rebornix 已提交
72 73 74
	) {
		super();
		this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostNotebook);
R
rebornix 已提交
75 76 77
		this.registerListeners();
	}

R
rebornix 已提交
78 79 80 81 82 83 84 85
	async $tryApplyEdits(viewType: string, resource: UriComponents, modelVersionId: number, edits: ICellEditOperation[], renderers: number[]): Promise<boolean> {
		let controller = this._notebookProviders.get(viewType);

		if (controller) {
			return controller.tryApplyEdits(resource, modelVersionId, edits, renderers);
		}

		return false;
R
rebornix 已提交
86 87
	}

R
rebornix 已提交
88
	registerListeners() {
R
rebornix 已提交
89
		this._register(this._notebookService.onDidChangeActiveEditor(e => {
R
rebornix 已提交
90 91 92
			this._proxy.$acceptDocumentAndEditorsDelta({
				newActiveEditor: e.uri
			});
R
rebornix 已提交
93
		}));
R
rebornix 已提交
94

R
rebornix 已提交
95 96 97 98 99 100 101
		const updateOrder = () => {
			let userOrder = this.configurationService.getValue<string[]>('notebook.displayOrder');
			this._proxy.$acceptDisplayOrder({
				defaultOrder: this.accessibilityService.isScreenReaderOptimized() ? ACCESSIBLE_NOTEBOOK_DISPLAY_ORDER : NOTEBOOK_DISPLAY_ORDER,
				userOrder: userOrder
			});
		};
R
rebornix 已提交
102

R
rebornix 已提交
103
		updateOrder();
R
rebornix 已提交
104

R
rebornix 已提交
105 106 107
		this._register(this.configurationService.onDidChangeConfiguration(e => {
			if (e.affectedKeys.indexOf('notebook.displayOrder') >= 0) {
				updateOrder();
R
rebornix 已提交
108
			}
R
rebornix 已提交
109 110 111 112 113
		}));

		this._register(this.accessibilityService.onDidChangeScreenReaderOptimized(() => {
			updateOrder();
		}));
R
rebornix 已提交
114 115
	}

R
rebornix 已提交
116 117
	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 已提交
118 119
	}

120 121
	async $unregisterNotebookRenderer(handle: number): Promise<void> {
		this._notebookService.unregisterNotebookRenderer(handle);
R
rebornix 已提交
122 123
	}

R
rebornix 已提交
124 125
	async $registerNotebookProvider(extension: NotebookExtensionDescription, viewType: string): Promise<void> {
		let controller = new MainThreadNotebookController(this._proxy, this, viewType);
R
rebornix 已提交
126
		this._notebookProviders.set(viewType, controller);
R
rebornix 已提交
127
		this._notebookService.registerNotebookController(viewType, extension, controller);
R
rebornix 已提交
128
		return;
R
rebornix 已提交
129 130
	}

R
rebornix 已提交
131
	async $unregisterNotebookProvider(viewType: string): Promise<void> {
R
rebornix 已提交
132 133
		this._notebookProviders.delete(viewType);
		this._notebookService.unregisterNotebookProvider(viewType);
R
rebornix 已提交
134 135 136
		return;
	}

R
rebornix 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
	async $registerNotebookKernel(extension: NotebookExtensionDescription, id: string, selectors: (string | IRelativePattern)[], preloads: UriComponents[]): Promise<void> {
		const kernel = new MainThreadNotebookKernel(this._proxy, id, selectors, extension.id, URI.revive(extension.location), preloads.map(preload => URI.revive(preload)));
		this._notebookKernels.set(id, kernel);
		this._notebookService.registerNotebookKernel(kernel);
		return;
	}

	async $unregisterNotebookKernel(id: string): Promise<void> {
		this._notebookKernels.delete(id);
		this._notebookService.unregisterNotebookKernel(id);
		return;
	}

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

		if (controller) {
R
rebornix 已提交
154
			controller.updateLanguages(resource, languages);
R
rebornix 已提交
155 156
		}
	}
R
rebornix 已提交
157

R
rebornix 已提交
158
	async $updateNotebookMetadata(viewType: string, resource: UriComponents, metadata: NotebookDocumentMetadata): Promise<void> {
R
rebornix 已提交
159 160 161 162 163 164 165
		let controller = this._notebookProviders.get(viewType);

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

166 167 168 169 170 171 172 173
	async $updateNotebookCellMetadata(viewType: string, resource: UriComponents, handle: number, metadata: NotebookCellMetadata): Promise<void> {
		let controller = this._notebookProviders.get(viewType);

		if (controller) {
			controller.updateNotebookCellMetadata(resource, handle, metadata);
		}
	}

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

179 180
	async executeNotebook(viewType: string, uri: URI, token: CancellationToken): Promise<void> {
		return this._proxy.$executeNotebook(viewType, uri, undefined, token);
R
rebornix 已提交
181
	}
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

	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 已提交
197 198 199
}

export class MainThreadNotebookController implements IMainNotebookController {
R
rebornix 已提交
200
	private _mapping: Map<string, MainThreadNotebookDocument> = new Map();
R
rebornix 已提交
201
	static documentHandle: number = 0;
R
rebornix 已提交
202 203

	constructor(
R
rebornix 已提交
204 205
		private readonly _proxy: ExtHostNotebookShape,
		private _mainThreadNotebook: MainThreadNotebooks,
R
rebornix 已提交
206
		private _viewType: string
R
rebornix 已提交
207 208 209
	) {
	}

R
revert.  
rebornix 已提交
210
	async createNotebook(viewType: string, uri: URI, forBackup: boolean, forceReload: boolean): Promise<NotebookTextModel | undefined> {
R
rebornix 已提交
211 212 213
		let mainthreadNotebook = this._mapping.get(URI.from(uri).toString());

		if (mainthreadNotebook) {
R
revert.  
rebornix 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
			if (forceReload) {
				const data = await this._proxy.$resolveNotebookData(viewType, uri);
				if (!data) {
					return;
				}

				mainthreadNotebook.textModel.languages = data.languages;
				mainthreadNotebook.textModel.metadata = data.metadata;
				mainthreadNotebook.textModel.applyEdit(mainthreadNotebook.textModel.versionId, [
					{ editType: CellEditType.Delete, count: mainthreadNotebook.textModel.cells.length, index: 0 },
					{ editType: CellEditType.Insert, index: 0, cells: data.cells }
				]);
			}
R
rebornix 已提交
227 228 229 230
			return mainthreadNotebook.textModel;
		}

		let document = new MainThreadNotebookDocument(this._proxy, MainThreadNotebookController.documentHandle++, viewType, uri);
R
rebornix 已提交
231
		await this.createNotebookDocument(document);
R
rebornix 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244

		if (forBackup) {
			return document.textModel;
		}

		// open notebook document
		const data = await this._proxy.$resolveNotebookData(viewType, uri);
		if (!data) {
			return;
		}

		document.textModel.languages = data.languages;
		document.textModel.metadata = data.metadata;
245 246 247 248 249 250 251

		if (data.cells.length) {
			document.textModel.initialize(data!.cells);
		} else {
			const mainCell = document.textModel.createCellTextModel([''], document.textModel.languages.length ? document.textModel.languages[0] : '', CellKind.Code, [], undefined);
			document.textModel.insertTemplateCell(mainCell);
		}
R
rebornix 已提交
252 253 254 255

		return document.textModel;
	}

R
rebornix 已提交
256
	async tryApplyEdits(resource: UriComponents, modelVersionId: number, edits: ICellEditOperation[], renderers: number[]): Promise<boolean> {
257
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
R
rebornix 已提交
258 259 260 261 262 263 264

		if (mainthreadNotebook) {
			mainthreadNotebook.updateRenderers(renderers);
			return mainthreadNotebook.applyEdit(modelVersionId, edits);
		}

		return false;
265 266
	}

267
	spliceNotebookCellOutputs(resource: UriComponents, cellHandle: number, splices: NotebookCellOutputsSplice[], renderers: number[]): void {
268
		let mainthreadNotebook = this._mapping.get(URI.from(resource).toString());
269 270
		mainthreadNotebook?.textModel.updateRenderers(renderers);
		mainthreadNotebook?.textModel.$spliceNotebookCellOutputs(cellHandle, splices);
271 272
	}

273
	async executeNotebook(viewType: string, uri: URI, token: CancellationToken): Promise<void> {
274
		return this._mainThreadNotebook.executeNotebook(viewType, uri, token);
R
rebornix 已提交
275 276
	}

277 278 279 280
	onDidReceiveMessage(uri: UriComponents, message: any): void {
		this._proxy.$onDidReceiveMessage(uri, message);
	}

R
rebornix 已提交
281 282
	async createNotebookDocument(document: MainThreadNotebookDocument): Promise<void> {
		this._mapping.set(document.uri.toString(), document);
R
rebornix 已提交
283 284 285

		await this._proxy.$acceptDocumentAndEditorsDelta({
			addedDocuments: [{
R
rebornix 已提交
286
				viewType: document.viewType,
R
rebornix 已提交
287
				handle: document.handle,
R
rebornix 已提交
288
				uri: document.uri
R
rebornix 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
			}]
		});
	}

	async removeNotebookDocument(notebook: INotebookTextModel): Promise<void> {
		let document = this._mapping.get(URI.from(notebook.uri).toString());

		if (!document) {
			return;
		}

		await this._proxy.$acceptDocumentAndEditorsDelta({ removedDocuments: [notebook.uri] });
		document.dispose();
		this._mapping.delete(URI.from(notebook.uri).toString());
	}

	// Methods for ExtHost
R
rebornix 已提交
306 307 308

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

R
rebornix 已提交
312
	updateNotebookMetadata(resource: UriComponents, metadata: NotebookDocumentMetadata) {
R
rebornix 已提交
313 314 315 316
		let document = this._mapping.get(URI.from(resource).toString());
		document?.textModel.updateNotebookMetadata(metadata);
	}

317 318 319 320 321
	updateNotebookCellMetadata(resource: UriComponents, handle: number, metadata: NotebookCellMetadata) {
		let document = this._mapping.get(URI.from(resource).toString());
		document?.textModel.updateNotebookCellMetadata(handle, metadata);
	}

322 323
	updateNotebookRenderers(resource: UriComponents, renderers: number[]): void {
		let document = this._mapping.get(URI.from(resource).toString());
324
		document?.textModel.updateRenderers(renderers);
325 326
	}

327 328
	async executeNotebookCell(uri: URI, handle: number, token: CancellationToken): Promise<void> {
		return this._proxy.$executeNotebook(this._viewType, uri, handle, token);
329 330
	}

R
rebornix 已提交
331 332
	async save(uri: URI, token: CancellationToken): Promise<boolean> {
		return this._proxy.$saveNotebook(this._viewType, uri, token);
333
	}
R
saveAs  
rebornix 已提交
334 335 336 337 338

	async saveAs(uri: URI, target: URI, token: CancellationToken): Promise<boolean> {
		return this._proxy.$saveNotebookAs(this._viewType, uri, target, token);

	}
R
rebornix 已提交
339
}
R
rebornix 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

export class MainThreadNotebookKernel implements INotebookKernelInfo {
	constructor(
		private readonly _proxy: ExtHostNotebookShape,
		readonly id: string,
		readonly selectors: (string | IRelativePattern)[],
		readonly extension: ExtensionIdentifier,
		readonly extensionLocation: URI,
		readonly preloads: URI[]
	) {
	}

	async executeNotebook(viewType: string, uri: URI, handle: number | undefined, token: CancellationToken): Promise<void> {
		return this._proxy.$executeNotebook2(this.id, viewType, uri, handle, token);
	}
}