editorActions.ts 24.3 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8 9
import nls = require('vs/nls');
import types = require('vs/base/common/types');
10
import {Action} from 'vs/base/common/actions';
E
Erich Gamma 已提交
11 12 13 14
import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor';
import {EditorInput, getUntitledOrFileResource, TextEditorOptions} from 'vs/workbench/common/editor';
import {QuickOpenEntryGroup} from 'vs/base/parts/quickopen/browser/quickOpenModel';
import {EditorQuickOpenEntry, EditorQuickOpenEntryGroup, IEditorQuickOpenEntry} from 'vs/workbench/browser/quickopen';
15
import {IWorkbenchEditorService, GroupArrangement} from 'vs/workbench/services/editor/common/editorService';
16
import {IQuickOpenService} from 'vs/workbench/services/quickopen/common/quickOpenService';
17
import {IPartService} from 'vs/workbench/services/part/common/partService';
18
import {Position, IEditor, Direction} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
19
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
20
import {IEditorIdentifier} from 'vs/workbench/common/editor/editorStacksModel';
B
Benjamin Pasero 已提交
21
import {IHistoryService} from 'vs/workbench/services/history/common/history';
E
Erich Gamma 已提交
22 23 24

export class SplitEditorAction extends Action {

25 26 27
	public static ID = 'workbench.action.splitEditor';
	public static LABEL = nls.localize('splitEditor', "Split Editor");

E
Erich Gamma 已提交
28 29 30 31
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

32
	public run(): TPromise<any> {
E
Erich Gamma 已提交
33 34 35 36

		// Can only split with active editor
		let activeEditor = this.editorService.getActiveEditor();
		if (!activeEditor) {
A
Alex Dima 已提交
37
			return TPromise.as(true);
E
Erich Gamma 已提交
38 39 40 41
		}

		// Return if the editor to split does not support split editing
		if (!(<BaseEditor>activeEditor).supportsSplitEditor()) {
A
Alex Dima 已提交
42
			return TPromise.as(true);
E
Erich Gamma 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		}

		// Count editors
		let visibleEditors = this.editorService.getVisibleEditors();
		let editorCount = visibleEditors.length;
		let targetPosition: Position;

		switch (editorCount) {

			// Open split editor to the right of left one
			case 1:
				targetPosition = Position.CENTER;
				break;

			// Special case two editors opened
			case 2:

				// Continue splitting to the right
				if (activeEditor.position === Position.CENTER) {
					targetPosition = Position.RIGHT;
				}

				// Replace the center editor with the contents of the left editor and push center to the right
				else if (activeEditor.position === Position.LEFT && !!getUntitledOrFileResource(activeEditor.input)) {
					let centerInput = visibleEditors[Position.CENTER].input;

					let options = new TextEditorOptions();
					options.preserveFocus = true;

					return this.editorService.openEditor(activeEditor.input, options, Position.CENTER).then(() => {
						return this.editorService.openEditor(centerInput, options, Position.RIGHT).then(() => {
74
							this.editorService.focusGroup(Position.CENTER);
E
Erich Gamma 已提交
75 76 77 78 79 80 81 82 83 84
						});
					});
				}
		}

		// Only split if the input is resource editor input
		if (!types.isUndefinedOrNull(targetPosition) && !!getUntitledOrFileResource(activeEditor.input)) {
			return this.editorService.openEditor(activeEditor.input, null, targetPosition);
		}

A
Alex Dima 已提交
85
		return TPromise.as(true);
E
Erich Gamma 已提交
86 87 88
	}
}

