editorActions.ts 44.6 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, POSITIONS } from 'vs/platform/editor/common/editor';
J
Johannes Rieken 已提交
19 20 21 22 23
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
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';
24
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
C
22768  
Cristian 已提交
25
import { IWindowsService } from 'vs/platform/windows/common/windows';
E
Erich Gamma 已提交
26 27 28

export class SplitEditorAction extends Action {

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

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

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

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

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

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

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

		switch (editorCount) {

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

			// Special case two editors opened
			case 2:

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

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

93 94 95
					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 已提交
96 97 98 99
					});
				}
		}

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

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

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

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

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

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

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

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

132 133 134 135 136 137 138 139 140
		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 已提交
141

142 143 144
		// 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 已提交
145
		} else {
146
			toPosition = fromPosition - 1;
I
initialshl 已提交
147 148
		}

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

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

155 156 157 158 159 160 161 162 163 164 165
		// 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 已提交
166

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

		return TPromise.as(true);
	}
}

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

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

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

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

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

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

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

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

207 208 209 210 211 212 213 214 215 216 217 218 219 220
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 已提交
221 222
		const activeEditor = this.editorService.getActiveEditor();
		if (activeEditor) {
D
Daniel Imms 已提交
223
			activeEditor.focus();
B
hygiene  
Benjamin Pasero 已提交
224
		}
D
Daniel Imms 已提交
225

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

230 231
export class FocusFirstGroupAction extends Action {

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

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

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

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

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

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

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

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

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

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

	protected abstract getReferenceEditorSide(): Position;

	protected abstract getTargetEditorSide(): Position;

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

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

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

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

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

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

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

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

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

				// For now only support to open files from history to the side
333
				if (input instanceof EditorInput) {
334
					if (hasResource(input, { filter: ['file', 'untitled'] })) {
335 336 337 338
						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 已提交
339 340 341 342
				}
			}
		}

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

347 348
export class FocusSecondGroupAction extends BaseFocusSideGroupAction {

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

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

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

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

371 372
export class FocusThirdGroupAction extends BaseFocusSideGroupAction {

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

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

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

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

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

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

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

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

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


B
Benjamin Pasero 已提交
418
		// Find the next position to the left/top
419 420 421
		let nextPosition: Position = Position.ONE;
		if (activeEditor.position === Position.THREE) {
			nextPosition = Position.TWO;
422 423 424 425
		} else if (activeEditor.position === Position.ONE) {
			// Get the last active position
			const lastPosition = this.editorGroupService.getStacksModel().groups.length - 1;
			nextPosition = lastPosition;
E
Erich Gamma 已提交
426 427 428
		}

		// Focus next position if provided
429
		this.editorGroupService.focusGroup(nextPosition);
430 431

		return TPromise.as(true);
E
Erich Gamma 已提交
432 433 434
	}
}

B
Benjamin Pasero 已提交
435
export class FocusNextGroup extends Action {
436

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

E
Erich Gamma 已提交
440 441 442 443 444 445 446 447 448 449 450
	private navigateActions: Action[];

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

		this.navigateActions = [];
451 452 453
		this.navigateActions[Position.ONE] = instantiationService.createInstance(FocusFirstGroupAction, FocusFirstGroupAction.ID, FocusFirstGroupAction.LABEL);
		this.navigateActions[Position.TWO] = instantiationService.createInstance(FocusSecondGroupAction, FocusSecondGroupAction.ID, FocusSecondGroupAction.LABEL);
		this.navigateActions[Position.THREE] = instantiationService.createInstance(FocusThirdGroupAction, FocusThirdGroupAction.ID, FocusThirdGroupAction.LABEL);
E
Erich Gamma 已提交
454 455
	}

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

B
Benjamin Pasero 已提交
458
		// Find the next position to the right/bottom to use
E
Erich Gamma 已提交
459
		let nextPosition: Position;
460
		const activeEditor = this.editorService.getActiveEditor();
