editorCommands.ts 18.6 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
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
10
import { TextCompareEditorVisibleContext, EditorInput, IEditorIdentifier, IEditorCommandsContext, ActiveEditorGroupEmptyContext, MultipleEditorGroupsContext, CloseDirection, IEditor, IEditorInput } from 'vs/workbench/common/editor';
I
isidor 已提交
11
import { INextEditorService } from 'vs/workbench/services/editor/common/nextEditorService';
12
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
13
import { TextDiffEditor } from 'vs/workbench/browser/parts/editor/textDiffEditor';
B
Benjamin Pasero 已提交
14
import { EditorGroup } from 'vs/workbench/common/editor/editorGroup';
I
isidor 已提交
15
import { KeyMod, KeyCode, KeyChord } from 'vs/base/common/keyCodes';
16
import { TPromise } from 'vs/base/common/winjs.base';
17
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
18
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
19
import { IDiffEditorOptions } from 'vs/editor/common/config/editorOptions';
I
isidor 已提交
20 21
import { IListService } from 'vs/platform/list/browser/listService';
import { List } from 'vs/base/browser/ui/list/listWidget';
I
isidor 已提交
22
import { distinct } from 'vs/base/common/arrays';
23
import { INextEditorGroupsService, INextEditorGroup, GroupDirection } from 'vs/workbench/services/group/common/nextEditorGroupsService';
24
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
25

26
export const MOVE_ACTIVE_EDITOR_COMMAND_ID = 'moveActiveEditor';
27
export const CLOSE_SAVED_EDITORS_COMMAND_ID = 'workbench.action.closeUnmodifiedEditors';
I
isidor 已提交
28
export const CLOSE_EDITORS_IN_GROUP_COMMAND_ID = 'workbench.action.closeEditorsInGroup';
29
export const CLOSE_EDITORS_TO_THE_RIGHT_COMMAND_ID = 'workbench.action.closeEditorsToTheRight';
I
isidor 已提交
30
export const CLOSE_EDITOR_COMMAND_ID = 'workbench.action.closeActiveEditor';
31
export const CLOSE_EDITOR_GROUP_COMMAND_ID = 'workbench.action.closeEditorGroup';
I
isidor 已提交
32
export const CLOSE_OTHER_EDITORS_IN_GROUP_COMMAND_ID = 'workbench.action.closeOtherEditors';
33
export const KEEP_EDITOR_COMMAND_ID = 'workbench.action.keepEditor';
B
Benjamin Pasero 已提交
34
export const SHOW_EDITORS_IN_GROUP = 'workbench.action.showEditorsInGroup';
35
export const TOGGLE_DIFF_INLINE_MODE = 'toggle.diff.editorMode';
B
Benjamin Pasero 已提交
36 37

export const NAVIGATE_ALL_EDITORS_GROUP_PREFIX = 'edt ';
B
Benjamin Pasero 已提交
38
export const NAVIGATE_IN_ACTIVE_GROUP_PREFIX = 'edt active ';
39

40 41 42 43
export interface ActiveEditorMoveArguments {
	to?: 'first' | 'last' | 'left' | 'right' | 'up' | 'down' | 'center' | 'position';
	by?: 'tab' | 'group';
	value?: number;
S
Sandeep Somavarapu 已提交
44 45
}

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

51
	if (!types.isString(arg.to)) {
S
Sandeep Somavarapu 已提交
52 53 54
		return false;
	}

55
	if (!types.isUndefined(arg.by) && !types.isString(arg.by)) {
S
Sandeep Somavarapu 已提交
56 57 58
		return false;
	}

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

	return true;
};

66
function registerActiveEditorMoveCommand(): void {
A
Alex Dima 已提交
67
	KeybindingsRegistry.registerCommandAndKeybindingRule({
68
		id: MOVE_ACTIVE_EDITOR_COMMAND_ID,
S
Sandeep Somavarapu 已提交
69
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
70
		when: EditorContextKeys.editorTextFocus,
S
Sandeep Somavarapu 已提交
71
		primary: null,
72
		handler: (accessor, args: any) => moveActiveEditor(args, accessor),
S
Sandeep Somavarapu 已提交
73
		description: {
74
			description: nls.localize('editorCommand.activeEditorMove.description', "Move the active editor by tabs or groups"),
S
Sandeep Somavarapu 已提交
75 76 77
			args: [
				{
					name: nls.localize('editorCommand.activeEditorMove.arg.name', "Active editor move argument"),
78
					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 已提交
79 80 81 82 83 84 85
					constraint: isActiveEditorMoveArg
				}
			]
		}
	});
}