B
Benjamin Pasero 已提交
89
export class NavigateBetweenGroupsAction extends Action {
90

B
Benjamin Pasero 已提交
91 92
	public static ID = 'workbench.action.navigateEditorGroups';
	public static LABEL = nls.localize('navigateEditorGroups', "Navigate Between Editor Groups");
E
Erich Gamma 已提交
93 94 95 96 97

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

98
	public run(): TPromise<any> {
E
Erich Gamma 已提交
99 100 101 102

		// Can cycle split with active editor
		let activeEditor = this.editorService.getActiveEditor();
		if (!activeEditor) {
A
Alex Dima 已提交
103
			return TPromise.as(false);
E
Erich Gamma 已提交
104 105 106 107 108 109 110
		}

		// Cycle to the left and use module to start at 0 again
		let visibleEditors = this.editorService.getVisibleEditors();
		let editorCount = visibleEditors.length;
		let newIndex = (activeEditor.position + 1) % editorCount;

111 112 113
		this.editorService.focusGroup(<Position>newIndex);

		return TPromise.as(true);
E
Erich Gamma 已提交
114 115 116
	}
}

117 118 119
export class FocusFirstGroupAction extends Action {

	public static ID = 'workbench.action.focusFirstEditor';
B
Benjamin Pasero 已提交
120
	public static LABEL = nls.localize('focusFirstEditorGroup', "Focus Left Editor Group");
E
Erich Gamma 已提交
121 122 123 124 125 126 127 128 129 130

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IQuickOpenService private quickOpenService: IQuickOpenService
	) {
		super(id, label);
	}

131
	public run(): TPromise<any> {
E
Erich Gamma 已提交
132 133 134 135 136

		// Find left editor and focus it
		let editors = this.editorService.getVisibleEditors();
		for (var editor of editors) {
			if (editor.position === Position.LEFT) {
137 138 139
				this.editorService.focusGroup(Position.LEFT);

				return TPromise.as(true);
E
Erich Gamma 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
			}
		}

		// Since no editor is currently opened, try to open last history entry to the target side
		let history = this.quickOpenService.getEditorHistory();
		for (var input of history) {

			// For now only support to open resources from history to the side
			if (!!getUntitledOrFileResource(input)) {
				return this.editorService.openEditor(input, null, Position.LEFT).then(() => {

					// Automatically clean up stale history entries when the input can not be opened
					if (!input.matches(this.editorService.getActiveEditorInput())) {
						this.quickOpenService.removeEditorHistoryEntry(input);
					}
				});
			}
		}

A
Alex Dima 已提交
159
		return TPromise.as(true);
E
Erich Gamma 已提交
160 161 162
	}
}

163
export abstract class BaseFocusSideGroupAction extends Action {
E
Erich Gamma 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IQuickOpenService private quickOpenService: IQuickOpenService
	) {
		super(id, label);
	}

	protected abstract getReferenceEditorSide(): Position;

	protected abstract getTargetEditorSide(): Position;

178
	public run(): TPromise<any> {
E
Erich Gamma 已提交
179 180 181 182 183 184 185 186 187

		// Require at least the reference editor to be visible
		let editors = this.editorService.getVisibleEditors();
		let referenceEditor: IEditor;
		for (let i = 0; i < editors.length; i++) {
			let editor = editors[i];

			// Target editor exists so focus it
			if (editor.position === this.getTargetEditorSide()) {
188 189 190
				this.editorService.focusGroup(editor.position);

				return TPromise.as(true);
E
Erich Gamma 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
			}

			// Remember reference editor
			if (editor.position === this.getReferenceEditorSide()) {
				referenceEditor = editor;
			}
		}

		// Require the reference editor to be visible and supporting split editor
		if (referenceEditor && (<BaseEditor>referenceEditor).supportsSplitEditor()) {
			return this.editorService.openEditor(referenceEditor.input, null, this.getTargetEditorSide());
		}

		// Otherwise try to find a history entry to open to the target editor side
		else if (referenceEditor) {
			let history = this.quickOpenService.getEditorHistory();
			for (var input of history) {

				// For now only support to open files from history to the side
				if (!!getUntitledOrFileResource(input)) {
					return this.editorService.openEditor(input, null, this.getTargetEditorSide()).then(() => {

						// Automatically clean up stale history entries when the input can not be opened
						if (!input.matches(this.editorService.getActiveEditorInput())) {
							this.quickOpenService.removeEditorHistoryEntry(input);
						}
					});
				}
			}
		}

A
Alex Dima 已提交
222
		return TPromise.as(true);
E
Erich Gamma 已提交
223 224 225
	}
}

