editorActions.ts 43.8 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';

J
Johannes Rieken 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8
import nls = require('vs/nls');
J
Johannes Rieken 已提交
9
import { Action } from 'vs/base/common/actions';
10
import { mixin } from 'vs/base/common/objects';
S
Sandeep Somavarapu 已提交
11
import { getCodeEditor } from 'vs/editor/common/services/codeEditorService';
12
import { EditorInput, hasResource, TextEditorOptions, EditorOptions, IEditorIdentifier, IEditorContext, ActiveEditorMoveArguments, ActiveEditorMovePositioning, EditorCommands, ConfirmResult } from 'vs/workbench/common/editor';
J
Johannes Rieken 已提交
13 14 15
import { QuickOpenEntryGroup } from 'vs/base/parts/quickopen/browser/quickOpenModel';
import { EditorQuickOpenEntry, EditorQuickOpenEntryGroup, IEditorQuickOpenEntry, QuickOpenAction } from 'vs/workbench/browser/quickopen';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
16
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
J
Johannes Rieken 已提交
17
import { IPartService } from 'vs/workbench/services/part/common/partService';
18
import { Position, IEditor, Direction, IResourceInput, IEditorInput } from 'vs/platform/editor/common/editor';
J
Johannes Rieken 已提交
19 20 21 22
import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IEditorGroupService, GroupArrangement } from 'vs/workbench/services/group/common/groupService';
import { ICommandService } from 'vs/platform/commands/common/commands';
23
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
C
22768  
Cristian 已提交
24
import { IWindowsService } from 'vs/platform/windows/common/windows';
E
Erich Gamma 已提交
25 26 27

export class SplitEditorAction extends Action {

28 29 30
	public static ID = 'workbench.action.splitEditor';
	public static LABEL = nls.localize('splitEditor', "Split Editor");

31 32 33 34 35 36
	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IEditorGroupService private editorGroupService: IEditorGroupService
	) {
I
isidor 已提交
37
		super(id, label, 'split-editor-action');
E
Erich Gamma 已提交
38 39
	}

B
Benjamin Pasero 已提交
40
	public run(context?: IEditorContext): TPromise<any> {
41 42 43 44 45 46
		let editorToSplit: IEditor;
		if (context) {
			editorToSplit = this.editorService.getVisibleEditors()[this.editorGroupService.getStacksModel().positionOfGroup(context.group)];
		} else {
			editorToSplit = this.editorService.getActiveEditor();
		}
E
Erich Gamma 已提交
47

48 49
		// Can only split with target editor
		if (!editorToSplit) {
A
Alex Dima 已提交
50
			return TPromise.as(true);
E
Erich Gamma 已提交
51 52 53
		}

		// Return if the editor to split does not support split editing
54
		if (editorToSplit.input instanceof EditorInput && !(<EditorInput>editorToSplit.input).supportsSplitEditor()) {
A
Alex Dima 已提交
55
			return TPromise.as(true);
E
Erich Gamma 已提交
56 57
		}

58 59
		// Options
		let options: EditorOptions;
S
Sandeep Somavarapu 已提交
60 61
		const codeEditor = getCodeEditor(editorToSplit);
		if (codeEditor) {
62
			options = TextEditorOptions.fromEditor(codeEditor);
63 64 65 66 67
		} else {
			options = new EditorOptions();
		}
		options.pinned = true;

E
Erich Gamma 已提交
68
		// Count editors
69 70
		const visibleEditors = this.editorService.getVisibleEditors();
		const editorCount = visibleEditors.length;
E
Erich Gamma 已提交
71 72 73 74
		let targetPosition: Position;

		switch (editorCount) {

B
Benjamin Pasero 已提交
75
			// Open split editor to the right/bottom of left/top one
E
Erich Gamma 已提交
76
			case 1:
77
				targetPosition = Position.TWO;
E
Erich Gamma 已提交
78 79 80 81 82
				break;

			// Special case two editors opened
			case 2:

B
Benjamin Pasero 已提交
83
				// Continue splitting to the right/bottom
84 85
				if (editorToSplit.position === Position.TWO) {
					targetPosition = Position.THREE;
E
Erich Gamma 已提交
86 87
				}

88
				// Push the second group to the right/bottom to make room for the splitted input
89
				else if (editorToSplit.position === Position.ONE) {
E
Erich Gamma 已提交
90 91
					options.preserveFocus = true;

92 93 94
					return this.editorService.openEditor(editorToSplit.input, options, Position.THREE).then(() => {
						this.editorGroupService.moveGroup(Position.THREE, Position.TWO);
						this.editorGroupService.focusGroup(Position.TWO);
E
Erich Gamma 已提交
95 96 97 98
					});
				}
		}

99 100
		// Only split if we have a target position to split to
		if (typeof targetPosition === 'number') {
101
			return this.editorService.openEditor(editorToSplit.input, options, targetPosition);
E
Erich Gamma 已提交
102 103
		}

A
Alex Dima 已提交
104
		return TPromise.as(true);
E
Erich Gamma 已提交
105 106 107
	}
}