461 462 463

		const lastPosition = POSITIONS[POSITIONS.length - 1];
		if (!activeEditor || activeEditor.position === lastPosition) {
464 465 466 467 468
			nextPosition = Position.ONE;
		} else if (activeEditor.position === Position.ONE) {
			nextPosition = Position.TWO;
		} else if (activeEditor.position === Position.TWO) {
			nextPosition = Position.THREE;
E
Erich Gamma 已提交
469 470 471
		}

		// Run the action for the target next position
472
		if (typeof nextPosition === 'number' && this.navigateActions[nextPosition]) {
E
Erich Gamma 已提交
473 474 475
			return this.navigateActions[nextPosition].run(event);
		}

A
Alex Dima 已提交
476
		return TPromise.as(true);
E
Erich Gamma 已提交
477 478 479 480 481 482 483 484
	}
}

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

485 486
	constructor(
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
487
		@IEditorGroupService private editorGroupService: IEditorGroupService
488
	) {
E
Erich Gamma 已提交
489 490 491
		super(OpenToSideAction.OPEN_TO_SIDE_ID, OpenToSideAction.OPEN_TO_SIDE_LABEL);

		this.updateEnablement();
492 493 494 495
		this.updateClass();
	}

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

498
		this.class = editorGroupLayoutVertical ? 'quick-open-sidebyside-vertical' : 'quick-open-sidebyside-horizontal';
E
Erich Gamma 已提交
499 500 501
	}

	private updateEnablement(): void {
502
		const activeEditor = this.editorService.getActiveEditor();
503
		this.enabled = (!activeEditor || activeEditor.position !== Position.THREE);
E
Erich Gamma 已提交
504 505
	}

506
	public run(context: any): TPromise<any> {
E
Erich Gamma 已提交
507 508
		let entry = toEditorQuickOpenEntry(context);
		if (entry) {
509
			const input = entry.getInput();
B
Benjamin Pasero 已提交
510
			if (input instanceof EditorInput) {
511
				return this.editorService.openEditor(input, entry.getOptions(), true);
B
Benjamin Pasero 已提交
512 513
			}

514 515 516 517
			const resourceInput = input as IResourceInput;
			resourceInput.options = mixin(resourceInput.options, entry.getOptions());

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

A
Alex Dima 已提交
520
		return TPromise.as(false);
E
Erich Gamma 已提交
521 522 523
	}
}

524
export function toEditorQuickOpenEntry(element: any): IEditorQuickOpenEntry {
E
Erich Gamma 已提交
525 526 527

	// QuickOpenEntryGroup
	if (element instanceof QuickOpenEntryGroup) {
528
		const group = <QuickOpenEntryGroup>element;
E
Erich Gamma 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542
		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 {
543

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

547 548 549 550 551 552
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
I
isidor 已提交
553
		super(id, label, 'close-editor-action');
E
Erich Gamma 已提交
554 555
	}

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

559
		// Close Active Editor
560
		if (typeof position !== 'number') {
561
			const activeEditor = this.editorService.getActiveEditor();
B
Benjamin Pasero 已提交
562
			if (activeEditor) {
563
				return this.editorService.closeEditor(activeEditor.position, activeEditor.input);
B
Benjamin Pasero 已提交
564
			}
565 566
		}

I
isidor 已提交
567
		let input = context ? context.editor : null;
I
isidor 已提交
568
		if (!input) {
B
Benjamin Pasero 已提交
569

I
isidor 已提交
570
			// Get Top Editor at Position
571
			const visibleEditors = this.editorService.getVisibleEditors();
I
isidor 已提交
572
			if (visibleEditors[position]) {
573
				input = visibleEditors[position].input;
I
isidor 已提交
574 575 576 577 578
			}
		}

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

A
Alex Dima 已提交
581
		return TPromise.as(false);
E
Erich Gamma 已提交
582 583 584
	}
}

M
misoguy 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
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 已提交
600 601 602 603
		if (activeEditor && activeEditor.input) {
			const input = activeEditor.input;
			const position = activeEditor.position;

M
misoguy 已提交
604
			return activeEditor.input.revert().then(ok =>
B
Benjamin Pasero 已提交
605
				this.editorService.closeEditor(position, input)
M
misoguy 已提交
606 607 608 609 610 611 612
			);
		}

		return TPromise.as(false);
	}
}

