editorActions.ts 33.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
import nls = require('vs/nls');
9
import {Action} from 'vs/base/common/actions';
E
Erich Gamma 已提交
10
import {BaseEditor} from 'vs/workbench/browser/parts/editor/baseEditor';
11
import {EditorInput, getUntitledOrFileResource, TextEditorOptions, EditorOptions, IEditorIdentifier} from 'vs/workbench/common/editor';
E
Erich Gamma 已提交
12
import {QuickOpenEntryGroup} from 'vs/base/parts/quickopen/browser/quickOpenModel';
B
Benjamin Pasero 已提交
13
import {EditorQuickOpenEntry, EditorQuickOpenEntryGroup, IEditorQuickOpenEntry, QuickOpenAction} from 'vs/workbench/browser/quickopen';
14
import {IWorkbenchEditorService, GroupArrangement} from 'vs/workbench/services/editor/common/editorService';
15
import {IQuickOpenService, IPickOpenEntry} from 'vs/workbench/services/quickopen/common/quickOpenService';
16
import {IPartService} from 'vs/workbench/services/part/common/partService';
B
Benjamin Pasero 已提交
17
import {Position, IEditor, Direction, IResourceInput} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
18
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
B
Benjamin Pasero 已提交
19
import {IHistoryService} from 'vs/workbench/services/history/common/history';
B
Benjamin Pasero 已提交
20
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
E
Erich Gamma 已提交
21 22 23

export class SplitEditorAction extends Action {

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

E
Erich Gamma 已提交
27
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
I
isidor 已提交
28
		super(id, label, 'split-editor-action');
E
Erich Gamma 已提交
29 30
	}

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

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

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

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

64 65
				// Push the center group to the right to make room for the splitted input
				else if (activeEditor.position === Position.LEFT) {
E
Erich Gamma 已提交
66 67
					let options = new TextEditorOptions();
					options.preserveFocus = true;
68
					options.pinned = true;
E
Erich Gamma 已提交
69

70 71 72
					return this.editorService.openEditor(activeEditor.input, options, Position.RIGHT).then(() => {
						this.editorService.moveGroup(Position.RIGHT, Position.CENTER);
						this.editorService.focusGroup(Position.CENTER);
E
Erich Gamma 已提交
73 74 75 76
					});
				}
		}

77 78
		// Only split if we have a target position to split to
		if (typeof targetPosition === 'number') {
79
			return this.editorService.openEditor(activeEditor.input, EditorOptions.create({ pinned: true }), targetPosition);
E
Erich Gamma 已提交
80 81
		}

A
Alex Dima 已提交
82
		return TPromise.as(true);
E
Erich Gamma 已提交
83 84 85
	}
}

B
Benjamin Pasero 已提交
86
export class NavigateBetweenGroupsAction extends Action {
87

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

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

95
	public run(): TPromise<any> {
E
Erich Gamma 已提交
96 97 98 99

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

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

108 109 110
		this.editorService.focusGroup(<Position>newIndex);

		return TPromise.as(true);
E
Erich Gamma 已提交
111 112 113
	}
}

114 115
export class FocusFirstGroupAction extends Action {

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

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

128
	public run(): TPromise<any> {
E
Erich Gamma 已提交
129 130 131 132 133

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

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

		// 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 已提交
156
		return TPromise.as(true);
E
Erich Gamma 已提交
157 158 159
	}
}

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

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

	protected abstract getReferenceEditorSide(): Position;

	protected abstract getTargetEditorSide(): Position;

175
	public run(): TPromise<any> {
E
Erich Gamma 已提交
176 177 178 179 180 181 182 183 184

		// 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()) {
185 186 187
				this.editorService.focusGroup(editor.position);

				return TPromise.as(true);
E
Erich Gamma 已提交
188 189 190 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
			}

			// 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 已提交
219
		return TPromise.as(true);
E
Erich Gamma 已提交
220 221 222
	}
}

