editorCommands.ts 15.0 KB
Newer Older
S
Sandeep Somavarapu 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
import * as types from 'vs/base/common/types';
J
Johannes Rieken 已提交
8 9 10
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
11
import { ActiveEditorMoveArguments, ActiveEditorMovePositioning, ActiveEditorMovePositioningBy, EditorCommands, TextCompareEditorVisible, IEditorContext, EditorInput, EditorFocusedInOpenEditorsContext } from 'vs/workbench/common/editor';
J
Johannes Rieken 已提交
12 13
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditor, Position, POSITIONS } from 'vs/platform/editor/common/editor';
14
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
15
import { TextDiffEditor } from 'vs/workbench/browser/parts/editor/textDiffEditor';
J
Johannes Rieken 已提交
16
import { EditorStacksModel } from 'vs/workbench/common/editor/editorStacksModel';
17
import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/commands';
J
Johannes Rieken 已提交
18 19
import { IMessageService, Severity, CloseAction } from 'vs/platform/message/common/message';
import { Action } from 'vs/base/common/actions';
20
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
21
import { TPromise } from 'vs/base/common/winjs.base';
22
import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
23 24

export const CLOSE_UNMODIFIED_EDITORS_COMMAND_ID = 'workbench.command.closeUnmodifiedEditors';
25 26
export const CLOSE_UNMODIFIED_EDITORS_LABEL = nls.localize('closeUnmodifiedEditors', "Close Unmodified Editors in Group");

27
export const CLOSE_EDITORS_IN_GROUP_COMMAND_ID = 'workbench.command.closeEditorsInGroup';
28 29
export const CLOSE_EDITORS_IN_GROUP_LABEL = nls.localize('closeEditorsInGroup', "Close All Editors in Group");

I
isidor 已提交
30
export const CLOSE_EDITOR_COMMAND_ID = 'workbench.command.closeActiveEditor';
31 32
export const CLOSE_EDITOR_LABEL = nls.localize('closeEditor', "Close Editor");

I
isidor 已提交
33
export const CLOSE_OTHER_EDITORS_IN_GROUP_COMMAND_ID = 'workbench.command.closeOtherEditors';
34 35
export const CLOSE_OTHER_EDITORS_IN_GROUP_LABEL = nls.localize('closeOtherEditorsInGroup', "Close Other Editors");

36

37
export function setup(): void {
38 39
	registerActiveEditorMoveCommand();
	registerDiffEditorCommands();
40
	registerOpenEditorAtIndexCommands();
41
	registerEditorCommands();
42
	handleCommandDeprecations();
S
Sandeep Somavarapu 已提交
43 44
}

B
Benjamin Pasero 已提交
45
const isActiveEditorMoveArg = function (arg: ActiveEditorMoveArguments): boolean {
S
Sandeep Somavarapu 已提交
46 47 48 49
	if (!types.isObject(arg)) {
		return false;
	}

50
	const activeEditorMoveArg: ActiveEditorMoveArguments = arg;
S
Sandeep Somavarapu 已提交
51 52 53 54 55 56 57 58 59

	if (!types.isString(activeEditorMoveArg.to)) {
		return false;
	}

	if (!types.isUndefined(activeEditorMoveArg.by) && !types.isString(activeEditorMoveArg.by)) {
		return false;
	}

60
	if (!types.isUndefined(activeEditorMoveArg.value) && !types.isNumber(activeEditorMoveArg.value)) {
S
Sandeep Somavarapu 已提交
61 62 63 64 65 66
		return false;
	}

	return true;
};

67
function registerActiveEditorMoveCommand(): void {
A
Alex Dima 已提交
68
	KeybindingsRegistry.registerCommandAndKeybindingRule({
69
		id: EditorCommands.MoveActiveEditor,
S
Sandeep Somavarapu 已提交
70
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
71
		when: EditorContextKeys.textFocus,
S
Sandeep Somavarapu 已提交
72
		primary: null,
73
		handler: (accessor, args: any) => moveActiveEditor(args, accessor),
S
Sandeep Somavarapu 已提交
74
		description: {
75
			description: nls.localize('editorCommand.activeEditorMove.description', "Move the active editor by tabs or groups"),
S
Sandeep Somavarapu 已提交
76 77 78
			args: [
				{
					name: nls.localize('editorCommand.activeEditorMove.arg.name', "Active editor move argument"),
J
Joao Moreno 已提交
79
					description: nls.localize('editorCommand.activeEditorMove.arg.description', "Argument Properties:\n\t* 'to': String value providing where to move.\n\t* 'by': String value providing the unit for move. By tab or by group.\n\t* 'value': Number value providing how many positions or an absolute position to move."),
S
Sandeep Somavarapu 已提交
80 81 82 83 84 85 86
					constraint: isActiveEditorMoveArg
				}
			]
		}
	});
}