226 227 228
export class FocusSecondGroupAction extends BaseFocusSideGroupAction {

	public static ID = 'workbench.action.focusSecondEditor';
B
Benjamin Pasero 已提交
229
	public static LABEL = nls.localize('focusSecondEditorGroup', "Focus Center Editor Group");
E
Erich Gamma 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IQuickOpenService quickOpenService: IQuickOpenService
	) {
		super(id, label, editorService, quickOpenService);
	}

	protected getReferenceEditorSide(): Position {
		return Position.LEFT;
	}

	protected getTargetEditorSide(): Position {
		return Position.CENTER;
	}
}

249 250 251
export class FocusThirdGroupAction extends BaseFocusSideGroupAction {

	public static ID = 'workbench.action.focusThirdEditor';
B
Benjamin Pasero 已提交
252
	public static LABEL = nls.localize('focusThirdEditorGroup', "Focus Right Editor Group");
E
Erich Gamma 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
		@IQuickOpenService quickOpenService: IQuickOpenService
	) {
		super(id, label, editorService, quickOpenService);
	}

	protected getReferenceEditorSide(): Position {
		return Position.CENTER;
	}

	protected getTargetEditorSide(): Position {
		return Position.RIGHT;
	}
}

B
Benjamin Pasero 已提交
272
export class FocusPreviousGroup extends Action {
273

B
Benjamin Pasero 已提交
274 275
	public static ID = 'workbench.action.focusPreviousGroup';
	public static LABEL = nls.localize('focusPreviousGroup', "Focus Previous Group");
E
Erich Gamma 已提交
276 277 278 279 280

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

281
	public run(): TPromise<any> {
E
Erich Gamma 已提交
282 283 284 285

		// Require an active editor
		let activeEditor = this.editorService.getActiveEditor();
		if (!activeEditor) {
A
Alex Dima 已提交
286
			return TPromise.as(true);
E
Erich Gamma 已提交
287 288 289 290 291 292 293 294 295 296
		}


		// Find the next position to the left
		let nextPosition: Position = Position.LEFT;
		if (activeEditor.position === Position.RIGHT) {
			nextPosition = Position.CENTER;
		}

		// Focus next position if provided
297 298 299
		this.editorService.focusGroup(nextPosition);

		return TPromise.as(true);
E
Erich Gamma 已提交
300 301 302
	}
}

B
Benjamin Pasero 已提交
303
export class FocusNextGroup extends Action {
304

B
Benjamin Pasero 已提交
305 306
	public static ID = 'workbench.action.focusNextGroup';
	public static LABEL = nls.localize('focusNextGroup', "Focus Next Group");
307

E
Erich Gamma 已提交
308 309 310 311 312 313 314 315 316 317 318
	private navigateActions: Action[];

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IInstantiationService instantiationService: IInstantiationService
	) {
		super(id, label);

		this.navigateActions = [];
319 320 321
		this.navigateActions[Position.LEFT] = instantiationService.createInstance(FocusFirstGroupAction, FocusFirstGroupAction.ID, FocusFirstGroupAction.LABEL);
		this.navigateActions[Position.CENTER] = instantiationService.createInstance(FocusSecondGroupAction, FocusSecondGroupAction.ID, FocusSecondGroupAction.LABEL);
		this.navigateActions[Position.RIGHT] = instantiationService.createInstance(FocusThirdGroupAction, FocusThirdGroupAction.ID, FocusThirdGroupAction.LABEL);
E
Erich Gamma 已提交
322 323
	}