B
Benjamin Pasero 已提交
613
export class CloseLeftEditorsInGroupAction extends Action {
614

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

618 619 620
	constructor(
		id: string,
		label: string,
B
Benjamin Pasero 已提交
621 622
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IEditorGroupService private groupService: IEditorGroupService
623
	) {
624 625 626
		super(id, label);
	}

B
Benjamin Pasero 已提交
627
	public run(context?: IEditorContext): TPromise<any> {
628
		const editor = getTarget(this.editorService, this.groupService, context);
B
Benjamin Pasero 已提交
629 630
		if (editor) {
			return this.editorService.closeEditors(editor.position, editor.input, Direction.LEFT);
631 632 633 634 635 636 637 638
		}

		return TPromise.as(false);
	}
}

export class CloseRightEditorsInGroupAction extends Action {

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

B
Benjamin Pasero 已提交
642 643 644 645 646 647
	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IEditorGroupService private groupService: IEditorGroupService
	) {
648 649 650
		super(id, label);
	}

B
Benjamin Pasero 已提交
651
	public run(context?: IEditorContext): TPromise<any> {
652
		const editor = getTarget(this.editorService, this.groupService, context);
B
Benjamin Pasero 已提交
653 654
		if (editor) {
			return this.editorService.closeEditors(editor.position, editor.input, Direction.RIGHT);
655 656 657 658 659 660
		}

		return TPromise.as(false);
	}
}

E
Erich Gamma 已提交
661 662
export class CloseAllEditorsAction extends Action {

663 664 665
	public static ID = 'workbench.action.closeAllEditors';
	public static LABEL = nls.localize('closeAllEditors', "Close All Editors");

666 667 668 669 670 671
	constructor(
		id: string,
		label: string,
		@ITextFileService private textFileService: ITextFileService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
I
isidor 已提交
672
		super(id, label, 'action-close-all-files');
E
Erich Gamma 已提交
673 674
	}

675
	public run(): TPromise<any> {
676 677 678 679 680 681 682 683 684

		// 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) {
685
			return undefined;
686 687 688 689
		}

		let saveOrRevertPromise: TPromise<boolean>;
		if (confirm === ConfirmResult.DONT_SAVE) {
690
			saveOrRevertPromise = this.textFileService.revertAll(null, { soft: true }).then(() => true);
691 692 693 694 695 696 697 698
		} else {
			saveOrRevertPromise = this.textFileService.saveAll(true).then(res => res.results.every(r => r.success));
		}

		return saveOrRevertPromise.then(success => {
			if (success) {
				return this.editorService.closeAllEditors();
			}
699
			return undefined;
700
		});
E
Erich Gamma 已提交
701 702 703
	}
}

704 705 706 707 708
export class CloseEditorsInOtherGroupsAction extends Action {

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

709 710 711 712 713 714
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
715 716 717
		super(id, label);
	}

B
Benjamin Pasero 已提交
718
	public run(context?: IEditorContext): TPromise<any> {
719
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
720
		if (typeof position !== 'number') {
721
			const activeEditor = this.editorService.getActiveEditor();
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
			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 已提交
738
	public static LABEL = nls.localize('closeOtherEditorsInGroup', "Close Other Editors");
E
Erich Gamma 已提交
739

740 741 742 743 744 745
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
E
Erich Gamma 已提交
746 747 748
		super(id, label);
	}

B
Benjamin Pasero 已提交
749
	public run(context?: IEditorContext): TPromise<any> {
750
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
I
isidor 已提交
751
		let input = context ? context.editor : null;
I
isidor 已提交
752 753

		// If position or input are not passed in take the position and input of the active editor.
754 755
		const active = this.editorService.getActiveEditor();
		if (active) {
I
isidor 已提交
756
			position = typeof position === 'number' ? position : active.position;
757
			input = input ? input : <EditorInput>active.input;
I
isidor 已提交
758 759 760 761
		}

		if (typeof position === 'number' && input) {
			return this.editorService.closeEditors(position, input);
762 763 764
		}

		return TPromise.as(false);
E
Erich Gamma 已提交
765 766 767
	}
}

