editorGroup.ts 18.3 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

M
Matt Bierner 已提交
6
import { Event, Emitter, once } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
7
import { Extensions, IEditorInputFactoryRegistry, EditorInput, toResource, IEditorIdentifier, IEditorCloseEvent, GroupIdentifier, SideBySideEditorInput, CloseDirection } from 'vs/workbench/common/editor';
8
import { URI } from 'vs/base/common/uri';
J
Johannes Rieken 已提交
9
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
10
import { IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration';
11
import { dispose, IDisposable, Disposable } from 'vs/base/common/lifecycle';
12
import { Registry } from 'vs/platform/registry/common/platform';
B
wip  
Benjamin Pasero 已提交
13
import { ResourceMap } from 'vs/base/common/map';
M
Matt Bierner 已提交
14
import { coalesce } from 'vs/base/common/arrays';
B
Benjamin Pasero 已提交
15

B
Benjamin Pasero 已提交
16 17 18 19 20 21 22
const EditorOpenPositioning = {
	LEFT: 'left',
	RIGHT: 'right',
	FIRST: 'first',
	LAST: 'last'
};

B
Benjamin Pasero 已提交
23
export interface EditorCloseEvent extends IEditorCloseEvent {
24 25 26
	editor: EditorInput;
}

27
export interface EditorIdentifier extends IEditorIdentifier {
I
isidor 已提交
28
	groupId: GroupIdentifier;
29
	editor: EditorInput;
30 31
}

B
Benjamin Pasero 已提交
32 33 34
export interface IEditorOpenOptions {
	pinned?: boolean;
	active?: boolean;
B
Benjamin Pasero 已提交
35
	index?: number;
B
Benjamin Pasero 已提交
36 37
}

38
export interface ISerializedEditorInput {
39 40 41 42
	id: string;
	value: string;
}

43
export interface ISerializedEditorGroup {
44
	id: number;
45 46 47 48 49
	editors: ISerializedEditorInput[];
	mru: number[];
	preview: number;
}

50 51 52 53 54 55
export function isSerializedEditorGroup(obj?: any): obj is ISerializedEditorGroup {
	const group = obj as ISerializedEditorGroup;

	return obj && typeof obj === 'object' && Array.isArray(group.editors) && Array.isArray(group.mru);
}

B
Benjamin Pasero 已提交
56
export class EditorGroup extends Disposable {
57 58 59

	private static IDS = 0;

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	//#region events

	private readonly _onDidEditorActivate = this._register(new Emitter<EditorInput>());
	get onDidEditorActivate(): Event<EditorInput> { return this._onDidEditorActivate.event; }

	private readonly _onDidEditorOpen = this._register(new Emitter<EditorInput>());
	get onDidEditorOpen(): Event<EditorInput> { return this._onDidEditorOpen.event; }

	private readonly _onDidEditorClose = this._register(new Emitter<EditorCloseEvent>());
	get onDidEditorClose(): Event<EditorCloseEvent> { return this._onDidEditorClose.event; }

	private readonly _onDidEditorDispose = this._register(new Emitter<EditorInput>());
	get onDidEditorDispose(): Event<EditorInput> { return this._onDidEditorDispose.event; }

	private readonly _onDidEditorBecomeDirty = this._register(new Emitter<EditorInput>());
	get onDidEditorBecomeDirty(): Event<EditorInput> { return this._onDidEditorBecomeDirty.event; }

	private readonly _onDidEditorLabelChange = this._register(new Emitter<EditorInput>());
	get onDidEditorLabelChange(): Event<EditorInput> { return this._onDidEditorLabelChange.event; }

	private readonly _onDidEditorMove = this._register(new Emitter<EditorInput>());
	get onDidEditorMove(): Event<EditorInput> { return this._onDidEditorMove.event; }

	private readonly _onDidEditorPin = this._register(new Emitter<EditorInput>());
	get onDidEditorPin(): Event<EditorInput> { return this._onDidEditorPin.event; }

	private readonly _onDidEditorUnpin = this._register(new Emitter<EditorInput>());
	get onDidEditorUnpin(): Event<EditorInput> { return this._onDidEditorUnpin.event; }

	//#endregion
B
Benjamin Pasero 已提交
90

91 92
	private _id: GroupIdentifier;

93 94 95
	private editors: EditorInput[] = [];
	private mru: EditorInput[] = [];
	private mapResourceToEditorCount: ResourceMap<number> = new ResourceMap<number>();
96 97 98 99 100 101

	private preview: EditorInput; // editor in preview state
	private active: EditorInput;  // editor in active state

	private editorOpenPositioning: 'left' | 'right' | 'first' | 'last';

102
	constructor(
B
Benjamin Pasero 已提交
103
		labelOrSerializedGroup: ISerializedEditorGroup,
104 105
		@IInstantiationService private instantiationService: IInstantiationService,
		@IConfigurationService private configurationService: IConfigurationService
106
	) {
107 108
		super();

109 110
		if (isSerializedEditorGroup(labelOrSerializedGroup)) {
			this.deserialize(labelOrSerializedGroup);
111
		} else {
112
			this._id = EditorGroup.IDS++;
113
		}
114

115
		this.onConfigurationUpdated();
116 117 118 119
		this.registerListeners();
	}

	private registerListeners(): void {
120
		this._register(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationUpdated(e)));
121 122
	}

123
	private onConfigurationUpdated(event?: IConfigurationChangeEvent): void {
124
		this.editorOpenPositioning = this.configurationService.getValue('workbench.editor.openPositioning');
B
Benjamin Pasero 已提交
125 126
	}

127
	get id(): GroupIdentifier {
128 129 130
		return this._id;
	}

131
	get count(): number {
132 133 134
		return this.editors.length;
	}

135
	getEditors(mru?: boolean): EditorInput[] {
B
Benjamin Pasero 已提交
136
		return mru ? this.mru.slice(0) : this.editors.slice(0);
B
Benjamin Pasero 已提交
137 138
	}

139 140 141
	getEditor(index: number): EditorInput;
	getEditor(resource: URI): EditorInput;
	getEditor(arg1: any): EditorInput {
B
Benjamin Pasero 已提交
142 143 144 145 146 147 148 149 150 151 152
		if (typeof arg1 === 'number') {
			return this.editors[arg1];
		}

		const resource: URI = arg1;
		if (!this.contains(resource)) {
			return null; // fast check for resource opened or not
		}

		for (let i = 0; i < this.editors.length; i++) {
			const editor = this.editors[i];
153
			const editorResource = toResource(editor, { supportSideBySide: true });
B
Benjamin Pasero 已提交
154
			if (editorResource && editorResource.toString() === resource.toString()) {
B
Benjamin Pasero 已提交
155
				return editor;
B
Benjamin Pasero 已提交
156 157 158 159
			}
		}

		return null;
160 161
	}

162
	get activeEditor(): EditorInput {
B
Benjamin Pasero 已提交
163 164 165
		return this.active;
	}

166
	isActive(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
167
		return this.matches(this.active, editor);
B
Benjamin Pasero 已提交
168 169
	}

170
	get previewEditor(): EditorInput {
B
Benjamin Pasero 已提交
171 172 173
		return this.preview;
	}

174
	isPreview(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
175
		return this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
176 177
	}

178
	openEditor(editor: EditorInput, options?: IEditorOpenOptions): void {
B
Benjamin Pasero 已提交
179 180 181
		const index = this.indexOf(editor);

		const makePinned = options && options.pinned;
B
Benjamin Pasero 已提交
182
		const makeActive = (options && options.active) || !this.activeEditor || (!makePinned && this.matches(this.preview, this.activeEditor));
B
Benjamin Pasero 已提交
183 184 185

		// New editor
		if (index === -1) {
186 187
			let targetIndex: number;
			const indexOfActive = this.indexOf(this.active);
B
Benjamin Pasero 已提交
188

189 190 191 192
			// Insert into specific position
			if (options && typeof options.index === 'number') {
				targetIndex = options.index;
			}
B
Benjamin Pasero 已提交
193

194
			// Insert to the BEGINNING
195
			else if (this.editorOpenPositioning === EditorOpenPositioning.FIRST) {
196 197 198 199
				targetIndex = 0;
			}

			// Insert to the END
200
			else if (this.editorOpenPositioning === EditorOpenPositioning.LAST) {
201
				targetIndex = this.editors.length;
202
			}
B
Benjamin Pasero 已提交
203

204
			// Insert to the LEFT of active editor
205
			else if (this.editorOpenPositioning === EditorOpenPositioning.LEFT) {
206 207 208
				if (indexOfActive === 0 || !this.editors.length) {
					targetIndex = 0; // to the left becoming first editor in list
				} else {
209
					targetIndex = indexOfActive; // to the left of active editor
B
Benjamin Pasero 已提交
210
				}
211
			}
B
Benjamin Pasero 已提交
212

213 214 215 216 217
			// Insert to the RIGHT of active editor
			else {
				targetIndex = indexOfActive + 1;
			}

218 219 220
			// Insert into our list of editors if pinned or we have no preview editor
			if (makePinned || !this.preview) {
				this.splice(targetIndex, false, editor);
B
Benjamin Pasero 已提交
221 222
			}

223 224
			// Handle preview
			if (!makePinned) {
B
Benjamin Pasero 已提交
225 226

				// Replace existing preview with this editor if we have a preview
227 228
				if (this.preview) {
					const indexOfPreview = this.indexOf(this.preview);
229 230
					if (targetIndex > indexOfPreview) {
						targetIndex--; // accomodate for the fact that the preview editor closes
231 232
					}

233
					this.replaceEditor(this.preview, editor, targetIndex, !makeActive);
234
				}
B
Benjamin Pasero 已提交
235

B
Benjamin Pasero 已提交
236 237 238
				this.preview = editor;
			}

239
			// Listeners
B
Benjamin Pasero 已提交
240
			this.registerEditorListeners(editor);
241

B
Benjamin Pasero 已提交
242
			// Event
243
			this._onDidEditorOpen.fire(editor);
B
Benjamin Pasero 已提交
244

245
			// Handle active
B
Benjamin Pasero 已提交
246
			if (makeActive) {
B
Benjamin Pasero 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
				this.setActive(editor);
			}
		}

		// Existing editor
		else {

			// Pin it
			if (makePinned) {
				this.pin(editor);
			}

			// Activate it
			if (makeActive) {
				this.setActive(editor);
			}
B
Benjamin Pasero 已提交
263 264 265 266 267

			// Respect index
			if (options && typeof options.index === 'number') {
				this.moveEditor(editor, options.index);
			}
B
Benjamin Pasero 已提交
268 269 270
		}
	}

B
Benjamin Pasero 已提交
271
	private registerEditorListeners(editor: EditorInput): void {
272
		const unbind: IDisposable[] = [];
273 274

		// Re-emit disposal of editor input as our own event
275 276
		const onceDispose = once(editor.onDispose);
		unbind.push(onceDispose(() => {
277
			if (this.indexOf(editor) >= 0) {
278
				this._onDidEditorDispose.fire(editor);
279
			}
280 281 282 283
		}));

		// Re-Emit dirty state changes
		unbind.push(editor.onDidChangeDirty(() => {
284
			this._onDidEditorBecomeDirty.fire(editor);
285
		}));
286

B
Benjamin Pasero 已提交
287 288
		// Re-Emit label changes
		unbind.push(editor.onDidChangeLabel(() => {
289
			this._onDidEditorLabelChange.fire(editor);
B
Benjamin Pasero 已提交
290 291
		}));

292
		// Clean up dispose listeners once the editor gets closed
293
		unbind.push(this.onDidEditorClose(event => {
294
			if (event.editor.matches(editor)) {
295
				dispose(unbind);
296
			}
297
		}));
298 299
	}

300 301
	private replaceEditor(toReplace: EditorInput, replaceWidth: EditorInput, replaceIndex: number, openNext = true): void {
		const event = this.doCloseEditor(toReplace, openNext, true); // optimization to prevent multiple setActive() in one call
302 303 304 305 306 307 308

		// We want to first add the new editor into our model before emitting the close event because
		// firing the close event can trigger a dispose on the same editor that is now being added.
		// This can lead into opening a disposed editor which is not what we want.
		this.splice(replaceIndex, false, replaceWidth);

		if (event) {
309
			this._onDidEditorClose.fire(event);
310 311 312
		}
	}

313
	closeEditor(editor: EditorInput, openNext = true): number {
314
		const event = this.doCloseEditor(editor, openNext, false);
315 316

		if (event) {
317
			this._onDidEditorClose.fire(event);
318 319

			return event.index;
320
		}
321 322

		return void 0;
323 324
	}

325
	private doCloseEditor(editor: EditorInput, openNext: boolean, replaced: boolean): EditorCloseEvent {
B
Benjamin Pasero 已提交
326 327
		const index = this.indexOf(editor);
		if (index === -1) {
328
			return null; // not found
B
Benjamin Pasero 已提交
329 330 331
		}

		// Active Editor closed
B
Benjamin Pasero 已提交
332
		if (openNext && this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
333 334

			// More than one editor
B
Benjamin Pasero 已提交
335 336
			if (this.mru.length > 1) {
				this.setActive(this.mru[1]); // active editor is always first in MRU, so pick second editor after as new active
B
Benjamin Pasero 已提交
337 338 339 340 341 342 343 344 345
			}

			// One Editor
			else {
				this.active = null;
			}
		}

		// Preview Editor closed
B
Benjamin Pasero 已提交
346
		if (this.matches(this.preview, editor)) {
B
Benjamin Pasero 已提交
347 348 349
			this.preview = null;
		}

B
Benjamin Pasero 已提交
350
		// Remove from arrays
B
Benjamin Pasero 已提交
351
		this.splice(index, true);
B
Benjamin Pasero 已提交
352 353

		// Event
I
isidor 已提交
354
		return { editor, replaced, index, groupId: this.id };
B
Benjamin Pasero 已提交
355 356
	}

B
Benjamin Pasero 已提交
357
	closeEditors(except: EditorInput, direction?: CloseDirection): void {
B
Benjamin Pasero 已提交
358 359 360 361 362 363
		const index = this.indexOf(except);
		if (index === -1) {
			return; // not found
		}

		// Close to the left
B
Benjamin Pasero 已提交
364
		if (direction === CloseDirection.LEFT) {
B
Benjamin Pasero 已提交
365 366 367 368 369 370
			for (let i = index - 1; i >= 0; i--) {
				this.closeEditor(this.editors[i]);
			}
		}

		// Close to the right
B
Benjamin Pasero 已提交
371
		else if (direction === CloseDirection.RIGHT) {
B
Benjamin Pasero 已提交
372 373 374 375 376 377 378
			for (let i = this.editors.length - 1; i > index; i--) {
				this.closeEditor(this.editors[i]);
			}
		}

		// Both directions
		else {
B
Benjamin Pasero 已提交
379
			this.mru.filter(e => !this.matches(e, except)).forEach(e => this.closeEditor(e));
B
Benjamin Pasero 已提交
380 381 382
		}
	}

383
	closeAllEditors(): void {
384 385

		// Optimize: close all non active editors first to produce less upstream work
B
Benjamin Pasero 已提交
386
		this.mru.filter(e => !this.matches(e, this.active)).forEach(e => this.closeEditor(e));
387 388 389
		this.closeEditor(this.active);
	}

390
	moveEditor(editor: EditorInput, toIndex: number): void {
B
Benjamin Pasero 已提交
391 392 393 394 395 396 397 398 399 400
		const index = this.indexOf(editor);
		if (index < 0) {
			return;
		}

		// Move
		this.editors.splice(index, 1);
		this.editors.splice(toIndex, 0, editor);

		// Event
401
		this._onDidEditorMove.fire(editor);
B
Benjamin Pasero 已提交
402 403
	}

404
	setActive(editor: EditorInput): void {
B
Benjamin Pasero 已提交
405 406 407 408 409
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}

B
Benjamin Pasero 已提交
410
		if (this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
411 412 413 414 415
			return; // already active
		}

		this.active = editor;

B
Benjamin Pasero 已提交
416
		// Bring to front in MRU list
B
Benjamin Pasero 已提交
417 418
		this.setMostRecentlyUsed(editor);

B
Benjamin Pasero 已提交
419
		// Event
420
		this._onDidEditorActivate.fire(editor);
B
Benjamin Pasero 已提交
421 422
	}