324
	public run(event?: any): TPromise<any> {
E
Erich Gamma 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

		// Find the next position to the right to use
		let nextPosition: Position;
		let activeEditor = this.editorService.getActiveEditor();
		if (!activeEditor) {
			nextPosition = Position.LEFT;
		} else if (activeEditor.position === Position.LEFT) {
			nextPosition = Position.CENTER;
		} else if (activeEditor.position === Position.CENTER) {
			nextPosition = Position.RIGHT;
		}

		// Run the action for the target next position
		if (!types.isUndefinedOrNull(nextPosition) && this.navigateActions[nextPosition]) {
			return this.navigateActions[nextPosition].run(event);
		}

A
Alex Dima 已提交
342
		return TPromise.as(true);
E
Erich Gamma 已提交
343 344 345 346 347 348 349 350
	}
}

export class OpenToSideAction extends Action {

	public static OPEN_TO_SIDE_ID = 'workbench.action.openToSide';
	public static OPEN_TO_SIDE_LABEL = nls.localize('openToSide', "Open to the Side");

351
	constructor( @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
E
Erich Gamma 已提交
352 353 354 355 356 357 358 359 360 361 362 363
		super(OpenToSideAction.OPEN_TO_SIDE_ID, OpenToSideAction.OPEN_TO_SIDE_LABEL);

		this.class = 'quick-open-sidebyside';

		this.updateEnablement();
	}

	private updateEnablement(): void {
		let activeEditor = this.editorService.getActiveEditor();
		this.enabled = (!activeEditor || activeEditor.position !== Position.RIGHT);
	}

364
	public run(context: any): TPromise<any> {
E
Erich Gamma 已提交
365 366
		let entry = toEditorQuickOpenEntry(context);
		if (entry) {
B
Benjamin Pasero 已提交
367 368 369 370 371 372 373 374 375
			let typedInputPromise: TPromise<EditorInput>;
			let input = entry.getInput();
			if (input instanceof EditorInput) {
				typedInputPromise = TPromise.as(input);
			} else {
				typedInputPromise = this.editorService.inputToType(input);
			}

			return typedInputPromise.then(typedInput => this.editorService.openEditor(typedInput, entry.getOptions(), true));
E
Erich Gamma 已提交
376 377
		}

A
Alex Dima 已提交
378
		return TPromise.as(false);
E
Erich Gamma 已提交
379 380 381
	}
}

382
export function toEditorQuickOpenEntry(element: any): IEditorQuickOpenEntry {
E
Erich Gamma 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

	// QuickOpenEntryGroup
	if (element instanceof QuickOpenEntryGroup) {
		let group = <QuickOpenEntryGroup>element;
		if (group.getEntry()) {
			element = group.getEntry();
		}
	}

	// EditorQuickOpenEntry or EditorQuickOpenEntryGroup both implement IEditorQuickOpenEntry
	if (element instanceof EditorQuickOpenEntry || element instanceof EditorQuickOpenEntryGroup) {
		return element;
	}

	return null;
}

export class CloseEditorAction extends Action {
401 402 403 404

