editorGroup.ts 36.8 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

M
Matt Bierner 已提交
8
import { Event, Emitter, once } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
9
import { Extensions, IEditorInputFactoryRegistry, EditorInput, toResource, IEditorGroup, IEditorIdentifier, IEditorCloseEvent, GroupIdentifier, SideBySideEditorInput, CloseDirection } from 'vs/workbench/common/editor';
10
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
11
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
12
import { IConfigurationService, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration';
13
import { dispose, IDisposable, Disposable } from 'vs/base/common/lifecycle';
14
import { Registry } from 'vs/platform/registry/common/platform';
B
wip  
Benjamin Pasero 已提交
15
import { ResourceMap } from 'vs/base/common/map';
B
Benjamin Pasero 已提交
16

B
Benjamin Pasero 已提交
17 18 19 20 21 22 23 24 25
const EditorOpenPositioning = {
	LEFT: 'left',
	RIGHT: 'right',
	FIRST: 'first',
	LAST: 'last'
};

const OPEN_POSITIONING_CONFIG = 'workbench.editor.openPositioning';

B
Benjamin Pasero 已提交
26
export interface EditorCloseEvent extends IEditorCloseEvent {
27 28 29
	editor: EditorInput;
}

30
export interface EditorIdentifier extends IEditorIdentifier {
I
isidor 已提交
31
	groupId: GroupIdentifier;
32
	editor: EditorInput;
33 34
}

B
Benjamin Pasero 已提交
35 36 37
export interface IEditorOpenOptions {
	pinned?: boolean;
	active?: boolean;
B
Benjamin Pasero 已提交
38
	index?: number;
B
Benjamin Pasero 已提交
39 40
}

41
export interface ISerializedEditorInput {
42 43 44 45
	id: string;
	value: string;
}

46
export interface ISerializedEditorGroup {
47
	id: number;
48 49 50 51 52
	editors: ISerializedEditorInput[];
	mru: number[];
	preview: number;
}

53 54 55 56 57 58
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);
}

59
export class EditorGroup extends Disposable implements IEditorGroup {
60 61 62

	private static IDS = 0;

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 90 91 92
	//#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 已提交
93

94 95
	private _id: GroupIdentifier;

96 97 98
	private editors: EditorInput[] = [];
	private mru: EditorInput[] = [];
	private mapResourceToEditorCount: ResourceMap<number> = new ResourceMap<number>();
99 100 101 102 103 104

	private preview: EditorInput; // editor in preview state
	private active: EditorInput;  // editor in active state

	private editorOpenPositioning: 'left' | 'right' | 'first' | 'last';

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

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

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

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

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

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

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

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

142 143 144
	getEditor(index: number): EditorInput;
	getEditor(resource: URI): EditorInput;
	getEditor(arg1: any): EditorInput {
B
Benjamin Pasero 已提交
145 146 147 148 149 150 151 152 153 154 155
		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
		}

		for (let i = 0; i < this.editors.length; i++) {
			const editor = this.editors[i];
156
			const editorResource = toResource(editor, { supportSideBySide: true });
B
Benjamin Pasero 已提交
157
			if (editorResource && editorResource.toString() === resource.toString()) {
B
Benjamin Pasero 已提交
158
				return editor;
B
Benjamin Pasero 已提交
159 160 161 162
			}
		}

		return null;
163 164
	}

165
	get activeEditor(): EditorInput {
B
Benjamin Pasero 已提交
166 167 168
		return this.active;
	}

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

173
	get previewEditor(): EditorInput {
B
Benjamin Pasero 已提交
174 175 176
		return this.preview;
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

B
Benjamin Pasero 已提交
239 240 241
				this.preview = editor;
			}

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

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

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

		// Existing editor
		else {

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

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

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

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

		// Re-emit disposal of editor input as our own event
278 279
		const onceDispose = once(editor.onDispose);
		unbind.push(onceDispose(() => {
280
			if (this.indexOf(editor) >= 0) {
281
				this._onDidEditorDispose.fire(editor);
282
			}
283 284 285 286
		}));

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

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

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

303 304
	private replaceEditor(toReplace: EditorInput, replaceWidth: EditorInput, replaceIndex: number, openNext = true): void {
		const event = this.doCloseEditor(toReplace, openNext, true); // optimization to prevent multiple setActive() in one call
305 306 307 308 309 310 311

		// 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.
		this.splice(replaceIndex, false, replaceWidth);

		if (event) {
312
			this._onDidEditorClose.fire(event);
313 314 315
		}
	}

316
	closeEditor(editor: EditorInput, openNext = true): number {
317
		const event = this.doCloseEditor(editor, openNext, false);
318 319

		if (event) {
320
			this._onDidEditorClose.fire(event);
321 322

			return event.index;
323
		}
324 325

		return void 0;
326 327
	}

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

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

			// More than one editor
B
Benjamin Pasero 已提交
338 339
			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 已提交
340 341 342 343 344 345 346 347 348
			}

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

		// Preview Editor closed
