files.ts 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import URI from 'vs/base/common/uri';
import { IListService } from 'vs/platform/list/browser/listService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { FileStat, OpenEditor } from 'vs/workbench/parts/files/common/explorerModel';
I
isidor 已提交
12
import { toResource } from 'vs/workbench/common/editor';
13
import { Tree } from 'vs/base/parts/tree/browser/treeImpl';
14
import { List } from 'vs/base/browser/ui/list/listWidget';
15
import { IFileStat } from 'vs/platform/files/common/files';
16 17 18

// Commands can get exeucted from a command pallete, from a context menu or from some list using a keybinding
// To cover all these cases we need to properly compute the resource on which the command is being executed
19
export function getResourceForCommand(resource: URI | object | { from: string }, listService: IListService, editorService: IWorkbenchEditorService): URI {
20 21 22
	if (URI.isUri(resource)) {
		return resource;
	}
23 24 25 26 27
	let list = listService.lastFocusedList;
	if ('from' in resource && resource.from === 'menu') {
		// Ignore list if the command is triggered from the main menu
		list = undefined;
	}
28 29 30 31 32 33

	if (list && list.isDOMFocused()) {
		const focus = list.getFocus();
		if (focus instanceof FileStat) {
			return focus.resource;
		} else if (focus instanceof OpenEditor) {
34
			return focus.getResource();
35 36 37 38 39 40
		}
	}

	return toResource(editorService.getActiveEditorInput(), { supportSideBySide: true });
}

41
export function getMultiSelectedResources(resource: URI | object | { from: string }, listService: IListService, editorService: IWorkbenchEditorService): URI[] {
42
	const list = listService.lastFocusedList;
43 44 45
	if (list && list.isDOMFocused()) {
		// Explorer
		if (list instanceof Tree) {
46 47 48 49 50 51 52 53
			const focus: IFileStat = list.getFocus();
			// If the resource is passed it has to be a part of the returned context.
			if (focus && (!URI.isUri(resource) || focus.resource.toString() === resource.toString())) {
				const selection = list.getSelection();
				// We only respect the selection if it contains the focused element.
				if (selection && selection.indexOf(focus) >= 0) {
					return selection.map(fs => fs.resource);
				}
54 55
			}
		}
I
isidor 已提交
56

57 58
		// Open editors view
		if (list instanceof List) {
I
isidor 已提交
59
			const focus = list.getFocusedElements();
60 61 62 63 64 65 66
			// If the resource is passed it has to be a part of the returned context.
			if (focus.length && (!URI.isUri(resource) || (focus[0] instanceof OpenEditor && focus[0].getResource().toString() === resource.toString()))) {
				const selection = list.getSelectedElements();
				// We only respect the selection if it contains the focused element.
				if (selection && selection.indexOf(focus[0]) >= 0) {
					return selection.filter(s => s instanceof OpenEditor).map((oe: OpenEditor) => oe.getResource());
				}
67
			}
68 69 70 71 72 73
		}
	}

	const result = getResourceForCommand(resource, listService, editorService);
	return !!result ? [result] : [];
}