87
function moveActiveEditor(args: ActiveEditorMoveArguments = {}, accessor: ServicesAccessor): void {
I
isidor 已提交
88
	const showTabs = accessor.get(IEditorGroupService).getTabOptions().showTabs;
S
Sandeep Somavarapu 已提交
89
	args.to = args.to || ActiveEditorMovePositioning.RIGHT;
I
isidor 已提交
90
	args.by = showTabs ? args.by || ActiveEditorMovePositioningBy.TAB : ActiveEditorMovePositioningBy.GROUP;
91
	args.value = types.isUndefined(args.value) ? 1 : args.value;
S
Sandeep Somavarapu 已提交
92

93
	const activeEditor = accessor.get(IWorkbenchEditorService).getActiveEditor();
94 95 96 97 98 99 100
	if (activeEditor) {
		switch (args.by) {
			case ActiveEditorMovePositioningBy.TAB:
				return moveActiveTab(args, activeEditor, accessor);
			case ActiveEditorMovePositioningBy.GROUP:
				return moveActiveEditorToGroup(args, activeEditor, accessor);
		}
S
Sandeep Somavarapu 已提交
101 102 103
	}
}

104
function moveActiveTab(args: ActiveEditorMoveArguments, activeEditor: IEditor, accessor: ServicesAccessor): void {
105
	const editorGroupsService: IEditorGroupService = accessor.get(IEditorGroupService);
S
Sandeep Somavarapu 已提交
106
	const editorGroup = editorGroupsService.getStacksModel().groupAt(activeEditor.position);
107
	let index = editorGroup.indexOf(activeEditor.input);
S
Sandeep Somavarapu 已提交
108 109 110 111 112 113 114 115
	switch (args.to) {
		case ActiveEditorMovePositioning.FIRST:
			index = 0;
			break;
		case ActiveEditorMovePositioning.LAST:
			index = editorGroup.count - 1;
			break;
		case ActiveEditorMovePositioning.LEFT:
116
			index = index - args.value;
S
Sandeep Somavarapu 已提交
117 118
			break;
		case ActiveEditorMovePositioning.RIGHT:
119
			index = index + args.value;
S
Sandeep Somavarapu 已提交
120 121
			break;
		case ActiveEditorMovePositioning.CENTER:
122
			index = Math.round(editorGroup.count / 2) - 1;
S
Sandeep Somavarapu 已提交
123 124
			break;
		case ActiveEditorMovePositioning.POSITION:
125
			index = args.value - 1;
S
Sandeep Somavarapu 已提交
126 127
			break;
	}
128

S
Sandeep Somavarapu 已提交
129
	index = index < 0 ? 0 : index >= editorGroup.count ? editorGroup.count - 1 : index;
130
	editorGroupsService.moveEditor(activeEditor.input, editorGroup, editorGroup, { index });
S
Sandeep Somavarapu 已提交
131 132
}

133
function moveActiveEditorToGroup(args: ActiveEditorMoveArguments, activeEditor: IEditor, accessor: ServicesAccessor): void {
134
	let newPosition = activeEditor.position;
S
Sandeep Somavarapu 已提交
135 136
	switch (args.to) {
		case ActiveEditorMovePositioning.LEFT:
S
Sandeep Somavarapu 已提交
137 138 139 140 141 142
			newPosition = newPosition - 1;
			break;
		case ActiveEditorMovePositioning.RIGHT:
			newPosition = newPosition + 1;
			break;
		case ActiveEditorMovePositioning.FIRST:
143
			newPosition = Position.ONE;
S
Sandeep Somavarapu 已提交
144 145
			break;
		case ActiveEditorMovePositioning.LAST:
146
			newPosition = Position.THREE;
S
Sandeep Somavarapu 已提交
147 148
			break;
		case ActiveEditorMovePositioning.CENTER:
149
			newPosition = Position.TWO;
S
Sandeep Somavarapu 已提交
150 151
			break;
		case ActiveEditorMovePositioning.POSITION:
152
			newPosition = args.value - 1;
S
Sandeep Somavarapu 已提交
153 154
			break;
	}
155

S
Sandeep Somavarapu 已提交
156 157
	newPosition = POSITIONS.indexOf(newPosition) !== -1 ? newPosition : activeEditor.position;
	accessor.get(IEditorGroupService).moveEditor(activeEditor.input, activeEditor.position, newPosition);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
}

