Skip to content

Commit

Permalink
Implement #8234
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Jul 22, 2016
1 parent 4c83c56 commit b7b2bdd
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/vs/workbench/browser/parts/editor/editor.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {CloseEditorsInGroupAction, CloseEditorsInOtherGroupsAction, CloseAllEdit
GlobalQuickOpenAction, OpenPreviousEditorFromHistoryAction, QuickOpenNavigateNextAction, QuickOpenNavigatePreviousAction, ShowAllEditorsAction, NAVIGATE_ALL_EDITORS_GROUP_PREFIX, ClearEditorHistoryAction, ShowEditorsInCenterGroupAction,
NAVIGATE_IN_CENTER_GROUP_PREFIX, ShowEditorsInRightGroupAction, NAVIGATE_IN_RIGHT_GROUP_PREFIX, RemoveFromEditorHistoryAction, FocusLastEditorInStackAction, OpenNextRecentlyUsedEditorInGroupAction, MoveEditorToLeftGroupAction, MoveEditorToRightGroupAction
} from 'vs/workbench/browser/parts/editor/editorActions';
import {registerEditorComamnds} from 'vs/workbench/browser/parts/editor/editorCommands';

// Register String Editor
(<IEditorRegistry>Registry.as(EditorExtensions.Editors)).registerEditor(
Expand Down Expand Up @@ -405,4 +406,6 @@ configurationRegistry.registerConfiguration({
'description': nls.localize('editorOpenPositioning', "Controls where editors open. Select 'left' or 'right' to open editors to the left or right of the current active one. Select 'first' or 'last' to open editors independently from the currently active one.")
}
}
});
});

registerEditorComamnds();
128 changes: 128 additions & 0 deletions src/vs/workbench/browser/parts/editor/editorCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*---------------------------------------------------------------------------------------------
* 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 * as editorCommon from 'vs/editor/common/editorCommon';
import { KbExpr } from 'vs/platform/keybinding/common/keybinding';
import { IEditor, Position, POSITIONS } from 'vs/platform/editor/common/editor';

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

if (!types.isUndefined(activeEditorMoveArg.amount) && !types.isNumber(activeEditorMoveArg.amount)) {
return false;
}

return true;
};


function _registerActiveEditorMoveCommand() {
KeybindingsRegistry.registerCommandDesc({
id: EditorCommands.ActiveEditorMove,
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
when: KbExpr.has(editorCommon.KEYBINDING_CONTEXT_EDITOR_TEXT_FOCUS),
primary: null,
handler: (accessor, args: any) => _moveActiveEditor(args, accessor),
description: {
description: nls.localize('editorCommand.activeEditorMove.description', "Command to move active editor"),
args: [
{
name: nls.localize('editorCommand.activeEditorMove.arg.name', "Active editor move argument"),
constraint: isActiveEditorMoveArg
}
]
}
});
}

function _moveActiveEditor(args: ActiveEditorMoveArguments = {}, accessor: ServicesAccessor) {
let tabsShown = !!(<IWorkbenchEditorConfiguration>accessor.get(IConfigurationService).getConfiguration()).workbench.editor.showTabs;
args.by = tabsShown ? args.by || ActiveEditorMovePositioningBy.TAB : ActiveEditorMovePositioningBy.GROUP;
args.to = args.to || ActiveEditorMovePositioning.RIGHT;
args.amount = args.amount || 1;

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:
index = index - args.amount;
break;
case ActiveEditorMovePositioning.RIGHT:
index = index + args.amount;
break;
case ActiveEditorMovePositioning.CENTER:
index = Math.round(editorGroup.count / 2);
break;
case ActiveEditorMovePositioning.POSITION:
index = args.amount;
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:
newPosition = args.amount;
break;
}
newPosition = POSITIONS.indexOf(newPosition) !== -1 ? newPosition : activeEditor.position;
accessor.get(IEditorGroupService).moveEditor(activeEditor.input, activeEditor.position, newPosition);
}
26 changes: 25 additions & 1 deletion src/vs/workbench/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,4 +820,28 @@ export interface IWorkbenchEditorConfiguration {
openPositioning: string;
}
};
}
}

export const ActiveEditorMovePositioning = {
FIRST: 'first',
LAST: 'last',
LEFT: 'left',
RIGHT: 'right',
CENTER: 'center',
POSITION: 'position',
};

export const ActiveEditorMovePositioningBy = {
TAB: 'tab',
GROUP: 'group'
};

export interface ActiveEditorMoveArguments {
to?: string;
by?: string;
amount?: number;
}

export var EditorCommands = {
ActiveEditorMove: 'activeEditorMove'
};

0 comments on commit b7b2bdd

Please sign in to comment.