B
Benjamin Pasero 已提交
768
export class CloseEditorsInGroupAction extends Action {
I
isidor 已提交
769

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

773 774 775 776 777 778
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
B
Benjamin Pasero 已提交
779
		super(id, label);
I
isidor 已提交
780 781
	}

B
Benjamin Pasero 已提交
782
	public run(context?: IEditorContext): TPromise<any> {
783
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
I
isidor 已提交
784
		if (typeof position !== 'number') {
785
			const activeEditor = this.editorService.getActiveEditor();
I
isidor 已提交
786 787 788 789 790 791 792 793
			if (activeEditor) {
				position = activeEditor.position;
			}
		}

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

		return TPromise.as(false);
I
isidor 已提交
796 797 798
	}
}

B
Benjamin Pasero 已提交
799
export class MoveGroupLeftAction extends Action {
E
Erich Gamma 已提交
800

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

804 805 806 807 808 809
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
E
Erich Gamma 已提交
810 811 812
		super(id, label);
	}

B
Benjamin Pasero 已提交
813
	public run(context?: IEditorContext): TPromise<any> {
814
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
815
		if (typeof position !== 'number') {
816
			const activeEditor = this.editorService.getActiveEditor();
817
			if (activeEditor && (activeEditor.position === Position.TWO || activeEditor.position === Position.THREE)) {
818 819 820 821 822
				position = activeEditor.position;
			}
		}

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

B
Benjamin Pasero 已提交
825
			// Move group
826
			this.editorGroupService.moveGroup(position, newPosition);
E
Erich Gamma 已提交
827 828
		}

A
Alex Dima 已提交
829
		return TPromise.as(false);
E
Erich Gamma 已提交
830 831 832
	}
}

B
Benjamin Pasero 已提交
833
export class MoveGroupRightAction extends Action {
E
Erich Gamma 已提交
834

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

838 839 840 841 842 843
	constructor(
		id: string,
		label: string,
		@IEditorGroupService private editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
E
Erich Gamma 已提交
844 845 846
		super(id, label);
	}

B
Benjamin Pasero 已提交
847
	public run(context?: IEditorContext): TPromise<any> {
848
		let position = context ? this.editorGroupService.getStacksModel().positionOfGroup(context.group) : null;
849
		if (typeof position !== 'number') {
850 851
			const activeEditor = this.editorService.getActiveEditor();
			const editors = this.editorService.getVisibleEditors();
852

853
			if ((editors.length === 2 && activeEditor.position === Position.ONE) || (editors.length === 3 && activeEditor.position !== Position.THREE)) {
854 855 856 857 858
				position = activeEditor.position;
			}
		}

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

B
Benjamin Pasero 已提交
861
			// Move group
862
			this.editorGroupService.moveGroup(position, newPosition);
E
Erich Gamma 已提交
863 864
		}

A
Alex Dima 已提交
865
		return TPromise.as(false);
E
Erich Gamma 已提交
866 867 868
	}
}

869
export class MinimizeOtherGroupsAction extends Action {
E
Erich Gamma 已提交
870

871 872 873
	public static ID = 'workbench.action.minimizeOtherEditors';
	public static LABEL = nls.localize('minimizeOtherEditorGroups', "Minimize Other Editor Groups");

874
	constructor(id: string, label: string, @IEditorGroupService private editorGroupService: IEditorGroupService) {
E
Erich Gamma 已提交
875 876 877
		super(id, label);
	}

878
	public run(): TPromise<any> {
879
		this.editorGroupService.arrangeGroups(GroupArrangement.MINIMIZE_OTHERS);
E
Erich Gamma 已提交
880

A
Alex Dima 已提交
881
		return TPromise.as(false);
E
Erich Gamma 已提交
882 883 884
	}
}

885
export class EvenGroupWidthsAction extends Action {
E
Erich Gamma 已提交
886

887 888 889
	public static ID = 'workbench.action.evenEditorWidths';
	public static LABEL = nls.localize('evenEditorGroups', "Even Editor Group Widths");

890
	constructor(id: string, label: string, @IEditorGroupService private editorGroupService: IEditorGroupService) {
E
Erich Gamma 已提交
891 892 893
		super(id, label);
	}

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

A
Alex Dima 已提交
897
		return TPromise.as(false);
E
Erich Gamma 已提交
898 899 900
	}
}