function registerDiffEditorCommands(): void {
	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'workbench.action.compareEditor.nextChange',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: TextCompareEditorVisible,
		primary: null,
		handler: accessor => navigateInDiffEditor(accessor, true)
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'workbench.action.compareEditor.previousChange',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: TextCompareEditorVisible,
		primary: null,
		handler: accessor => navigateInDiffEditor(accessor, false)
	});

	function navigateInDiffEditor(accessor: ServicesAccessor, next: boolean): void {
		let editorService = accessor.get(IWorkbenchEditorService);
		const candidates = [editorService.getActiveEditor(), ...editorService.getVisibleEditors()].filter(e => e instanceof TextDiffEditor);

		if (candidates.length > 0) {
			next ? (<TextDiffEditor>candidates[0]).getDiffNavigator().next() : (<TextDiffEditor>candidates[0]).getDiffNavigator().previous();
		}
	}

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: '_workbench.printStacksModel',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
		handler(accessor: ServicesAccessor) {
			console.log(`${accessor.get(IEditorGroupService).getStacksModel().toString()}\n\n`);
		},
		when: undefined,
		primary: undefined
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: '_workbench.validateStacksModel',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
		handler(accessor: ServicesAccessor) {
			(<EditorStacksModel>accessor.get(IEditorGroupService).getStacksModel()).validate();
		},
		when: undefined,
		primary: undefined
	});
}

function handleCommandDeprecations(): void {
	const mapDeprecatedCommands = {
209 210
		'workbench.action.files.newFile': 'explorer.newFile',
		'workbench.action.files.newFolder': 'explorer.newFolder'
211 212 213
	};

	Object.keys(mapDeprecatedCommands).forEach(deprecatedCommandId => {
214
		const newCommandId: string = mapDeprecatedCommands[deprecatedCommandId];
215 216 217 218 219 220 221 222 223 224 225 226 227

		KeybindingsRegistry.registerCommandAndKeybindingRule({
			id: deprecatedCommandId,
			weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
			handler(accessor: ServicesAccessor) {
				const messageService = accessor.get(IMessageService);
				const commandService = accessor.get(ICommandService);

				messageService.show(Severity.Warning, {
					message: nls.localize('commandDeprecated', "Command **{0}** has been removed. You can use **{1}** instead", deprecatedCommandId, newCommandId),
					actions: [
						new Action('openKeybindings', nls.localize('openKeybindings', "Configure Keyboard Shortcuts"), null, true, () => {
							return commandService.executeCommand('workbench.action.openGlobalKeybindings');
228 229
						}),
						CloseAction
230 231 232 233 234 235 236
					]
				});
			},
			when: undefined,
			primary: undefined
		});
	});
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}

function registerOpenEditorAtIndexCommands(): void {

	// Keybindings to focus a specific index in the tab folder if tabs are enabled
	for (let i = 0; i < 9; i++) {
		const editorIndex = i;
		const visibleIndex = i + 1;

		KeybindingsRegistry.registerCommandAndKeybindingRule({
			id: 'workbench.action.openEditorAtIndex' + visibleIndex,
			weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
			when: void 0,
			primary: KeyMod.Alt | toKeyCode(visibleIndex),
			mac: { primary: KeyMod.WinCtrl | toKeyCode(visibleIndex) },
			handler: accessor => {
				const editorService = accessor.get(IWorkbenchEditorService);
				const editorGroupService = accessor.get(IEditorGroupService);

				const active = editorService.getActiveEditor();
				if (active) {
					const group = editorGroupService.getStacksModel().groupAt(active.position);
					const editor = group.getEditor(editorIndex);

					if (editor) {
						return editorService.openEditor(editor);
					}
				}
265 266

				return void 0;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
			}
		});
	}

	function toKeyCode(index: number): KeyCode {
		switch (index) {
			case 0: return KeyCode.KEY_0;
			case 1: return KeyCode.KEY_1;
			case 2: return KeyCode.KEY_2;
			case 3: return KeyCode.KEY_3;
			case 4: return KeyCode.KEY_4;
			case 5: return KeyCode.KEY_5;
			case 6: return KeyCode.KEY_6;
			case 7: return KeyCode.KEY_7;
			case 8: return KeyCode.KEY_8;
			case 9: return KeyCode.KEY_9;
		}
284 285

		return void 0;
286
	}
287 288
}