86 87 88 89
function moveActiveEditor(args: ActiveEditorMoveArguments = Object.create(null), accessor: ServicesAccessor): void {
	args.to = args.to || 'right';
	args.by = args.by || 'tab';
	args.value = typeof args.value === 'number' ? args.value : 1;
S
Sandeep Somavarapu 已提交
90

91 92
	const activeControl = accessor.get(INextEditorService).activeControl;
	if (activeControl) {
93
		switch (args.by) {
94 95 96 97
			case 'tab':
				return moveActiveTab(args, activeControl, accessor);
			case 'group':
				return moveActiveEditorToGroup(args, activeControl, accessor);
98
		}
S
Sandeep Somavarapu 已提交
99 100 101
	}
}

102 103 104
function moveActiveTab(args: ActiveEditorMoveArguments, control: IEditor, accessor: ServicesAccessor): void {
	const group = control.group;
	let index = group.getIndexOfEditor(control.input);
S
Sandeep Somavarapu 已提交
105
	switch (args.to) {
106
		case 'first':
S
Sandeep Somavarapu 已提交
107 108
			index = 0;
			break;
109
		case 'last':
110
			index = group.count - 1;
S
Sandeep Somavarapu 已提交
111
			break;
112
		case 'left':
113
			index = index - args.value;
S
Sandeep Somavarapu 已提交
114
			break;
115
		case 'right':
116
			index = index + args.value;
S
Sandeep Somavarapu 已提交
117
			break;
118
		case 'center':
119
			index = Math.round(group.count / 2) - 1;
S
Sandeep Somavarapu 已提交
120
			break;
121
		case 'position':
122
			index = args.value - 1;
S
Sandeep Somavarapu 已提交
123 124
			break;
	}
125

126
	index = index < 0 ? 0 : index >= group.count ? group.count - 1 : index;
127
	group.moveEditor(control.input, group, { index });
S
Sandeep Somavarapu 已提交
128 129
}

130 131 132 133 134 135 136
function moveActiveEditorToGroup(args: ActiveEditorMoveArguments, control: IEditor, accessor: ServicesAccessor): void {
	const editorGroupService = accessor.get(INextEditorGroupsService);

	const groups = editorGroupService.groups;
	const sourceGroup = control.group;
	let targetGroup: INextEditorGroup;

S
Sandeep Somavarapu 已提交
137
	switch (args.to) {
138 139 140 141 142
		case 'left':
			targetGroup = editorGroupService.findNeighbourGroup(sourceGroup, GroupDirection.LEFT);
			break;
		case 'right':
			targetGroup = editorGroupService.findNeighbourGroup(sourceGroup, GroupDirection.RIGHT);
S
Sandeep Somavarapu 已提交
143
			break;
144 145
		case 'up':
			targetGroup = editorGroupService.findNeighbourGroup(sourceGroup, GroupDirection.UP);
S
Sandeep Somavarapu 已提交
146
			break;
147 148
		case 'down':
			targetGroup = editorGroupService.findNeighbourGroup(sourceGroup, GroupDirection.DOWN);
S
Sandeep Somavarapu 已提交
149
			break;
150 151
		case 'first':
			targetGroup = groups[0];
S
Sandeep Somavarapu 已提交
152
			break;
153 154
		case 'last':
			targetGroup = groups[groups.length - 1];
S
Sandeep Somavarapu 已提交
155
			break;
156 157 158 159 160
		case 'center':
			targetGroup = groups[(groups.length / 2) - 1];
			break;
		case 'position':
			targetGroup = groups[args.value - 1];
S
Sandeep Somavarapu 已提交
161 162
			break;
	}
163

164 165 166
	if (targetGroup) {
		sourceGroup.moveEditor(control.input, targetGroup);
	}
167 168 169 170 171 172
}

