editorGroup.ts 17.8 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.
 *--------------------------------------------------------------------------------------------*/

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

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

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

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

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

36
export interface ISerializedEditorInput {
37 38 39 40
	id: string;
	value: string;
}

41
export interface ISerializedEditorGroup {
42
	id: number;
43 44
	editors: ISerializedEditorInput[];
	mru: number[];
M
Matt Bierner 已提交
45
	preview?: number;
46 47
}

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

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

B
Benjamin Pasero 已提交
54
export class EditorGroup extends Disposable {
55 56 57

	private static IDS = 0;

58 59 60
	//#region events

	private readonly _onDidEditorActivate = this._register(new Emitter<EditorInput>());
61
	readonly onDidEditorActivate = this._onDidEditorActivate.event;
62 63

	private readonly _onDidEditorOpen = this._register(new Emitter<EditorInput>());
64
	readonly onDidEditorOpen = this._onDidEditorOpen.event;
65 66

	private readonly _onDidEditorClose = this._register(new Emitter<EditorCloseEvent>());
67
	readonly onDidEditorClose = this._onDidEditorClose.event;
68 69

	private readonly _onDidEditorDispose = this._register(new Emitter<EditorInput>());
70
	readonly onDidEditorDispose = this._onDidEditorDispose.event;
71 72

	private readonly _onDidEditorBecomeDirty = this._register(new Emitter<EditorInput>());
73
	readonly onDidEditorBecomeDirty = this._onDidEditorBecomeDirty.event;
74 75

	private readonly _onDidEditorLabelChange = this._register(new Emitter<EditorInput>());
76
	readonly onDidEditorLabelChange = this._onDidEditorLabelChange.event;
77 78

	private readonly _onDidEditorMove = this._register(new Emitter<EditorInput>());
79
	readonly onDidEditorMove = this._onDidEditorMove.event;
80 81

	private readonly _onDidEditorPin = this._register(new Emitter<EditorInput>());
82
	readonly onDidEditorPin = this._onDidEditorPin.event;
83 84

	private readonly _onDidEditorUnpin = this._register(new Emitter<EditorInput>());
85
	readonly onDidEditorUnpin = this._onDidEditorUnpin.event;
86 87

