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

'use strict';

import Event, {Emitter} from 'vs/base/common/event';
import {EditorInput} from 'vs/workbench/common/editor';
10 11 12 13 14 15
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {ILifecycleService} from 'vs/platform/lifecycle/common/lifecycle';
import {dispose, IDisposable} from 'vs/base/common/lifecycle';
import {IEditorRegistry, Extensions} from 'vs/workbench/browser/parts/editor/baseEditor';
import {Registry} from 'vs/platform/platform';
B
Benjamin Pasero 已提交
16

B
Benjamin Pasero 已提交
17
/// --- API-Start ----
B
Benjamin Pasero 已提交
18 19 20

export interface IEditorGroup {

21 22
	label: string;
	count: number;
B
Benjamin Pasero 已提交
23 24 25 26 27 28 29 30 31
	activeEditor: EditorInput;
	previewEditor: EditorInput;

	onEditorActivated: Event<EditorInput>;
	onEditorOpened: Event<EditorInput>;
	onEditorClosed: Event<EditorInput>;
	onEditorPinned: Event<EditorInput>;
	onEditorUnpinned: Event<EditorInput>;

B
Benjamin Pasero 已提交
32
	getEditors(mru?: boolean): EditorInput[];
B
Benjamin Pasero 已提交
33
	openEditor(editor: EditorInput, options?: IEditorOpenOptions): void;
34
	closeEditor(editor: EditorInput): void;
35
	closeAllEditors(): void;
B
Benjamin Pasero 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	setActive(editor: EditorInput): void;
	isActive(editor: EditorInput): boolean;
	isPreview(editor: EditorInput): boolean;
	isPinned(editor: EditorInput): boolean;

	pin(editor: EditorInput): void;
	unpin(editor: EditorInput): void;
}

export interface IEditorStacksModel {

	onGroupOpened: Event<IEditorGroup>;
	onGroupClosed: Event<IEditorGroup>;
	onGroupActivated: Event<IEditorGroup>;

	groups: IEditorGroup[];
	activeGroup: IEditorGroup;

	openGroup(label: string): IEditorGroup;
55

B
Benjamin Pasero 已提交
56
	closeGroup(group: IEditorGroup): void;
57 58
	closeAllGroups(): void;

B
Benjamin Pasero 已提交
59 60 61 62 63 64 65 66
	setActive(group: IEditorGroup): void;
}

export interface IEditorOpenOptions {
	pinned?: boolean;
	active?: boolean;
}

B
Benjamin Pasero 已提交
67 68
/// --- API-End ----

B
Benjamin Pasero 已提交
69 70 71 72 73 74 75 76 77 78
// Close Others
// Close Editors to the Right
// Move Editor
// Move Group

export enum Direction {
	LEFT,
	RIGHT
}

79
let CONFIG_OPEN_EDITOR_DIRECTION = Direction.RIGHT; // open new editors to the right of existing ones
80
export function setOpenEditorDirection(dir: Direction): void {
81 82 83 84 85 86
	CONFIG_OPEN_EDITOR_DIRECTION = dir;
}

let CONFIG_ALLOW_EMPTY_GROUPS = false; // setting to allow empty groups or not
export function setAllowEmptyGroups(allow: boolean): void {
	CONFIG_ALLOW_EMPTY_GROUPS = allow;
87
}
B
Benjamin Pasero 已提交
88

89
export interface ISerializedEditorInput {
90 91 92 93
	id: string;
	value: string;
}

94
export interface ISerializedEditorGroup {
95 96 97 98 99 100
	label: string;
	editors: ISerializedEditorInput[];
	mru: number[];
	preview: number;
}

B
Benjamin Pasero 已提交
101
export class EditorGroup implements IEditorGroup {
102 103
	private _label: string;

B
Benjamin Pasero 已提交
104 105
	private editors: EditorInput[];
	private mru: EditorInput[];
B
Benjamin Pasero 已提交
106 107 108 109 110 111 112 113 114 115

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

