editorGroup.ts 17.9 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';
B
Benjamin Pasero 已提交
7
import { Extensions, IEditorInputFactoryRegistry, EditorInput, IEditorIdentifier, IEditorCloseEvent, GroupIdentifier, CloseDirection, SideBySideEditorInput, IEditorInput, EditorsOrder } 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
}

B
Benjamin Pasero 已提交
48 49
export function isSerializedEditorGroup(obj?: unknown): obj is ISerializedEditorGroup {
	const group = obj as ISerializedEditorGroup;
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
	//#region events

60 61
	private readonly _onDidActivateEditor = this._register(new Emitter<EditorInput>());
	readonly onDidActivateEditor = this._onDidActivateEditor.event;
62

63 64
	private readonly _onDidOpenEditor = this._register(new Emitter<EditorInput>());
	readonly onDidOpenEditor = this._onDidOpenEditor.event;
65

66 67
	private readonly _onDidCloseEditor = this._register(new Emitter<EditorCloseEvent>());
	readonly onDidCloseEditor = this._onDidCloseEditor.event;
68

69 70
	private readonly _onDidDisposeEditor = this._register(new Emitter<EditorInput>());
	readonly onDidDisposeEditor = this._onDidDisposeEditor.event;
71

72 73
	private readonly _onDidChangeEditorDirty = this._register(new Emitter<EditorInput>());
	readonly onDidChangeEditorDirty = this._onDidChangeEditorDirty.event;
74

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

78 79
	private readonly _onDidMoveEditor = this._register(new Emitter<EditorInput>());
	readonly onDidMoveEditor = this._onDidMoveEditor.event;
80

81 82
	private readonly _onDidChangeEditorPinned = this._register(new Emitter<EditorInput>());
	readonly onDidChangeEditorPinned = this._onDidChangeEditorPinned.event;
83 84

	//#endregion
B
Benjamin Pasero 已提交
85

B
Benjamin Pasero 已提交
86
	private _id: GroupIdentifier;
B
Benjamin Pasero 已提交
87
	get id(): GroupIdentifier { return this._id; }
88

89 90
	private editors: EditorInput[] = [];
	private mru: EditorInput[] = [];
91

B
Benjamin Pasero 已提交
92 93
	private preview: EditorInput | null = null; // editor in preview state
	private active: EditorInput | null = null;  // editor in active state
94

B
Benjamin Pasero 已提交
95 96
	private editorOpenPositioning: ('left' | 'right' | 'first' | 'last') | undefined;
	private focusRecentEditorAfterClose: boolean | undefined;
97

98
	constructor(
99
		labelOrSerializedGroup: ISerializedEditorGroup | undefined,
100 101
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@IConfigurationService private readonly configurationService: IConfigurationService
102
	) {
103 104
		super();

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

111
		this.onConfigurationUpdated();
112 113 114 115
		this.registerListeners();
	}

	private registerListeners(): void {
116
		this._register(this.configurationService.onDidChangeConfiguration(e => this.onConfigurationUpdated(e)));
117 118
	}

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

124
	get count(): number {
125 126 127
		return this.editors.length;
	}

B
Benjamin Pasero 已提交
128 129
	getEditors(order: EditorsOrder): EditorInput[] {
		return order === EditorsOrder.MOST_RECENTLY_ACTIVE ? this.mru.slice(0) : this.editors.slice(0);
B
Benjamin Pasero 已提交
130 131
	}

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

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

140
	isActive(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
141
		return this.matches(this.active, editor);
B
Benjamin Pasero 已提交
142 143
	}

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

148
	isPreview(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
149
		return this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
150 151
	}

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

156 157
		const existingEditor = this.findEditor(candidate);

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

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

169
			// Insert to the BEGINNING
170
			else if (this.editorOpenPositioning === EditorOpenPositioning.FIRST) {
171 172 173 174
				targetIndex = 0;
			}

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

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

188 189 190 191 192
			// Insert to the RIGHT of active editor
			else {
				targetIndex = indexOfActive + 1;
			}

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

198 199
			// Handle preview
			if (!makePinned) {
B
Benjamin Pasero 已提交
200 201

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

208
					this.replaceEditor(this.preview, newEditor, targetIndex, !makeActive);
209
				}
B
Benjamin Pasero 已提交
210

211
				this.preview = newEditor;
B
Benjamin Pasero 已提交
212 213
			}

214
			// Listeners
215
			this.registerEditorListeners(newEditor);
216

B
Benjamin Pasero 已提交
217
			// Event
218
			this._onDidOpenEditor.fire(newEditor);
B
Benjamin Pasero 已提交
219

220
			// Handle active
B
Benjamin Pasero 已提交
221
			if (makeActive) {
222
				this.doSetActive(newEditor);
B
Benjamin Pasero 已提交
223
			}
224 225

			return newEditor;