223 224
export class FocusSecondGroupAction extends BaseFocusSideGroupAction {

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

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

246 247
export class FocusThirdGroupAction extends BaseFocusSideGroupAction {

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

	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 已提交
269
export class FocusPreviousGroup extends Action {
270

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

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

278
	public run(): TPromise<any> {
E
Erich Gamma 已提交
279 280 281 282

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


		// 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
294 295 296
		this.editorService.focusGroup(nextPosition);

		return TPromise.as(true);
E
Erich Gamma 已提交
297 298 299
	}
}

B
Benjamin Pasero 已提交
300
export class FocusNextGroup extends Action {
301

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

E
Erich Gamma 已提交
305 306 307 308 309 310 311 312 313 314 315
	private navigateActions: Action[];

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

		this.navigateActions = [];
316 317 318
		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 已提交
319 320
	}

321
	public run(event?: any): TPromise<any> {
E
Erich Gamma 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334

		// 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
335
		if (typeof nextPosition === 'number' && this.navigateActions[nextPosition]) {
E
Erich Gamma 已提交
336 337 338
			return this.navigateActions[nextPosition].run(event);
		}

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

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

348
	constructor( @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
E
Erich Gamma 已提交
349 350 351 352 353 354 355 356 357 358 359 360
		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);
	}

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

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

A
Alex Dima 已提交
375
		return TPromise.as(false);
E
Erich Gamma 已提交
376 377 378
	}
}

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

	// 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 {
398 399 400 401

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

E
Erich Gamma 已提交
402
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
I
isidor 已提交
403
		super(id, label, 'close-editor-action');
E
Erich Gamma 已提交
404 405
	}