	private _onEditorActivated: Emitter<EditorInput>;
	private _onEditorOpened: Emitter<EditorInput>;
	private _onEditorClosed: Emitter<EditorInput>;
	private _onEditorPinned: Emitter<EditorInput>;
	private _onEditorUnpinned: Emitter<EditorInput>;

116 117 118 119
	constructor(
		arg1: string | ISerializedEditorGroup,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
B
Benjamin Pasero 已提交
120
		this.editors = [];
B
Benjamin Pasero 已提交
121 122
		this.mru = [];

123 124 125 126 127 128
		if (typeof arg1 === 'object') {
			this.deserialize(arg1);
		} else {
			this._label = arg1;
		}

B
Benjamin Pasero 已提交
129 130 131 132 133 134 135
		this._onEditorActivated = new Emitter<EditorInput>();
		this._onEditorOpened = new Emitter<EditorInput>();
		this._onEditorClosed = new Emitter<EditorInput>();
		this._onEditorPinned = new Emitter<EditorInput>();
		this._onEditorUnpinned = new Emitter<EditorInput>();
	}

136 137 138 139
	public get label(): string {
		return this._label;
	}

140 141 142 143
	public get count(): number {
		return this.editors.length;
	}

B
Benjamin Pasero 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	public get onEditorActivated(): Event<EditorInput> {
		return this._onEditorActivated.event;
	}

	public get onEditorOpened(): Event<EditorInput> {
		return this._onEditorOpened.event;
	}

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

	public get onEditorPinned(): Event<EditorInput> {
		return this._onEditorPinned.event;
	}

	public get onEditorUnpinned(): Event<EditorInput> {
		return this._onEditorUnpinned.event;
	}

B
Benjamin Pasero 已提交
164 165
	public getEditors(mru?: boolean): EditorInput[] {
		return mru ? this.mru.slice(0) : this.editors.slice(0);
B
Benjamin Pasero 已提交
166 167 168 169 170 171 172
	}

	public get activeEditor(): EditorInput {
		return this.active;
	}

	public isActive(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
173
		return this.matches(this.active, editor);
B
Benjamin Pasero 已提交
174 175 176 177 178 179 180
	}

	public get previewEditor(): EditorInput {
		return this.preview;
	}

	public isPreview(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
181
		return this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
182 183 184 185 186 187
	}

	public openEditor(editor: EditorInput, options?: IEditorOpenOptions): void {
		const index = this.indexOf(editor);

		const makePinned = options && options.pinned;
B
Benjamin Pasero 已提交
188
		const makeActive = (options && options.active) || !this.activeEditor || (!makePinned && this.matches(this.preview, this.activeEditor));
B
Benjamin Pasero 已提交
189 190 191 192

		// New editor
		if (index === -1) {

B
Benjamin Pasero 已提交
193
			// Insert into our list of editors if pinned or we have no preview editor
B
Benjamin Pasero 已提交
194
			if (makePinned || !this.preview) {
B
Benjamin Pasero 已提交
195
				const indexOfActive = this.indexOf(this.active);
B
Benjamin Pasero 已提交
196 197

				// Insert to the RIGHT of active editor
198
				if (CONFIG_OPEN_EDITOR_DIRECTION === Direction.RIGHT) {
B
Benjamin Pasero 已提交
199 200 201 202 203 204
					this.splice(indexOfActive + 1, false, editor);
				}

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

212 213
			// Handle preview
			if (!makePinned) {
B
Benjamin Pasero 已提交
214 215

				// Replace existing preview with this editor if we have a preview
216 217
				if (this.preview) {
					const indexOfPreview = this.indexOf(this.preview);
B
Benjamin Pasero 已提交
218
					this.closeEditor(this.preview, !makeActive); // optimization to prevent multiple setActive() in one call
219 220
					this.splice(indexOfPreview, false, editor);
				}
B
Benjamin Pasero 已提交
221

B
Benjamin Pasero 已提交
222 223 224 225 226 227
				this.preview = editor;
			}

			// Event
			this._onEditorOpened.fire(editor);

228
			// Handle active
B
Benjamin Pasero 已提交
229
			if (makeActive) {
B
Benjamin Pasero 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
				this.setActive(editor);
			}
		}

		// Existing editor
		else {

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

			// Activate it
			if (makeActive) {
				this.setActive(editor);
			}
		}
	}

B
Benjamin Pasero 已提交
249
	public closeEditor(editor: EditorInput, openNext = true): void {
B
Benjamin Pasero 已提交
250 251 252 253 254 255
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}

		// Active Editor closed
B
Benjamin Pasero 已提交
256
		if (openNext && this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
257 258

			// More than one editor
B
Benjamin Pasero 已提交
259 260
			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 已提交
261 262 263 264 265 266 267 268 269
			}

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

		// Preview Editor closed
B
Benjamin Pasero 已提交
270
		if (this.matches(this.preview, editor)) {
B
Benjamin Pasero 已提交
271 272 273
			this.preview = null;
		}

B
Benjamin Pasero 已提交
274
		// Remove from arrays
B
Benjamin Pasero 已提交
275
		this.splice(index, true);
B
Benjamin Pasero 已提交
276 277 278 279 280

		// Event
		this._onEditorClosed.fire(editor);
	}

281 282 283 284 285 286 287
	public closeAllEditors(): void {

		// Optimize: close all non active editors first to produce less upstream work
		this.mru.filter(e => e !== this.active).forEach(e => this.closeEditor(e));
		this.closeEditor(this.active);
	}

B
Benjamin Pasero 已提交
288 289 290 291 292 293
	public setActive(editor: EditorInput): void {
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}

B
Benjamin Pasero 已提交
294
		if (this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
295 296 297 298 299
			return; // already active
		}

