editorStacksModel.ts 15.8 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;
B
Benjamin Pasero 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	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;
	closeGroup(group: IEditorGroup): void;
	setActive(group: IEditorGroup): void;
}

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

B
Benjamin Pasero 已提交
63 64
/// --- API-End ----

B
Benjamin Pasero 已提交
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
// N Groups with labels (start with Left, Center, Right)
// Group has a List of editors
// Group can have N editors state pinned and 1 state preview
// Group has 1 active edutir
// Group has MRV(isible) list of editors

// Model has actions to work with inputs
// Open
//   To the left / to the right (setting)
// Close
// Close Others
// Close Editors to the Right
// Close All
// Close All in Group
// Move Editor
// Move Group
// Pin Editor
// Unpin Editor

// Model has resulting events from operations

// Can be serialized and restored

export enum Direction {
	LEFT,
	RIGHT
}

93 94 95 96
let DEFAULT_OPEN_EDITOR_DIRECTION = Direction.RIGHT; // open new editors to the right of existing ones
export function setOpenEditorDirection(dir: Direction): void {
	DEFAULT_OPEN_EDITOR_DIRECTION = dir;
}
B
Benjamin Pasero 已提交
97

98 99 100 101 102 103 104 105 106 107 108 109
interface ISerializedEditorInput {
	id: string;
	value: string;
}

interface ISerializedEditorGroup {
	label: string;
	editors: ISerializedEditorInput[];
	mru: number[];
	preview: number;
}

B
Benjamin Pasero 已提交
110
export class EditorGroup implements IEditorGroup {
111 112
	private _label: string;

B
Benjamin Pasero 已提交
113 114
	private editors: EditorInput[];
	private mru: EditorInput[];
B
Benjamin Pasero 已提交
115 116 117 118 119 120 121 122 123 124

	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>;

125 126 127 128
	constructor(
		arg1: string | ISerializedEditorGroup,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
B
Benjamin Pasero 已提交
129
		this.editors = [];
B
Benjamin Pasero 已提交
130 131
		this.mru = [];

132 133 134 135 136 137
		if (typeof arg1 === 'object') {
			this.deserialize(arg1);
		} else {
			this._label = arg1;
		}

B
Benjamin Pasero 已提交
138 139 140 141 142 143 144
		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>();
	}

145 146 147 148
	public get label(): string {
		return this._label;
	}

149 150 151 152
	public get count(): number {
		return this.editors.length;
	}

B
Benjamin Pasero 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	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 已提交
173 174
	public getEditors(mru?: boolean): EditorInput[] {
		return mru ? this.mru.slice(0) : this.editors.slice(0);
B
Benjamin Pasero 已提交
175 176 177 178 179 180 181
	}

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

	public isActive(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
182
		return this.matches(this.active, editor);
B
Benjamin Pasero 已提交
183 184 185 186 187 188 189
	}

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

	public isPreview(editor: EditorInput): boolean {
B
Benjamin Pasero 已提交
190
		return this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
191 192 193 194 195 196
	}

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

		const makePinned = options && options.pinned;
B
Benjamin Pasero 已提交
197
		const makeActive = (options && options.active) || !this.activeEditor || (!makePinned && this.matches(this.preview, this.activeEditor));
B
Benjamin Pasero 已提交
198 199 200 201

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

B
Benjamin Pasero 已提交
202
			// Insert into our list of editors if pinned or we have no preview editor
B
Benjamin Pasero 已提交
203
			if (makePinned || !this.preview) {
B
Benjamin Pasero 已提交
204
				const indexOfActive = this.indexOf(this.active);
B
Benjamin Pasero 已提交
205 206 207 208 209 210 211 212 213

				// Insert to the RIGHT of active editor
				if (DEFAULT_OPEN_EDITOR_DIRECTION === Direction.RIGHT) {
					this.splice(indexOfActive + 1, false, editor);
				}

				// Insert to the LEFT of active editor
				else {
					if (indexOfActive === 0 || !this.editors.length) {
B
Benjamin Pasero 已提交
214
						this.splice(0, false, editor); // to the left becoming first editor in list
B
Benjamin Pasero 已提交
215
					} else {
B
Benjamin Pasero 已提交
216
						this.splice(indexOfActive - 1, false, editor); // to the left of active editor
B
Benjamin Pasero 已提交
217 218 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);
B
Benjamin Pasero 已提交
227
					this.closeEditor(this.preview, !makeActive); // optimization to prevent multiple setActive() in one call
228 229
					this.splice(indexOfPreview, false, editor);
				}
B
Benjamin Pasero 已提交
230

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

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

237
			// Handle active
B
Benjamin Pasero 已提交
238
			if (makeActive) {
B
Benjamin Pasero 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
				this.setActive(editor);
			}
		}