B
Benjamin Pasero 已提交
226 227 228 229 230 231 232
		}

		// Existing editor
		else {

			// Pin it
			if (makePinned) {
233
				this.doPin(existingEditor);
B
Benjamin Pasero 已提交
234 235 236 237
			}

			// Activate it
			if (makeActive) {
238
				this.doSetActive(existingEditor);
B
Benjamin Pasero 已提交
239
			}
B
Benjamin Pasero 已提交
240 241 242

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

			return existingEditor;
B
Benjamin Pasero 已提交
247 248 249
		}
	}

B
Benjamin Pasero 已提交
250
	private registerEditorListeners(editor: EditorInput): void {
251
		const listeners = new DisposableStore();
252 253

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

		// Re-Emit dirty state changes
262
		listeners.add(editor.onDidChangeDirty(() => {
263
			this._onDidChangeEditorDirty.fire(editor);
264
		}));
265

B
Benjamin Pasero 已提交
266
		// Re-Emit label changes
267
		listeners.add(editor.onDidChangeLabel(() => {
268
			this._onDidChangeEditorLabel.fire(editor);
B
Benjamin Pasero 已提交
269 270
		}));

271
		// Clean up dispose listeners once the editor gets closed
272
		listeners.add(this.onDidCloseEditor(event => {
273
			if (event.editor.matches(editor)) {
274
				dispose(listeners);
275
			}
276
		}));
277 278
	}

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

		// 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 已提交
285
		this.splice(replaceIndex, false, replaceWith);
286 287

		if (event) {
288
			this._onDidCloseEditor.fire(event);
289 290 291
		}
	}

292
	closeEditor(candidate: EditorInput, openNext = true): EditorInput | undefined {
293
		const event = this.doCloseEditor(candidate, openNext, false);
294 295

		if (event) {
296
			this._onDidCloseEditor.fire(event);
297

298
			return event.editor;
299
		}
300

R
Rob Lourens 已提交
301
		return undefined;
302 303
	}

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

310 311
		const editor = this.editors[index];

B
Benjamin Pasero 已提交
312
		// Active Editor closed
B
Benjamin Pasero 已提交
313
		if (openNext && this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
314 315

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

328
				this.doSetActive(newActive);
B
Benjamin Pasero 已提交
329 330 331 332 333 334 335 336 337
			}

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

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

B
Benjamin Pasero 已提交
342
		// Remove from arrays
B
Benjamin Pasero 已提交
343
		this.splice(index, true);
B
Benjamin Pasero 已提交
344 345

		// Event
I
isidor 已提交
346
		return { editor, replaced, index, groupId: this.id };
B
Benjamin Pasero 已提交
347 348
	}

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

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

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

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

375
	closeAllEditors(): void {
376 377

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

384
	moveEditor(candidate: EditorInput, toIndex: number): EditorInput | undefined {
385
		const index = this.indexOf(candidate);
B
Benjamin Pasero 已提交
386
		if (index < 0 || toIndex === index) {
B
Benjamin Pasero 已提交
387 388 389
			return;
		}

390 391
		const editor = this.editors[index];

B
Benjamin Pasero 已提交
392 393 394 395 396
		// Move
		this.editors.splice(index, 1);
		this.editors.splice(toIndex, 0, editor);

		// Event
397
		this._onDidMoveEditor.fire(editor);
398 399

		return editor;
B
Benjamin Pasero 已提交
400 401
	}

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

408
		this.doSetActive(editor);
409 410

		return editor;
411 412 413
	}

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

		this.active = editor;

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

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

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

435
		this.doPin(editor);
436 437

		return editor;
438 439 440
	}

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

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

		// Event
449
		this._onDidChangeEditorPinned.fire(editor);
B
Benjamin Pasero 已提交
450 451
	}

452
	unpin(candidate: EditorInput): EditorInput | undefined {
453 454
		const editor = this.findEditor(candidate);
		if (!editor) {
455 456
			return; // not found
		}
457

458
		this.doUnpin(editor);
459 460

		return editor;
461 462 463
	}

	private doUnpin(editor: EditorInput): void {
B
Benjamin Pasero 已提交
464 465 466 467 468 469 470 471 472
		if (!this.isPinned(editor)) {
			return; // can only unpin a pinned editor
		}

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

		// Event
473
		this._onDidChangeEditorPinned.fire(editor);
B
Benjamin Pasero 已提交
474 475

		// Close old preview editor if any
M
Matt Bierner 已提交
476 477 478
		if (oldPreview) {
			this.closeEditor(oldPreview);
		}
B
Benjamin Pasero 已提交
479 480
	}

481 482 483
	isPinned(editor: EditorInput): boolean;
	isPinned(index: number): boolean;
	isPinned(arg1: EditorInput | number): boolean {
B
Benjamin Pasero 已提交
484 485 486 487
		if (!this.preview) {
			return true; // no preview editor
		}

488 489 490 491 492 493 494 495 496 497 498
		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 已提交
499 500 501
			return false; // editor not found
		}

B
Benjamin Pasero 已提交
502
		return !this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