		this.active = editor;

B
Benjamin Pasero 已提交
300
		// Bring to front in MRU list
B
Benjamin Pasero 已提交
301 302
		this.setMostRecentlyUsed(editor);

B
Benjamin Pasero 已提交
303 304 305 306 307
		// Event
		this._onEditorActivated.fire(editor);
	}

	public pin(editor: EditorInput): void {
B
Benjamin Pasero 已提交
308 309
		if (!this.isPreview(editor)) {
			return; // can only pin a preview editor
B
Benjamin Pasero 已提交
310 311 312 313 314 315 316 317 318
		}

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

		// Event
		this._onEditorPinned.fire(editor);
	}

B
Benjamin Pasero 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	public unpin(editor: EditorInput): void {
		if (!this.isPinned(editor)) {
			return; // can only unpin a pinned editor
		}

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

		// Event
		this._onEditorUnpinned.fire(editor);

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

B
Benjamin Pasero 已提交
335 336 337 338 339 340 341 342 343 344
	public isPinned(editor: EditorInput): boolean {
		const index = this.indexOf(editor);
		if (index === -1) {
			return false; // editor not found
		}

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

B
Benjamin Pasero 已提交
345
		return !this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
346 347
	}

B
Benjamin Pasero 已提交
348 349 350
	private splice(index: number, del: boolean, editor?: EditorInput): void {

		// Perform on editors array
B
Benjamin Pasero 已提交
351
		const args: any[] = [index, del ? 1 : 0];
352 353 354
		if (editor) {
			args.push(editor);
		}
B
Benjamin Pasero 已提交
355 356

		const editorToDeleteOrReplace = this.editors[index];
357
		this.editors.splice.apply(this.editors, args);
B
Benjamin Pasero 已提交
358 359 360 361 362 363

		// Add: make it LRU editor
		if (!del && editor) {
			this.mru.push(editor);
		}

B
Benjamin Pasero 已提交
364
		// Remove / Replace
B
Benjamin Pasero 已提交
365
		else {
B
Benjamin Pasero 已提交
366 367 368 369 370 371 372 373 374 375 376
			const indexInMRU = this.indexOf(editorToDeleteOrReplace, this.mru);

			// Remove: remove from MRU
			if (del && !editor) {
				this.mru.splice(indexInMRU, 1);
			}

			// Replace: replace MRU at location
			else {
				this.mru.splice(indexInMRU, 1, editor);
			}
B
Benjamin Pasero 已提交
377 378 379 380
		}
	}

	private indexOf(candidate: EditorInput, editors = this.editors): number {
B
Benjamin Pasero 已提交
381 382
		if (!candidate) {
			return -1;
B
Benjamin Pasero 已提交
383 384
		}

B
Benjamin Pasero 已提交
385
		for (let i = 0; i < editors.length; i++) {
B
Benjamin Pasero 已提交
386
			if (this.matches(editors[i], candidate)) {
B
Benjamin Pasero 已提交
387 388 389 390 391 392
				return i;
			}
		}

		return -1;
	}
B
Benjamin Pasero 已提交
393 394 395 396 397 398 399 400 401 402

	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 已提交
403
		this.mru.splice(mruIndex, 1);
B
Benjamin Pasero 已提交
404

B
Benjamin Pasero 已提交
405
		// Set editor to front
B
Benjamin Pasero 已提交
406 407
		this.mru.unshift(editor);
	}