B
Benjamin Pasero 已提交
349
		if (this.matches(this.preview, editor)) {
B
Benjamin Pasero 已提交
350 351 352
			this.preview = null;
		}

B
Benjamin Pasero 已提交
353
		// Remove from arrays
B
Benjamin Pasero 已提交
354
		this.splice(index, true);
B
Benjamin Pasero 已提交
355 356

		// Event
I
isidor 已提交
357
		return { editor, replaced, index, groupId: this.id };
B
Benjamin Pasero 已提交
358 359
	}

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

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

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

		// Both directions
		else {
B
Benjamin Pasero 已提交
382
			this.mru.filter(e => !this.matches(e, except)).forEach(e => this.closeEditor(e));
B
Benjamin Pasero 已提交
383 384 385
		}
	}

386
	closeAllEditors(): void {
387 388

		// Optimize: close all non active editors first to produce less upstream work
B
Benjamin Pasero 已提交
389
		this.mru.filter(e => !this.matches(e, this.active)).forEach(e => this.closeEditor(e));
390 391 392
		this.closeEditor(this.active);
	}

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

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

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

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

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

		this.active = editor;

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

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

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

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

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

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

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

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

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

		// Event
458
		this._onDidEditorUnpin.fire(editor);
B
Benjamin Pasero 已提交
459 460 461 462 463

		// Close old preview editor if any
		this.closeEditor(oldPreview);
	}

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 {
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
wip  
Benjamin Pasero 已提交
530
			let counter = this.mapResourceToEditorCount.get(resource) || 0;
B
Benjamin Pasero 已提交
531
			let newCounter: number;
532 533 534 535 536 537 538 539
			if (remove) {
				if (counter > 1) {
					newCounter = counter - 1;
				}
			} else {
				newCounter = counter + 1;
			}

B
wip  
Benjamin Pasero 已提交
540
			this.mapResourceToEditorCount.set(resource, newCounter);
541 542 543
		}
	}

544
	indexOf(candidate: EditorInput, editors = this.editors): number {
B
Benjamin Pasero 已提交
545 546
		if (!candidate) {
			return -1;
B
Benjamin Pasero 已提交
547 548
		}

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

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

558 559 560
	contains(editorOrResource: EditorInput | URI): boolean;
	contains(editor: EditorInput, supportSideBySide?: boolean): boolean;
	contains(editorOrResource: EditorInput | URI, supportSideBySide?: boolean): boolean {
561
		if (editorOrResource instanceof EditorInput) {
562 563 564 565 566 567 568 569 570 571 572 573 574
			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;
575 576
		}

577
		const counter = this.mapResourceToEditorCount.get(editorOrResource);
578 579

		return typeof counter === 'number' && counter > 0;
580 581
	}

B
Benjamin Pasero 已提交
582 583 584 585 586 587 588 589 590
	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 已提交
591
		this.mru.splice(mruIndex, 1);
B
Benjamin Pasero 已提交
592

B
Benjamin Pasero 已提交
593
		// Set editor to front
B
Benjamin Pasero 已提交
594 595
		this.mru.unshift(editor);
	}
B
Benjamin Pasero 已提交
596 597 598 599

	private matches(editorA: EditorInput, editorB: EditorInput): boolean {
		return !!editorA && !!editorB && editorA.matches(editorB);
	}
600