108
export class JoinTwoGroupsAction extends Action {
I
initialshl 已提交
109

110 111
	public static ID = 'workbench.action.joinTwoGroups';
	public static LABEL = nls.localize('joinTwoGroups', "Join Editors of Two Groups");
I
initialshl 已提交
112 113 114 115 116 117 118

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IEditorGroupService private editorGroupService: IEditorGroupService
	) {
119
		super(id, label);
I
initialshl 已提交
120 121 122 123
	}

	public run(context?: IEditorContext): TPromise<any> {

124
		const editorStacksModel = this.editorGroupService.getStacksModel();
I
initialshl 已提交
125 126

		// Return if has no other group to join to
127
		if (editorStacksModel.groups.length < 2) {
I
initialshl 已提交
128 129 130
			return TPromise.as(true);
		}

131 132 133 134 135 136 137 138 139
		let fromPosition: number;
		let toPosition: number;

		// Joining group is from context, or the active group
		if (context) {
			fromPosition = editorStacksModel.positionOfGroup(context.group);
		} else {
			fromPosition = editorStacksModel.positionOfGroup(editorStacksModel.activeGroup);
		}
I
initialshl 已提交
140

141 142 143
		// Target group is next group if joining from position one, otherwise it is the previous group
		if (fromPosition === Position.ONE) {
			toPosition = fromPosition + 1;
I
initialshl 已提交
144
		} else {
145
			toPosition = fromPosition - 1;
I
initialshl 已提交
146 147
		}

148 149
		const fromGroup = editorStacksModel.groupAt(fromPosition);
		const toGroup = editorStacksModel.groupAt(toPosition);
I
initialshl 已提交
150

151 152
		const activeEditor = fromGroup.activeEditor;
		const fromGroupEditors = fromGroup.getEditors();
I
initialshl 已提交
153

154 155 156 157 158 159 160 161 162 163 164
		// Insert the editors to the start if moving to the next group, otherwise insert to the end
		// If an editor exists in both groups, its index is respected as in the joining group
		const movingToNextGroup = fromPosition < toPosition;
		let index = movingToNextGroup ? 0 : toGroup.count;

		// Inactive and preserve focus options are used to prevent unnecessary switchings of active editor or group
		fromGroupEditors.forEach(e => {
			const inactive = e !== activeEditor;
			this.editorGroupService.moveEditor(e, fromPosition, toPosition, { index, inactive, preserveFocus: inactive });
			index = movingToNextGroup ? index + 1 : toGroup.count;
		});
I
initialshl 已提交
165

166 167
		// Focus may be lost when the joining group is closed, regain focus on the target group
		this.editorGroupService.focusGroup(toGroup);
I
initialshl 已提交
168 169 170 171 172

		return TPromise.as(true);
	}
}

B
Benjamin Pasero 已提交
173
export class NavigateBetweenGroupsAction extends Action {
174

B
Benjamin Pasero 已提交
175 176
	public static ID = 'workbench.action.navigateEditorGroups';
	public static LABEL = nls.localize('navigateEditorGroups', "Navigate Between Editor Groups");
E
Erich Gamma 已提交
177

178 179 180 181 182 183
	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IEditorGroupService private editorGroupService: IEditorGroupService
	) {
E
Erich Gamma 已提交
184 185 186
		super(id, label);
	}

187
	public run(): TPromise<any> {
E
Erich Gamma 已提交
188 189

		// Can cycle split with active editor
190
		const activeEditor = this.editorService.getActiveEditor();
E
Erich Gamma 已提交
191
		if (!activeEditor) {
A
Alex Dima 已提交
192
			return TPromise.as(false);
E
Erich Gamma 已提交
193 194
		}

B
Benjamin Pasero 已提交
195
		// Cycle to the left/top and use module to start at 0 again
196 197 198
		const visibleEditors = this.editorService.getVisibleEditors();
		const editorCount = visibleEditors.length;
		const newIndex = (activeEditor.position + 1) % editorCount;
E
Erich Gamma 已提交
199

200
		this.editorGroupService.focusGroup(<Position>newIndex);
201 202

		return TPromise.as(true);
E
Erich Gamma 已提交
203 204 205
	}
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219
export class FocusActiveGroupAction extends Action {

	public static ID = 'workbench.action.focusActiveEditorGroup';
	public static LABEL = nls.localize('focusActiveEditorGroup', "Focus Active Editor Group");

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

	public run(): TPromise<any> {
B
hygiene  
Benjamin Pasero 已提交
220 221
		const activeEditor = this.editorService.getActiveEditor();
		if (activeEditor) {
D
Daniel Imms 已提交
222
			activeEditor.focus();
B
hygiene  
Benjamin Pasero 已提交
223
		}
D
Daniel Imms 已提交
224

225
		return TPromise.as(true);
E
Erich Gamma 已提交
226 227 228
	}
}

229 230
export class FocusFirstGroupAction extends Action {

B
Benjamin Pasero 已提交
231
	public static ID = 'workbench.action.focusFirstEditorGroup';
232
	public static LABEL = nls.localize('focusFirstEditorGroup', "Focus First Editor Group");
E
Erich Gamma 已提交
233 234 235 236 237

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
238
		@IEditorGroupService private editorGroupService: IEditorGroupService,
239
		@IHistoryService private historyService: IHistoryService
E
Erich Gamma 已提交
240 241 242 243
	) {
		super(id, label);
	}

244
	public run(): TPromise<any> {
E
Erich Gamma 已提交
245

B
Benjamin Pasero 已提交
246
		// Find left/top editor and focus it
247
		const editors = this.editorService.getVisibleEditors();
B
Benjamin Pasero 已提交
248
		for (let editor of editors) {
249 250
			if (editor.position === Position.ONE) {
				this.editorGroupService.focusGroup(Position.ONE);
251 252

				return TPromise.as(true);
E
Erich Gamma 已提交
253 254 255 256
			}
		}

		// Since no editor is currently opened, try to open last history entry to the target side
257
		const history = this.historyService.getHistory();
B
Benjamin Pasero 已提交
258
		for (let input of history) {
E
Erich Gamma 已提交
259

260 261
			// For now only support to open files from history to the side
			if (input instanceof EditorInput) {
262
				if (hasResource(input, { filter: ['file', 'untitled'] })) {
263
					return this.editorService.openEditor(input, null, Position.ONE);
264 265
				}
			} else {
266
				return this.editorService.openEditor(input as IResourceInput, Position.ONE);
E
Erich Gamma 已提交
267 268 269
			}
		}

A
Alex Dima 已提交
270
		return TPromise.as(true);
E
Erich Gamma 已提交
271 272 273
	}
}

274
export abstract class BaseFocusSideGroupAction extends Action {
E
Erich Gamma 已提交
275 276 277 278 279

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
280
		@IEditorGroupService private editorGroupService: IEditorGroupService,
281
		@IHistoryService private historyService: IHistoryService
E
Erich Gamma 已提交
282 283 284 285 286 287 288 289
	) {
		super(id, label);
	}

	protected abstract getReferenceEditorSide(): Position;

	protected abstract getTargetEditorSide(): Position;