	public static ID = 'workbench.action.closeEditor';
	public static LABEL = nls.localize('closeEditor', "Close Editor");

B
Benjamin Pasero 已提交
405
	private position: Position;
E
Erich Gamma 已提交
406 407 408 409 410

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

B
Benjamin Pasero 已提交
411 412 413 414
	public setPosition(position: Position): void {
		this.position = position;
	}

415
	public run(): TPromise<any> {
416 417 418

		// Close Active Editor
		if (typeof this.position !== 'number') {
B
Benjamin Pasero 已提交
419 420
			let activeEditor = this.editorService.getActiveEditor();
			if (activeEditor) {
421
				return this.editorService.closeEditor(activeEditor.position, activeEditor.input);
B
Benjamin Pasero 已提交
422
			}
423 424 425 426 427 428
		}

		// Close Editor at Position
		let visibleEditors = this.editorService.getVisibleEditors();
		if (visibleEditors[this.position]) {
			return this.editorService.closeEditor(this.position, visibleEditors[this.position].input);
E
Erich Gamma 已提交
429 430
		}

A
Alex Dima 已提交
431
		return TPromise.as(false);
E
Erich Gamma 已提交
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 458 459 460 461 462 463 464 465 466 467 468 469 470
export class CloseEditorsInGroupAction extends Action {

	public static ID = 'workbench.action.closeEditorsInGroup';
	public static LABEL = nls.localize('closeEditorsInGroup', "Close All Editors in Group");

	private position: Position;

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

	public setPosition(position: Position): void {
		this.position = position;
	}

	public run(): TPromise<any> {
		let position: Position;
		if (typeof this.position === 'number') {
			position = this.position;
		} else {
			let activeEditor = this.editorService.getActiveEditor();
			if (activeEditor) {
				position = activeEditor.position;
			}
		}

		if (typeof position === 'number') {
			return this.editorService.closeEditors(position);
		}

		return TPromise.as(false);
	}
}

export class CloseLeftEditorsInGroupAction extends Action {

B
Benjamin Pasero 已提交
471 472
	public static ID = 'workbench.action.closeEditorsToTheLeft';
	public static LABEL = nls.localize('closeEditorsToTheLeft', "Close Editors to the Left");
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

	public run(): TPromise<any> {
		let activeEditor = this.editorService.getActiveEditor();
		if (activeEditor) {
			return this.editorService.closeEditors(activeEditor.position, activeEditor.input, Direction.LEFT);
		}

		return TPromise.as(false);
	}
}

export class CloseRightEditorsInGroupAction extends Action {

B
Benjamin Pasero 已提交
490 491
	public static ID = 'workbench.action.closeEditorsToTheRight';
	public static LABEL = nls.localize('closeEditorsToTheRight', "Close Editors to the Right");
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

	public run(): TPromise<any> {
		let activeEditor = this.editorService.getActiveEditor();
		if (activeEditor) {
			return this.editorService.closeEditors(activeEditor.position, activeEditor.input, Direction.RIGHT);
		}

		return TPromise.as(false);
	}
}

E
Erich Gamma 已提交
507 508
export class CloseAllEditorsAction extends Action {

509 510 511
	public static ID = 'workbench.action.closeAllEditors';
	public static LABEL = nls.localize('closeAllEditors', "Close All Editors");

E
Erich Gamma 已提交
512 513 514 515
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

516
	public run(): TPromise<any> {
517
		return this.editorService.closeAllEditors();
E
Erich Gamma 已提交
518 519 520
	}
}

521 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
export class CloseEditorsInOtherGroupsAction extends Action {

	public static ID = 'workbench.action.closeEditorsInOtherGroups';
	public static LABEL = nls.localize('closeEditorsInOtherGroups', "Close Editors in Other Groups");

	private position: Position;

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

	public setPosition(position: Position): void {
		this.position = position;
	}

	public run(): TPromise<any> {
		let position: Position;
		if (typeof this.position === 'number') {
			position = this.position;
		} else {
			let activeEditor = this.editorService.getActiveEditor();
			if (activeEditor) {
				position = activeEditor.position;
			}
		}

		if (typeof position === 'number') {
			return this.editorService.closeAllEditors(position);
		}

		return TPromise.as(false);
	}
}

export class CloseOtherEditorsInGroupAction extends Action {

	public static ID = 'workbench.action.closeOtherEditors';
B
Benjamin Pasero 已提交
558
	public static LABEL = nls.localize('closeOtherEditorsInGroup', "Close Other Editors");
E
Erich Gamma 已提交
559 560 561 562 563

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

564
	public run(): TPromise<any> {
565 566
		const active = this.editorService.getActiveEditor();
		if (active) {
567
			return this.editorService.closeEditors(active.position, active.input);
568 569 570
		}

		return TPromise.as(false);
E
Erich Gamma 已提交
571 572 573
	}
}

B
Benjamin Pasero 已提交
574
export class MoveGroupLeftAction extends Action {
E
Erich Gamma 已提交
575

576 577 578 579 580
	public static ID = 'workbench.action.moveActiveEditorLeft';
	public static LABEL = nls.localize('moveActiveGroupLeft', "Move Editor Group Left");

	private position: Position;

E
Erich Gamma 已提交
581 582 583 584
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

585 586 587 588 589
	public setPosition(position: Position): void {
		this.position = position;
		this.enabled = (position !== Position.LEFT);
	}

590
	public run(): TPromise<any> {
591 592 593 594 595 596 597 598 599 600 601 602
		let position: Position;
		if (typeof this.position === 'number') {
			position = this.position;
		} else {
			let activeEditor = this.editorService.getActiveEditor();
			if (activeEditor && (activeEditor.position === Position.CENTER || activeEditor.position === Position.RIGHT)) {
				position = activeEditor.position;
			}
		}

		if (typeof position === 'number') {
			let newPosition = (position === Position.CENTER) ? Position.LEFT : Position.CENTER;
E
Erich Gamma 已提交
603

B
Benjamin Pasero 已提交
604
			// Move group
605
			this.editorService.moveGroup(position, newPosition);
E
Erich Gamma 已提交
606 607
		}

A
Alex Dima 已提交
608
		return TPromise.as(false);
E
Erich Gamma 已提交
609 610 611
	}
}

B
Benjamin Pasero 已提交
612
export class MoveGroupRightAction extends Action {
E
Erich Gamma 已提交
613

614 615 616 617 618
	public static ID = 'workbench.action.moveActiveEditorRight';
	public static LABEL = nls.localize('moveActiveGroupRight', "Move Editor Group Right");

	private position: Position;

E
Erich Gamma 已提交
619 620 621 622
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

623 624 625 626 627
	public setPosition(position: Position): void {
		this.position = position;
		this.enabled = (position !== Position.RIGHT);
	}

628
	public run(): TPromise<any> {
629 630 631 632 633 634 635 636 637 638 639 640 641 642
		let position: Position;
		if (typeof this.position === 'number') {
			position = this.position;
		} else {
			let activeEditor = this.editorService.getActiveEditor();
			let editors = this.editorService.getVisibleEditors();

			if ((editors.length === 2 && activeEditor.position === Position.LEFT) || (editors.length === 3 && activeEditor.position !== Position.RIGHT)) {
				position = activeEditor.position;
			}
		}

		if (typeof position === 'number') {
			let newPosition = (position === Position.LEFT) ? Position.CENTER : Position.RIGHT;
E
Erich Gamma 已提交
643

B
Benjamin Pasero 已提交
644
			// Move group
645
			this.editorService.moveGroup(position, newPosition);
E
Erich Gamma 已提交
646 647
		}

A
Alex Dima 已提交
648
		return TPromise.as(false);
E
Erich Gamma 已提交
649 650 651
	}
}

652
export class MinimizeOtherGroupsAction extends Action {
E
Erich Gamma 已提交
653

654 655 656
	public static ID = 'workbench.action.minimizeOtherEditors';
	public static LABEL = nls.localize('minimizeOtherEditorGroups', "Minimize Other Editor Groups");

E
Erich Gamma 已提交
657 658 659 660
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

661
	public run(): TPromise<any> {
662
		this.editorService.arrangeGroups(GroupArrangement.MINIMIZE_OTHERS);
E
Erich Gamma 已提交
663

A
Alex Dima 已提交
664
		return TPromise.as(false);
E
Erich Gamma 已提交
665 666 667
	}
}

668
export class EvenGroupWidthsAction extends Action {
E
Erich Gamma 已提交
669

670 671 672
	public static ID = 'workbench.action.evenEditorWidths';
	public static LABEL = nls.localize('evenEditorGroups', "Even Editor Group Widths");

E
Erich Gamma 已提交
673 674 675 676
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

677
	public run(): TPromise<any> {
678
		this.editorService.arrangeGroups(GroupArrangement.EVEN_WIDTH);
E
Erich Gamma 已提交
679

A
Alex Dima 已提交
680
		return TPromise.as(false);
E
Erich Gamma 已提交
681 682 683
	}
}

684
export class MaximizeGroupAction extends Action {
685

686 687 688
	public static ID = 'workbench.action.maximizeEditor';
	public static LABEL = nls.localize('maximizeEditor', "Maximize Editor Group and Hide Sidebar");

689 690 691 692 693 694 695 696 697
	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IPartService private partService: IPartService
	) {
		super(id, label);
	}

698
	public run(): TPromise<any> {
699
		if (this.editorService.getActiveEditor()) {
700
			this.editorService.arrangeGroups(GroupArrangement.MINIMIZE_OTHERS);
701 702 703
			this.partService.setSideBarHidden(true);
		}

A
Alex Dima 已提交
704
		return TPromise.as(false);
705 706 707
	}
}

708
export class PinEditorAction extends Action {
709

710 711
	public static ID = 'workbench.action.pinEditor';
	public static LABEL = nls.localize('pinEditor', "Pin Editor");
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		let editor = this.editorService.getActiveEditor();
		if (editor) {
			this.editorService.pinEditor(editor.position, editor.input);
		}