423
	pin(editor: EditorInput): void {
424 425 426 427 428
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}

B
Benjamin Pasero 已提交
429 430
		if (!this.isPreview(editor)) {
			return; // can only pin a preview editor
B
Benjamin Pasero 已提交
431 432 433 434 435 436
		}

		// Convert the preview editor to be a pinned editor
		this.preview = null;

		// Event
437
		this._onDidEditorPin.fire(editor);
B
Benjamin Pasero 已提交
438 439
	}

440
	unpin(editor: EditorInput): void {
441 442 443 444
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}
445

B
Benjamin Pasero 已提交
446 447 448 449 450 451 452 453 454
		if (!this.isPinned(editor)) {
			return; // can only unpin a pinned editor
		}

		// Set new
		const oldPreview = this.preview;
		this.preview = editor;

		// Event
455
		this._onDidEditorUnpin.fire(editor);
B
Benjamin Pasero 已提交
456 457 458 459 460

		// Close old preview editor if any
		this.closeEditor(oldPreview);
	}

461 462 463
	isPinned(editor: EditorInput): boolean;
	isPinned(index: number): boolean;
	isPinned(arg1: EditorInput | number): boolean {
464 465 466 467 468 469 470 471 472 473 474
		let editor: EditorInput;
		let index: number;
		if (typeof arg1 === 'number') {
			editor = this.editors[arg1];
			index = arg1;
		} else {
			editor = arg1;
			index = this.indexOf(editor);
		}

		if (index === -1 || !editor) {
B
Benjamin Pasero 已提交
475 476 477 478 479 480 481
			return false; // editor not found
		}

		if (!this.preview) {
			return true; // no preview editor
		}

B
Benjamin Pasero 已提交
482
		return !this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
483 484
	}