290
	public run(): TPromise<any> {
E
Erich Gamma 已提交
291 292

		// Require at least the reference editor to be visible
293
		const editors = this.editorService.getVisibleEditors();
E
Erich Gamma 已提交
294 295
		let referenceEditor: IEditor;
		for (let i = 0; i < editors.length; i++) {
296
			const editor = editors[i];
E
Erich Gamma 已提交
297 298 299

			// Target editor exists so focus it
			if (editor.position === this.getTargetEditorSide()) {
300
				this.editorGroupService.focusGroup(editor.position);
301 302

				return TPromise.as(true);
E
Erich Gamma 已提交
303 304 305 306 307 308 309 310 311
			}

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

		// Require the reference editor to be visible and supporting split editor
312
		if (referenceEditor && (<EditorInput>referenceEditor.input).supportsSplitEditor()) {
313 314 315

			// Options
			let options: EditorOptions;
S
Sandeep Somavarapu 已提交
316 317
			const codeEditor = getCodeEditor(referenceEditor);
			if (codeEditor) {
318
				options = TextEditorOptions.fromEditor(codeEditor, { pinned: true });
319 320
			} else {
				options = EditorOptions.create({ pinned: true });
321 322 323
			}

			return this.editorService.openEditor(referenceEditor.input, options, this.getTargetEditorSide());
E
Erich Gamma 已提交
324 325 326 327
		}

		// Otherwise try to find a history entry to open to the target editor side
		else if (referenceEditor) {
328
			const history = this.historyService.getHistory();
B
Benjamin Pasero 已提交
329
			for (let input of history) {
E
Erich Gamma 已提交
330 331

				// For now only support to open files from history to the side
332
				if (input instanceof EditorInput) {
333
					if (hasResource(input, { filter: ['file', 'untitled'] })) {
334 335 336 337
						return this.editorService.openEditor(input, { pinned: true }, this.getTargetEditorSide());
					}
				} else {
					return this.editorService.openEditor({ resource: (input as IResourceInput).resource, options: { pinned: true } }, this.getTargetEditorSide());
E
Erich Gamma 已提交
338 339 340 341
				}
			}
		}

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

346 347
export class FocusSecondGroupAction extends BaseFocusSideGroupAction {

B
Benjamin Pasero 已提交
348
	public static ID = 'workbench.action.focusSecondEditorGroup';
349
	public static LABEL = nls.localize('focusSecondEditorGroup', "Focus Second Editor Group");
E
Erich Gamma 已提交
350 351 352 353 354

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
355
		@IEditorGroupService editorGroupService: IEditorGroupService,
356
		@IHistoryService historyService: IHistoryService
E
Erich Gamma 已提交
357
	) {
358
		super(id, label, editorService, editorGroupService, historyService);
E
Erich Gamma 已提交
359 360 361
	}

	protected getReferenceEditorSide(): Position {
362
		return Position.ONE;
E
Erich Gamma 已提交
363 364 365
	}

	protected getTargetEditorSide(): Position {
366
		return Position.TWO;
E
Erich Gamma 已提交
367 368 369
	}
}

370 371
export class FocusThirdGroupAction extends BaseFocusSideGroupAction {

B
Benjamin Pasero 已提交
372
	public static ID = 'workbench.action.focusThirdEditorGroup';
373
	public static LABEL = nls.localize('focusThirdEditorGroup', "Focus Third Editor Group");
E
Erich Gamma 已提交
374 375 376 377 378

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService,
379
		@IEditorGroupService editorGroupService: IEditorGroupService,
380
		@IHistoryService historyService: IHistoryService
E
Erich Gamma 已提交
381
	) {
382
		super(id, label, editorService, editorGroupService, historyService);
E
Erich Gamma 已提交
383 384 385
	}

	protected getReferenceEditorSide(): Position {
386
		return Position.TWO;
E
Erich Gamma 已提交
387 388 389
	}

	protected getTargetEditorSide(): Position {
390
		return Position.THREE;
E
Erich Gamma 已提交
391 392 393
	}
}

B
Benjamin Pasero 已提交
394
export class FocusPreviousGroup extends Action {
395

B
Benjamin Pasero 已提交
396 397
	public static ID = 'workbench.action.focusPreviousGroup';
	public static LABEL = nls.localize('focusPreviousGroup', "Focus Previous Group");
E
Erich Gamma 已提交
398

399 400 401 402 403 404
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
E
Erich Gamma 已提交
405 406 407
		super(id, label);
	}

408
	public run(): TPromise<any> {
E
Erich Gamma 已提交
409 410

		// Require an active editor
411
		const activeEditor = this.editorService.getActiveEditor();
E
Erich Gamma 已提交
412
		if (!activeEditor) {
A
Alex Dima 已提交
413
			return TPromise.as(true);
E
Erich Gamma 已提交
414 415
		}

416 417
		const stacks = this.editorGroupService.getStacksModel();
		const groupCount = stacks.groups.length;
E
Erich Gamma 已提交
418

419 420 421
		// Nothing to do if the only group
		if (groupCount === 1) {
			return TPromise.as(true);
E
Erich Gamma 已提交
422 423
		}

424 425 426
		// Nevigate to the previous group or to the last group if the first group is active
		const newPositionIndex = (activeEditor.position + groupCount - 1) % groupCount;
		this.editorGroupService.focusGroup(<Position>newPositionIndex);
427 428

		return TPromise.as(true);
E
Erich Gamma 已提交
429 430 431
	}
}

B
Benjamin Pasero 已提交
432
export class FocusNextGroup extends Action {
433

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

E
Erich Gamma 已提交
437 438 439
	constructor(
		id: string,
		label: string,
440 441
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
E
Erich Gamma 已提交
442 443 444 445
	) {
		super(id, label);
	}

446
	public run(event?: any): TPromise<any> {
E
Erich Gamma 已提交
447

448
		const activeEditor = this.editorService.getActiveEditor();
449

450 451
		if (!activeEditor) {
			return TPromise.as(true);
E
Erich Gamma 已提交
452 453
		}

454 455 456 457 458 459
		const stacks = this.editorGroupService.getStacksModel();
		const groupCount = stacks.groups.length;

		// Nowhere to switch if the only group
		if (groupCount === 1) {
			return TPromise.as(true);
E
Erich Gamma 已提交
460 461
		}

462 463 464 465
		// Nevigate to the next group or to the first group if the last group is active
		const newPositionIndex = (activeEditor.position + 1) % groupCount;
		this.editorGroupService.focusGroup(<Position>newPositionIndex);

A
Alex Dima 已提交
466
		return TPromise.as(true);
E
Erich Gamma 已提交
467 468 469 470 471 472 473 474
	}
}

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