function registerDiffEditorCommands(): void {
	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'workbench.action.compareEditor.nextChange',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
173
		when: TextCompareEditorVisibleContext,
174 175 176 177 178 179 180
		primary: null,
		handler: accessor => navigateInDiffEditor(accessor, true)
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: 'workbench.action.compareEditor.previousChange',
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
181
		when: TextCompareEditorVisibleContext,
182 183 184 185 186
		primary: null,
		handler: accessor => navigateInDiffEditor(accessor, false)
	});

	function navigateInDiffEditor(accessor: ServicesAccessor, next: boolean): void {
187 188
		const editorService = accessor.get(INextEditorService);
		const candidates = [editorService.activeControl, ...editorService.visibleControls].filter(e => e instanceof TextDiffEditor);
189 190 191 192 193 194 195

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

	KeybindingsRegistry.registerCommandAndKeybindingRule({
196 197
		id: TOGGLE_DIFF_INLINE_MODE,
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
198
		when: void 0,
199
		primary: void 0,
B
Benjamin Pasero 已提交
200
		handler: (accessor, resource, context: IEditorCommandsContext) => {
201 202
			const editorGroupService = accessor.get(INextEditorGroupsService);

I
isidor 已提交
203
			const { control } = resolveCommandsContext(editorGroupService, context);
204 205 206 207
			if (control instanceof TextDiffEditor) {
				const widget = control.getControl();
				const isInlineMode = !widget.renderSideBySide;
				widget.updateOptions(<IDiffEditorOptions>{
208 209 210 211
					renderSideBySide: isInlineMode
				});
			}
		}
212
	});
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
}

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 => {
229
				const editorService = accessor.get(INextEditorService);
230

231 232
				const activeControl = editorService.activeControl;
				if (activeControl) {
233
					const editor = activeControl.group.getEditor(editorIndex);
234
					if (editor) {
B
Benjamin Pasero 已提交
235
						return editorService.openEditor(editor).then(() => void 0);
236 237
					}
				}
238 239

				return void 0;
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
			}
		});
	}

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

		return void 0;
259
	}
260 261
}

262
function registerEditorCommands() {
263

I
isidor 已提交
264
	KeybindingsRegistry.registerCommandAndKeybindingRule({
265
		id: CLOSE_SAVED_EDITORS_COMMAND_ID,
I
isidor 已提交
266
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
267
		when: void 0,
I
isidor 已提交
268
		primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.KEY_U),
I
isidor 已提交
269
		handler: (accessor, resource: URI | object, context: IEditorCommandsContext) => {
I
isidor 已提交
270 271 272
			const editorGroupService = accessor.get(INextEditorGroupsService);
			const contexts = getMultiSelectedEditorContexts(context, accessor.get(IListService), editorGroupService);
			if (contexts.length === 0 && editorGroupService.activeGroup) {
273
				contexts.push({ groupId: editorGroupService.activeGroup.id }); // If command is triggered from the command palette use the active group
I
isidor 已提交
274
			}
275

I
isidor 已提交
276 277 278
			return TPromise.join(distinct(contexts.map(c => c.groupId)).map(groupId =>
				editorGroupService.getGroup(groupId).closeEditors({ savedOnly: true })
			));
279 280 281
		}
	});

I
isidor 已提交
282
	KeybindingsRegistry.registerCommandAndKeybindingRule({
283
		id: CLOSE_EDITORS_IN_GROUP_COMMAND_ID,
I
isidor 已提交
284
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
285
		when: void 0,
I
isidor 已提交
286
		primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.KEY_W),
I
isidor 已提交
287
		handler: (accessor, resource: URI | object, context: IEditorCommandsContext) => {
I
isidor 已提交
288 289
			const editorGroupService = accessor.get(INextEditorGroupsService);
			const contexts = getMultiSelectedEditorContexts(context, accessor.get(IListService), editorGroupService);
290
			const distinctGroupIds = distinct(contexts.map(c => c.groupId));
291

I
isidor 已提交
292 293
			if (distinctGroupIds.length === 0) {
				distinctGroupIds.push(editorGroupService.activeGroup.id);
294 295
			}

I
isidor 已提交
296 297 298
			return TPromise.join(distinctGroupIds.map(groupId =>
				editorGroupService.getGroup(groupId).closeAllEditors()
			));
299 300 301
		}
	});