		return TPromise.as(true);
	}
}

731
export class UnpinEditorAction extends Action {
732

733 734
	public static ID = 'workbench.action.unpinEditor';
	public static LABEL = nls.localize('unpinEditor', "Unpin Editor");
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		let editor = this.editorService.getActiveEditor();
		if (editor) {
			this.editorService.unpinEditor(editor.position, editor.input);
		}

		return TPromise.as(true);
	}
}

754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
export abstract class BaseNavigateEditorAction extends Action {

	constructor(id: string, label: string, protected editorService: IWorkbenchEditorService) {
		super(id, label);
	}

	public run(): TPromise<any> {
		const model = this.editorService.getStacksModel();
		const result = this.navigate();
		if (result) {
			return this.editorService.openEditor(result.editor, null, model.positionOfGroup(result.group));
		}

		return TPromise.as(false);
	}

	protected abstract navigate(): IEditorIdentifier;
}

export class OpenNextEditor extends BaseNavigateEditorAction {

	public static ID = 'workbench.action.nextEditor';
	public static LABEL = nls.localize('openNextEditor', "Open Next Editor");

	constructor(id: string, label: string, @IWorkbenchEditorService editorService: IWorkbenchEditorService) {
		super(id, label, editorService);
	}

	protected navigate(): IEditorIdentifier {
B
Benjamin Pasero 已提交
783
		return this.editorService.getStacksModel().next();
784 785 786 787 788 789 790 791 792 793 794 795 796
	}
}