901
export class MaximizeGroupAction extends Action {
902

903 904 905
	public static ID = 'workbench.action.maximizeEditor';
	public static LABEL = nls.localize('maximizeEditor', "Maximize Editor Group and Hide Sidebar");

906 907 908 909
	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
910
		@IEditorGroupService private editorGroupService: IEditorGroupService,
911 912 913 914 915
		@IPartService private partService: IPartService
	) {
		super(id, label);
	}

916
	public run(): TPromise<any> {
917
		if (this.editorService.getActiveEditor()) {
918
			this.editorGroupService.arrangeGroups(GroupArrangement.MINIMIZE_OTHERS);
919
			return this.partService.setSideBarHidden(true);
920 921
		}

A
Alex Dima 已提交
922
		return TPromise.as(false);
923 924 925
	}
}

B
Benjamin Pasero 已提交
926
export class KeepEditorAction extends Action {
927

B
Benjamin Pasero 已提交
928 929
	public static ID = 'workbench.action.keepEditor';
	public static LABEL = nls.localize('keepEditor', "Keep Editor");
930 931 932 933

	constructor(
		id: string,
		label: string,
934
		@IEditorGroupService private editorGroupService: IEditorGroupService,
935 936 937 938 939
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(id, label);
	}

B
Benjamin Pasero 已提交
940
	public run(context?: IEditorContext): TPromise<any> {
941
		const target = getTarget(this.editorService, this.editorGroupService, context);
B
Benjamin Pasero 已提交
942 943
		if (target) {
			this.editorGroupService.pinEditor(target.position, target.input);
944 945 946 947 948 949
		}

		return TPromise.as(true);
	}
}

B
Benjamin Pasero 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962
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;
}

963 964
export abstract class BaseNavigateEditorAction extends Action {

965 966 967 968 969 970
	constructor(
		id: string,
		label: string,
		protected editorGroupService: IEditorGroupService,
		protected editorService: IWorkbenchEditorService
	) {
971 972 973 974
		super(id, label);
	}

	public run(): TPromise<any> {
975
		const model = this.editorGroupService.getStacksModel();
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
		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");

992 993 994 995 996 997 998
	constructor(
		id: string,
		label: string,
		@IEditorGroupService editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService
	) {
		super(id, label, editorGroupService, editorService);
999 1000 1001
	}

	protected navigate(): IEditorIdentifier {
1002
		return this.editorGroupService.getStacksModel().next(true /* jump groups */);
1003 1004 1005 1006 1007 1008 1009 1010
	}
}

export class OpenPreviousEditor extends BaseNavigateEditorAction {

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

1011 1012 1013 1014 1015 1016 1017
	constructor(
		id: string,
		label: string,
		@IEditorGroupService editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService
	) {
		super(id, label, editorGroupService, editorService);
1018 1019 1020
	}

	protected navigate(): IEditorIdentifier {
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 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
		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 */);
1060
	}
B
Benjamin Pasero 已提交
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
}

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 已提交
1093
}
1094 1095 1096 1097 1098 1099 1100 1101 1102

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 已提交
1103
		@IHistoryService private historyService: IHistoryService
1104 1105 1106 1107 1108
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
1109
		this.historyService.reopenLastClosedEditor();
1110 1111 1112

		return TPromise.as(false);
	}
B
Benjamin Pasero 已提交
1113 1114
}

1115
export class ClearRecentFilesAction extends Action {
C
22768  
Cristian 已提交
1116

1117
	public static ID = 'workbench.action.clearRecentFiles';
C
Cristian 已提交
1118
	public static LABEL = nls.localize('clearRecentFiles', "Clear Recent Files");
C
22768  
Cristian 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134

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

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

		return TPromise.as(false);
	}
}

1135
export const NAVIGATE_IN_GROUP_ONE_PREFIX = 'edt one ';
B
Benjamin Pasero 已提交
1136

