editorGroup.ts 19.3 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, IEditorInput, SideBySideEditor } 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, Disposable, DisposableStore } 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
export function isSerializedEditorGroup(obj?: any): obj is ISerializedEditorGroup {
51
	const group: ISerializedEditorGroup = obj;
52 53 54 55

	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
	//#region events

	private readonly _onDidEditorActivate = this._register(new Emitter<EditorInput>());
63
	readonly onDidEditorActivate: Event<EditorInput> = this._onDidEditorActivate.event;
64 65

	private readonly _onDidEditorOpen = this._register(new Emitter<EditorInput>());
66
	readonly onDidEditorOpen: Event<EditorInput> = this._onDidEditorOpen.event;
67 68

	private readonly _onDidEditorClose = this._register(new Emitter<EditorCloseEvent>());
69
	readonly onDidEditorClose: Event<EditorCloseEvent> = this._onDidEditorClose.event;
70 71

	private readonly _onDidEditorDispose = this._register(new Emitter<EditorInput>());
72
	readonly onDidEditorDispose: Event<EditorInput> = this._onDidEditorDispose.event;
73 74

	private readonly _onDidEditorBecomeDirty = this._register(new Emitter<EditorInput>());
75
	readonly onDidEditorBecomeDirty: Event<EditorInput> = this._onDidEditorBecomeDirty.event;
76 77

	private readonly _onDidEditorLabelChange = this._register(new Emitter<EditorInput>());
78
	readonly onDidEditorLabelChange: Event<EditorInput> = this._onDidEditorLabelChange.event;
79 80

	private readonly _onDidEditorMove = this._register(new Emitter<EditorInput>());
81
	readonly onDidEditorMove: Event<EditorInput> = this._onDidEditorMove.event;
82 83

	private readonly _onDidEditorPin = this._register(new Emitter<EditorInput>());
84
	readonly onDidEditorPin: Event<EditorInput> = this._onDidEditorPin.event;
85 86

	private readonly _onDidEditorUnpin = this._register(new Emitter<EditorInput>());
87
	readonly onDidEditorUnpin: Event<EditorInput> = this._onDidEditorUnpin.event;
88 89

	//#endregion
B
Benjamin Pasero 已提交
90

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

94 95 96
	private editors: EditorInput[] = [];
	private mru: EditorInput[] = [];
	private mapResourceToEditorCount: ResourceMap<number> = new ResourceMap<number>();
97

B
Benjamin Pasero 已提交
98 99
	private preview: EditorInput | null = null; // editor in preview state
	private active: EditorInput | null = null;  // editor in active state
100

B
Benjamin Pasero 已提交
101 102
	private editorOpenPositioning: ('left' | 'right' | 'first' | 'last') | undefined;
	private focusRecentEditorAfterClose: boolean | undefined;
103

104
	constructor(
105
		labelOrSerializedGroup: ISerializedEditorGroup | undefined,
106 107
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@IConfigurationService private readonly configurationService: IConfigurationService
108
	) {
109 110
		super();

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

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

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

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

130
	get count(): number {
131 132 133
		return this.editors.length;
	}

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

138 139 140
	getEditor(index: number): EditorInput | undefined;
	getEditor(resource: URI): EditorInput | undefined;
	getEditor(arg1: number | URI): EditorInput | undefined {
B
Benjamin Pasero 已提交
141 142 143 144 145 146
		if (typeof arg1 === 'number') {
			return this.editors[arg1];
		}

		const resource: URI = arg1;
		if (!this.contains(resource)) {
147
			return undefined; // fast check for resource opened or not
B
Benjamin Pasero 已提交
148 149
		}

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

157
		return undefined;
158 159
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		// Existing editor
		else {

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

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

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

B
Benjamin Pasero 已提交
269
	private registerEditorListeners(editor: EditorInput): void {
270
		const listeners = new DisposableStore();
271 272

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

		// Re-Emit dirty state changes
281
		listeners.add(editor.onDidChangeDirty(() => {
282
			this._onDidEditorBecomeDirty.fire(editor);
283
		}));
284

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

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

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

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

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

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

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

			return event.index;
318
		}
319

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

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

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

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

345
				this.setActive(newActive);
B
Benjamin Pasero 已提交
346 347 348 349 350 351 352 353 354
			}

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

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

B
Benjamin Pasero 已提交
359
		// Remove from arrays
B
Benjamin Pasero 已提交
360
		this.splice(index, true);
B
Benjamin Pasero 已提交
361 362

		// Event
I
isidor 已提交
363
		return { editor, replaced, index, groupId: this.id };
B
Benjamin Pasero 已提交
364 365
	}

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

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

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

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

392
	closeAllEditors(): void {
393 394

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

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

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

		// Event
412
		this._onDidEditorMove.fire(editor);
B
Benjamin Pasero 已提交
413 414
	}

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

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

		this.active = editor;

B
Benjamin Pasero 已提交
427
		// Bring to front in MRU list
B
Benjamin Pasero 已提交
428 429
		this.setMostRecentlyUsed(editor);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

501
		const args: (number | EditorInput)[] = [index, del ? 1 : 0];
502 503 504
		if (editor) {
			args.push(editor);
		}
B
Benjamin Pasero 已提交
505

506
		// Perform on editors array
507
		this.editors.splice.apply(this.editors, args);
B
Benjamin Pasero 已提交
508

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

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

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

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

534
	private updateResourceMap(editor: EditorInput, remove: boolean): void {
B
Benjamin Pasero 已提交
535
		const resource = toResource(editor, { supportSideBySide: SideBySideEditor.MASTER });
536
		if (resource) {
537 538 539

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

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

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

			if (newCounter > 0) {
				this.mapResourceToEditorCount.set(resource, newCounter);
			} else {
				this.mapResourceToEditorCount.delete(resource);
			}
558 559 560
		}
	}

561
	indexOf(candidate: IEditorInput | null, editors = this.editors): number {
B
Benjamin Pasero 已提交
562 563
		if (!candidate) {
			return -1;
B
Benjamin Pasero 已提交
564 565
		}

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

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

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

594
		const counter = this.mapResourceToEditorCount.get(editorOrResource);
595 596

		return typeof counter === 'number' && counter > 0;
597 598
	}

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

B
Benjamin Pasero 已提交
610
		// Set editor to front
B
Benjamin Pasero 已提交
611 612
		this.mru.unshift(editor);
	}
B
Benjamin Pasero 已提交
613

614
	private matches(editorA: IEditorInput | null, editorB: IEditorInput | null): boolean {
B
Benjamin Pasero 已提交
615 616
		return !!editorA && !!editorB && editorA.matches(editorB);
	}
617

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

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

					if (this.preview === e) {
						serializablePreviewIndex = serializableEditors.length - 1;
					}
650 651 652 653
				}
			}
		});

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

		return {
657
			id: this.id,
658 659
			editors: serializedEditors,
			mru: serializableMru,
B
Benjamin Pasero 已提交
660
			preview: serializablePreviewIndex,
661 662 663
		};
	}

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

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

B
Benjamin Pasero 已提交
684 685 686 687
				return editor;
			}

			return null;
M
Matt Bierner 已提交
688
		}));
B
Benjamin Pasero 已提交
689

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

692
		this.active = this.mru[0];
B
Benjamin Pasero 已提交
693

M
Matt Bierner 已提交
694 695 696
		if (typeof data.preview === 'number') {
			this.preview = this.editors[data.preview];
		}
B
Benjamin Pasero 已提交
697 698

		return this._id;
699 700
	}
}