export class OpenPreviousEditor extends BaseNavigateEditorAction {

	public static ID = 'workbench.action.previousEditor';
	public static LABEL = nls.localize('openPreviousEditor', "Open Previous Editor");

	constructor(id: string, label: string, @IWorkbenchEditorService editorService: IWorkbenchEditorService) {
		super(id, label, editorService);
	}

	protected navigate(): IEditorIdentifier {
B
Benjamin Pasero 已提交
797
		return this.editorService.getStacksModel().previous();
798
	}
B
Benjamin Pasero 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
}

export class NavigateForwardAction extends Action {

	public static ID = 'workbench.action.navigateForward';
	public static LABEL = nls.localize('navigateNext', "Go Forward");

	constructor(id: string, label: string, @IHistoryService private historyService: IHistoryService) {
		super(id, label);
	}

	public run(): TPromise<any> {
		this.historyService.forward();

		return TPromise.as(null);
	}
}

export class NavigateBackwardsAction extends Action {

	public static ID = 'workbench.action.navigateBack';
	public static LABEL = nls.localize('navigatePrevious', "Go Back");

	constructor(id: string, label: string, @IHistoryService private historyService: IHistoryService) {
		super(id, label);
	}

	public run(): TPromise<any> {
		this.historyService.back();

		return TPromise.as(null);
	}
831
}