		// Existing editor
		else {

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

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

B
Benjamin Pasero 已提交
258
	public closeEditor(editor: EditorInput, openNext = true): void {
B
Benjamin Pasero 已提交
259 260 261 262 263 264
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}

		// Active Editor closed
B
Benjamin Pasero 已提交
265
		if (openNext && this.matches(this.active, editor)) {
B
Benjamin Pasero 已提交
266 267

			// More than one editor
B
Benjamin Pasero 已提交
268 269
			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 已提交
270 271 272 273 274 275 276 277 278
			}

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

		// Preview Editor closed
B
Benjamin Pasero 已提交
279
		if (this.matches(this.preview, editor)) {
B
Benjamin Pasero 已提交
280 281 282
			this.preview = null;
		}

B
Benjamin Pasero 已提交
283
		// Remove from arrays
B
Benjamin Pasero 已提交
284
		this.splice(index, true);
B
Benjamin Pasero 已提交
285 286 287 288 289 290 291 292 293 294 295

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

	public setActive(editor: EditorInput): void {
		const index = this.indexOf(editor);
		if (index === -1) {
			return; // not found
		}

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

		this.active = editor;

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

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

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

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

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

B
Benjamin Pasero 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	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 已提交
337 338 339 340 341 342 343 344 345 346
	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 已提交
347
		return !this.matches(this.preview, editor);
B
Benjamin Pasero 已提交
348 349
	}

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

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

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

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

B
Benjamin Pasero 已提交
366
		// Remove / Replace
B
Benjamin Pasero 已提交
367
		else {
B
Benjamin Pasero 已提交
368 369 370 371 372 373 374 375 376 377 378
			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 已提交
379 380 381 382
		}
	}

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

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

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

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

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

	private matches(editorA: EditorInput, editorB: EditorInput): boolean {
		return !!editorA && !!editorB && editorA.matches(editorB);
	}
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 456 457

	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 已提交
458 459 460
}

export class EditorStacksModel implements IEditorStacksModel {
461 462 463 464 465

	private static STORAGE_KEY = 'editorStacks.model';

	private toDispose: IDisposable[];

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

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

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

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

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

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

	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 {
514
		const group = this.instantiationService.createInstance(EditorGroup, label);
B
Benjamin Pasero 已提交
515 516 517 518 519 520 521 522

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

		// Subsequent group (add to the right of active)
		else {
B
Benjamin Pasero 已提交
523
			this._groups.splice(this.indexOf(this.active) + 1, 0, group);
B
Benjamin Pasero 已提交
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 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
		}

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

		// Splice from groups
		this._groups.splice(index, 1);

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

	public setActive(group: EditorGroup): void {
		this.active = group;

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

	private indexOf(group: EditorGroup): number {
		return this._groups.indexOf(group);
	}
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

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

		// Exclude empty groups (can happen if an editor cannot be serialized)
		let serializedGroups = this._groups.map(g => g.serialize());
		let serializedNonEmptyGroups: ISerializedEditorGroup[] = [];
		serializedGroups.forEach((g, index) => {

			// non empty group
			if (g.editors.length > 0) {
				serializedNonEmptyGroups.push(g);
			}

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

		// Determine serializable active index
		let serializableActiveIndex: number;
		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
		}

		const serialized: ISerializedEditorStacksModel = {
			groups: serializedNonEmptyGroups,
			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 已提交
634
}