475 476
	constructor(
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
477
		@IEditorGroupService private editorGroupService: IEditorGroupService
478
	) {
E
Erich Gamma 已提交
479 480 481
		super(OpenToSideAction.OPEN_TO_SIDE_ID, OpenToSideAction.OPEN_TO_SIDE_LABEL);

		this.updateEnablement();
482 483 484 485
		this.updateClass();
	}

	public updateClass(): void {
486
		const editorGroupLayoutVertical = (this.editorGroupService.getGroupOrientation() !== 'horizontal');
487

488
		this.class = editorGroupLayoutVertical ? 'quick-open-sidebyside-vertical' : 'quick-open-sidebyside-horizontal';
E
Erich Gamma 已提交
489 490 491
	}

	private updateEnablement(): void {
492
		const activeEditor = this.editorService.getActiveEditor();
493
		this.enabled = (!activeEditor || activeEditor.position !== Position.THREE);
E
Erich Gamma 已提交
494 495
	}

496
	public run(context: any): TPromise<any> {
E
Erich Gamma 已提交
497 498
		let entry = toEditorQuickOpenEntry(context);
		if (entry) {
499
			const input = entry.getInput();
B
Benjamin Pasero 已提交
500
			if (input instanceof EditorInput) {
501
				return this.editorService.openEditor(input, entry.getOptions(), true);
B
Benjamin Pasero 已提交
502 503
			}

504 505 506 507
			const resourceInput = input as IResourceInput;
			resourceInput.options = mixin(resourceInput.options, entry.getOptions());

			return this.editorService.openEditor(resourceInput, true);
E
Erich Gamma 已提交
508 509
		}

A
Alex Dima 已提交
510
		return TPromise.as(false);
E
Erich Gamma 已提交
511 512 513
	}
}