1137
export class ShowEditorsInGroupOneAction extends QuickOpenAction {
B
Benjamin Pasero 已提交
1138

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

1142 1143 1144
	constructor(
		actionId: string,
		actionLabel: string,
1145
		@IQuickOpenService quickOpenService: IQuickOpenService
1146
	) {
1147
		super(actionId, actionLabel, NAVIGATE_IN_GROUP_ONE_PREFIX, quickOpenService);
1148 1149

		this.class = 'show-group-editors-action';
B
Benjamin Pasero 已提交
1150
	}
1151
}
1152

1153
export const NAVIGATE_IN_GROUP_TWO_PREFIX = 'edt two ';
1154

1155
export class ShowEditorsInGroupTwoAction extends QuickOpenAction {
1156

1157 1158
	public static ID = 'workbench.action.showEditorsInSecondGroup';
	public static LABEL = nls.localize('showEditorsInSecondGroup', "Show Editors in Second Group");
1159 1160 1161 1162

	constructor(
		actionId: string,
		actionLabel: string,
1163
		@IQuickOpenService quickOpenService: IQuickOpenService
1164
	) {
1165
		super(actionId, actionLabel, NAVIGATE_IN_GROUP_TWO_PREFIX, quickOpenService);
1166 1167

		this.class = 'show-group-editors-action';
1168 1169
	}
}
1170

1171
export const NAVIGATE_IN_GROUP_THREE_PREFIX = 'edt three ';
1172

1173
export class ShowEditorsInGroupThreeAction extends QuickOpenAction {
1174

1175 1176
	public static ID = 'workbench.action.showEditorsInThirdGroup';
	public static LABEL = nls.localize('showEditorsInThirdGroup', "Show Editors in Third Group");
1177 1178 1179 1180

	constructor(
		actionId: string,
		actionLabel: string,
1181
		@IQuickOpenService quickOpenService: IQuickOpenService
1182
	) {
1183
		super(actionId, actionLabel, NAVIGATE_IN_GROUP_THREE_PREFIX, quickOpenService);
1184 1185

		this.class = 'show-group-editors-action';
1186
	}
B
Benjamin Pasero 已提交
1187 1188
}

B
Benjamin Pasero 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
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)) {
1211
			case Position.TWO:
B
Benjamin Pasero 已提交
1212
				return this.quickOpenService.show(NAVIGATE_IN_GROUP_TWO_PREFIX);
1213
			case Position.THREE:
1214
				return this.quickOpenService.show(NAVIGATE_IN_GROUP_THREE_PREFIX);
B
Benjamin Pasero 已提交
1215 1216
		}

1217
		return this.quickOpenService.show(NAVIGATE_IN_GROUP_ONE_PREFIX);
B
Benjamin Pasero 已提交
1218 1219 1220
	}
}

1221
export const NAVIGATE_ALL_EDITORS_GROUP_PREFIX = 'edt ';
B
Benjamin Pasero 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

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

1233
export class BaseQuickOpenEditorInGroupAction extends Action {
B
Benjamin Pasero 已提交
1234 1235 1236 1237 1238

	constructor(
		id: string,
		label: string,
		@IQuickOpenService private quickOpenService: IQuickOpenService,
1239
		@IKeybindingService private keybindingService: IKeybindingService,
1240
		@IEditorGroupService private editorGroupService: IEditorGroupService
B
Benjamin Pasero 已提交
1241 1242 1243 1244 1245
	) {
		super(id, label);
	}

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

1248
		const stacks = this.editorGroupService.getStacksModel();
1249 1250
		if (stacks.activeGroup) {
			const activePosition = stacks.positionOfGroup(stacks.activeGroup);
1251
			let prefix = NAVIGATE_IN_GROUP_ONE_PREFIX;
1252

B
Benjamin Pasero 已提交
1253
			if (activePosition === Position.TWO) {
1254
				prefix = NAVIGATE_IN_GROUP_TWO_PREFIX;
B
Benjamin Pasero 已提交
1255
			} else if (activePosition === Position.THREE) {
1256
				prefix = NAVIGATE_IN_GROUP_THREE_PREFIX;
1257 1258
			}

B
Benjamin Pasero 已提交
1259
			this.quickOpenService.show(prefix, { quickNavigateConfiguration: { keybindings: keys } });
1260
		}
B
Benjamin Pasero 已提交
1261 1262 1263 1264 1265