B
Benjamin Pasero 已提交
485
	private splice(index: number, del: boolean, editor?: EditorInput): void {
486
		const editorToDeleteOrReplace = this.editors[index];
B
Benjamin Pasero 已提交
487

B
Benjamin Pasero 已提交
488
		const args: any[] = [index, del ? 1 : 0];
489 490 491
		if (editor) {
			args.push(editor);
		}
B
Benjamin Pasero 已提交
492

493
		// Perform on editors array
494
		this.editors.splice.apply(this.editors, args);
B
Benjamin Pasero 已提交
495

496
		// Add
B
Benjamin Pasero 已提交
497
		if (!del && editor) {
498 499
			this.mru.push(editor); // make it LRU editor
			this.updateResourceMap(editor, false /* add */); // add new to resource map
B
Benjamin Pasero 已提交
500 501
		}

B
Benjamin Pasero 已提交
502
		// Remove / Replace
B
Benjamin Pasero 已提交
503
		else {
B
Benjamin Pasero 已提交
504 505
			const indexInMRU = this.indexOf(editorToDeleteOrReplace, this.mru);

506
			// Remove
B
Benjamin Pasero 已提交
507
			if (del && !editor) {
508 509
				this.mru.splice(indexInMRU, 1); // remove from MRU
				this.updateResourceMap(editorToDeleteOrReplace, true /* delete */); // remove from resource map
B
Benjamin Pasero 已提交
510 511
			}

512
			// Replace
B
Benjamin Pasero 已提交
513
			else {
514 515 516
				this.mru.splice(indexInMRU, 1, editor); // replace MRU at location
				this.updateResourceMap(editor, false /* add */); // add new to resource map
				this.updateResourceMap(editorToDeleteOrReplace, true /* delete */); // remove replaced from resource map
B
Benjamin Pasero 已提交
517
			}
B
Benjamin Pasero 已提交
518 519 520
		}
	}