514
export function toEditorQuickOpenEntry(element: any): IEditorQuickOpenEntry {
E
Erich Gamma 已提交
515 516 517

	// QuickOpenEntryGroup
	if (element instanceof QuickOpenEntryGroup) {
518
		const group = <QuickOpenEntryGroup>element;
E
Erich Gamma 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532
		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 {
533

B
Benjamin Pasero 已提交
534
	public static ID = 'workbench.action.closeActiveEditor';
535 536
	public static LABEL = nls.localize('closeEditor', "Close Editor");

537 538 539 540 541 542
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
I
isidor 已提交
543
		super(id, label, 'close-editor-action');
E
Erich Gamma 已提交
544 545
	}

B
Benjamin Pasero 已提交
546
	public run(context?: IEditorContext): TPromise<any> {
547
		const position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
548

549
		// Close Active Editor
550
		if (typeof position !== 'number') {
551
			const activeEditor = this.editorService.getActiveEditor();
B
Benjamin Pasero 已提交
552
			if (activeEditor) {
553
				return this.editorService.closeEditor(activeEditor.position, activeEditor.input);
B
Benjamin Pasero 已提交
554
			}
555 556
		}

I
isidor 已提交
557
		let input = context ? context.editor : null;
I
isidor 已提交
558
		if (!input) {
B
Benjamin Pasero 已提交
559

I
isidor 已提交
560
			// Get Top Editor at Position
561
			const visibleEditors = this.editorService.getVisibleEditors();
I
isidor 已提交
562
			if (visibleEditors[position]) {
563
				input = visibleEditors[position].input;
I
isidor 已提交
564 565 566 567 568
			}
		}

		if (input) {
			return this.editorService.closeEditor(position, input);
E
Erich Gamma 已提交
569 570
		}

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

M
misoguy 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
export class RevertAndCloseEditorAction extends Action {

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

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

	public run(): TPromise<any> {
		const activeEditor = this.editorService.getActiveEditor();
B
Benjamin Pasero 已提交
590 591 592 593
		if (activeEditor && activeEditor.input) {
			const input = activeEditor.input;
			const position = activeEditor.position;

M
misoguy 已提交
594
			return activeEditor.input.revert().then(ok =>
B
Benjamin Pasero 已提交
595
				this.editorService.closeEditor(position, input)
M
misoguy 已提交
596 597 598 599 600 601 602
			);
		}

		return TPromise.as(false);
	}
}

B
Benjamin Pasero 已提交
603
export class CloseLeftEditorsInGroupAction extends Action {
604

B
Benjamin Pasero 已提交
605 606
	public static ID = 'workbench.action.closeEditorsToTheLeft';
	public static LABEL = nls.localize('closeEditorsToTheLeft', "Close Editors to the Left");
607

608 609 610
	constructor(
		id: string,
		label: string,
B
Benjamin Pasero 已提交
611 612
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IEditorGroupService private groupService: IEditorGroupService
613
	) {
614 615 616
		super(id, label);
	}

B
Benjamin Pasero 已提交
617
	public run(context?: IEditorContext): TPromise<any> {
618
		const editor = getTarget(this.editorService, this.groupService, context);
B
Benjamin Pasero 已提交
619 620
		if (editor) {
			return this.editorService.closeEditors(editor.position, editor.input, Direction.LEFT);
621 622 623 624 625 626 627 628
		}

		return TPromise.as(false);
	}
}

export class CloseRightEditorsInGroupAction extends Action {

B
Benjamin Pasero 已提交
629 630
	public static ID = 'workbench.action.closeEditorsToTheRight';
	public static LABEL = nls.localize('closeEditorsToTheRight', "Close Editors to the Right");
631

B
Benjamin Pasero 已提交
632 633 634 635 636 637
	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IEditorGroupService private groupService: IEditorGroupService
	) {
638 639 640
		super(id, label);
	}

B
Benjamin Pasero 已提交
641
	public run(context?: IEditorContext): TPromise<any> {
642
		const editor = getTarget(this.editorService, this.groupService, context);
B
Benjamin Pasero 已提交
643 644
		if (editor) {
			return this.editorService.closeEditors(editor.position, editor.input, Direction.RIGHT);
645 646 647 648 649 650
		}

		return TPromise.as(false);
	}
}

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

653 654 655
	public static ID = 'workbench.action.closeAllEditors';
	public static LABEL = nls.localize('closeAllEditors', "Close All Editors");

656 657 658 659 660 661
	constructor(
		id: string,
		label: string,
		@ITextFileService private textFileService: ITextFileService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
I
isidor 已提交
662
		super(id, label, 'action-close-all-files');
E
Erich Gamma 已提交
663 664
	}

665
	public run(): TPromise<any> {
666 667 668 669 670 671 672 673 674

		// Just close all if there are no or one dirty editor
		if (this.textFileService.getDirty().length < 2) {
			return this.editorService.closeAllEditors();
		}

		// Otherwise ask for combined confirmation
		const confirm = this.textFileService.confirmSave();
		if (confirm === ConfirmResult.CANCEL) {
675
			return undefined;
676 677 678 679
		}

		let saveOrRevertPromise: TPromise<boolean>;
		if (confirm === ConfirmResult.DONT_SAVE) {
680
			saveOrRevertPromise = this.textFileService.revertAll(null, { soft: true }).then(() => true);
681 682 683 684 685 686 687 688
		} else {
			saveOrRevertPromise = this.textFileService.saveAll(true).then(res => res.results.every(r => r.success));
		}

		return saveOrRevertPromise.then(success => {
			if (success) {
				return this.editorService.closeAllEditors();
			}
689
			return undefined;
690
		});
E
Erich Gamma 已提交
691 692 693
	}
}

694 695 696 697 698
export class CloseEditorsInOtherGroupsAction extends Action {

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

699 700 701 702 703 704
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
705 706 707
		super(id, label);
	}

B
Benjamin Pasero 已提交
708
	public run(context?: IEditorContext): TPromise<any> {
709
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
710
		if (typeof position !== 'number') {
711
			const activeEditor = this.editorService.getActiveEditor();
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
			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 已提交
728
	public static LABEL = nls.localize('closeOtherEditorsInGroup', "Close Other Editors");
E
Erich Gamma 已提交
729

730 731 732 733 734 735
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
E
Erich Gamma 已提交
736 737 738
		super(id, label);
	}

B
Benjamin Pasero 已提交
739
	public run(context?: IEditorContext): TPromise<any> {
740
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
I
isidor 已提交
741
		let input = context ? context.editor : null;
I
isidor 已提交
742 743

		// If position or input are not passed in take the position and input of the active editor.
744 745
		const active = this.editorService.getActiveEditor();
		if (active) {
I
isidor 已提交
746
			position = typeof position === 'number' ? position : active.position;
747
			input = input ? input : <EditorInput>active.input;
I
isidor 已提交
748 749 750 751
		}

		if (typeof position === 'number' && input) {
			return this.editorService.closeEditors(position, input);
752 753 754
		}

		return TPromise.as(false);
E
Erich Gamma 已提交
755 756 757
	}
}

B
Benjamin Pasero 已提交
758
export class CloseEditorsInGroupAction extends Action {
I
isidor 已提交
759

B
Benjamin Pasero 已提交
760 761
	public static ID = 'workbench.action.closeEditorsInGroup';
	public static LABEL = nls.localize('closeEditorsInGroup', "Close All Editors in Group");
I
isidor 已提交
762

763 764 765 766 767 768
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
B
Benjamin Pasero 已提交
769
		super(id, label);
I
isidor 已提交
770 771
	}

B
Benjamin Pasero 已提交
772
	public run(context?: IEditorContext): TPromise<any> {
773
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
I
isidor 已提交
774
		if (typeof position !== 'number') {
775
			const activeEditor = this.editorService.getActiveEditor();
I
isidor 已提交
776 777 778 779 780 781 782 783
			if (activeEditor) {
				position = activeEditor.position;
			}
		}

		if (typeof position === 'number') {
			return this.editorService.closeEditors(position);
		}
B
Benjamin Pasero 已提交
784 785

		return TPromise.as(false);
I
isidor 已提交
786 787 788
	}
}

B
Benjamin Pasero 已提交
789
export class MoveGroupLeftAction extends Action {
E
Erich Gamma 已提交
790

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

794 795 796 797 798 799
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
E
Erich Gamma 已提交
800 801 802
		super(id, label);
	}

B
Benjamin Pasero 已提交
803
	public run(context?: IEditorContext): TPromise<any> {
804
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
805
		if (typeof position !== 'number') {
806
			const activeEditor = this.editorService.getActiveEditor();
807
			if (activeEditor && (activeEditor.position === Position.TWO || activeEditor.position === Position.THREE)) {
808 809 810 811 812
				position = activeEditor.position;
			}
		}

		if (typeof position === 'number') {
813
			const newPosition = (position === Position.TWO) ? Position.ONE : Position.TWO;
E
Erich Gamma 已提交
814

B
Benjamin Pasero 已提交
815
			// Move group
816
			this.editorGroupService.moveGroup(position, newPosition);
E
Erich Gamma 已提交
817 818
		}

A
Alex Dima 已提交
819
		return TPromise.as(false);
E
Erich Gamma 已提交
820 821 822
	}
}

B
Benjamin Pasero 已提交
823
export class MoveGroupRightAction extends Action {
E
Erich Gamma 已提交
824

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

828 829 830 831 832 833
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
E
Erich Gamma 已提交
834 835 836
		super(id, label);
	}