601
	clone(): EditorGroup {
B
Benjamin Pasero 已提交
602
		const group = this.instantiationService.createInstance(EditorGroup, void 0);
603 604 605 606 607 608 609 610 611 612 613
		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 {
614
		const registry = Registry.as<IEditorInputFactoryRegistry>(Extensions.EditorInputFactories);
615 616 617 618 619 620

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

					if (this.preview === e) {
						serializablePreviewIndex = serializableEditors.length - 1;
					}
633 634 635 636
				}
			}
		});

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

		return {
640
			id: this.id,
641 642
			editors: serializedEditors,
			mru: serializableMru,
B
Benjamin Pasero 已提交
643
			preview: serializablePreviewIndex,
644 645 646 647
		};
	}

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

650 651 652 653 654 655 656 657
		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
		}

658
		this.editors = data.editors.map(e => {
B
Benjamin Pasero 已提交
659 660 661
			const factory = registry.getEditorInputFactory(e.id);
			if (factory) {
				const editor = factory.deserialize(this.instantiationService, e.value);
662

B
Benjamin Pasero 已提交
663
				this.registerEditorListeners(editor);
B
Benjamin Pasero 已提交
664
				this.updateResourceMap(editor, false /* add */);
665

B
Benjamin Pasero 已提交
666 667 668 669 670
				return editor;
			}

			return null;
		}).filter(e => !!e);
671 672 673 674 675 676
		this.mru = data.mru.map(i => this.editors[i]);
		this.active = this.mru[0];
		this.preview = this.editors[data.preview];
	}
}

B
Benjamin Pasero 已提交
677
// //#region TODO@grid legacy stacks model
678

B
Benjamin Pasero 已提交
679 680 681 682 683 684
// interface ISerializedEditorStacksModel {
// 	groups: ISerializedEditorGroup[];
// 	active: number;
// }

// export class EditorStacksModel implements IEditorStacksModel {
685

B
Benjamin Pasero 已提交
686 687 688 689
// 	private static readonly STORAGE_KEY = 'editorStacks.model';

// 	private toDispose: IDisposable[];
// 	private loaded: boolean;
690

B
Benjamin Pasero 已提交
691 692 693
// 	private _groups: EditorGroup[];
// 	private _activeGroup: EditorGroup;
// 	private groupToIdentifier: { [id: number]: EditorGroup };
694

B
Benjamin Pasero 已提交
695 696 697 698 699 700
// 	private readonly _onGroupOpened: Emitter<EditorGroup>;
// 	private readonly _onGroupClosed: Emitter<EditorGroup>;
// 	private readonly _onGroupMoved: Emitter<EditorGroup>;
// 	private readonly _onGroupActivated: Emitter<EditorGroup>;
// 	private readonly _onGroupDeactivated: Emitter<EditorGroup>;
// 	private readonly _onGroupRenamed: Emitter<EditorGroup>;
701

B
Benjamin Pasero 已提交
702 703 704 705
// 	private readonly _onEditorDisposed: Emitter<EditorIdentifier>;
// 	private readonly _onEditorDirty: Emitter<EditorIdentifier>;
// 	private readonly _onEditorLabelChange: Emitter<EditorIdentifier>;
// 	private readonly _onEditorOpened: Emitter<EditorIdentifier>;
706

B
Benjamin Pasero 已提交
707 708
// 	private readonly _onWillCloseEditor: Emitter<EditorCloseEvent>;
// 	private readonly _onEditorClosed: Emitter<EditorCloseEvent>;
709

B
Benjamin Pasero 已提交
710
// 	private readonly _onModelChanged: Emitter<IStacksModelChangeEvent>;
711

B
Benjamin Pasero 已提交
712 713 714 715 716 717 718
// 	constructor(
// 		private restoreFromStorage: boolean,
// 		@IStorageService private storageService: IStorageService,
// 		@ILifecycleService private lifecycleService: ILifecycleService,
// 		@IInstantiationService private instantiationService: IInstantiationService
// 	) {
// 		this.toDispose = [];
719

B
Benjamin Pasero 已提交
720 721
// 		this._groups = [];
// 		this.groupToIdentifier = Object.create(null);
722

B
Benjamin Pasero 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735
// 		this._onGroupOpened = new Emitter<EditorGroup>();
// 		this._onGroupClosed = new Emitter<EditorGroup>();
// 		this._onGroupActivated = new Emitter<EditorGroup>();
// 		this._onGroupDeactivated = new Emitter<EditorGroup>();
// 		this._onGroupMoved = new Emitter<EditorGroup>();
// 		this._onGroupRenamed = new Emitter<EditorGroup>();
// 		this._onModelChanged = new Emitter<IStacksModelChangeEvent>();
// 		this._onEditorDisposed = new Emitter<EditorIdentifier>();
// 		this._onEditorDirty = new Emitter<EditorIdentifier>();
// 		this._onEditorLabelChange = new Emitter<EditorIdentifier>();
// 		this._onEditorOpened = new Emitter<EditorIdentifier>();
// 		this._onWillCloseEditor = new Emitter<EditorCloseEvent>();
// 		this._onEditorClosed = new Emitter<EditorCloseEvent>();
736

