editorGroup.ts 19.1 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

	private editorOpenPositioning: 'left' | 'right' | 'first' | 'last';
101
	private closeTabsInMRUOrder: boolean;
102

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

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

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

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

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

129
	get id(): GroupIdentifier {
130 131 132
		return this._id;
	}

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

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

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

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

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

		return null;
161 162
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		// Existing editor
		else {

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

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

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

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

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

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

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

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

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

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

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

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

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

			return event.index;
321
		}
322

R
Rob Lourens 已提交
323
		return undefined;
324 325
	}

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

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

			// More than one editor
B
Benjamin Pasero 已提交
336
			if (this.mru.length > 1) {
337 338 339
				let newActive: EditorInput;
				if (this.closeTabsInMRUOrder) {
					newActive = this.mru[1]; // active editor is always first in MRU, so pick second editor after as new active
B
Benjamin Pasero 已提交
340
				} else {
341 342
					if (index === this.editors.length - 1) {
						newActive = this.editors[index - 1]; // last editor is closed, pick previous as new active
B
Benjamin Pasero 已提交
343
					} else {
344 345 346
						newActive = this.editors[index + 1]; // pick next editor as new active
					}
				}
B
Benjamin Pasero 已提交
347

348
				this.setActive(newActive);
B
Benjamin Pasero 已提交
349 350 351 352 353 354 355 356 357
			}

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

		// Preview Editor closed
B
Benjamin Pasero 已提交
358
		if (this.matches(this.preview, editor)) {
B
Benjamin Pasero 已提交
359 360 361
			this.preview = null;
		}

B
Benjamin Pasero 已提交
362
		// Remove from arrays
B
Benjamin Pasero 已提交
363
		this.splice(index, true);
B
Benjamin Pasero 已提交
364 365

		// Event
I
isidor 已提交
366
		return { editor, replaced, index, groupId: this.id };
B
Benjamin Pasero 已提交
367 368
	}

B
Benjamin Pasero 已提交
369
	closeEditors(except: EditorInput, direction?: CloseDirection): void {
B
Benjamin Pasero 已提交
370 371 372 373 374 375
		const index = this.indexOf(except);
		if (index === -1) {
			return; // not found
		}

		// Close to the left
B
Benjamin Pasero 已提交
376
		if (direction === CloseDirection.LEFT) {
B
Benjamin Pasero 已提交
377 378 379 380 381 382
			for (let i = index - 1; i >= 0; i--) {
				this.closeEditor(this.editors[i]);
			}
		}

		// Close to the right
B
Benjamin Pasero 已提交
383
		else if (direction === CloseDirection.RIGHT) {
B
Benjamin Pasero 已提交
384 385 386 387 388 389 390
			for (let i = this.editors.length - 1; i > index; i--) {
				this.closeEditor(this.editors[i]);
			}
		}

		// Both directions
		else {
B
Benjamin Pasero 已提交
391
			this.mru.filter(e => !this.matches(e, except)).forEach(e => this.closeEditor(e));
B
Benjamin Pasero 已提交
392 393 394
		}
	}

395
	closeAllEditors(): void {
396 397

		// Optimize: close all non active editors first to produce less upstream work
B
Benjamin Pasero 已提交
398
		this.mru.filter(e => !this.matches(e, this.active)).forEach(e => this.closeEditor(e));
M
Matt Bierner 已提交
399 400 401
		if (this.active) {
			this.closeEditor(this.active);
		}
402 403
	}

404
	moveEditor(editor: EditorInput, toIndex: number): void {
B
Benjamin Pasero 已提交
405 406 407 408 409 410 411 412 413 414
		const index = this.indexOf(editor);
		if (index < 0) {
			return;
		}

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

		// Event
415
		this._onDidEditorMove.fire(editor);
B
Benjamin Pasero 已提交
416 417
	}

418
	setActive(editor: EditorInput): void {
B
Benjamin Pasero 已提交
419 420 421 422 423
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}

B
Benjamin Pasero 已提交
424
		if (this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
425 426 427 428 429
			return; // already active
		}

		this.active = editor;

B
Benjamin Pasero 已提交
430
		// Bring to front in MRU list
B
Benjamin Pasero 已提交
431 432
		this.setMostRecentlyUsed(editor);

B
Benjamin Pasero 已提交
433
		// Event
434
		this._onDidEditorActivate.fire(editor);
B
Benjamin Pasero 已提交
435 436
	}

437
	pin(editor: EditorInput): void {
438 439 440 441 442
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}

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

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

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

454
	unpin(editor: EditorInput): void {
455 456 457 458
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}
459

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
504
		const args: any[] = [index, del ? 1 : 0];
505 506 507
		if (editor) {
			args.push(editor);
		}
B
Benjamin Pasero 已提交
508

509
		// Perform on editors array
510
		this.editors.splice.apply(this.editors, args);
B
Benjamin Pasero 已提交
511

512
		// Add
B
Benjamin Pasero 已提交
513
		if (!del && editor) {
514 515
			this.mru.push(editor); // make it LRU editor
			this.updateResourceMap(editor, false /* add */); // add new to resource map
B
Benjamin Pasero 已提交
516 517
		}

B
Benjamin Pasero 已提交
518
		// Remove / Replace