B
Benjamin Pasero 已提交
837
	public run(context?: IEditorContext): TPromise<any> {
838
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
839
		if (typeof position !== 'number') {
840 841
			const activeEditor = this.editorService.getActiveEditor();
			const editors = this.editorService.getVisibleEditors();
842

843
			if ((editors.length === 2 && activeEditor.position === Position.ONE) || (editors.length === 3 && activeEditor.position !== Position.THREE)) {
844 845 846 847 848
				position = activeEditor.position;
			}
		}

		if (typeof position === 'number') {
849
			const newPosition = (position === Position.ONE) ? Position.TWO : Position.THREE;
E
Erich Gamma 已提交
850

B
Benjamin Pasero 已提交
851
			// Move group
852
			this.editorGroupService.moveGroup(position, newPosition);
E
Erich Gamma 已提交
853 854
		}

A
Alex Dima 已提交
855
		return TPromise.as(false);
E
Erich Gamma 已提交
856 857 858
	}
}

859
export class MinimizeOtherGroupsAction extends Action {
E
Erich Gamma 已提交
860

861 862 863
	public static ID = 'workbench.action.minimizeOtherEditors';
	public static LABEL = nls.localize('minimizeOtherEditorGroups', "Minimize Other Editor Groups");

864
	constructor(id: string, label: string, @IEditorGroupService private editorGroupService: IEditorGroupService) {
E
Erich Gamma 已提交
865 866 867
		super(id, label);
	}

868
	public run(): TPromise<any> {
869
		this.editorGroupService.arrangeGroups(GroupArrangement.MINIMIZE_OTHERS);
E
Erich Gamma 已提交
870

A
Alex Dima 已提交
871
		return TPromise.as(false);
E
Erich Gamma 已提交
872 873 874
	}
}

875
export class EvenGroupWidthsAction extends Action {
E
Erich Gamma 已提交
876

877 878 879
	public static ID = 'workbench.action.evenEditorWidths';
	public static LABEL = nls.localize('evenEditorGroups', "Even Editor Group Widths");

880
	constructor(id: string, label: string, @IEditorGroupService private editorGroupService: IEditorGroupService) {
E
Erich Gamma 已提交
881 882 883
		super(id, label);
	}

884
	public run(): TPromise<any> {
B
Benjamin Pasero 已提交
885
		this.editorGroupService.arrangeGroups(GroupArrangement.EVEN);
E
Erich Gamma 已提交
886

A
Alex Dima 已提交
887
		return TPromise.as(false);
E
Erich Gamma 已提交
888 889 890
	}
}

891
export class MaximizeGroupAction extends Action {
892

893 894 895
	public static ID = 'workbench.action.maximizeEditor';
	public static LABEL = nls.localize('maximizeEditor', "Maximize Editor Group and Hide Sidebar");

896 897 898 899
	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
900
		@IEditorGroupService private editorGroupService: IEditorGroupService,
901 902 903 904 905
		@IPartService private partService: IPartService
	) {
		super(id, label);
	}

906
	public run(): TPromise<any> {
907
		if (this.editorService.getActiveEditor()) {
908
			this.editorGroupService.arrangeGroups(GroupArrangement.MINIMIZE_OTHERS);
909
			return this.partService.setSideBarHidden(true);
910 911
		}

A
Alex Dima 已提交
912
		return TPromise.as(false);
913 914 915
	}
}

B
Benjamin Pasero 已提交
916
export class KeepEditorAction extends Action {
917

B
Benjamin Pasero 已提交
918 919
	public static ID = 'workbench.action.keepEditor';
	public static LABEL = nls.localize('keepEditor', "Keep Editor");
920 921 922 923

	constructor(
		id: string,
		label: string,
924
		@IEditorGroupService private editorGroupService: IEditorGroupService,
925 926 927 928 929
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(id, label);
	}

B
Benjamin Pasero 已提交
930
	public run(context?: IEditorContext): TPromise<any> {
931
		const target = getTarget(this.editorService, this.editorGroupService, context);
B
Benjamin Pasero 已提交
932 933
		if (target) {
			this.editorGroupService.pinEditor(target.position, target.input);
934 935 936 937 938 939
		}

		return TPromise.as(true);
	}
}

B
Benjamin Pasero 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952
function getTarget(editorService: IWorkbenchEditorService, editorGroupService: IEditorGroupService, context?: IEditorContext): { input: IEditorInput, position: Position } {
	if (context) {
		return { input: context.editor, position: editorGroupService.getStacksModel().positionOfGroup(context.group) };
	}

	const activeEditor = editorService.getActiveEditor();
	if (activeEditor) {
		return { input: activeEditor.input, position: activeEditor.position };
	}

	return null;
}

953 954
export abstract class BaseNavigateEditorAction extends Action {

955 956 957 958 959 960
	constructor(
		id: string,
		label: string,
		protected editorGroupService: IEditorGroupService,
		protected editorService: IWorkbenchEditorService
	) {
961 962 963 964
		super(id, label);
	}

	public run(): TPromise<any> {
965
		const model = this.editorGroupService.getStacksModel();
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
		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");

982 983 984 985 986 987 988
	constructor(
		id: string,
		label: string,
		@IEditorGroupService editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService
	) {
		super(id, label, editorGroupService, editorService);
989 990 991
	}

	protected navigate(): IEditorIdentifier {
992
		return this.editorGroupService.getStacksModel().next(true /* jump groups */);
993 994 995 996 997 998 999 1000
	}
}

export class OpenPreviousEditor extends BaseNavigateEditorAction {

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

1001 1002 1003 1004 1005 1006 1007
	constructor(
		id: string,
		label: string,
		@IEditorGroupService editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService
	) {
		super(id, label, editorGroupService, editorService);
1008 1009 1010
	}

	protected navigate(): IEditorIdentifier {
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
		return this.editorGroupService.getStacksModel().previous(true /* jump groups */);
	}
}

export class OpenNextEditorInGroup extends BaseNavigateEditorAction {

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

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

	protected navigate(): IEditorIdentifier {
		return this.editorGroupService.getStacksModel().next(false /* do NOT jump groups */);
	}
}

export class OpenPreviousEditorInGroup extends BaseNavigateEditorAction {

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

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