B
Benjamin Pasero 已提交
737
// 		this.toDispose.push(this._onGroupOpened, this._onGroupClosed, this._onGroupActivated, this._onGroupDeactivated, this._onGroupMoved, this._onGroupRenamed, this._onModelChanged, this._onEditorDisposed, this._onEditorDirty, this._onEditorLabelChange, this._onEditorOpened, this._onEditorClosed, this._onWillCloseEditor);
738

B
Benjamin Pasero 已提交
739 740
// 		this.registerListeners();
// 	}
741

B
Benjamin Pasero 已提交
742 743 744
// 	private registerListeners(): void {
// 		this.toDispose.push(this.lifecycleService.onShutdown(reason => this.onShutdown()));
// 	}
745

B
Benjamin Pasero 已提交
746 747 748
// 	get onGroupOpened(): Event<EditorGroup> {
// 		return this._onGroupOpened.event;
// 	}
B
Benjamin Pasero 已提交
749

B
Benjamin Pasero 已提交
750 751 752
// 	get onGroupClosed(): Event<EditorGroup> {
// 		return this._onGroupClosed.event;
// 	}
B
Benjamin Pasero 已提交
753

B
Benjamin Pasero 已提交
754 755 756
// 	get onGroupActivated(): Event<EditorGroup> {
// 		return this._onGroupActivated.event;
// 	}
757

B
Benjamin Pasero 已提交
758 759 760
// 	get onGroupDeactivated(): Event<EditorGroup> {
// 		return this._onGroupDeactivated.event;
// 	}
B
Benjamin Pasero 已提交
761

B
Benjamin Pasero 已提交
762 763 764
// 	get onGroupMoved(): Event<EditorGroup> {
// 		return this._onGroupMoved.event;
// 	}
B
Benjamin Pasero 已提交
765

B
Benjamin Pasero 已提交
766 767 768
// 	get onGroupRenamed(): Event<EditorGroup> {
// 		return this._onGroupRenamed.event;
// 	}
B
Benjamin Pasero 已提交
769

B
Benjamin Pasero 已提交
770 771 772
// 	get onModelChanged(): Event<IStacksModelChangeEvent> {
// 		return this._onModelChanged.event;
// 	}
B
Benjamin Pasero 已提交
773

B
Benjamin Pasero 已提交
774 775 776
// 	get onEditorDisposed(): Event<EditorIdentifier> {
// 		return this._onEditorDisposed.event;
// 	}
B
Benjamin Pasero 已提交
777

B
Benjamin Pasero 已提交
778 779 780
// 	get onEditorDirty(): Event<EditorIdentifier> {
// 		return this._onEditorDirty.event;
// 	}
B
Benjamin Pasero 已提交
781

B
Benjamin Pasero 已提交
782 783 784
// 	get onEditorLabelChange(): Event<EditorIdentifier> {
// 		return this._onEditorLabelChange.event;
// 	}
B
Benjamin Pasero 已提交
785

B
Benjamin Pasero 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
// 	get onEditorOpened(): Event<EditorIdentifier> {
// 		return this._onEditorOpened.event;
// 	}

// 	get onWillCloseEditor(): Event<EditorCloseEvent> {
// 		return this._onWillCloseEditor.event;
// 	}

// 	get onEditorClosed(): Event<EditorCloseEvent> {
// 		return this._onEditorClosed.event;
// 	}

// 	get groups(): EditorGroup[] {
// 		this.ensureLoaded();

// 		return this._groups.slice(0);
// 	}

// 	get activeGroup(): EditorGroup {
// 		this.ensureLoaded();
B
Benjamin Pasero 已提交
806

B
Benjamin Pasero 已提交
807 808 809 810 811 812 813 814 815 816 817 818
// 		return this._activeGroup;
// 	}

// 	isActive(group: EditorGroup): boolean {
// 		return this.activeGroup === group;
// 	}