B
Benjamin Pasero 已提交
519
		else {
B
Benjamin Pasero 已提交
520 521
			const indexInMRU = this.indexOf(editorToDeleteOrReplace, this.mru);

522
			// Remove
B
Benjamin Pasero 已提交
523
			if (del && !editor) {
524 525
				this.mru.splice(indexInMRU, 1); // remove from MRU
				this.updateResourceMap(editorToDeleteOrReplace, true /* delete */); // remove from resource map
B
Benjamin Pasero 已提交
526 527
			}

528
			// Replace
B
Benjamin Pasero 已提交
529
			else if (del && editor) {
530 531 532
				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 已提交
533
			}
B
Benjamin Pasero 已提交
534 535 536
		}
	}

537
	private updateResourceMap(editor: EditorInput, remove: boolean): void {
538
		const resource = toResource(editor, { supportSideBySide: true });
539
		if (resource) {
540 541 542

			// 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 已提交
543 544 545
			const counter = this.mapResourceToEditorCount.get(resource) || 0;

			// Add
B
Benjamin Pasero 已提交
546
			let newCounter: number;
B
Benjamin Pasero 已提交
547
			if (!remove) {
548 549 550
				newCounter = counter + 1;
			}

B
Benjamin Pasero 已提交
551 552 553 554 555 556 557 558 559 560
			// Delete
			else {
				newCounter = counter - 1;
			}

			if (newCounter > 0) {
				this.mapResourceToEditorCount.set(resource, newCounter);
			} else {
				this.mapResourceToEditorCount.delete(resource);
			}
561 562 563
		}
	}

M
Matt Bierner 已提交
564
	indexOf(candidate: EditorInput | null, editors = this.editors): number {
B
Benjamin Pasero 已提交
565 566
		if (!candidate) {
			return -1;
B
Benjamin Pasero 已提交
567 568
		}

B
Benjamin Pasero 已提交
569
		for (let i = 0; i < editors.length; i++) {
B
Benjamin Pasero 已提交
570
			if (this.matches(editors[i], candidate)) {
B
Benjamin Pasero 已提交
571 572 573 574 575 576
				return i;
			}
		}

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

578 579 580
	contains(editorOrResource: EditorInput | URI): boolean;
	contains(editor: EditorInput, supportSideBySide?: boolean): boolean;
	contains(editorOrResource: EditorInput | URI, supportSideBySide?: boolean): boolean {
581
		if (editorOrResource instanceof EditorInput) {
582 583 584 585 586 587 588 589 590 591 592 593 594
			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;
595 596
		}

597
		const counter = this.mapResourceToEditorCount.get(editorOrResource);
598 599

		return typeof counter === 'number' && counter > 0;
600 601
	}

B
Benjamin Pasero 已提交
602 603 604 605 606 607 608 609 610
	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 已提交
611
		this.mru.splice(mruIndex, 1);
B
Benjamin Pasero 已提交
612

B
Benjamin Pasero 已提交
613
		// Set editor to front
B
Benjamin Pasero 已提交
614 615
		this.mru.unshift(editor);
	}
B
Benjamin Pasero 已提交
616

M
Matt Bierner 已提交
617
	private matches(editorA: EditorInput | null, editorB: EditorInput | null): boolean {
B
Benjamin Pasero 已提交
618 619
		return !!editorA && !!editorB && editorA.matches(editorB);
	}
620

621
	clone(): EditorGroup {
R
Rob Lourens 已提交
622
		const group = this.instantiationService.createInstance(EditorGroup, undefined);
623 624 625 626 627 628 629 630 631 632 633
		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 {
634
		const registry = Registry.as<IEditorInputFactoryRegistry>(Extensions.EditorInputFactories);
635 636 637 638 639 640

		// 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 已提交
641
		let serializablePreviewIndex: number | undefined;
642
		this.editors.forEach(e => {
643
			let factory = registry.getEditorInputFactory(e.getTypeId());
644 645 646
			if (factory) {
				let value = factory.serialize(e);
				if (typeof value === 'string') {
647
					serializedEditors.push({ id: e.getTypeId(), value });
648
					serializableEditors.push(e);
B
Benjamin Pasero 已提交
649 650 651 652

					if (this.preview === e) {
						serializablePreviewIndex = serializableEditors.length - 1;
					}
653 654 655 656
				}
			}
		});

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

		return {
660
			id: this.id,
661 662
			editors: serializedEditors,
			mru: serializableMru,
B
Benjamin Pasero 已提交
663
			preview: serializablePreviewIndex,
664 665 666 667
		};
	}

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

670 671 672 673 674 675 676 677
		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 已提交
678
		this.editors = coalesce(data.editors.map(e => {
B
Benjamin Pasero 已提交
679 680 681
			const factory = registry.getEditorInputFactory(e.id);
			if (factory) {
				const editor = factory.deserialize(this.instantiationService, e.value);
682

B
Benjamin Pasero 已提交
683
				this.registerEditorListeners(editor);
B
Benjamin Pasero 已提交
684
				this.updateResourceMap(editor, false /* add */);
685

B
Benjamin Pasero 已提交
686 687 688 689
				return editor;
			}

			return null;
M
Matt Bierner 已提交
690
		}));
691 692
		this.mru = data.mru.map(i => this.editors[i]);
		this.active = this.mru[0];
M
Matt Bierner 已提交
693 694 695
		if (typeof data.preview === 'number') {
			this.preview = this.editors[data.preview];
		}
696 697
	}
}