	protected navigate(): IEditorIdentifier {
		return this.editorGroupService.getStacksModel().previous(false /* do NOT jump groups */);
1050
	}
B
Benjamin Pasero 已提交
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
}

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 已提交
1083
}
1084 1085 1086 1087 1088 1089 1090 1091 1092

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,
B
Benjamin Pasero 已提交
1093
		@IHistoryService private historyService: IHistoryService
1094 1095 1096 1097 1098
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
1099
		this.historyService.reopenLastClosedEditor();
1100 1101 1102

		return TPromise.as(false);
	}
B
Benjamin Pasero 已提交
1103 1104
}

1105
export class ClearRecentFilesAction extends Action {
C
22768  
Cristian 已提交
1106

1107
	public static ID = 'workbench.action.clearRecentFiles';
C
Cristian 已提交
1108
	public static LABEL = nls.localize('clearRecentFiles', "Clear Recent Files");
C
22768  
Cristian 已提交
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

	constructor(
		id: string,
		label: string,
		@IWindowsService private windowsService: IWindowsService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		this.windowsService.clearRecentPathsList();

		return TPromise.as(false);
	}
}

1125
export const NAVIGATE_IN_GROUP_ONE_PREFIX = 'edt one ';
B
Benjamin Pasero 已提交
1126

1127
export class ShowEditorsInGroupOneAction extends QuickOpenAction {
B
Benjamin Pasero 已提交
1128

1129 1130
	public static ID = 'workbench.action.showEditorsInFirstGroup';
	public static LABEL = nls.localize('showEditorsInFirstGroup', "Show Editors in First Group");
B
Benjamin Pasero 已提交
1131

1132 1133 1134
	constructor(
		actionId: string,
		actionLabel: string,
1135
		@IQuickOpenService quickOpenService: IQuickOpenService
1136
	) {
1137
		super(actionId, actionLabel, NAVIGATE_IN_GROUP_ONE_PREFIX, quickOpenService);
1138 1139

		this.class = 'show-group-editors-action';
B
Benjamin Pasero 已提交
1140
	}
1141
}
1142

1143
export const NAVIGATE_IN_GROUP_TWO_PREFIX = 'edt two ';
1144

1145
export class ShowEditorsInGroupTwoAction extends QuickOpenAction {
1146

1147 1148
	public static ID = 'workbench.action.showEditorsInSecondGroup';
	public static LABEL = nls.localize('showEditorsInSecondGroup', "Show Editors in Second Group");
1149 1150 1151 1152

	constructor(
		actionId: string,
		actionLabel: string,
1153
		@IQuickOpenService quickOpenService: IQuickOpenService
1154
	) {
1155
		super(actionId, actionLabel, NAVIGATE_IN_GROUP_TWO_PREFIX, quickOpenService);
1156 1157

		this.class = 'show-group-editors-action';
1158 1159
	}
}
1160

1161
export const NAVIGATE_IN_GROUP_THREE_PREFIX = 'edt three ';
1162

1163
export class ShowEditorsInGroupThreeAction extends QuickOpenAction {
1164

1165 1166
	public static ID = 'workbench.action.showEditorsInThirdGroup';
	public static LABEL = nls.localize('showEditorsInThirdGroup', "Show Editors in Third Group");
1167 1168 1169 1170

	constructor(
		actionId: string,
		actionLabel: string,
1171
		@IQuickOpenService quickOpenService: IQuickOpenService
1172
	) {
1173
		super(actionId, actionLabel, NAVIGATE_IN_GROUP_THREE_PREFIX, quickOpenService);
1174 1175

		this.class = 'show-group-editors-action';
1176
	}
B
Benjamin Pasero 已提交
1177 1178
}

B
Benjamin Pasero 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
export class ShowEditorsInGroupAction extends Action {

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

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

	public run(context?: IEditorContext): TPromise<any> {
		const stacks = this.editorGroupService.getStacksModel();
		const groupCount = stacks.groups.length;
		if (groupCount <= 1 || !context) {
			return this.quickOpenService.show(NAVIGATE_ALL_EDITORS_GROUP_PREFIX);
		}

		switch (stacks.positionOfGroup(context.group)) {
1201
			case Position.TWO:
B
Benjamin Pasero 已提交
1202
				return this.quickOpenService.show(NAVIGATE_IN_GROUP_TWO_PREFIX);
1203
			case Position.THREE:
1204
				return this.quickOpenService.show(NAVIGATE_IN_GROUP_THREE_PREFIX);
B
Benjamin Pasero 已提交
1205 1206
		}

1207
		return this.quickOpenService.show(NAVIGATE_IN_GROUP_ONE_PREFIX);
B
Benjamin Pasero 已提交
1208 1209 1210
	}
}

1211
export const NAVIGATE_ALL_EDITORS_GROUP_PREFIX = 'edt ';
B
Benjamin Pasero 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

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

1223
export class BaseQuickOpenEditorInGroupAction extends Action {
B
Benjamin Pasero 已提交
1224 1225 1226 1227 1228

	constructor(
		id: string,
		label: string,
		@IQuickOpenService private quickOpenService: IQuickOpenService,
1229
		@IKeybindingService private keybindingService: IKeybindingService,
1230
		@IEditorGroupService private editorGroupService: IEditorGroupService
B
Benjamin Pasero 已提交
1231 1232 1233 1234 1235
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
1236
		const keys = this.keybindingService.lookupKeybindings(this.id);
B
Benjamin Pasero 已提交
1237

1238
		const stacks = this.editorGroupService.getStacksModel();
1239 1240
		if (stacks.activeGroup) {
			const activePosition = stacks.positionOfGroup(stacks.activeGroup);
1241
			let prefix = NAVIGATE_IN_GROUP_ONE_PREFIX;
1242

B
Benjamin Pasero 已提交
1243
			if (activePosition === Position.TWO) {
1244
				prefix = NAVIGATE_IN_GROUP_TWO_PREFIX;
B
Benjamin Pasero 已提交
1245
			} else if (activePosition === Position.THREE) {
1246
				prefix = NAVIGATE_IN_GROUP_THREE_PREFIX;
1247 1248
			}

B
Benjamin Pasero 已提交
1249
			this.quickOpenService.show(prefix, { quickNavigateConfiguration: { keybindings: keys } });
1250
		}
B
Benjamin Pasero 已提交
1251 1252 1253 1254 1255

		return TPromise.as(true);
	}
}