503 504
	}

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

508
		// Perform on editors array
509
		if (editor) {
510 511 512
			this.editors.splice(index, del ? 1 : 0, editor);
		} else {
			this.editors.splice(index, del ? 1 : 0);
513
		}
B
Benjamin Pasero 已提交
514

515
		// Add
B
Benjamin Pasero 已提交
516
		if (!del && editor) {
517 518 519 520 521 522 523 524 525 526 527 528 529
			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 已提交
530 531
		}

B
Benjamin Pasero 已提交
532
		// Remove / Replace
B
Benjamin Pasero 已提交
533
		else {
B
Benjamin Pasero 已提交
534 535
			const indexInMRU = this.indexOf(editorToDeleteOrReplace, this.mru);

536
			// Remove
B
Benjamin Pasero 已提交
537
			if (del && !editor) {
538
				this.mru.splice(indexInMRU, 1); // remove from MRU
B
Benjamin Pasero 已提交
539 540
			}

541
			// Replace
B
Benjamin Pasero 已提交
542
			else if (del && editor) {
543
				this.mru.splice(indexInMRU, 1, editor); // replace MRU at location
B
Benjamin Pasero 已提交
544
			}
545 546
		}
	}
B
Benjamin Pasero 已提交
547

548
	indexOf(candidate: IEditorInput | null, editors = this.editors): number {
B
Benjamin Pasero 已提交
549 550
		if (!candidate) {
			return -1;
B
Benjamin Pasero 已提交
551 552
		}

B
Benjamin Pasero 已提交
553
		for (let i = 0; i < editors.length; i++) {
B
Benjamin Pasero 已提交
554
			if (this.matches(editors[i], candidate)) {
B
Benjamin Pasero 已提交
555 556 557 558 559 560
				return i;
			}
		}

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

562
	private findEditor(candidate: EditorInput | null): EditorInput | undefined {
563 564 565 566 567 568 569 570
		const index = this.indexOf(candidate, this.editors);
		if (index === -1) {
			return undefined;
		}

		return this.editors[index];
	}

571
	contains(candidate: EditorInput, searchInSideBySideEditors?: boolean): boolean {
572 573 574 575
		for (const editor of this.editors) {
			if (this.matches(editor, candidate)) {
				return true;
			}
576

577 578 579 580 581
			if (searchInSideBySideEditors && editor instanceof SideBySideEditorInput) {
				if (this.matches(editor.master, candidate) || this.matches(editor.details, candidate)) {
					return true;
				}
			}
582 583 584 585 586
		}

		return false;
	}

587
	private matches(editor: IEditorInput | null, candidate: IEditorInput | null): boolean {
588 589 590 591
		if (!editor || !candidate) {
			return false;
		}

592
		return editor.matches(candidate);
B
Benjamin Pasero 已提交
593
	}
594

595
	clone(): EditorGroup {
R
Rob Lourens 已提交
596
		const group = this.instantiationService.createInstance(EditorGroup, undefined);
597 598 599 600 601 602 603 604 605 606
		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 {
607
		const registry = Registry.as<IEditorInputFactoryRegistry>(Extensions.EditorInputFactories);
608 609 610 611 612 613

		// 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 已提交
614
		let serializablePreviewIndex: number | undefined;
615
		this.editors.forEach(e => {
616
			const factory = registry.getEditorInputFactory(e.getTypeId());
617
			if (factory) {
618
				const value = factory.serialize(e);
619
				if (typeof value === 'string') {
620
					serializedEditors.push({ id: e.getTypeId(), value });
621
					serializableEditors.push(e);
B
Benjamin Pasero 已提交
622 623 624 625

					if (this.preview === e) {
						serializablePreviewIndex = serializableEditors.length - 1;
					}
626 627 628 629
				}
			}
		});

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

		return {
633
			id: this.id,
634 635
			editors: serializedEditors,
			mru: serializableMru,
B
Benjamin Pasero 已提交
636
			preview: serializablePreviewIndex,
637 638 639
		};
	}

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

643 644 645 646 647 648 649 650
		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 已提交
651
		this.editors = coalesce(data.editors.map(e => {
B
Benjamin Pasero 已提交
652 653
			const factory = registry.getEditorInputFactory(e.id);
			if (factory) {
B
Benjamin Pasero 已提交
654 655 656 657
				const editor = factory.deserialize(this.instantiationService, e.value);
				if (editor) {
					this.registerEditorListeners(editor);
				}
658

B
Benjamin Pasero 已提交
659 660 661 662
				return editor;
			}

			return null;
M
Matt Bierner 已提交
663
		}));
B
Benjamin Pasero 已提交
664

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

667
		this.active = this.mru[0];
B
Benjamin Pasero 已提交
668

M
Matt Bierner 已提交
669 670 671
		if (typeof data.preview === 'number') {
			this.preview = this.editors[data.preview];
		}
B
Benjamin Pasero 已提交
672 673

		return this._id;
674 675
	}
}