editorGroup.ts 18.0 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
}

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;
	}

B
Benjamin Pasero 已提交
131 132
	getEditors(order: EditorsOrder): EditorInput[] {
		return order === EditorsOrder.MOST_RECENTLY_ACTIVE ? 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
	closeEditor(candidate: EditorInput, openNext = true): EditorInput | undefined {
296
		const event = this.doCloseEditor(candidate, openNext, false);
297 298

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

301
			return event.editor;
302
		}
303

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

307
	private doCloseEditor(candidate: EditorInput, openNext: boolean, replaced: boolean): EditorCloseEvent | undefined {
308
		const index = this.indexOf(candidate);
B
Benjamin Pasero 已提交
309
		if (index === -1) {
310
			return undefined; // 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
	moveEditor(candidate: EditorInput, toIndex: number): EditorInput | undefined {
388
		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);
401 402

		return editor;
B
Benjamin Pasero 已提交
403 404
	}

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

411
		this.doSetActive(editor);
412 413

		return editor;
414 415 416
	}

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

		this.active = editor;

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

B
Benjamin Pasero 已提交
428
		// Event
429
		this._onDidEditorActivate.fire(editor);
B
Benjamin Pasero 已提交
430 431
	}

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

438
		this.doPin(editor);
439 440

		return editor;
441 442 443
	}

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

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

		// Event
452
		this._onDidEditorPin.fire(editor);
B
Benjamin Pasero 已提交
453 454
	}

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

461
		this.doUnpin(editor);
462 463

		return editor;
464 465 466
	}

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

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

		// Event
476
		this._onDidEditorUnpin.fire(editor);
B
Benjamin Pasero 已提交
477 478

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

484 485 486
	isPinned(editor: EditorInput): boolean;
	isPinned(index: number): boolean;
	isPinned(arg1: EditorInput | number): boolean {
487 488 489 490 491 492 493 494 495 496 497
		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 已提交
498 499 500 501 502 503 504
			return false; // editor not found
		}

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

B
Benjamin Pasero 已提交
505
		return !this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
506 507
	}

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

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

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

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

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

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

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

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

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

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

		return this.editors[index];
	}

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

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

		return false;
	}

590
	private matches(editor: IEditorInput | null, candidate: IEditorInput | null): boolean {
591 592 593 594
		if (!editor || !candidate) {
			return false;
		}

595
		return editor.matches(candidate);
B
Benjamin Pasero 已提交
596
	}
597

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

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

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

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

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

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

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

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

			return null;
M
Matt Bierner 已提交
666
		}));
B
Benjamin Pasero 已提交
667

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

670
		this.active = this.mru[0];
B
Benjamin Pasero 已提交
671

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

		return this._id;
677 678
	}
}