editorCommands.ts 10.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 } from 'vs/workbench/common/editor';
J
Johannes Rieken 已提交
12 13 14
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditor, Position, POSITIONS } from 'vs/platform/editor/common/editor';
import { EditorContextKeys } from 'vs/editor/common/editorCommon';
15
import { TextDiffEditor } from 'vs/workbench/browser/parts/editor/textDiffEditor';
J
Johannes Rieken 已提交
16 17 18 19
import { EditorStacksModel } from 'vs/workbench/common/editor/editorStacksModel';
import { ICommandService } from 'vs/platform/commands/common/commands';
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

22
export function setup(): void {
23 24
	registerActiveEditorMoveCommand();
	registerDiffEditorCommands();
25
	registerOpenEditorAtIndexCommands();
26
	handleCommandDeprecations();
S
Sandeep Somavarapu 已提交
27 28
}

B
Benjamin Pasero 已提交
29
const isActiveEditorMoveArg = function (arg: ActiveEditorMoveArguments): boolean {
S
Sandeep Somavarapu 已提交
30 31 32 33
	if (!types.isObject(arg)) {
		return false;
	}

34
	const activeEditorMoveArg: ActiveEditorMoveArguments = arg;
S
Sandeep Somavarapu 已提交
35 36 37 38 39 40 41 42 43

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

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

44
	if (!types.isUndefined(activeEditorMoveArg.value) && !types.isNumber(activeEditorMoveArg.value)) {
S
Sandeep Somavarapu 已提交
45 46 47 48 49 50
		return false;
	}

	return true;
};

51
function registerActiveEditorMoveCommand(): void {
A
Alex Dima 已提交
52
	KeybindingsRegistry.registerCommandAndKeybindingRule({
53
		id: EditorCommands.MoveActiveEditor,
S
Sandeep Somavarapu 已提交
54
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
A
Alex Dima 已提交
55
		when: EditorContextKeys.TextFocus,
S
Sandeep Somavarapu 已提交
56
		primary: null,
57
		handler: (accessor, args: any) => moveActiveEditor(args, accessor),
S
Sandeep Somavarapu 已提交
58
		description: {
59
			description: nls.localize('editorCommand.activeEditorMove.description', "Move the active editor by tabs or groups"),
S
Sandeep Somavarapu 已提交
60 61 62
			args: [
				{
					name: nls.localize('editorCommand.activeEditorMove.arg.name', "Active editor move argument"),
63
					description: nls.localize('editorCommand.activeEditorMove.arg.description', `Argument Properties:
64 65 66
						* 'to': String value providing where to move.
						* 'by': String value providing the unit for move. By tab or by group.
						* 'value': Number value providing how many positions or an absolute position to move.
67
					`),
S
Sandeep Somavarapu 已提交
68 69 70 71 72 73 74
					constraint: isActiveEditorMoveArg
				}
			]
		}
	});
}

75
function moveActiveEditor(args: ActiveEditorMoveArguments = {}, accessor: ServicesAccessor): void {
I
isidor 已提交
76
	const showTabs = accessor.get(IEditorGroupService).getTabOptions().showTabs;
S
Sandeep Somavarapu 已提交
77
	args.to = args.to || ActiveEditorMovePositioning.RIGHT;
I
isidor 已提交
78
	args.by = showTabs ? args.by || ActiveEditorMovePositioningBy.TAB : ActiveEditorMovePositioningBy.GROUP;
79
	args.value = types.isUndefined(args.value) ? 1 : args.value;
S
Sandeep Somavarapu 已提交
80

81
	const activeEditor = accessor.get(IWorkbenchEditorService).getActiveEditor();
82 83 84 85 86 87 88
	if (activeEditor) {
		switch (args.by) {
			case ActiveEditorMovePositioningBy.TAB:
				return moveActiveTab(args, activeEditor, accessor);
			case ActiveEditorMovePositioningBy.GROUP:
				return moveActiveEditorToGroup(args, activeEditor, accessor);
		}
S
Sandeep Somavarapu 已提交
89 90 91
	}
}