289
function registerEditorCommands() {
290

291
	CommandsRegistry.registerCommand({
292
		id: CLOSE_UNMODIFIED_EDITORS_COMMAND_ID,
I
isidor 已提交
293
		handler: (accessor, args: IEditorContext) => {
294 295 296
			const editorGroupService = accessor.get(IEditorGroupService);
			const editorService = accessor.get(IWorkbenchEditorService);

I
isidor 已提交
297
			let position = args ? editorGroupService.getStacksModel().positionOfGroup(args.group) : null;
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

			// If position is not passed in take the position of the active editor.
			if (typeof position !== 'number') {
				const active = editorService.getActiveEditor();
				if (active) {
					position = active.position;
				}
			}

			if (typeof position === 'number') {
				return editorService.closeEditors(position, { unmodifiedOnly: true });
			}

			return TPromise.as(false);
		}
	});

315 316 317 318 319
	MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
		group: 'close',
		command: {
			id: CLOSE_UNMODIFIED_EDITORS_COMMAND_ID,
			title: CLOSE_UNMODIFIED_EDITORS_LABEL
320 321
		},
		when: EditorFocusedInOpenEditorsContext
322 323
	});

324
	CommandsRegistry.registerCommand({
325
		id: CLOSE_EDITORS_IN_GROUP_COMMAND_ID,
I
isidor 已提交
326
		handler: (accessor, args: IEditorContext) => {
327 328 329
			const editorGroupService = accessor.get(IEditorGroupService);
			const editorService = accessor.get(IWorkbenchEditorService);

I
isidor 已提交
330
			let position = args ? editorGroupService.getStacksModel().positionOfGroup(args.group) : null;
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
			if (typeof position !== 'number') {
				const activeEditor = editorService.getActiveEditor();
				if (activeEditor) {
					position = activeEditor.position;
				}
			}

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

			return TPromise.as(false);
		}
	});

346 347 348 349 350
	MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
		group: 'close',
		command: {
			id: CLOSE_EDITORS_IN_GROUP_COMMAND_ID,
			title: CLOSE_EDITORS_IN_GROUP_LABEL
351 352
		},
		when: EditorFocusedInOpenEditorsContext
353 354
	});

355
	CommandsRegistry.registerCommand({
I
isidor 已提交
356 357 358
		id: CLOSE_EDITOR_COMMAND_ID,
		handler: (accessor, args: IEditorContext) => {
			const editorGroupService = accessor.get(IEditorGroupService);
359
			const editorService = accessor.get(IWorkbenchEditorService);
I
isidor 已提交
360 361 362 363 364 365 366 367 368

			const position = args ? editorGroupService.getStacksModel().positionOfGroup(args.group) : null;

			// Close Active Editor
			if (typeof position !== 'number') {
				const activeEditor = editorService.getActiveEditor();
				if (activeEditor) {
					return editorService.closeEditor(activeEditor.position, activeEditor.input);
				}
369 370
			}

I
isidor 已提交
371 372 373 374 375 376 377
			let input = args ? args.editor : null;
			if (!input) {

				// Get Top Editor at Position
				const visibleEditors = editorService.getVisibleEditors();
				if (visibleEditors[position]) {
					input = visibleEditors[position].input;
378
				}
I
isidor 已提交
379 380 381 382 383 384 385
			}

			if (input) {
				return editorService.closeEditor(position, input);
			}

			return TPromise.as(false);
386 387 388
		}
	});

389 390 391 392 393
	MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
		group: 'close',
		command: {
			id: CLOSE_EDITOR_COMMAND_ID,
			title: CLOSE_EDITOR_LABEL
394 395
		},
		when: EditorFocusedInOpenEditorsContext
396 397
	});

398
	CommandsRegistry.registerCommand({
I
isidor 已提交
399 400 401
		id: CLOSE_OTHER_EDITORS_IN_GROUP_COMMAND_ID,
		handler: (accessor, args: IEditorContext) => {
			const editorGroupService = accessor.get(IEditorGroupService);
402 403
			const editorService = accessor.get(IWorkbenchEditorService);

I
isidor 已提交
404 405 406 407 408 409 410 411
			let position = args ? editorGroupService.getStacksModel().positionOfGroup(args.group) : null;
			let input = args ? args.editor : null;

			// If position or input are not passed in take the position and input of the active editor.
			const active = editorService.getActiveEditor();
			if (active) {
				position = typeof position === 'number' ? position : active.position;
				input = input ? input : <EditorInput>active.input;
412 413
			}

I
isidor 已提交
414 415
			if (typeof position === 'number' && input) {
				return editorService.closeEditors(position, { except: input });
416 417
			}

I
isidor 已提交
418
			return TPromise.as(false);
419 420
		}
	});
421 422 423 424 425 426

	MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
		group: 'close',
		command: {
			id: CLOSE_OTHER_EDITORS_IN_GROUP_COMMAND_ID,
			title: CLOSE_OTHER_EDITORS_IN_GROUP_LABEL
427 428
		},
		when: EditorFocusedInOpenEditorsContext
429
	});
430
}