521
	private updateResourceMap(editor: EditorInput, remove: boolean): void {
522
		const resource = toResource(editor, { supportSideBySide: true });
523
		if (resource) {
524 525 526

			// It is possible to have the same resource opened twice (once as normal input and once as diff input)
			// So we need to do ref counting on the resource to provide the correct picture
B
wip  
Benjamin Pasero 已提交
527
			let counter = this.mapResourceToEditorCount.get(resource) || 0;
B
Benjamin Pasero 已提交
528
			let newCounter: number;
529 530 531 532 533 534 535 536
			if (remove) {
				if (counter > 1) {
					newCounter = counter - 1;
				}
			} else {
				newCounter = counter + 1;
			}

B
wip  
Benjamin Pasero 已提交
537
			this.mapResourceToEditorCount.set(resource, newCounter);
538 539 540
		}
	}

541
	indexOf(candidate: EditorInput, editors = this.editors): number {
B
Benjamin Pasero 已提交
542 543
		if (!candidate) {
			return -1;
B
Benjamin Pasero 已提交
544 545
		}

B
Benjamin Pasero 已提交
546
		for (let i = 0; i < editors.length; i++) {
B
Benjamin Pasero 已提交
547
			if (this.matches(editors[i], candidate)) {
B
Benjamin Pasero 已提交
548 549 550 551 552 553
				return i;
			}
		}

		return -1;
	}