92
function moveActiveTab(args: ActiveEditorMoveArguments, activeEditor: IEditor, accessor: ServicesAccessor): void {
93
	const editorGroupsService: IEditorGroupService = accessor.get(IEditorGroupService);
S
Sandeep Somavarapu 已提交
94
	const editorGroup = editorGroupsService.getStacksModel().groupAt(activeEditor.position);
95
	let index = editorGroup.indexOf(activeEditor.input);
S
Sandeep Somavarapu 已提交
96 97 98 99 100 101 102 103
	switch (args.to) {
		case ActiveEditorMovePositioning.FIRST:
			index = 0;
			break;
		case ActiveEditorMovePositioning.LAST:
			index = editorGroup.count - 1;
			break;
		case ActiveEditorMovePositioning.LEFT:
104
			index = index - args.value;
S
Sandeep Somavarapu 已提交
105 106
			break;
		case ActiveEditorMovePositioning.RIGHT:
107
			index = index + args.value;
S
Sandeep Somavarapu 已提交
108 109
			break;
		case ActiveEditorMovePositioning.CENTER:
110
			index = Math.round(editorGroup.count / 2) - 1;
S
Sandeep Somavarapu 已提交
111 112
			break;
		case ActiveEditorMovePositioning.POSITION:
113
			index = args.value - 1;
S
Sandeep Somavarapu 已提交
114 115
			break;
	}
116

S
Sandeep Somavarapu 已提交
117
	index = index < 0 ? 0 : index >= editorGroup.count ? editorGroup.count - 1 : index;
118
	editorGroupsService.moveEditor(activeEditor.input, editorGroup, editorGroup, { index });
S
Sandeep Somavarapu 已提交
119 120
}

121
function moveActiveEditorToGroup(args: ActiveEditorMoveArguments, activeEditor: IEditor, accessor: ServicesAccessor): void {
122
	let newPosition = activeEditor.position;
S
Sandeep Somavarapu 已提交
123 124
	switch (args.to) {
		case ActiveEditorMovePositioning.LEFT:
S
Sandeep Somavarapu 已提交
125 126 127 128 129 130
			newPosition = newPosition - 1;
			break;
		case ActiveEditorMovePositioning.RIGHT:
			newPosition = newPosition + 1;
			break;
		case ActiveEditorMovePositioning.FIRST:
131
			newPosition = Position.ONE;
S
Sandeep Somavarapu 已提交
132 133
			break;
		case ActiveEditorMovePositioning.LAST:
134
			newPosition = Position.THREE;
S
Sandeep Somavarapu 已提交
135 136
			break;
		case ActiveEditorMovePositioning.CENTER:
137
			newPosition = Position.TWO;
S
Sandeep Somavarapu 已提交
138 139
			break;
		case ActiveEditorMovePositioning.POSITION:
140
			newPosition = args.value - 1;
S
Sandeep Somavarapu 已提交
141 142
			break;
	}
143

S
Sandeep Somavarapu 已提交
144 145
	newPosition = POSITIONS.indexOf(newPosition) !== -1 ? newPosition : activeEditor.position;
	accessor.get(IEditorGroupService).moveEditor(activeEditor.input, activeEditor.position, newPosition);
146 147 148 149 150 151 152 153 154 155 156 157 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
}

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 = {
197 198
		'workbench.action.files.newFile': 'explorer.newFile',
		'workbench.action.files.newFolder': 'explorer.newFolder'
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	};

	Object.keys(mapDeprecatedCommands).forEach(deprecatedCommandId => {
		const newCommandId = mapDeprecatedCommands[deprecatedCommandId];

		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');
216 217
						}),
						CloseAction
218 219 220 221 222 223 224
					]
				});
			},
			when: undefined,
			primary: undefined
		});
	});
225 226 227 228 229 230 231 232 233 234 235 236 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 265 266 267 268 269 270 271 272
}

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

	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;
		}
		return undefined;
	}
S
Sandeep Somavarapu 已提交
273
}