1256 1257 1258
export class OpenPreviousRecentlyUsedEditorInGroupAction extends BaseQuickOpenEditorInGroupAction {

	public static ID = 'workbench.action.openPreviousRecentlyUsedEditorInGroup';
1259
	public static LABEL = nls.localize('openPreviousRecentlyUsedEditorInGroup', "Open Previous Recently Used Editor in Group");
1260 1261 1262 1263 1264 1265

	constructor(
		id: string,
		label: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IKeybindingService keybindingService: IKeybindingService,
1266
		@IEditorGroupService editorGroupService: IEditorGroupService
1267
	) {
1268
		super(id, label, quickOpenService, keybindingService, editorGroupService);
1269 1270 1271 1272 1273 1274
	}
}

export class OpenNextRecentlyUsedEditorInGroupAction extends BaseQuickOpenEditorInGroupAction {

	public static ID = 'workbench.action.openNextRecentlyUsedEditorInGroup';
1275
	public static LABEL = nls.localize('openNextRecentlyUsedEditorInGroup', "Open Next Recently Used Editor in Group");
1276 1277 1278 1279 1280 1281

	constructor(
		id: string,
		label: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IKeybindingService keybindingService: IKeybindingService,
1282
		@IEditorGroupService editorGroupService: IEditorGroupService
1283
	) {
1284
		super(id, label, quickOpenService, keybindingService, editorGroupService);
1285 1286 1287
	}
}

1288
export class OpenPreviousEditorFromHistoryAction extends Action {
B
Benjamin Pasero 已提交
1289

1290
	public static ID = 'workbench.action.openPreviousEditorFromHistory';
B
Benjamin Pasero 已提交
1291
	public static LABEL = nls.localize('navigateEditorHistoryByInput', "Open Previous Editor from History");
B
Benjamin Pasero 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302

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

	public run(): TPromise<any> {
1303
		const keys = this.keybindingService.lookupKeybindings(this.id);
B
Benjamin Pasero 已提交
1304

B
Benjamin Pasero 已提交
1305
		this.quickOpenService.show(null, { quickNavigateConfiguration: { keybindings: keys } });
B
Benjamin Pasero 已提交
1306 1307 1308 1309 1310

		return TPromise.as(true);
	}
}

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
export class OpenNextRecentlyUsedEditorAction extends Action {

	public static ID = 'workbench.action.openNextRecentlyUsedEditor';
	public static LABEL = nls.localize('openNextRecentlyUsedEditor', "Open Next Recently Used Editor");

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

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

		return TPromise.as(null);
	}
}

export class OpenPreviousRecentlyUsedEditorAction extends Action {

	public static ID = 'workbench.action.openPreviousRecentlyUsedEditor';
	public static LABEL = nls.localize('openPreviousRecentlyUsedEditor', "Open Previous Recently Used Editor");

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

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

		return TPromise.as(null);
	}
}

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
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,
		@IHistoryService private historyService: IHistoryService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {

1358
		// Editor history
1359 1360 1361 1362 1363 1364
		this.historyService.clear();

		return TPromise.as(true);
	}
}

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
export class FocusLastEditorInStackAction extends Action {

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

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

	public run(): TPromise<any> {
		const active = this.editorService.getActiveEditor();
		if (active) {
			const group = this.editorGroupService.getStacksModel().groupAt(active.position);
			const editor = group.getEditor(group.count - 1);

			if (editor) {
				return this.editorService.openEditor(editor);
			}
		}

1390 1391 1392 1393
		return TPromise.as(true);
	}
}

1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
export class MoveEditorLeftInGroupAction extends Action {

	public static ID = 'workbench.action.moveEditorLeftInGroup';
	public static LABEL = nls.localize('moveEditorLeft', "Move Editor Left");

	constructor(
		id: string,
		label: string,
		@ICommandService private commandService: ICommandService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		const args: ActiveEditorMoveArguments = {
1409
			to: ActiveEditorMovePositioning.LEFT
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
		};
		this.commandService.executeCommand(EditorCommands.MoveActiveEditor, args);

		return TPromise.as(true);
	}
}

export class MoveEditorRightInGroupAction extends Action {

	public static ID = 'workbench.action.moveEditorRightInGroup';
	public static LABEL = nls.localize('moveEditorRight', "Move Editor Right");

	constructor(
		id: string,
		label: string,
		@ICommandService private commandService: ICommandService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		const args: ActiveEditorMoveArguments = {
1432
			to: ActiveEditorMovePositioning.RIGHT
1433 1434 1435 1436 1437 1438 1439
		};
		this.commandService.executeCommand(EditorCommands.MoveActiveEditor, args);

		return TPromise.as(true);
	}
}

1440
export class MoveEditorToPreviousGroupAction extends Action {
1441

1442 1443
	public static ID = 'workbench.action.moveEditorToPreviousGroup';
	public static LABEL = nls.localize('moveEditorToPreviousGroup', "Move Editor into Previous Group");
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

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

	public run(): TPromise<any> {
		const activeEditor = this.editorService.getActiveEditor();
1456
		if (activeEditor && activeEditor.position !== Position.ONE) {
1457 1458 1459 1460 1461 1462 1463
			this.editorGroupService.moveEditor(activeEditor.input, activeEditor.position, activeEditor.position - 1);
		}

		return TPromise.as(true);
	}
}

1464
export class MoveEditorToNextGroupAction extends Action {
1465

1466 1467
	public static ID = 'workbench.action.moveEditorToNextGroup';
	public static LABEL = nls.localize('moveEditorToNextGroup', "Move Editor into Next Group");
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

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

	public run(): TPromise<any> {
		const activeEditor = this.editorService.getActiveEditor();
1480
		if (activeEditor && activeEditor.position !== Position.THREE) {
1481 1482 1483
			this.editorGroupService.moveEditor(activeEditor.input, activeEditor.position, activeEditor.position + 1);
		}

1484 1485
		return TPromise.as(true);
	}
D
Daniel Imms 已提交
1486
}