B
Benjamin Pasero 已提交
554

555 556 557
	contains(editorOrResource: EditorInput | URI): boolean;
	contains(editor: EditorInput, supportSideBySide?: boolean): boolean;
	contains(editorOrResource: EditorInput | URI, supportSideBySide?: boolean): boolean {
558
		if (editorOrResource instanceof EditorInput) {
559 560 561 562 563 564 565 566 567 568 569 570 571
			const index = this.indexOf(editorOrResource);
			if (index >= 0) {
				return true;
			}

			if (supportSideBySide && editorOrResource instanceof SideBySideEditorInput) {
				const index = this.indexOf(editorOrResource.master);
				if (index >= 0) {
					return true;
				}
			}

			return false;
572 573
		}

574
		const counter = this.mapResourceToEditorCount.get(editorOrResource);
575 576

		return typeof counter === 'number' && counter > 0;
577 578
	}

B
Benjamin Pasero 已提交
579 580 581 582 583 584 585 586 587
	private setMostRecentlyUsed(editor: EditorInput): void {
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // editor not found
		}

		const mruIndex = this.indexOf(editor, this.mru);

		// Remove old index
B
Benjamin Pasero 已提交
588
		this.mru.splice(mruIndex, 1);
B
Benjamin Pasero 已提交
589