B
Benjamin Pasero 已提交
408 409 410 411

	private matches(editorA: EditorInput, editorB: EditorInput): boolean {
		return !!editorA && !!editorB && editorA.matches(editorB);
	}
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

	public serialize(): ISerializedEditorGroup {
		let registry = (<IEditorRegistry>Registry.as(Extensions.Editors));

		// 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[] = [];
		this.editors.forEach(e => {
			let factory = registry.getEditorInputFactory(e.getId());
			if (factory) {
				let value = factory.serialize(e);
				if (typeof value === 'string') {
					serializedEditors.push({ id: e.getId(), value });
					serializableEditors.push(e);
				}
			}
		});

		const serializableMru = this.mru.filter(e => serializableEditors.indexOf(e) >= 0).map(e => serializableEditors.indexOf(e));

		return {
			label: this.label,
			editors: serializedEditors,
			mru: serializableMru,
			preview: serializableEditors.indexOf(this.preview),
		};
	}

	private deserialize(data: ISerializedEditorGroup): void {
		let registry = (<IEditorRegistry>Registry.as(Extensions.Editors));

		this._label = data.label;
		this.editors = data.editors.map(e => registry.getEditorInputFactory(e.id).deserialize(this.instantiationService, e.value));
		this.mru = data.mru.map(i => this.editors[i]);
		this.active = this.mru[0];
		this.preview = this.editors[data.preview];
	}
}

interface ISerializedEditorStacksModel {
	groups: ISerializedEditorGroup[];
	active: number;
B
Benjamin Pasero 已提交
456 457 458
}

export class EditorStacksModel implements IEditorStacksModel {
459 460 461 462 463

	private static STORAGE_KEY = 'editorStacks.model';

	private toDispose: IDisposable[];

B
Benjamin Pasero 已提交
464
	private _groups: EditorGroup[];
B
Benjamin Pasero 已提交
465
	private active: EditorGroup;
B
Benjamin Pasero 已提交
466 467 468 469 470

	private _onGroupOpened: Emitter<EditorGroup>;
	private _onGroupClosed: Emitter<EditorGroup>;
	private _onGroupActivated: Emitter<EditorGroup>;

471 472 473 474 475 476 477
	constructor(
		@IStorageService private storageService: IStorageService,
		@ILifecycleService private lifecycleService: ILifecycleService,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		this.toDispose = [];

B
Benjamin Pasero 已提交
478 479 480 481
		this._groups = [];
		this._onGroupOpened = new Emitter<EditorGroup>();
		this._onGroupClosed = new Emitter<EditorGroup>();
		this._onGroupActivated = new Emitter<EditorGroup>();
482 483 484 485 486 487 488

		this.load();
		this.registerListeners();
	}

	private registerListeners(): void {
		this.toDispose.push(this.lifecycleService.onShutdown(() => this.onShutdown()));
B
Benjamin Pasero 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	}

	public get onGroupOpened(): Event<EditorGroup> {
		return this._onGroupOpened.event;
	}