// 	getGroup(id: GroupIdentifier): EditorGroup {
// 		this.ensureLoaded();

// 		return this.groupToIdentifier[id];
// 	}
819

B
Benjamin Pasero 已提交
820 821
// 	openGroup(label: string, activate = true, index?: number): EditorGroup {
// 		this.ensureLoaded();
822

B
Benjamin Pasero 已提交
823 824 825 826 827 828
// 		const group = this.doCreateGroup(label);

// 		// Direct index provided
// 		if (typeof index === 'number') {
// 			this._groups[index] = group;
// 		}
829

B
Benjamin Pasero 已提交
830 831 832 833
// 		// First group
// 		else if (!this._activeGroup) {
// 			this._groups.push(group);
// 		}
834

B
Benjamin Pasero 已提交
835 836 837 838
// 		// Subsequent group (open to the right of active one)
// 		else {
// 			this._groups.splice(this.indexOf(this._activeGroup) + 1, 0, group);
// 		}
839

B
Benjamin Pasero 已提交
840 841
// 		// Event
// 		this.fireEvent(this._onGroupOpened, group, true);
842

B
Benjamin Pasero 已提交
843 844 845 846
// 		// Activate if we are first or set to activate groups
// 		if (!this._activeGroup || activate) {
// 			this.setActive(group);
// 		}
847

B
Benjamin Pasero 已提交
848 849
// 		return group;
// 	}
850

B
Benjamin Pasero 已提交
851 852
// 	renameGroup(group: EditorGroup, label: string): void {
// 		this.ensureLoaded();
853

B
Benjamin Pasero 已提交
854 855
// 		this.fireEvent(this._onGroupRenamed, group, false);
// 	}
856

B
Benjamin Pasero 已提交
857 858
// 	closeGroup(group: EditorGroup): void {
// 		this.ensureLoaded();
859

B
Benjamin Pasero 已提交
860 861 862 863
// 		const index = this.indexOf(group);
// 		if (index < 0) {
// 			return; // group does not exist
// 		}
864

B
Benjamin Pasero 已提交
865 866
// 		// Active group closed: Find a new active one to the right
// 		if (group === this._activeGroup) {
B
Benjamin Pasero 已提交
867

B
Benjamin Pasero 已提交
868 869 870 871 872 873 874 875
// 			// More than one group
// 			if (this._groups.length > 1) {
// 				let newActiveGroup: EditorGroup;
// 				if (this._groups.length > index + 1) {
// 					newActiveGroup = this._groups[index + 1]; // make next group to the right active
// 				} else {
// 					newActiveGroup = this._groups[index - 1]; // make next group to the left active
// 				}
876

B
Benjamin Pasero 已提交
877 878
// 				this.setActive(newActiveGroup);
// 			}
B
Benjamin Pasero 已提交
879

B
Benjamin Pasero 已提交
880 881 882 883 884
// 			// One group
// 			else {
// 				this._activeGroup = null;
// 			}
// 		}
885

B
Benjamin Pasero 已提交
886 887 888
// 		// Close Editors in Group first and dispose then
// 		group.closeAllEditors();
// 		group.dispose();
889

B
Benjamin Pasero 已提交
890 891 892
// 		// Splice from groups
// 		this._groups.splice(index, 1);
// 		this.groupToIdentifier[group.id] = void 0;
893

B
Benjamin Pasero 已提交
894 895 896 897 898 899
// 		// Events
// 		this.fireEvent(this._onGroupClosed, group, true);
// 		for (let i = index; i < this._groups.length; i++) {
// 			this.fireEvent(this._onGroupMoved, this._groups[i], true); // send move event for groups to the right that moved to the left into the closed group position
// 		}
// 	}
900

B
Benjamin Pasero 已提交
901 902
// 	closeGroups(except?: EditorGroup): void {
// 		this.ensureLoaded();
903

B
Benjamin Pasero 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
// 		// Optimize: close all non active groups first to produce less upstream work
// 		this.groups.filter(g => g !== this._activeGroup && g !== except).forEach(g => this.closeGroup(g));

// 		// Close active unless configured to skip
// 		if (this._activeGroup !== except) {
// 			this.closeGroup(this._activeGroup);
// 		}
// 	}

// 	setActive(group: EditorGroup): void {
// 		this.ensureLoaded();