B
Benjamin Pasero 已提交
590
		// Set editor to front
B
Benjamin Pasero 已提交
591 592
		this.mru.unshift(editor);
	}
B
Benjamin Pasero 已提交
593 594 595 596

	private matches(editorA: EditorInput, editorB: EditorInput): boolean {
		return !!editorA && !!editorB && editorA.matches(editorB);
	}
597

598
	clone(): EditorGroup {
B
Benjamin Pasero 已提交
599
		const group = this.instantiationService.createInstance(EditorGroup, void 0);
600 601 602 603 604 605 606 607 608 609 610
		group.editors = this.editors.slice(0);
		group.mru = this.mru.slice(0);
		group.mapResourceToEditorCount = this.mapResourceToEditorCount.clone();
		group.preview = this.preview;
		group.active = this.active;
		group.editorOpenPositioning = this.editorOpenPositioning;

		return group;
	}

	serialize(): ISerializedEditorGroup {
611
		const registry = Registry.as<IEditorInputFactoryRegistry>(Extensions.EditorInputFactories);
612 613 614 615 616 617

		// Serialize all editor inputs so that we can store them.
		// Editors that cannot be serialized need to be ignored
		// from mru, active and preview if any.
		let serializableEditors: EditorInput[] = [];
		let serializedEditors: ISerializedEditorInput[] = [];
B
Benjamin Pasero 已提交
618
		let serializablePreviewIndex: number;
619
		this.editors.forEach(e => {
620
			let factory = registry.getEditorInputFactory(e.getTypeId());
621 622 623
			if (factory) {
				let value = factory.serialize(e);
				if (typeof value === 'string') {
624
					serializedEditors.push({ id: e.getTypeId(), value });
625
					serializableEditors.push(e);
B
Benjamin Pasero 已提交
626 627 628 629

					if (this.preview === e) {
						serializablePreviewIndex = serializableEditors.length - 1;
					}
630 631 632 633
				}
			}
		});

B
Benjamin Pasero 已提交
634
		const serializableMru = this.mru.map(e => this.indexOf(e, serializableEditors)).filter(i => i >= 0);
635 636

		return {
637
			id: this.id,
638 639
			editors: serializedEditors,
			mru: serializableMru,
B
Benjamin Pasero 已提交
640
			preview: serializablePreviewIndex,
641 642 643 644
		};
	}

	private deserialize(data: ISerializedEditorGroup): void {
645
		const registry = Registry.as<IEditorInputFactoryRegistry>(Extensions.EditorInputFactories);
646

647 648 649 650 651 652 653 654
		if (typeof data.id === 'number') {
			this._id = data.id;

			EditorGroup.IDS = Math.max(data.id + 1, EditorGroup.IDS); // make sure our ID generator is always larger
		} else {
			this._id = EditorGroup.IDS++; // backwards compatibility
		}

M
Matt Bierner 已提交
655
		this.editors = coalesce(data.editors.map(e => {
B
Benjamin Pasero 已提交
656 657 658
			const factory = registry.getEditorInputFactory(e.id);
			if (factory) {
				const editor = factory.deserialize(this.instantiationService, e.value);
659

B
Benjamin Pasero 已提交
660
				this.registerEditorListeners(editor);
B
Benjamin Pasero 已提交
661
				this.updateResourceMap(editor, false /* add */);
662

B
Benjamin Pasero 已提交
663 664 665 666
				return editor;
			}

			return null;
M
Matt Bierner 已提交
667
		}));
668 669 670 671 672
		this.mru = data.mru.map(i => this.editors[i]);
		this.active = this.mru[0];
		this.preview = this.editors[data.preview];
	}
}