	public get onGroupClosed(): Event<EditorGroup> {
		return this._onGroupClosed.event;
	}

	public get onGroupActivated(): Event<EditorGroup> {
		return this._onGroupActivated.event;
	}

	public get groups(): EditorGroup[] {
		return this._groups.slice(0);
	}

	public get activeGroup(): EditorGroup {
		return this.active;
	}

	public openGroup(label: string): EditorGroup {
512
		const group = this.instantiationService.createInstance(EditorGroup, label);
B
Benjamin Pasero 已提交
513 514 515 516 517 518 519 520

		// First group
		if (!this.active) {
			this._groups.push(group);
		}

		// Subsequent group (add to the right of active)
		else {
B
Benjamin Pasero 已提交
521
			this._groups.splice(this.indexOf(this.active) + 1, 0, group);
B
Benjamin Pasero 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
		}

		// Event
		this._onGroupOpened.fire(group);

		// Make active
		this.setActive(group);

		return group;
	}

	public closeGroup(group: EditorGroup): void {
		const index = this.indexOf(group);
		if (index < 0) {
			return; // group does not exist
		}

		// Active group closed: Find a new active one to the right
		if (group === this.active) {

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

				this.setActive(newActiveGroup);
			}

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

560 561 562
		// Close Editors in Group first
		group.closeAllEditors();

B
Benjamin Pasero 已提交
563 564 565 566 567 568 569
		// Splice from groups
		this._groups.splice(index, 1);

		// Event
		this._onGroupClosed.fire(group);
	}

570 571 572 573 574 575 576
	public closeAllGroups(): void {

		// Optimize: close all non active groups first to produce less upstream work
		this.groups.filter(g => g !== this.active).forEach(g => this.closeGroup(g));
		this.closeGroup(this.active);
	}

B
Benjamin Pasero 已提交
577 578 579 580 581 582 583 584 585
	public setActive(group: EditorGroup): void {
		this.active = group;

		this._onGroupActivated.fire(this.active);
	}

	private indexOf(group: EditorGroup): number {
		return this._groups.indexOf(group);
	}
586 587 588 589 590 591

	private save(): void {
		let activeIndex = this.indexOf(this.active);
		let activeIsEmptyGroup = false;

		let serializedGroups = this._groups.map(g => g.serialize());
592
		let serializableActiveIndex = activeIndex;
593

594 595 596 597
		// Exclude empty groups (can happen if an editor cannot be serialized)
		if (!CONFIG_ALLOW_EMPTY_GROUPS) {
			let serializedNonEmptyGroups: ISerializedEditorGroup[] = [];
			serializedGroups.forEach((g, index) => {
598

599 600 601 602 603 604 605
				// non empty group
				if (g.editors.length > 0) {
					serializedNonEmptyGroups.push(g);
				}

				// empty group that is active
				else if (activeIndex === index) {
606 607
					activeIsEmptyGroup = true; // our active group is empty after serialization!
				}
608
			});
609

610 611 612 613 614 615 616 617 618 619
			serializedGroups = serializedNonEmptyGroups;

			// Determine serializable active index
			if (activeIsEmptyGroup && serializedGroups.length > 0) {
				serializableActiveIndex = 0; // just make first group active if active is empty and we have other groups to pick from
			} else if (activeIsEmptyGroup) {
				serializableActiveIndex = void 0; // there are no groups to make active
			} else {
				serializableActiveIndex = activeIndex; // active group is not empty and can be serialized
			}
620 621 622
		}

		const serialized: ISerializedEditorStacksModel = {
623
			groups: serializedGroups,
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
			active: serializableActiveIndex
		};

		this.storageService.store(EditorStacksModel.STORAGE_KEY, JSON.stringify(serialized), StorageScope.WORKSPACE);
	}

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

			this._groups = serialized.groups.map(s => this.instantiationService.createInstance(EditorGroup, s));
			this.active = this._groups[serialized.active];
		}
	}

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

		dispose(this.toDispose);
	}
B
Benjamin Pasero 已提交
645
}