// 		if (this._activeGroup === group) {
// 			return;
// 		}

// 		const oldActiveGroup = this._activeGroup;
// 		this._activeGroup = group;

// 		this.fireEvent(this._onGroupActivated, group, false);
// 		if (oldActiveGroup) {
// 			this.fireEvent(this._onGroupDeactivated, oldActiveGroup, false);
// 		}
// 	}

// 	moveGroup(group: EditorGroup, toIndex: number): void {
// 		this.ensureLoaded();

// 		const index = this.indexOf(group);
// 		if (index < 0) {
// 			return;
// 		}

// 		// Move
// 		this._groups.splice(index, 1);
// 		this._groups.splice(toIndex, 0, group);

// 		// Event
// 		for (let i = Math.min(index, toIndex); i <= Math.max(index, toIndex) && i < this._groups.length; i++) {
// 			this.fireEvent(this._onGroupMoved, this._groups[i], true); // send move event for groups to the right that moved to the left into the closed group position
// 		}
// 	}

// 	private indexOf(group: EditorGroup): number {
// 		return this._groups.indexOf(group);
// 	}

// 	findGroup(editor: EditorInput, activeOnly?: boolean): EditorGroup {
// 		const groupsToCheck = (this.activeGroup ? [this.activeGroup] : []).concat(this.groups.filter(g => g !== this.activeGroup));

// 		for (let i = 0; i < groupsToCheck.length; i++) {
// 			const group = groupsToCheck[i];
// 			const editorsToCheck = (group.activeEditor ? [group.activeEditor] : []).concat(group.getEditors().filter(e => e !== group.activeEditor));

// 			for (let j = 0; j < editorsToCheck.length; j++) {
// 				const editorToCheck = editorsToCheck[j];

// 				if ((!activeOnly || group.isActive(editorToCheck)) && editor.matches(editorToCheck)) {
// 					return group;
// 				}
// 			}
// 		}

// 		return void 0;
// 	}

// 	positionOfGroup(group: IEditorGroup): Position;
// 	positionOfGroup(group: EditorGroup): Position;
// 	positionOfGroup(group: EditorGroup): Position {
// 		return this.indexOf(group);
// 	}

// 	groupAt(position: Position): EditorGroup {
// 		this.ensureLoaded();

// 		return this._groups[position];
// 	}

// 	next(jumpGroups: boolean, cycleAtEnd = true): IEditorIdentifier {
// 		this.ensureLoaded();

// 		if (!this.activeGroup) {
// 			return null;
// 		}

// 		const index = this.activeGroup.indexOf(this.activeGroup.activeEditor);

// 		// Return next in group
// 		if (index + 1 < this.activeGroup.count) {
// 			return { group: this.activeGroup.id, editor: this.activeGroup.getEditor(index + 1) };
// 		}

// 		// Return first if we are not jumping groups
// 		if (!jumpGroups) {
// 			if (!cycleAtEnd) {
// 				return null;
// 			}
// 			return { group: this.activeGroup.id, editor: this.activeGroup.getEditor(0) };
// 		}

// 		// Return first in next group
// 		const indexOfGroup = this.indexOf(this.activeGroup);
// 		const nextGroup = this.groups[indexOfGroup + 1];
// 		if (nextGroup) {
// 			return { group: nextGroup.id, editor: nextGroup.getEditor(0) };
// 		}

// 		// Return null if we are not cycling at the end
// 		if (!cycleAtEnd) {
// 			return null;
// 		}

// 		// Return first in first group
// 		const firstGroup = this.groups[0];
// 		return { group: firstGroup.id, editor: firstGroup.getEditor(0) };
// 	}

// 	previous(jumpGroups: boolean, cycleAtStart = true): IEditorIdentifier {
// 		this.ensureLoaded();

// 		if (!this.activeGroup) {
// 			return null;
// 		}

// 		const index = this.activeGroup.indexOf(this.activeGroup.activeEditor);

// 		// Return previous in group
// 		if (index > 0) {
// 			return { group: this.activeGroup.id, editor: this.activeGroup.getEditor(index - 1) };
// 		}

// 		// Return last if we are not jumping groups
// 		if (!jumpGroups) {
// 			if (!cycleAtStart) {
// 				return null;
// 			}
// 			return { group: this.activeGroup.id, editor: this.activeGroup.getEditor(this.activeGroup.count - 1) };
// 		}