I
isidor 已提交
302
	KeybindingsRegistry.registerCommandAndKeybindingRule({
I
isidor 已提交
303
		id: CLOSE_EDITOR_COMMAND_ID,
I
isidor 已提交
304
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
305
		when: void 0,
I
isidor 已提交
306 307
		primary: KeyMod.CtrlCmd | KeyCode.KEY_W,
		win: { primary: KeyMod.CtrlCmd | KeyCode.F4, secondary: [KeyMod.CtrlCmd | KeyCode.KEY_W] },
I
isidor 已提交
308
		handler: (accessor, resource: URI | object, context: IEditorCommandsContext) => {
I
isidor 已提交
309 310 311 312 313
			const editorGroupService = accessor.get(INextEditorGroupsService);
			const contexts = getMultiSelectedEditorContexts(context, accessor.get(IListService), editorGroupService);
			const activeGroup = editorGroupService.activeGroup;
			if (contexts.length === 0 && activeGroup && activeGroup.activeEditor) {
				contexts.push({ groupId: activeGroup.id, editorIndex: activeGroup.getIndexOfEditor(activeGroup.activeEditor) });
314
			}
315

I
isidor 已提交
316 317 318 319 320 321
			const groupIds = distinct(contexts.map(context => context.groupId));
			return TPromise.join(groupIds.map(groupId => {
				const group = editorGroupService.getGroup(groupId);
				const editors = contexts.filter(c => c.groupId === groupId).map(c => group.getEditor(c.editorIndex));
				return group.closeEditors(editors);
			}));
322
		}
323 324 325 326 327 328 329 330 331
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: CLOSE_EDITOR_GROUP_COMMAND_ID,
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: ContextKeyExpr.and(ActiveEditorGroupEmptyContext, MultipleEditorGroupsContext),
		primary: KeyMod.CtrlCmd | KeyCode.KEY_W,
		win: { primary: KeyMod.CtrlCmd | KeyCode.F4, secondary: [KeyMod.CtrlCmd | KeyCode.KEY_W] },
		handler: (accessor, resource: URI | object, context: IEditorCommandsContext) => {
332 333 334 335 336 337 338 339
			const editorGroupService = accessor.get(INextEditorGroupsService);

			let group: INextEditorGroup;
			if (context && typeof context.groupId === 'number') {
				group = editorGroupService.getGroup(context.groupId);
			} else {
				group = editorGroupService.activeGroup;
			}
340

341
			editorGroupService.removeGroup(group);
342
		}
343 344
	});

I
isidor 已提交
345
	KeybindingsRegistry.registerCommandAndKeybindingRule({
I
isidor 已提交
346
		id: CLOSE_OTHER_EDITORS_IN_GROUP_COMMAND_ID,
I
isidor 已提交
347
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
348 349
		when: void 0,
		primary: void 0,
I
isidor 已提交
350
		mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_T },
I
isidor 已提交
351
		handler: (accessor, resource: URI | object, context: IEditorCommandsContext) => {
I
isidor 已提交
352 353
			const editorGroupService = accessor.get(INextEditorGroupsService);
			const contexts = getMultiSelectedEditorContexts(context, accessor.get(IListService), editorGroupService);
354

355 356
			if (contexts.length === 0) {
				// Cover the case when run from command palette
I
isidor 已提交
357 358 359
				const activeGroup = editorGroupService.activeGroup;
				if (activeGroup && activeGroup.activeEditor) {
					contexts.push({ groupId: activeGroup.id, editorIndex: activeGroup.getIndexOfEditor(activeGroup.activeEditor) });
360 361 362 363
				}
			}

			const groupIds = distinct(contexts.map(context => context.groupId));
364

I
isidor 已提交
365 366 367 368
			return TPromise.join(groupIds.map(groupId => {
				const group = editorGroupService.getGroup(groupId);
				const editors = contexts.filter(c => c.groupId === groupId).map(c => group.getEditor(c.editorIndex));
				const editorsToClose = group.editors.filter(e => editors.indexOf(e) === -1);
369

I
isidor 已提交
370 371
				return group.closeEditors(editorsToClose);
			}));
372 373 374 375 376 377 378 379
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: CLOSE_EDITORS_TO_THE_RIGHT_COMMAND_ID,
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: void 0,
		primary: void 0,
380
		handler: (accessor, resource: URI, context: IEditorCommandsContext) => {
381
			const editorGroupService = accessor.get(INextEditorGroupsService);
382

I
isidor 已提交
383
			const { group, editor } = resolveCommandsContext(editorGroupService, context);
384 385
			if (group && editor) {
				return group.closeEditors({ direction: CloseDirection.RIGHT, except: editor });
386 387
			}

388 389 390 391 392 393 394 395 396
			return TPromise.as(false);
		}
	});

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: KEEP_EDITOR_COMMAND_ID,
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: void 0,
		primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.Enter),
397
		handler: (accessor, resource: URI, context: IEditorCommandsContext) => {
398
			const editorGroupService = accessor.get(INextEditorGroupsService);
399

I
isidor 已提交
400
			const { group, editor } = resolveCommandsContext(editorGroupService, context);
401 402
			if (group && editor) {
				return group.pinEditor(editor);
403 404
			}

I
isidor 已提交
405
			return TPromise.as(false);
406 407
		}
	});
