workspaceCommands.ts 8.7 KB
Newer Older
I
isidor 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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 { TPromise } from 'vs/base/common/winjs.base';
9
import * as nls from 'vs/nls';
I
isidor 已提交
10
import { IWindowService } from 'vs/platform/windows/common/windows';
I
isidor 已提交
11
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
I
isidor 已提交
12 13
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import URI from 'vs/base/common/uri';
I
isidor 已提交
14
import * as resources from 'vs/base/common/resources';
I
isidor 已提交
15 16
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { dirname } from 'vs/base/common/paths';
I
isidor 已提交
17
import { CancellationToken } from 'vs/base/common/cancellation';
I
isidor 已提交
18
import { mnemonicButtonLabel } from 'vs/base/common/labels';
I
isidor 已提交
19 20
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { IHistoryService } from 'vs/workbench/services/history/common/history';
I
isidor 已提交
21
import { FileKind, isParent } from 'vs/platform/files/common/files';
I
isidor 已提交
22
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
I
isidor 已提交
23 24
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { isLinux } from 'vs/base/common/platform';
I
isidor 已提交
25
import { IUriDisplayService } from 'vs/platform/uriDisplay/common/uriDisplay';
C
Christof Marti 已提交
26 27 28 29
import { IQuickInputService, IPickOptions, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { getIconClasses } from 'vs/workbench/browser/labels';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
I
isidor 已提交
30

I
isidor 已提交
31
export const ADD_ROOT_FOLDER_COMMAND_ID = 'addRootFolder';
I
isidor 已提交
32 33
export const ADD_ROOT_FOLDER_LABEL = nls.localize('addFolderToWorkspace', "Add Folder to Workspace...");

I
isidor 已提交
34 35
export const PICK_WORKSPACE_FOLDER_COMMAND_ID = '_workbench.pickWorkspaceFolder';

I
isidor 已提交
36 37 38 39 40 41 42 43 44
function pickFolders(buttonLabel: string, title: string, windowService: IWindowService, contextService: IWorkspaceContextService, historyService: IHistoryService): TPromise<string[]> {
	return windowService.showOpenDialog({
		buttonLabel,
		title,
		properties: ['multiSelections', 'openDirectory', 'createDirectory'],
		defaultPath: defaultFolderPath(contextService, historyService)
	});
}

I
isidor 已提交
45
export function defaultFolderPath(contextService: IWorkspaceContextService, historyService: IHistoryService): string {
I
isidor 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
	let candidate: URI;

	// Check for last active file root first...
	candidate = historyService.getLastActiveWorkspaceRoot('file');

	// ...then for last active file
	if (!candidate) {
		candidate = historyService.getLastActiveFile();
	}

	return candidate ? dirname(candidate.fsPath) : void 0;
}

I
isidor 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

function services(accessor: ServicesAccessor): { windowService: IWindowService, historyService: IHistoryService, contextService: IWorkspaceContextService, environmentService: IEnvironmentService } {
	return {
		windowService: accessor.get(IWindowService),
		historyService: accessor.get(IHistoryService),
		contextService: accessor.get(IWorkspaceContextService),
		environmentService: accessor.get(IEnvironmentService)
	};
}

export function defaultFilePath(contextService: IWorkspaceContextService, historyService: IHistoryService): string {
	let candidate: URI;

	// Check for last active file first...
	candidate = historyService.getLastActiveFile();

	// ...then for last active file root
	if (!candidate) {
		candidate = historyService.getLastActiveWorkspaceRoot('file');
	}

	return candidate ? dirname(candidate.fsPath) : void 0;
}

export function defaultWorkspacePath(contextService: IWorkspaceContextService, historyService: IHistoryService, environmentService: IEnvironmentService): string {

	// Check for current workspace config file first...
	if (contextService.getWorkbenchState() === WorkbenchState.WORKSPACE && !isUntitledWorkspace(contextService.getWorkspace().configuration.fsPath, environmentService)) {
		return dirname(contextService.getWorkspace().configuration.fsPath);
	}

	// ...then fallback to default folder path
	return defaultFolderPath(contextService, historyService);
}

function isUntitledWorkspace(path: string, environmentService: IEnvironmentService): boolean {
	return isParent(path, environmentService.workspacesHome, !isLinux /* ignore case */);
}

I
isidor 已提交
98 99
// Command registration

I
isidor 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
CommandsRegistry.registerCommand({
	id: 'workbench.action.files.openFileFolderInNewWindow',
	handler: (accessor: ServicesAccessor) => {
		const { windowService, historyService, contextService } = services(accessor);

		windowService.pickFileFolderAndOpen({ forceNewWindow: true, dialogOptions: { defaultPath: defaultFilePath(contextService, historyService) } });
	}
});

CommandsRegistry.registerCommand({
	id: '_files.pickFolderAndOpen',
	handler: (accessor: ServicesAccessor, forceNewWindow: boolean) => {
		const { windowService, historyService, contextService } = services(accessor);

		windowService.pickFolderAndOpen({ forceNewWindow, dialogOptions: { defaultPath: defaultFolderPath(contextService, historyService) } });
	}
});

CommandsRegistry.registerCommand({
	id: 'workbench.action.files.openFolderInNewWindow',
	handler: (accessor: ServicesAccessor) => {
		const { windowService, historyService, contextService } = services(accessor);

		windowService.pickFolderAndOpen({ forceNewWindow: true, dialogOptions: { defaultPath: defaultFolderPath(contextService, historyService) } });
	}
});

CommandsRegistry.registerCommand({
	id: 'workbench.action.files.openFileInNewWindow',
	handler: (accessor: ServicesAccessor) => {
		const { windowService, historyService, contextService } = services(accessor);

		windowService.pickFileAndOpen({ forceNewWindow: true, dialogOptions: { defaultPath: defaultFilePath(contextService, historyService) } });
	}
});

CommandsRegistry.registerCommand({
	id: 'workbench.action.openWorkspaceInNewWindow',
	handler: (accessor: ServicesAccessor) => {
		const { windowService, historyService, contextService, environmentService } = services(accessor);

		windowService.pickWorkspaceAndOpen({ forceNewWindow: true, dialogOptions: { defaultPath: defaultWorkspacePath(contextService, historyService, environmentService) } });
	}
});

I
isidor 已提交
145 146 147
CommandsRegistry.registerCommand({
	id: ADD_ROOT_FOLDER_COMMAND_ID,
	handler: (accessor) => {
I
isidor 已提交
148 149
		const viewletService = accessor.get(IViewletService);
		const workspaceEditingService = accessor.get(IWorkspaceEditingService);
I
isidor 已提交
150 151 152 153 154 155 156
		return pickFolders(mnemonicButtonLabel(nls.localize({ key: 'add', comment: ['&& denotes a mnemonic'] }, "&&Add")), nls.localize('addFolderToWorkspaceTitle', "Add Folder to Workspace"),
			accessor.get(IWindowService), accessor.get(IWorkspaceContextService), accessor.get(IHistoryService)).then(folders => {
				if (!folders || !folders.length) {
					return null;
				}

				// Add and show Files Explorer viewlet
B
Benjamin Pasero 已提交
157 158 159
				return workspaceEditingService.addFolders(folders.map(folder => ({ uri: URI.file(folder) })))
					.then(() => viewletService.openViewlet(viewletService.getDefaultViewletId(), true))
					.then(() => void 0);
I
isidor 已提交
160 161 162
			});
	}
});
I
isidor 已提交
163

C
Christof Marti 已提交
164
CommandsRegistry.registerCommand(PICK_WORKSPACE_FOLDER_COMMAND_ID, function (accessor, args?: [IPickOptions<IQuickPickItem>, CancellationToken]) {
C
Christof Marti 已提交
165
	const quickInputService = accessor.get(IQuickInputService);
I
isidor 已提交
166 167
	const uriDisplayService = accessor.get(IUriDisplayService);
	const contextService = accessor.get(IWorkspaceContextService);
C
Christof Marti 已提交
168 169
	const modelService = accessor.get(IModelService);
	const modeService = accessor.get(IModeService);
I
isidor 已提交
170 171 172 173 174 175 176 177 178

	const folders = contextService.getWorkspace().folders;
	if (!folders.length) {
		return void 0;
	}

	const folderPicks = folders.map(folder => {
		return {
			label: folder.name,
I
isidor 已提交
179
			description: uriDisplayService.getLabel(resources.dirname(folder.uri), true),
I
isidor 已提交
180
			folder,
C
Christof Marti 已提交
181 182
			iconClasses: getIconClasses(modelService, modeService, folder.uri, FileKind.ROOT_FOLDER)
		} as IQuickPickItem;
I
isidor 已提交
183 184
	});

C
Christof Marti 已提交
185
	let options: IPickOptions<IQuickPickItem>;
I
isidor 已提交
186 187 188 189 190 191 192 193
	if (args) {
		options = args[0];
	}

	if (!options) {
		options = Object.create(null);
	}

C
Christof Marti 已提交
194 195
	if (!options.activeItem) {
		options.activeItem = folderPicks[0];
I
isidor 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	}

	if (!options.placeHolder) {
		options.placeHolder = nls.localize('workspaceFolderPickerPlaceholder', "Select workspace folder");
	}

	if (typeof options.matchOnDescription !== 'boolean') {
		options.matchOnDescription = true;
	}

	let token: CancellationToken;
	if (args) {
		token = args[1];
	}

	if (!token) {
		token = CancellationToken.None;
	}

C
Christof Marti 已提交
215
	return quickInputService.pick(folderPicks, options, token).then(pick => {
I
isidor 已提交
216 217 218 219 220 221 222
		if (!pick) {
			return void 0;
		}

		return folders[folderPicks.indexOf(pick)];
	});
});