// 		// Return last in previous group
// 		const indexOfGroup = this.indexOf(this.activeGroup);
// 		const previousGroup = this.groups[indexOfGroup - 1];
// 		if (previousGroup) {
// 			return { group: previousGroup.id, editor: previousGroup.getEditor(previousGroup.count - 1) };
// 		}

// 		// Return null if we are not cycling at the start
// 		if (!cycleAtStart) {
// 			return null;
// 		}

// 		// Return last in last group
// 		const lastGroup = this.groups[this.groups.length - 1];
// 		return { group: lastGroup.id, editor: lastGroup.getEditor(lastGroup.count - 1) };
// 	}

// 	last(): IEditorIdentifier {
// 		this.ensureLoaded();

// 		if (!this.activeGroup) {
// 			return null;
// 		}

// 		return { group: this.activeGroup.id, editor: this.activeGroup.getEditor(this.activeGroup.count - 1) };
// 	}

// 	private save(): void {
// 		const serialized = this.serialize();

// 		if (serialized.groups.length) {
// 			this.storageService.store(EditorStacksModel.STORAGE_KEY, JSON.stringify(serialized), StorageScope.WORKSPACE);
// 		} else {
// 			this.storageService.remove(EditorStacksModel.STORAGE_KEY, StorageScope.WORKSPACE);
// 		}
// 	}

// 	private serialize(): ISerializedEditorStacksModel {

// 		// Exclude now empty groups (can happen if an editor cannot be serialized)
// 		let serializableGroups = this._groups.map(g => g.serialize()).filter(g => g.editors.length > 0);

// 		// Only consider active index if we do not have empty groups
// 		let serializableActiveIndex: number;
// 		if (serializableGroups.length > 0) {
// 			if (serializableGroups.length === this._groups.length) {
// 				serializableActiveIndex = this.indexOf(this._activeGroup);
// 			} else {
// 				serializableActiveIndex = 0;
// 			}
// 		}

// 		return {
// 			groups: serializableGroups,
// 			active: serializableActiveIndex
// 		};
// 	}

// 	private fireEvent(emitter: Emitter<EditorGroup>, group: EditorGroup, isStructuralChange: boolean): void {
// 		emitter.fire(group);
// 		this._onModelChanged.fire({ group, structural: isStructuralChange });
// 	}

// 	private ensureLoaded(): void {
// 		if (!this.loaded) {
// 			this.loaded = true;
// 			this.load();
// 		}
// 	}

// 	private load(): void {
// 		if (!this.restoreFromStorage) {
// 			return; // do not load from last session if the user explicitly asks to open a set of files
// 		}

// 		const modelRaw = this.storageService.get(EditorStacksModel.STORAGE_KEY, StorageScope.WORKSPACE);
// 		if (modelRaw) {
// 			const serialized: ISerializedEditorStacksModel = JSON.parse(modelRaw);

// 			const invalidId = this.doValidate(serialized);
// 			if (invalidId) {
// 				console.warn(`Ignoring invalid stacks model (Error code: ${invalidId}): ${JSON.stringify(serialized)}`);
// 				console.warn(serialized);
// 				return;
// 			}

// 			this._groups = serialized.groups.map(s => this.doCreateGroup(s));
// 			this._activeGroup = this._groups[serialized.active];
// 		}
// 	}

// 	private doValidate(serialized: ISerializedEditorStacksModel): number {
// 		if (!serialized.groups.length && typeof serialized.active === 'number') {
// 			return 1; // Invalid active (we have no groups, but an active one)
// 		}

// 		if (serialized.groups.length && !serialized.groups[serialized.active]) {
// 			return 2; // Invalid active (we cannot find the active one in group)
// 		}

// 		if (serialized.groups.length > 3) {
// 			return 3; // Too many groups
// 		}

// 		if (serialized.groups.some(g => !g.editors.length)) {
// 			return 4; // Some empty groups
// 		}

// 		if (serialized.groups.some(g => g.editors.length !== g.mru.length)) {
// 			return 5; // MRU out of sync with editors
// 		}

// 		if (serialized.groups.some(g => typeof g.preview === 'number' && !g.editors[g.preview])) {
// 			return 6; // Invalid preview editor
// 		}

// 		return 0;
// 	}