	//#endregion
B
Benjamin Pasero 已提交
88

B
Benjamin Pasero 已提交
89
	private _id: GroupIdentifier;
B
Benjamin Pasero 已提交
90
	get id(): GroupIdentifier { return this._id; }
91

92 93
	private editors: EditorInput[] = [];
	private mru: EditorInput[] = [];
94

B
Benjamin Pasero 已提交
95 96
	private preview: EditorInput | null = null; // editor in preview state
	private active: EditorInput | null = null;  // editor in active state
97

B
Benjamin Pasero 已提交
98 99
	private editorOpenPositioning: ('left' | 'right' | 'first' | 'last') | undefined;
	private focusRecentEditorAfterClose: boolean | undefined;
100

101
	constructor(
102
		labelOrSerializedGroup: ISerializedEditorGroup | undefined,
103 104
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@IConfigurationService private readonly configurationService: IConfigurationService
105
	) {
106 107
		super();

108
		if (isSerializedEditorGroup(labelOrSerializedGroup)) {
B
Benjamin Pasero 已提交
109
			this._id = this.deserialize(labelOrSerializedGroup);
110
		} else {
111
			this._id = EditorGroup.IDS++;
112
		}
113

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

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

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

127
	get count(): number {
128 129 130
		return this.editors.length;
	}

131
	getEditors(mru?: boolean): EditorInput[] {
B
Benjamin Pasero 已提交
132
		return mru ? this.mru.slice(0) : this.editors.slice(0);
B
Benjamin Pasero 已提交
133 134
	}

B
Benjamin Pasero 已提交
135 136
	getEditorByIndex(index: number): EditorInput | undefined {
		return this.editors[index];
137 138
	}

M
Matt Bierner 已提交
139
	get activeEditor(): EditorInput | null {
B
Benjamin Pasero 已提交
140 141 142
		return this.active;
	}

143
	isActive(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
144
		return this.matches(this.active, editor);
B
Benjamin Pasero 已提交
145 146
	}

M
Matt Bierner 已提交
147
	get previewEditor(): EditorInput | null {
B
Benjamin Pasero 已提交
148 149 150
		return this.preview;
	}

151
	isPreview(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
152
		return this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
153 154
	}

155
	openEditor(candidate: EditorInput, options?: IEditorOpenOptions): EditorInput {
B
Benjamin Pasero 已提交
156 157
		const makePinned = options?.pinned;
		const makeActive = options?.active || !this.activeEditor || (!makePinned && this.matches(this.preview, this.activeEditor));
B
Benjamin Pasero 已提交
158

159 160
		const existingEditor = this.findEditor(candidate);

B
Benjamin Pasero 已提交
161
		// New editor
162 163
		if (!existingEditor) {
			const newEditor = candidate;
164
			const indexOfActive = this.indexOf(this.active);
B
Benjamin Pasero 已提交
165

166
			// Insert into specific position
167
			let targetIndex: number;
168 169 170
			if (options && typeof options.index === 'number') {
				targetIndex = options.index;
			}
B
Benjamin Pasero 已提交
171

172
			// Insert to the BEGINNING
173
			else if (this.editorOpenPositioning === EditorOpenPositioning.FIRST) {
174 175 176 177
				targetIndex = 0;
			}

			// Insert to the END
178
			else if (this.editorOpenPositioning === EditorOpenPositioning.LAST) {
179
				targetIndex = this.editors.length;
180
			}
B
Benjamin Pasero 已提交
181

182
			// Insert to the LEFT of active editor
183
			else if (this.editorOpenPositioning === EditorOpenPositioning.LEFT) {
184 185 186
				if (indexOfActive === 0 || !this.editors.length) {
					targetIndex = 0; // to the left becoming first editor in list
				} else {
187
					targetIndex = indexOfActive; // to the left of active editor
B
Benjamin Pasero 已提交
188
				}
189
			}
B
Benjamin Pasero 已提交
190

191 192 193 194 195
			// Insert to the RIGHT of active editor
			else {
				targetIndex = indexOfActive + 1;
			}

196 197
			// Insert into our list of editors if pinned or we have no preview editor
			if (makePinned || !this.preview) {
198
				this.splice(targetIndex, false, newEditor);
B
Benjamin Pasero 已提交
199 200
			}

201 202
			// Handle preview
			if (!makePinned) {
B
Benjamin Pasero 已提交
203 204

				// Replace existing preview with this editor if we have a preview
205 206
				if (this.preview) {
					const indexOfPreview = this.indexOf(this.preview);
207 208
					if (targetIndex > indexOfPreview) {
						targetIndex--; // accomodate for the fact that the preview editor closes
209 210
					}

211
					this.replaceEditor(this.preview, newEditor, targetIndex, !makeActive);
212
				}
B
Benjamin Pasero 已提交
213

214
				this.preview = newEditor;
B
Benjamin Pasero 已提交
215 216
			}

217
			// Listeners
218
			this.registerEditorListeners(newEditor);
219

B
Benjamin Pasero 已提交
220
			// Event
221
			this._onDidEditorOpen.fire(newEditor);
B
Benjamin Pasero 已提交
222

223
			// Handle active
B
Benjamin Pasero 已提交
224
			if (makeActive) {
225
				this.doSetActive(newEditor);
B
Benjamin Pasero 已提交
226
			}
227 228

			return newEditor;
B
Benjamin Pasero 已提交
229 230 231 232 233 234 235
		}

		// Existing editor
		else {

			// Pin it
			if (makePinned) {
236
				this.doPin(existingEditor);
B
Benjamin Pasero 已提交
237 238 239 240
			}

			// Activate it
			if (makeActive) {
241
				this.doSetActive(existingEditor);
B
Benjamin Pasero 已提交
242
			}
B
Benjamin Pasero 已提交
243 244 245

			// Respect index
			if (options && typeof options.index === 'number') {
246
				this.moveEditor(existingEditor, options.index);
B
Benjamin Pasero 已提交
247
			}
248 249

			return existingEditor;
B
Benjamin Pasero 已提交
250 251 252
		}
	}

B
Benjamin Pasero 已提交
253
	private registerEditorListeners(editor: EditorInput): void {
254
		const listeners = new DisposableStore();
255 256

		// Re-emit disposal of editor input as our own event
J
Joao Moreno 已提交
257
		const onceDispose = Event.once(editor.onDispose);
258
		listeners.add(onceDispose(() => {
259
			if (this.indexOf(editor) >= 0) {
260
				this._onDidEditorDispose.fire(editor);
261
			}
262 263 264
		}));

		// Re-Emit dirty state changes
265
		listeners.add(editor.onDidChangeDirty(() => {
266
			this._onDidEditorBecomeDirty.fire(editor);
267
		}));
268

B
Benjamin Pasero 已提交
269
		// Re-Emit label changes
270
		listeners.add(editor.onDidChangeLabel(() => {
271
			this._onDidEditorLabelChange.fire(editor);
B
Benjamin Pasero 已提交
272 273
		}));

274
		// Clean up dispose listeners once the editor gets closed
275
		listeners.add(this.onDidEditorClose(event => {
276
			if (event.editor.matches(editor)) {
277
				dispose(listeners);
278
			}
279
		}));
280 281
	}

B
Benjamin Pasero 已提交
282
	private replaceEditor(toReplace: EditorInput, replaceWith: EditorInput, replaceIndex: number, openNext = true): void {
283
		const event = this.doCloseEditor(toReplace, openNext, true); // optimization to prevent multiple setActive() in one call
284 285 286 287

		// 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.
B
Benjamin Pasero 已提交
288
		this.splice(replaceIndex, false, replaceWith);
289 290

		if (event) {
291
			this._onDidEditorClose.fire(event);
292 293 294
		}
	}

295 296
	closeEditor(candidate: EditorInput, openNext = true): number | undefined {
		const event = this.doCloseEditor(candidate, openNext, false);
297 298

		if (event) {
299
			this._onDidEditorClose.fire(event);
300 301

			return event.index;
302
		}
303

R
Rob Lourens 已提交
304
		return undefined;
305 306
	}

307 308
	private doCloseEditor(candidate: EditorInput, openNext: boolean, replaced: boolean): EditorCloseEvent | null {
		const index = this.indexOf(candidate);
B
Benjamin Pasero 已提交
309
		if (index === -1) {
310
			return null; // not found
B
Benjamin Pasero 已提交
311 312
		}

313 314
		const editor = this.editors[index];

B
Benjamin Pasero 已提交
315
		// Active Editor closed
B
Benjamin Pasero 已提交
316
		if (openNext && this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
317 318

			// More than one editor
B
Benjamin Pasero 已提交
319
			if (this.mru.length > 1) {
320
				let newActive: EditorInput;
321
				if (this.focusRecentEditorAfterClose) {
322
					newActive = this.mru[1]; // active editor is always first in MRU, so pick second editor after as new active
B
Benjamin Pasero 已提交
323
				} else {
324 325
					if (index === this.editors.length - 1) {
						newActive = this.editors[index - 1]; // last editor is closed, pick previous as new active
B
Benjamin Pasero 已提交
326
					} else {
327 328 329
						newActive = this.editors[index + 1]; // pick next editor as new active
					}
				}
B
Benjamin Pasero 已提交
330

331
				this.doSetActive(newActive);
B
Benjamin Pasero 已提交
332 333 334 335 336 337 338 339 340
			}

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

		// Preview Editor closed
B
Benjamin Pasero 已提交
341
		if (this.matches(this.preview, editor)) {
B
Benjamin Pasero 已提交
342 343 344
			this.preview = null;
		}

B
Benjamin Pasero 已提交
345
		// Remove from arrays
B
Benjamin Pasero 已提交
346
		this.splice(index, true);
B
Benjamin Pasero 已提交
347 348

		// Event
I
isidor 已提交
349
		return { editor, replaced, index, groupId: this.id };
B
Benjamin Pasero 已提交
350 351
	}

B
Benjamin Pasero 已提交
352
	closeEditors(except: EditorInput, direction?: CloseDirection): void {
B
Benjamin Pasero 已提交
353 354 355 356 357 358
		const index = this.indexOf(except);
		if (index === -1) {
			return; // not found
		}

		// Close to the left
B
Benjamin Pasero 已提交
359
		if (direction === CloseDirection.LEFT) {
B
Benjamin Pasero 已提交
360 361 362 363 364 365
			for (let i = index - 1; i >= 0; i--) {
				this.closeEditor(this.editors[i]);
			}
		}

		// Close to the right
B
Benjamin Pasero 已提交
366
		else if (direction === CloseDirection.RIGHT) {
B
Benjamin Pasero 已提交
367 368 369 370 371 372 373
			for (let i = this.editors.length - 1; i > index; i--) {
				this.closeEditor(this.editors[i]);
			}
		}

		// Both directions
		else {
B
Benjamin Pasero 已提交
374
			this.mru.filter(e => !this.matches(e, except)).forEach(e => this.closeEditor(e));
B
Benjamin Pasero 已提交
375 376 377
		}
	}

378
	closeAllEditors(): void {
379 380

		// Optimize: close all non active editors first to produce less upstream work
B
Benjamin Pasero 已提交
381
		this.mru.filter(e => !this.matches(e, this.active)).forEach(e => this.closeEditor(e));
M
Matt Bierner 已提交
382 383 384
		if (this.active) {
			this.closeEditor(this.active);
		}
385 386
	}

387 388
	moveEditor(candidate: EditorInput, toIndex: number): void {
		const index = this.indexOf(candidate);
B
Benjamin Pasero 已提交
389 390 391 392
		if (index < 0) {
			return;
		}

393 394
		const editor = this.editors[index];

B
Benjamin Pasero 已提交
395 396 397 398 399
		// Move
		this.editors.splice(index, 1);
		this.editors.splice(toIndex, 0, editor);

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

403 404 405
	setActive(candidate: EditorInput): void {
		const editor = this.findEditor(candidate);
		if (!editor) {
B
Benjamin Pasero 已提交
406 407 408
			return; // not found
		}

409 410 411 412
		this.doSetActive(editor);
	}

	private doSetActive(editor: EditorInput): void {
B
Benjamin Pasero 已提交
413
		if (this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
414 415 416 417 418
			return; // already active
		}

		this.active = editor;

B
Benjamin Pasero 已提交
419
		// Bring to front in MRU list
420 421 422
		const mruIndex = this.indexOf(editor, this.mru);
		this.mru.splice(mruIndex, 1);
		this.mru.unshift(editor);
B
Benjamin Pasero 已提交
423

B
Benjamin Pasero 已提交
424
		// Event
425
		this._onDidEditorActivate.fire(editor);
B
Benjamin Pasero 已提交
426 427
	}

428 429 430
	pin(candidate: EditorInput): void {
		const editor = this.findEditor(candidate);
		if (!editor) {
431 432 433
			return; // not found
		}

434 435 436 437
		this.doPin(editor);
	}

	private doPin(editor: EditorInput): void {
B
Benjamin Pasero 已提交
438 439
		if (!this.isPreview(editor)) {
			return; // can only pin a preview editor
B
Benjamin Pasero 已提交
440 441 442 443 444 445
		}

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

		// Event
446
		this._onDidEditorPin.fire(editor);
B
Benjamin Pasero 已提交
447 448
	}

449 450 451
	unpin(candidate: EditorInput): void {
		const editor = this.findEditor(candidate);
		if (!editor) {
452 453
			return; // not found
		}
454

455 456 457 458
		this.doUnpin(editor);
	}

	private doUnpin(editor: EditorInput): void {
B
Benjamin Pasero 已提交
459 460 461 462 463 464 465 466 467
		if (!this.isPinned(editor)) {
			return; // can only unpin a pinned editor
		}

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

		// Event
468
		this._onDidEditorUnpin.fire(editor);
B
Benjamin Pasero 已提交
469 470

		// Close old preview editor if any
M
Matt Bierner 已提交
471 472 473
		if (oldPreview) {
			this.closeEditor(oldPreview);
		}
B
Benjamin Pasero 已提交
474 475
	}

476 477 478
	isPinned(editor: EditorInput): boolean;
	isPinned(index: number): boolean;
	isPinned(arg1: EditorInput | number): boolean {
479 480 481 482 483 484 485 486 487 488 489
		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 已提交
490 491 492 493 494 495 496
			return false; // editor not found
		}

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

B
Benjamin Pasero 已提交
497
		return !this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
498 499
	}

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

503
		// Perform on editors array
504
		if (editor) {
505 506 507
			this.editors.splice(index, del ? 1 : 0, editor);
		} else {
			this.editors.splice(index, del ? 1 : 0);
508
		}
B
Benjamin Pasero 已提交
509

510
		// Add
B
Benjamin Pasero 已提交
511
		if (!del && editor) {
512 513 514 515 516 517 518 519 520 521 522 523 524
			if (this.mru.length === 0) {
				// the list of most recent editors is empty
				// so this editor can only be the most recent
				this.mru.push(editor);
			} else {
				// we have most recent editors. as such we
				// put this newly opened editor right after
				// the current most recent one because it cannot
				// be the most recently active one unless
				// it becomes active. but it is still more
				// active then any other editor in the list.
				this.mru.splice(1, 0, editor);
			}
B
Benjamin Pasero 已提交
525 526
		}

B
Benjamin Pasero 已提交
527
		// Remove / Replace
B
Benjamin Pasero 已提交
528
		else {
B
Benjamin Pasero 已提交
529 530
			const indexInMRU = this.indexOf(editorToDeleteOrReplace, this.mru);

531
			// Remove
B
Benjamin Pasero 已提交
532
			if (del && !editor) {
533
				this.mru.splice(indexInMRU, 1); // remove from MRU
B
Benjamin Pasero 已提交
534 535
			}

536
			// Replace
B
Benjamin Pasero 已提交
537
			else if (del && editor) {
538
				this.mru.splice(indexInMRU, 1, editor); // replace MRU at location
B
Benjamin Pasero 已提交
539
			}
540 541
		}
	}
B
Benjamin Pasero 已提交
542

543
	indexOf(candidate: IEditorInput | null, editors = this.editors): number {
B
Benjamin Pasero 已提交
544 545
		if (!candidate) {
			return -1;
B
Benjamin Pasero 已提交
546 547
		}

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

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

557 558 559 560 561 562 563 564 565
	private findEditor(candidate: IEditorInput | null): EditorInput | undefined {
		const index = this.indexOf(candidate, this.editors);
		if (index === -1) {
			return undefined;
		}

		return this.editors[index];
	}

566 567 568 569 570
	contains(candidate: EditorInput, searchInSideBySideEditors?: boolean): boolean {
		for (const editor of this.editors) {
			if (this.matches(editor, candidate)) {
				return true;
			}
571

572 573 574 575 576
			if (searchInSideBySideEditors && editor instanceof SideBySideEditorInput) {
				if (this.matches(editor.master, candidate) || this.matches(editor.details, candidate)) {
					return true;
				}
			}
577 578 579 580 581
		}

		return false;
	}

582
	private matches(editorA: IEditorInput | null, editorB: IEditorInput | null): boolean {
B
Benjamin Pasero 已提交
583 584
		return !!editorA && !!editorB && editorA.matches(editorB);
	}
585

586
	clone(): EditorGroup {
R
Rob Lourens 已提交
587
		const group = this.instantiationService.createInstance(EditorGroup, undefined);
588 589 590 591 592 593 594 595 596 597
		group.editors = this.editors.slice(0);
		group.mru = this.mru.slice(0);
		group.preview = this.preview;
		group.active = this.active;
		group.editorOpenPositioning = this.editorOpenPositioning;

		return group;
	}

	serialize(): ISerializedEditorGroup {
598
		const registry = Registry.as<IEditorInputFactoryRegistry>(Extensions.EditorInputFactories);
599 600 601 602 603 604

		// 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[] = [];
M
Matt Bierner 已提交
605
		let serializablePreviewIndex: number | undefined;
606
		this.editors.forEach(e => {
607
			const factory = registry.getEditorInputFactory(e.getTypeId());
608
			if (factory) {
609
				const value = factory.serialize(e);
610
				if (typeof value === 'string') {
611
					serializedEditors.push({ id: e.getTypeId(), value });
612
					serializableEditors.push(e);
B
Benjamin Pasero 已提交
613 614 615 616

					if (this.preview === e) {
						serializablePreviewIndex = serializableEditors.length - 1;
					}
617 618 619 620
				}
			}
		});

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

		return {
624
			id: this.id,
625 626
			editors: serializedEditors,
			mru: serializableMru,
B
Benjamin Pasero 已提交
627
			preview: serializablePreviewIndex,
628 629 630
		};
	}

B
Benjamin Pasero 已提交
631
	private deserialize(data: ISerializedEditorGroup): number {
632
		const registry = Registry.as<IEditorInputFactoryRegistry>(Extensions.EditorInputFactories);
633

634 635 636 637 638 639 640 641
		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 已提交
642
		this.editors = coalesce(data.editors.map(e => {
B
Benjamin Pasero 已提交
643 644
			const factory = registry.getEditorInputFactory(e.id);
			if (factory) {
B
Benjamin Pasero 已提交
645 646 647 648
				const editor = factory.deserialize(this.instantiationService, e.value);
				if (editor) {
					this.registerEditorListeners(editor);
				}
649

B
Benjamin Pasero 已提交
650 651 652 653
				return editor;
			}

			return null;
M
Matt Bierner 已提交
654
		}));
B
Benjamin Pasero 已提交
655

656
		this.mru = data.mru.map(i => this.editors[i]);
B
Benjamin Pasero 已提交
657

658
		this.active = this.mru[0];
B
Benjamin Pasero 已提交
659

M
Matt Bierner 已提交
660 661 662
		if (typeof data.preview === 'number') {
			this.preview = this.editors[data.preview];
		}
B
Benjamin Pasero 已提交
663 664

		return this._id;
665 666
	}
}