B
Benjamin Pasero 已提交
408 409 410 411 412 413

	KeybindingsRegistry.registerCommandAndKeybindingRule({
		id: SHOW_EDITORS_IN_GROUP,
		weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
		when: void 0,
		primary: void 0,
B
Benjamin Pasero 已提交
414
		handler: (accessor, resource: URI, context: IEditorCommandsContext) => {
I
isidor 已提交
415
			const editorGroupService = accessor.get(INextEditorGroupsService);
B
Benjamin Pasero 已提交
416 417
			const quickOpenService = accessor.get(IQuickOpenService);

B
Benjamin Pasero 已提交
418
			if (editorGroupService.count <= 1) {
B
Benjamin Pasero 已提交
419 420 421
				return quickOpenService.show(NAVIGATE_ALL_EDITORS_GROUP_PREFIX);
			}

B
Benjamin Pasero 已提交
422 423 424
			if (context && typeof context.groupId === 'number') {
				editorGroupService.activateGroup(editorGroupService.getGroup(context.groupId)); // we need the group to be active
			}
B
Benjamin Pasero 已提交
425

B
Benjamin Pasero 已提交
426
			return quickOpenService.show(NAVIGATE_IN_ACTIVE_GROUP_PREFIX);
B
Benjamin Pasero 已提交
427 428
		}
	});
429
}
430

I
isidor 已提交
431
function resolveCommandsContext(editorGroupService: INextEditorGroupsService, context?: IEditorCommandsContext): { group: INextEditorGroup, editor: IEditorInput, control: IEditor } {
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

	// Resolve from context
	let group = context && typeof context.groupId === 'number' ? editorGroupService.getGroup(context.groupId) : undefined;
	let editor = group && typeof context.editorIndex === 'number' ? group.getEditor(context.editorIndex) : undefined;
	let control = group ? group.activeControl : undefined;

	// Fallback to active group as needed
	if (!group) {
		group = editorGroupService.activeGroup;
		editor = <EditorInput>group.activeEditor;
		control = group.activeControl;
	}

	return { group, editor, control };
}

448
export function getMultiSelectedEditorContexts(editorContext: IEditorCommandsContext, listService: IListService, editorGroupService: INextEditorGroupsService): IEditorCommandsContext[] {
B
Benjamin Pasero 已提交
449 450
	// First check for a focused list to return the selected items from
	const list = listService.lastFocusedList;
I
isidor 已提交
451
	if (list instanceof List && list.isDOMFocused()) {
I
isidor 已提交
452
		const elementToContext = (element: IEditorIdentifier | EditorGroup) =>
I
isidor 已提交
453
			element instanceof EditorGroup ? { groupId: element.id, editorIndex: undefined } : { groupId: element.groupId, editorIndex: editorGroupService.getGroup(element.groupId).getIndexOfEditor(element.editor) };
454
		const onlyEditorGroupAndEditor = (e: IEditorIdentifier | EditorGroup) => e instanceof EditorGroup || ('editor' in e && 'group' in e);
I
isidor 已提交
455 456

		const focusedElements: (IEditorIdentifier | EditorGroup)[] = list.getFocusedElements().filter(onlyEditorGroupAndEditor);
457 458 459 460
		// need to take into account when editor context is { group: group }
		const focus = editorContext ? editorContext : focusedElements.length ? focusedElements.map(elementToContext)[0] : undefined;

		if (focus) {
I
isidor 已提交
461
			const selection: (IEditorIdentifier | EditorGroup)[] = list.getSelectedElements().filter(onlyEditorGroupAndEditor);
462
			// Only respect selection if it contains focused element
I
isidor 已提交
463
			if (selection && selection.some(s => s instanceof EditorGroup ? s.id === focus.groupId : s.groupId === focus.groupId && editorGroupService.getGroup(s.groupId).getIndexOfEditor(s.editor) === focus.editorIndex)) {
464 465
				return selection.map(elementToContext);
			}
I
isidor 已提交
466

467
			return [focus];
I
isidor 已提交
468 469 470
		}
	}

B
Benjamin Pasero 已提交
471
	// Otherwise go with passed in context
I
isidor 已提交
472 473
	return !!editorContext ? [editorContext] : [];
}
474 475 476 477 478 479 480

export function setup(): void {
	registerActiveEditorMoveCommand();
	registerDiffEditorCommands();
	registerOpenEditorAtIndexCommands();
	registerEditorCommands();
}