// 	private doCreateGroup(arg1: string | ISerializedEditorGroup): EditorGroup {
// 		const group = this.instantiationService.createInstance(EditorGroup, arg1);

// 		this.groupToIdentifier[group.id] = group;

// 		// Funnel editor changes in the group through our event aggregator
// 		const unbind: IDisposable[] = [];
// 		unbind.push(group.onDidEditorClose(event => this._onModelChanged.fire({ group, editor: event.editor, structural: true })));
// 		unbind.push(group.onDidEditorOpen(editor => this._onModelChanged.fire({ group, editor: editor, structural: true })));
// 		unbind.push(group.onDidEditorMove(editor => this._onModelChanged.fire({ group, editor: editor, structural: true })));
// 		unbind.push(group.onDidEditorActivate(editor => this._onModelChanged.fire({ group, editor })));
// 		unbind.push(group.onDidEditorBecomeDirty(editor => this._onModelChanged.fire({ group, editor })));
// 		unbind.push(group.onDidEditorLabelChange(editor => this._onModelChanged.fire({ group, editor })));
// 		unbind.push(group.onDidEditorPin(editor => this._onModelChanged.fire({ group, editor })));
// 		unbind.push(group.onDidEditorUnpin(editor => this._onModelChanged.fire({ group, editor })));
// 		unbind.push(group.onDidEditorOpen(editor => this._onEditorOpened.fire({ editor, group: group.id })));
// 		unbind.push(group.onDidEditorClose(event => {
// 			this._onWillCloseEditor.fire(event);
// 			this.handleOnEditorClosed(event);
// 			this._onEditorClosed.fire(event);
// 		}));
// 		unbind.push(group.onDidEditorDispose(editor => this._onEditorDisposed.fire({ editor, group: group.id })));
// 		unbind.push(group.onDidEditorBecomeDirty(editor => this._onEditorDirty.fire({ editor, group: group.id })));
// 		unbind.push(group.onDidEditorLabelChange(editor => this._onEditorLabelChange.fire({ editor, group: group.id })));
// 		unbind.push(this.onGroupClosed(g => {
// 			if (g === group) {
// 				dispose(unbind);
// 			}
// 		}));

// 		return group;
// 	}

// 	private handleOnEditorClosed(event: EditorCloseEvent): void {
// 		const editor = event.editor;
// 		const editorsToClose = [editor];

// 		// Include both sides of side by side editors when being closed and not opened multiple times
// 		if (editor instanceof SideBySideEditorInput && !this.isOpen(editor)) {
// 			editorsToClose.push(editor.master, editor.details);
// 		}

// 		// Close the editor when it is no longer open in any group including diff editors
// 		editorsToClose.forEach(editorToClose => {
// 			const resource = editorToClose ? editorToClose.getResource() : void 0; // prefer resource to not close right-hand side editors of a diff editor
// 			if (!this.isOpen(resource || editorToClose)) {
// 				editorToClose.close();
// 			}
// 		});
// 	}

// 	isOpen(editorOrResource: URI | EditorInput): boolean {
// 		return this._groups.some(group => group.contains(editorOrResource));
// 	}

// 	count(editor: EditorInput): number {
// 		return this._groups.filter(group => group.contains(editor)).length;
// 	}

// 	private onShutdown(): void {
// 		this.save();

// 		dispose(this.toDispose);
// 	}

// 	validate(): void {
// 		const serialized = this.serialize();
// 		const invalidId = this.doValidate(serialized);
// 		if (invalidId) {
// 			console.warn(`Ignoring invalid stacks model (Error code: ${invalidId}): ${JSON.stringify(serialized)}`);
// 			console.warn(serialized);
// 		} else {
// 			console.log('Stacks Model OK!');
// 		}
// 	}

// 	toString(): string {
// 		this.ensureLoaded();

// 		const lines: string[] = [];

// 		if (!this.groups.length) {
// 			return '<No Groups>';
// 		}

// 		this.groups.forEach(g => {
// 			g.getEditors().forEach(e => {
// 				let label = `\t${e.getName()}`;

// 				if (g.previewEditor === e) {
// 					label = `${label} [preview]`;
// 				}

// 				if (g.activeEditor === e) {
// 					label = `${label} [active]`;
// 				}

// 				lines.push(label);
// 			});
// 		});

// 		return lines.join('\n');
// 	}
// }

I
isidor 已提交
1267
// //#endregion