editorCommands.ts 5.2 KB
Newer Older
S
Sandeep Somavarapu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*---------------------------------------------------------------------------------------------
 *  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';
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';
A
Alex Dima 已提交
15
import { EditorKbExpr } from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
16

S
Sandeep Somavarapu 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
export function registerEditorComamnds() {
	_registerActiveEditorMoveCommand();
}

let isActiveEditorMoveArg= function(arg): boolean  {
	if (!types.isObject(arg)) {
		return false;
	}

	let activeEditorMoveArg: ActiveEditorMoveArguments = arg;

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

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

36
	if (!types.isUndefined(activeEditorMoveArg.value) && !types.isNumber(activeEditorMoveArg.value)) {
S
Sandeep Somavarapu 已提交
37 38 39 40 41 42 43 44
		return false;
	}

	return true;
};


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

function _moveActiveEditor(args: ActiveEditorMoveArguments = {}, accessor: ServicesAccessor) {
	let tabsShown = !!(<IWorkbenchEditorConfiguration>accessor.get(IConfigurationService).getConfiguration()).workbench.editor.showTabs;
	args.to = args.to || ActiveEditorMovePositioning.RIGHT;
72 73
	args.by = tabsShown ? args.by || ActiveEditorMovePositioningBy.TAB : ActiveEditorMovePositioningBy.GROUP;
	args.value = types.isUndefined(args.value) ? 1 : args.value;
S
Sandeep Somavarapu 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

	let activeEditor = accessor.get(IWorkbenchEditorService).getActiveEditor();

	switch (args.by) {
		case ActiveEditorMovePositioningBy.TAB:
			return _moveActiveTab(args, activeEditor, accessor);
		case ActiveEditorMovePositioningBy.GROUP:
			return _moveActiveEditorToGroup(args, activeEditor, accessor);
	}
}

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

function _moveActiveEditorToGroup(args: ActiveEditorMoveArguments, activeEditor: IEditor, accessor: ServicesAccessor) {
	let newPosition= activeEditor.position;
	switch (args.to) {
		case ActiveEditorMovePositioning.FIRST:
		case ActiveEditorMovePositioning.LEFT:
			newPosition = Position.LEFT;
			break;
		case ActiveEditorMovePositioning.LAST:
		case ActiveEditorMovePositioning.RIGHT:
			newPosition = Position.RIGHT;
			break;
		case ActiveEditorMovePositioning.CENTER:
			newPosition = Position.CENTER;
			break;
		case ActiveEditorMovePositioning.POSITION:
128
			newPosition = args.value - 1;
S
Sandeep Somavarapu 已提交
129 130 131 132 133
			break;
	}
	newPosition = POSITIONS.indexOf(newPosition) !== -1 ? newPosition : activeEditor.position;
	accessor.get(IEditorGroupService).moveEditor(activeEditor.input, activeEditor.position, newPosition);
}