I
isidor 已提交
406 407
	public run(context: IEditorContext): TPromise<any> {
		let position = context ? this.editorService.getStacksModel().positionOfGroup(context.group) : null;
408

409
		// Close Active Editor
410
		if (typeof position !== 'number') {
B
Benjamin Pasero 已提交
411 412
			let activeEditor = this.editorService.getActiveEditor();
			if (activeEditor) {
413
				return this.editorService.closeEditor(activeEditor.position, activeEditor.input);
B
Benjamin Pasero 已提交
414
			}
415 416
		}

I
isidor 已提交
417
		let input = context ? context.editor : null;
I
isidor 已提交
418
		if (!input) {
B
Benjamin Pasero 已提交
419

I
isidor 已提交
420 421 422
			// Get Top Editor at Position
			let visibleEditors = this.editorService.getVisibleEditors();
			if (visibleEditors[position]) {
423
				input = visibleEditors[position].input;
I
isidor 已提交
424 425 426 427 428
			}
		}

		if (input) {
			return this.editorService.closeEditor(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
export class CloseEditorsInGroupAction extends Action {

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

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

I
isidor 已提交
444 445
	public run(context: IEditorContext): TPromise<any> {
		let position = context ? this.editorService.getStacksModel().positionOfGroup(context.group) : null;
446
		if (typeof position !== 'number') {
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
			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 已提交
463 464
	public static ID = 'workbench.action.closeEditorsToTheLeft';
	public static LABEL = nls.localize('closeEditorsToTheLeft', "Close Editors to the Left");
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

	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 已提交
482 483
	public static ID = 'workbench.action.closeEditorsToTheRight';
	public static LABEL = nls.localize('closeEditorsToTheRight', "Close Editors to the Right");
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

	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 已提交
499 500
export class CloseAllEditorsAction extends Action {

501 502 503
	public static ID = 'workbench.action.closeAllEditors';
	public static LABEL = nls.localize('closeAllEditors', "Close All Editors");

E
Erich Gamma 已提交
504
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
I
isidor 已提交
505
		super(id, label, 'action-close-all-files');
E
Erich Gamma 已提交
506 507
	}

508
	public run(): TPromise<any> {
509
		return this.editorService.closeAllEditors();
E
Erich Gamma 已提交
510 511 512
	}
}

513 514 515 516 517 518 519 520 521
export class CloseEditorsInOtherGroupsAction extends Action {

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

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

I
isidor 已提交
522 523
	public run(context: IEditorContext): TPromise<any> {
		let position = context ? this.editorService.getStacksModel().positionOfGroup(context.group) : null;
524
		if (typeof position !== 'number') {
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
			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 已提交
542
	public static LABEL = nls.localize('closeOtherEditorsInGroup', "Close Other Editors");
E
Erich Gamma 已提交
543 544 545 546 547

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

I
isidor 已提交
548 549 550
	public run(context: IEditorContext): TPromise<any> {
		let position = context ? this.editorService.getStacksModel().positionOfGroup(context.group) : null;
		let input = context ? context.editor : null;
I
isidor 已提交
551 552

		// If position or input are not passed in take the position and input of the active editor.
553 554
		const active = this.editorService.getActiveEditor();
		if (active) {
I
isidor 已提交
555
			position = typeof position === 'number' ? position : active.position;
556
			input = input ? input : <EditorInput>active.input;
I
isidor 已提交
557 558 559 560
		}

		if (typeof position === 'number' && input) {
			return this.editorService.closeEditors(position, input);
561 562 563
		}

		return TPromise.as(false);
E
Erich Gamma 已提交
564 565 566
	}
}

I
isidor 已提交
567 568 569 570 571 572 573 574 575
export class CloseAllEditorsInGroupAction extends Action {

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

	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label, 'action-close-all-files');
	}

I
isidor 已提交
576 577
	public run(context: IEditorContext): TPromise<any> {
		let position = context ? this.editorService.getStacksModel().positionOfGroup(context.group) : null;
I
isidor 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590
		if (typeof position !== 'number') {
			let activeEditor = this.editorService.getActiveEditor();
			if (activeEditor) {
				position = activeEditor.position;
			}
		}

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

B
Benjamin Pasero 已提交
591
export class MoveGroupLeftAction extends Action {
E
Erich Gamma 已提交
592

B
Benjamin Pasero 已提交
593
	public static ID = 'workbench.action.moveActiveEditorGroupLeft';
594 595
	public static LABEL = nls.localize('moveActiveGroupLeft', "Move Editor Group Left");

E
Erich Gamma 已提交
596 597 598 599
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

I
isidor 已提交
600 601
	public run(context: IEditorContext): TPromise<any> {
		let position = context ? this.editorService.getStacksModel().positionOfGroup(context.group) : null;
602
		if (typeof position !== 'number') {
603 604 605 606 607 608 609 610
			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 已提交
611

B
Benjamin Pasero 已提交
612
			// Move group
613
			this.editorService.moveGroup(position, newPosition);
E
Erich Gamma 已提交
614 615
		}

A
Alex Dima 已提交
616
		return TPromise.as(false);
E
Erich Gamma 已提交
617 618 619
	}
}

B
Benjamin Pasero 已提交
620
export class MoveGroupRightAction extends Action {
E
Erich Gamma 已提交
621

B
Benjamin Pasero 已提交
622
	public static ID = 'workbench.action.moveActiveEditorGroupRight';
623 624
	public static LABEL = nls.localize('moveActiveGroupRight', "Move Editor Group Right");

E
Erich Gamma 已提交
625 626 627 628
	constructor(id: string, label: string, @IWorkbenchEditorService private editorService: IWorkbenchEditorService) {
		super(id, label);
	}

I
isidor 已提交
629 630
	public run(context: IEditorContext): TPromise<any> {
		let position = context ? this.editorService.getStacksModel().positionOfGroup(context.group) : null;
631
		if (typeof position !== 'number') {
632 633 634 635 636 637 638 639 640 641
			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 已提交
642

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

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

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

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

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

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

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

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

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

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

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

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

683
export class MaximizeGroupAction extends Action {
684

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

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

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

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

707
export class PinEditorAction extends Action {
708

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

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

730
export class UnpinEditorAction extends Action {
731

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

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

753 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
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 已提交
782
		return this.editorService.getStacksModel().next();
783 784 785 786 787 788 789 790 791 792 793 794 795
	}
}

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 已提交
796
		return this.editorService.getStacksModel().previous();
797
	}
B
Benjamin Pasero 已提交
798 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
}

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);
	}
I
isidor 已提交
830
}
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

export class ReopenClosedEditorAction extends Action {

	public static ID = 'workbench.action.reopenClosedEditor';
	public static LABEL = nls.localize('reopenClosedEditor', "Reopen Closed Editor");

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

	public run(): TPromise<any> {
		const stacks = this.editorService.getStacksModel();

		// Find an editor that was closed and is currently not opened in the group
		let lastClosedEditor = stacks.popLastClosedEditor();
		while (lastClosedEditor && stacks.activeGroup && stacks.activeGroup.indexOf(lastClosedEditor) >= 0) {
			lastClosedEditor = stacks.popLastClosedEditor();
		}

		if (lastClosedEditor) {
			this.editorService.openEditor(lastClosedEditor, EditorOptions.create({ pinned: true }));
		}

		return TPromise.as(false);
	}
B
Benjamin Pasero 已提交
861 862
}

863
export const NAVIGATE_IN_LEFT_GROUP_PREFIX = 'edt left ';
B
Benjamin Pasero 已提交
864

865
export class ShowEditorsInLeftGroupAction extends QuickOpenAction {
B
Benjamin Pasero 已提交
866

867 868
	public static ID = 'workbench.action.showEditorsInLeftGroup';
	public static LABEL = nls.localize('showEditorsInLeftGroup', "Show Editors in Left Group");
B
Benjamin Pasero 已提交
869

870 871 872 873 874 875
	constructor(
		actionId: string,
		actionLabel: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
876
		super(actionId, actionLabel, NAVIGATE_IN_LEFT_GROUP_PREFIX, quickOpenService);
877 878

		this.class = 'show-group-editors-action';
B
Benjamin Pasero 已提交
879
	}
880
}
881

882 883 884 885 886 887 888 889 890 891 892 893 894 895
export const NAVIGATE_IN_CENTER_GROUP_PREFIX = 'edt center ';

export class ShowEditorsInCenterGroupAction extends QuickOpenAction {

	public static ID = 'workbench.action.showEditorsInCenterGroup';
	public static LABEL = nls.localize('showEditorsInCenterGroup', "Show Editors in Center Group");

	constructor(
		actionId: string,
		actionLabel: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(actionId, actionLabel, NAVIGATE_IN_CENTER_GROUP_PREFIX, quickOpenService);
896 897

		this.class = 'show-group-editors-action';
898 899
	}
}
900

901 902 903 904 905 906 907 908 909 910 911 912 913 914
export const NAVIGATE_IN_RIGHT_GROUP_PREFIX = 'edt right ';

export class ShowEditorsInRightGroupAction extends QuickOpenAction {

	public static ID = 'workbench.action.showEditorsInRightGroup';
	public static LABEL = nls.localize('showEditorsInRightGroup', "Show Editors in Right Group");

	constructor(
		actionId: string,
		actionLabel: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(actionId, actionLabel, NAVIGATE_IN_RIGHT_GROUP_PREFIX, quickOpenService);
915 916

		this.class = 'show-group-editors-action';
917
	}
B
Benjamin Pasero 已提交
918 919
}

920
export const NAVIGATE_ALL_EDITORS_GROUP_PREFIX = 'edt ';
B
Benjamin Pasero 已提交
921 922 923 924 925 926 927 928 929 930 931

export class ShowAllEditorsAction extends QuickOpenAction {

	public static ID = 'workbench.action.showAllEditors';
	public static LABEL = nls.localize('showAllEditors', "Show All Editors");

	constructor(actionId: string, actionLabel: string, @IQuickOpenService quickOpenService: IQuickOpenService) {
		super(actionId, actionLabel, NAVIGATE_ALL_EDITORS_GROUP_PREFIX, quickOpenService);
	}
}

B
Benjamin Pasero 已提交
932 933 934 935 936 937 938 939 940
export class OpenPreviousEditorInGroupAction extends Action {

	public static ID = 'workbench.action.openPreviousEditorInGroup';
	public static LABEL = nls.localize('openPreviousEditorInGroup', "Open Previous in Editor Group");

	constructor(
		id: string,
		label: string,
		@IQuickOpenService private quickOpenService: IQuickOpenService,
941 942
		@IKeybindingService private keybindingService: IKeybindingService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
B
Benjamin Pasero 已提交
943 944 945 946 947 948 949
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		let keys = this.keybindingService.lookupKeybindings(this.id);

950 951 952 953 954 955 956 957 958 959 960 961
		const stacks = this.editorService.getStacksModel();
		if (stacks.activeGroup) {
			const activePosition = stacks.positionOfGroup(stacks.activeGroup);
			const count = stacks.groups.length;
			let prefix = NAVIGATE_IN_LEFT_GROUP_PREFIX;

			if (activePosition === Position.CENTER && count === 3) {
				prefix = NAVIGATE_IN_CENTER_GROUP_PREFIX;
			} else if (activePosition === Position.RIGHT || (activePosition === Position.CENTER && count === 2)) {
				prefix = NAVIGATE_IN_RIGHT_GROUP_PREFIX;
			}

B
Benjamin Pasero 已提交
962
			this.quickOpenService.show(prefix, { quickNavigateConfiguration: { keybindings: keys } });
963
		}
B
Benjamin Pasero 已提交
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987

		return TPromise.as(true);
	}
}

export class GlobalQuickOpenAction extends Action {

	public static ID = 'workbench.action.quickOpen';
	public static LABEL = nls.localize('quickOpen', "Go to File...");

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

		this.order = 100; // Allow other actions to position before or after
		this.class = 'quickopen';
	}

	public run(): TPromise<any> {
		this.quickOpenService.show(null);

		return TPromise.as(true);
	}
}

988
export class OpenPreviousEditorFromHistoryAction extends Action {
B
Benjamin Pasero 已提交
989

990
	public static ID = 'workbench.action.openPreviousEditorFromHistory';
B
Benjamin Pasero 已提交
991
	public static LABEL = nls.localize('navigateEditorHistoryByInput', "Open Previous Editor from History");
B
Benjamin Pasero 已提交
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004

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

	public run(): TPromise<any> {
		let keys = this.keybindingService.lookupKeybindings(this.id);

B
Benjamin Pasero 已提交
1005
		this.quickOpenService.show(null, { quickNavigateConfiguration: { keybindings: keys } });
B
Benjamin Pasero 已提交
1006 1007 1008 1009 1010

		return TPromise.as(true);
	}
}

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

	public static ID = 'workbench.action.clearEditorHistory';
	public static LABEL = nls.localize('clearEditorHistory', "Clear Editor History");

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

	public run(): TPromise<any> {

		// Quick open history
		this.quickOpenService.clearEditorHistory();

		// Editor cursor history
		this.historyService.clear();

		// Recently closed editors
		this.editorService.getStacksModel().clearLastClosedEditors();

		return TPromise.as(true);
	}
}

B
Benjamin Pasero 已提交
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
export class BaseQuickOpenNavigateAction extends Action {
	private navigateNext: boolean;

	constructor(
		id: string,
		label: string,
		navigateNext: boolean,
		@IQuickOpenService private quickOpenService: IQuickOpenService,
		@IKeybindingService private keybindingService: IKeybindingService
	) {
		super(id, label);

		this.navigateNext = navigateNext;
	}

	public run(event?: any): TPromise<any> {
		let keys = this.keybindingService.lookupKeybindings(this.id);

		this.quickOpenService.quickNavigate({
			keybindings: keys
		}, this.navigateNext);

		return TPromise.as(true);
	}
}

export class QuickOpenNavigateNextAction extends BaseQuickOpenNavigateAction {

	public static ID = 'workbench.action.quickOpenNavigateNext';
	public static LABEL = nls.localize('quickNavigateNext', "Navigate Next in Quick Open");

	constructor(
		id: string,
		label: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IKeybindingService keybindingService: IKeybindingService
	) {
		super(id, label, true, quickOpenService, keybindingService);
	}
}

export class QuickOpenNavigatePreviousAction extends BaseQuickOpenNavigateAction {

	public static ID = 'workbench.action.quickOpenNavigatePrevious';
	public static LABEL = nls.localize('quickNavigatePrevious', "Navigate Previous in Quick Open");

	constructor(
		id: string,
		label: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IKeybindingService keybindingService: IKeybindingService
	) {
		super(id, label, false, quickOpenService, keybindingService);
	}
1095 1096 1097 1098
}

interface IEditorPickOpenEntry extends IPickOpenEntry {
	identifier: IEditorIdentifier;
I
isidor 已提交
1099 1100 1101 1102 1103
}

export interface IEditorContext extends IEditorIdentifier {
	event: any;
}