editorGroup.ts 18.6 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, 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
	editors: ISerializedEditorInput[];
	mru: number[];
M
Matt Bierner 已提交
47
	preview?: number;
48 49
}

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

M
Matt Bierner 已提交
97 98
	private preview: EditorInput | null; // editor in preview state
	private active: EditorInput | null;  // editor in active state
99 100 101

	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
	}

M
Matt Bierner 已提交
139 140 141
	getEditor(index: number): EditorInput | null;
	getEditor(resource: URI): EditorInput | null;
	getEditor(arg1: any): EditorInput | null {
B
Benjamin Pasero 已提交
142 143 144 145 146 147 148 149 150
		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
		}

151
		for (const editor of this.editors) {
152
			const editorResource = toResource(editor, { supportSideBySide: true });
B
Benjamin Pasero 已提交
153
			if (editorResource && editorResource.toString() === resource.toString()) {
B
Benjamin Pasero 已提交
154
				return editor;
B
Benjamin Pasero 已提交
155 156 157 158
			}
		}

		return null;
159 160
	}

M
Matt Bierner 已提交
161
	get activeEditor(): EditorInput | null {
B
Benjamin Pasero 已提交
162 163 164
		return this.active;
	}

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

M
Matt Bierner 已提交
169
	get previewEditor(): EditorInput | null {
B
Benjamin Pasero 已提交
170 171 172
		return this.preview;
	}

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

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

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

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

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

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

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

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

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

217 218 219
			// 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 已提交
220 221
			}

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

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

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

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

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

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

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

		// Existing editor
		else {

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
299
	private replaceEditor(toReplace: EditorInput, replaceWith: EditorInput, replaceIndex: number, openNext = true): void {
300
		const event = this.doCloseEditor(toReplace, openNext, true); // optimization to prevent multiple setActive() in one call
301 302 303 304

		// 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 已提交
305
		this.splice(replaceIndex, false, replaceWith);
306 307

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

M
Matt Bierner 已提交
312
	closeEditor(editor: EditorInput, openNext = true): number | undefined {
313
		const event = this.doCloseEditor(editor, openNext, false);
314 315

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

			return event.index;
319
		}
320

R
Rob Lourens 已提交
321
		return undefined;
322 323
	}

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

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

			// More than one editor
B
Benjamin Pasero 已提交
334 335
			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 已提交
336 337 338 339 340 341 342 343 344
			}

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

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

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

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

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

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

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

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

382
	closeAllEditors(): void {
383 384

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

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

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

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

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

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

		this.active = editor;

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

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

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

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

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

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

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

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

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

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

		// Close old preview editor if any
M
Matt Bierner 已提交
459 460 461
		if (oldPreview) {
			this.closeEditor(oldPreview);
		}
B
Benjamin Pasero 已提交
462 463
	}

464 465 466
	isPinned(editor: EditorInput): boolean;
	isPinned(index: number): boolean;
	isPinned(arg1: EditorInput | number): boolean {
467 468 469 470 471 472 473 474 475 476 477
		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 已提交
478 479 480 481 482 483 484
			return false; // editor not found
		}

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

B
Benjamin Pasero 已提交
485
		return !this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
486 487
	}

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

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

496
		// Perform on editors array
497
		this.editors.splice.apply(this.editors, args);
B
Benjamin Pasero 已提交
498

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

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

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

515
			// Replace
B
Benjamin Pasero 已提交
516
			else if (del && editor) {
517 518 519
				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 已提交
520
			}
B
Benjamin Pasero 已提交
521 522 523
		}
	}

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

			// 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
Benjamin Pasero 已提交
530 531 532
			const counter = this.mapResourceToEditorCount.get(resource) || 0;

			// Add
B
Benjamin Pasero 已提交
533
			let newCounter: number;
B
Benjamin Pasero 已提交
534
			if (!remove) {
535 536 537
				newCounter = counter + 1;
			}

B
Benjamin Pasero 已提交
538 539 540 541 542 543 544 545 546 547
			// Delete
			else {
				newCounter = counter - 1;
			}

			if (newCounter > 0) {
				this.mapResourceToEditorCount.set(resource, newCounter);
			} else {
				this.mapResourceToEditorCount.delete(resource);
			}
548 549 550
		}
	}

M
Matt Bierner 已提交
551
	indexOf(candidate: EditorInput | 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 566 567
	contains(editorOrResource: EditorInput | URI): boolean;
	contains(editor: EditorInput, supportSideBySide?: boolean): boolean;
	contains(editorOrResource: EditorInput | URI, supportSideBySide?: boolean): boolean {
568
		if (editorOrResource instanceof EditorInput) {
569 570 571 572 573 574 575 576 577 578 579 580 581
			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;
582 583
		}

584
		const counter = this.mapResourceToEditorCount.get(editorOrResource);
585 586

		return typeof counter === 'number' && counter > 0;
587 588
	}

B
Benjamin Pasero 已提交
589 590 591 592 593 594 595 596 597
	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 已提交
598
		this.mru.splice(mruIndex, 1);
B
Benjamin Pasero 已提交
599

B
Benjamin Pasero 已提交
600
		// Set editor to front
B
Benjamin Pasero 已提交
601 602
		this.mru.unshift(editor);
	}
B
Benjamin Pasero 已提交
603

M
Matt Bierner 已提交
604
	private matches(editorA: EditorInput | null, editorB: EditorInput | null): boolean {
B
Benjamin Pasero 已提交
605 606
		return !!editorA && !!editorB && editorA.matches(editorB);
	}
607

608
	clone(): EditorGroup {
R
Rob Lourens 已提交
609
		const group = this.instantiationService.createInstance(EditorGroup, undefined);
610 611 612 613 614 615 616 617 618 619 620
		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 {
621
		const registry = Registry.as<IEditorInputFactoryRegistry>(Extensions.EditorInputFactories);
622 623 624 625 626 627

		// 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 已提交
628
		let serializablePreviewIndex: number | undefined;
629
		this.editors.forEach(e => {
630
			let factory = registry.getEditorInputFactory(e.getTypeId());
631 632 633
			if (factory) {
				let value = factory.serialize(e);
				if (typeof value === 'string') {
634
					serializedEditors.push({ id: e.getTypeId(), value });
635
					serializableEditors.push(e);
B
Benjamin Pasero 已提交
636 637 638 639

					if (this.preview === e) {
						serializablePreviewIndex = serializableEditors.length - 1;
					}
640 641 642 643
				}
			}
		});

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

		return {
647
			id: this.id,
648 649
			editors: serializedEditors,
			mru: serializableMru,
B
Benjamin Pasero 已提交
650
			preview: serializablePreviewIndex,
651 652 653 654
		};
	}

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

657 658 659 660 661 662 663 664
		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 已提交
665
		this.editors = coalesce(data.editors.map(e => {
B
Benjamin Pasero 已提交
666 667 668
			const factory = registry.getEditorInputFactory(e.id);
			if (factory) {
				const editor = factory.deserialize(this.instantiationService, e.value);
669

B
Benjamin Pasero 已提交
670
				this.registerEditorListeners(editor);
B
Benjamin Pasero 已提交
671
				this.updateResourceMap(editor, false /* add */);
672

B
Benjamin Pasero 已提交
673 674 675 676
				return editor;
			}

			return null;
M
Matt Bierner 已提交
677
		}));
678 679
		this.mru = data.mru.map(i => this.editors[i]);
		this.active = this.mru[0];
M
Matt Bierner 已提交
680 681 682
		if (typeof data.preview === 'number') {
			this.preview = this.editors[data.preview];
		}
683 684
	}
}