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 11 12 13 14 15 16 17 18 19 20
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
import { IWorkbenchEditorConfiguration, ActiveEditorMoveArguments, ActiveEditorMovePositioning, ActiveEditorMovePositioningBy, EditorCommands } from 'vs/workbench/common/editor';
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';
import { TextCompareEditorVisible, TextDiffEditor } from 'vs/workbench/browser/parts/editor/textDiffEditor';
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';
21 22 23 24 25

export function setup() {
	registerActiveEditorMoveCommand();
	registerDiffEditorCommands();
	handleCommandDeprecations();
S
Sandeep Somavarapu 已提交
26 27
}

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

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

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

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

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

	return true;
};

50
function registerActiveEditorMoveCommand() {
A
Alex Dima 已提交
51
	KeybindingsRegistry.registerCommandAndKeybindingRule({
52
		id: EditorCommands.MoveActiveEditor,
S
Sandeep Somavarapu 已提交
53
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
A
Alex Dima 已提交
54
		when: EditorContextKeys.TextFocus,
S
Sandeep Somavarapu 已提交
55
		primary: null,
56
		handler: (accessor, args: any) => moveActiveEditor(args, accessor),
S
Sandeep Somavarapu 已提交
57
		description: {
58
			description: nls.localize('editorCommand.activeEditorMove.description', "Move the active editor by tabs or groups"),
S
Sandeep Somavarapu 已提交
59 60 61
			args: [
				{
					name: nls.localize('editorCommand.activeEditorMove.arg.name', "Active editor move argument"),
62
					description: nls.localize('editorCommand.activeEditorMove.arg.description', `Argument Properties:
63 64 65
						* '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.
66
					`),
S
Sandeep Somavarapu 已提交
67 68 69 70 71 72 73
					constraint: isActiveEditorMoveArg
				}
			]
		}
	});
}

74
function moveActiveEditor(args: ActiveEditorMoveArguments = {}, accessor: ServicesAccessor) {
75 76
	const config = <IWorkbenchEditorConfiguration>accessor.get(IConfigurationService).getConfiguration();
	const tabsShown = config.workbench && config.workbench.editor && config.workbench.editor.showTabs;
S
Sandeep Somavarapu 已提交
77
	args.to = args.to || ActiveEditorMovePositioning.RIGHT;
78 79
	args.by = tabsShown ? args.by || ActiveEditorMovePositioningBy.TAB : ActiveEditorMovePositioningBy.GROUP;
	args.value = types.isUndefined(args.value) ? 1 : args.value;
S
Sandeep Somavarapu 已提交
80

81
	const activeEditor = accessor.get(IWorkbenchEditorService).getActiveEditor();
S
Sandeep Somavarapu 已提交
82 83 84

	switch (args.by) {
		case ActiveEditorMovePositioningBy.TAB:
85
			return moveActiveTab(args, activeEditor, accessor);
S
Sandeep Somavarapu 已提交
86
		case ActiveEditorMovePositioningBy.GROUP:
87
			return moveActiveEditorToGroup(args, activeEditor, accessor);
S
Sandeep Somavarapu 已提交
88 89 90
	}
}

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

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

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

S
Sandeep Somavarapu 已提交
143 144
	newPosition = POSITIONS.indexOf(newPosition) !== -1 ? newPosition : activeEditor.position;
	accessor.get(IEditorGroupService).moveEditor(activeEditor.input, activeEditor.position, newPosition);
145 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
}

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 = {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		'workbench.action.focusFirstEditor': 'workbench.action.focusFirstEditorGroup',
		'workbench.action.focusSecondEditor': 'workbench.action.focusSecondEditorGroup',
		'workbench.action.focusThirdEditor': 'workbench.action.focusThirdEditorGroup',
		'workbench.action.focusLeftEditor': 'workbench.action.focusPreviousGroup',
		'workbench.action.focusRightEditor': 'workbench.action.focusNextGroup',
		'workbench.action.moveActiveEditorLeft': 'workbench.action.moveActiveEditorGroupLeft',
		'workbench.action.moveActiveEditorRight': 'workbench.action.moveActiveEditorGroupRight',
		'workbench.action.openPreviousEditor': 'workbench.action.openPreviousEditorFromHistory',
		'workbench.files.action.addToWorkingFiles': 'workbench.action.keepEditor',
		'workbench.files.action.closeAllFiles': 'workbench.action.closeAllEditors',
		'workbench.files.action.closeFile': 'workbench.action.closeActiveEditor',
		'workbench.files.action.closeOtherFiles': 'workbench.action.closeOtherEditors',
		'workbench.files.action.focusWorkingFiles': 'workbench.files.action.focusOpenEditorsView',
		'workbench.files.action.openNextWorkingFile': 'workbench.action.nextEditor',
		'workbench.files.action.openPreviousWorkingFile': 'workbench.action.previousEditor',
		'workbench.files.action.reopenClosedFile': 'workbench.action.reopenClosedEditor',
		'workbench.files.action.workingFilesPicker': 'workbench.action.showAllEditors',
		'workbench.action.cycleEditor': 'workbench.action.navigateEditorGroups',
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		'workbench.action.terminal.focus': 'workbench.action.focusPanel'
	};

	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');
232 233
						}),
						CloseAction
234 235 236 237 238 239 240
					]
				});
			},
			when: undefined,
			primary: undefined
		});
	});
S
Sandeep Somavarapu 已提交
241
}