		return TPromise.as(true);
	}
}

1266 1267 1268
export class OpenPreviousRecentlyUsedEditorInGroupAction extends BaseQuickOpenEditorInGroupAction {

	public static ID = 'workbench.action.openPreviousRecentlyUsedEditorInGroup';
1269
	public static LABEL = nls.localize('openPreviousRecentlyUsedEditorInGroup', "Open Previous Recently Used Editor in Group");
1270 1271 1272 1273 1274 1275

	constructor(
		id: string,
		label: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IKeybindingService keybindingService: IKeybindingService,
1276
		@IEditorGroupService editorGroupService: IEditorGroupService
1277
	) {
1278
		super(id, label, quickOpenService, keybindingService, editorGroupService);
1279 1280 1281 1282 1283 1284
	}
}

export class OpenNextRecentlyUsedEditorInGroupAction extends BaseQuickOpenEditorInGroupAction {

	public static ID = 'workbench.action.openNextRecentlyUsedEditorInGroup';
1285
	public static LABEL = nls.localize('openNextRecentlyUsedEditorInGroup', "Open Next Recently Used Editor in Group");
1286 1287 1288 1289 1290 1291

	constructor(
		id: string,
		label: string,
		@IQuickOpenService quickOpenService: IQuickOpenService,
		@IKeybindingService keybindingService: IKeybindingService,
1292
		@IEditorGroupService editorGroupService: IEditorGroupService
1293
	) {
1294
		super(id, label, quickOpenService, keybindingService, editorGroupService);
1295 1296 1297
	}
}

1298
export class OpenPreviousEditorFromHistoryAction extends Action {
B
Benjamin Pasero 已提交
1299

1300
	public static ID = 'workbench.action.openPreviousEditorFromHistory';
B
Benjamin Pasero 已提交
1301
	public static LABEL = nls.localize('navigateEditorHistoryByInput', "Open Previous Editor from History");
B
Benjamin Pasero 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312

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

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

B
Benjamin Pasero 已提交
1315
		this.quickOpenService.show(null, { quickNavigateConfiguration: { keybindings: keys } });
B
Benjamin Pasero 已提交
1316 1317 1318 1319 1320

		return TPromise.as(true);
	}
}

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
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);
	}
}

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
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> {

1368
		// Editor history
1369 1370 1371 1372 1373 1374
		this.historyService.clear();

		return TPromise.as(true);
	}
}

1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
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);
			}
		}

1400 1401 1402 1403
		return TPromise.as(true);
	}
}

1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
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 = {
1419
			to: ActiveEditorMovePositioning.LEFT
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
		};
		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 = {
1442
			to: ActiveEditorMovePositioning.RIGHT
1443 1444 1445 1446 1447 1448 1449
		};
		this.commandService.executeCommand(EditorCommands.MoveActiveEditor, args);

		return TPromise.as(true);
	}
}

1450
export class MoveEditorToPreviousGroupAction extends Action {
1451

1452 1453
	public static ID = 'workbench.action.moveEditorToPreviousGroup';
	public static LABEL = nls.localize('moveEditorToPreviousGroup', "Move Editor into Previous Group");
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

	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();
1466
		if (activeEditor && activeEditor.position !== Position.ONE) {
1467 1468 1469 1470 1471 1472 1473
			this.editorGroupService.moveEditor(activeEditor.input, activeEditor.position, activeEditor.position - 1);
		}

		return TPromise.as(true);
	}
}

1474
export class MoveEditorToNextGroupAction extends Action {
1475

1476 1477
	public static ID = 'workbench.action.moveEditorToNextGroup';
	public static LABEL = nls.localize('moveEditorToNextGroup', "Move Editor into Next Group");
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489

	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();
1490
		if (activeEditor && activeEditor.position !== Position.THREE) {
1491 1492 1493
			this.editorGroupService.moveEditor(activeEditor.input, activeEditor.position, activeEditor.position + 1);
		}

1494 1495
		return TPromise.as(true);
	}
D
Daniel Imms 已提交
1496
}