workspaceActions.ts 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  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';
import { Action } from 'vs/base/common/actions';
import nls = require('vs/nls');
11
import { IWindowService, IWindowsService } from 'vs/platform/windows/common/windows';
12
import { ITelemetryData } from 'vs/platform/telemetry/common/telemetry';
13
import { IWorkspaceContextService, WorkbenchState, IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
B
Benjamin Pasero 已提交
14 15
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
16
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
17
import { WORKSPACE_FILTER, IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
B
Benjamin Pasero 已提交
18 19 20
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { isLinux } from 'vs/base/common/platform';
import { dirname } from 'vs/base/common/paths';
B
Benjamin Pasero 已提交
21
import * as resources from 'vs/base/common/resources';
22 23
import { mnemonicButtonLabel, getPathLabel } from 'vs/base/common/labels';
import { isParent, FileKind } from 'vs/platform/files/common/files';
B
Benjamin Pasero 已提交
24
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
25 26
import { IQuickOpenService, IFilePickOpenEntry, IPickOptions } from 'vs/platform/quickOpen/common/quickOpen';
import { CancellationToken } from 'vs/base/common/cancellation';
27
import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands';
28
import { IHistoryService } from 'vs/workbench/services/history/common/history';
I
isidor 已提交
29
import { ADD_ROOT_FOLDER_COMMAND_ID, ADD_ROOT_FOLDER_LABEL } from 'vs/workbench/browser/actions/workspaceCommands';
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

export class OpenFileAction extends Action {

	static ID = 'workbench.action.files.openFile';
	static LABEL = nls.localize('openFile', "Open File...");

	constructor(
		id: string,
		label: string,
		@IWindowService private windowService: IWindowService,
		@IHistoryService private historyService: IHistoryService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
	) {
		super(id, label);
	}

	run(event?: any, data?: ITelemetryData): TPromise<any> {
		return this.windowService.pickFileAndOpen({ telemetryExtraData: data, dialogOptions: { defaultPath: defaultFilePath(this.contextService, this.historyService) } });
	}
}
50 51 52 53 54 55 56 57 58

export class OpenFolderAction extends Action {

	static ID = 'workbench.action.files.openFolder';
	static LABEL = nls.localize('openFolder', "Open Folder...");

	constructor(
		id: string,
		label: string,
59 60 61
		@IWindowService private windowService: IWindowService,
		@IHistoryService private historyService: IHistoryService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
62 63 64 65
	) {
		super(id, label);
	}

66
	run(event?: any, data?: ITelemetryData): TPromise<any> {
67
		return this.windowService.pickFolderAndOpen({ telemetryExtraData: data, dialogOptions: { defaultPath: defaultFolderPath(this.contextService, this.historyService) } });
68 69 70 71 72 73 74 75 76 77 78
	}
}

export class OpenFileFolderAction extends Action {

	static ID = 'workbench.action.files.openFileFolder';
	static LABEL = nls.localize('openFileFolder', "Open...");

	constructor(
		id: string,
		label: string,
79 80 81
		@IWindowService private windowService: IWindowService,
		@IHistoryService private historyService: IHistoryService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
82 83 84 85
	) {
		super(id, label);
	}

86
	run(event?: any, data?: ITelemetryData): TPromise<any> {
87
		return this.windowService.pickFileFolderAndOpen({ telemetryExtraData: data, dialogOptions: { defaultPath: defaultFilePath(this.contextService, this.historyService) } });
88
	}
B
Benjamin Pasero 已提交
89 90
}

91 92 93 94 95 96 97 98 99 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
export const openFileFolderInNewWindowCommand = (accessor: ServicesAccessor) => {
	const { windowService, historyService, contextService } = services(accessor);

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

export const openFolderCommand = (accessor: ServicesAccessor, forceNewWindow: boolean) => {
	const { windowService, historyService, contextService } = services(accessor);

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

export const openFolderInNewWindowCommand = (accessor: ServicesAccessor) => {
	const { windowService, historyService, contextService } = services(accessor);

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

export const openFileInNewWindowCommand = (accessor: ServicesAccessor) => {
	const { windowService, historyService, contextService } = services(accessor);

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

export const openWorkspaceInNewWindowCommand = (accessor: ServicesAccessor) => {
	const { windowService, historyService, contextService, environmentService } = services(accessor);

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

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

B
Benjamin Pasero 已提交
130
export abstract class BaseWorkspacesAction extends Action {
B
Benjamin Pasero 已提交
131 132 133 134 135

	constructor(
		id: string,
		label: string,
		protected windowService: IWindowService,
B
Benjamin Pasero 已提交
136
		protected environmentService: IEnvironmentService,
137 138
		protected contextService: IWorkspaceContextService,
		protected historyService: IHistoryService
B
Benjamin Pasero 已提交
139 140 141 142
	) {
		super(id, label);
	}

143
	protected pickFolders(buttonLabel: string, title: string): TPromise<string[]> {
B
Benjamin Pasero 已提交
144
		return this.windowService.showOpenDialog({
B
Benjamin Pasero 已提交
145 146
			buttonLabel,
			title,
B
Benjamin Pasero 已提交
147
			properties: ['multiSelections', 'openDirectory', 'createDirectory'],
148
			defaultPath: defaultFolderPath(this.contextService, this.historyService)
B
Benjamin Pasero 已提交
149 150
		});
	}
B
Benjamin Pasero 已提交
151 152
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
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;
}

function defaultFolderPath(contextService: IWorkspaceContextService, historyService: IHistoryService): string {
	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;
}

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 已提交
196
export class AddRootFolderAction extends Action {
B
Benjamin Pasero 已提交
197

B
Benjamin Pasero 已提交
198
	static ID = 'workbench.action.addRootFolder';
I
isidor 已提交
199
	static LABEL = ADD_ROOT_FOLDER_LABEL;
B
Benjamin Pasero 已提交
200 201 202 203

	constructor(
		id: string,
		label: string,
I
isidor 已提交
204
		@ICommandService private commandService: ICommandService
B
Benjamin Pasero 已提交
205
	) {
I
isidor 已提交
206
		super(id, label);
B
Benjamin Pasero 已提交
207 208 209
	}

	public run(): TPromise<any> {
I
isidor 已提交
210
		return this.commandService.executeCommand(ADD_ROOT_FOLDER_COMMAND_ID);
S
Sandeep Somavarapu 已提交
211
	}
B
Benjamin Pasero 已提交
212
}
S
Sandeep Somavarapu 已提交
213

214 215 216 217 218 219 220 221 222 223 224 225
export class GlobalRemoveRootFolderAction extends BaseWorkspacesAction {

	static ID = 'workbench.action.removeRootFolder';
	static LABEL = nls.localize('globalRemoveFolderFromWorkspace', "Remove Folder from Workspace...");

	constructor(
		id: string,
		label: string,
		@IWindowService windowService: IWindowService,
		@IWorkspaceContextService contextService: IWorkspaceContextService,
		@IEnvironmentService environmentService: IEnvironmentService,
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
226 227
		@ICommandService private commandService: ICommandService,
		@IHistoryService historyService: IHistoryService
228
	) {
229
		super(id, label, windowService, environmentService, contextService, historyService);
230 231 232 233 234 235 236 237 238
	}

	public run(): TPromise<any> {
		const state = this.contextService.getWorkbenchState();

		// Workspace / Folder
		if (state === WorkbenchState.WORKSPACE || state === WorkbenchState.FOLDER) {
			return this.commandService.executeCommand<IWorkspaceFolder>(PICK_WORKSPACE_FOLDER_COMMAND).then(folder => {
				if (folder) {
S
Sandeep Somavarapu 已提交
239
					return this.workspaceEditingService.removeFolders([folder.uri]).then(() => true);
240 241 242 243 244 245 246 247 248 249
				}

				return true;
			});
		}

		return TPromise.as(true);
	}
}

I
isidor 已提交
250
export class RemoveRootFolderAction extends Action {
B
Benjamin Pasero 已提交
251

I
isidor 已提交
252
	static ID = 'workbench.action.removeRootFolder';
253
	static LABEL = nls.localize('removeFolderFromWorkspace', "Remove Folder from Workspace");
B
Benjamin Pasero 已提交
254 255

	constructor(
I
isidor 已提交
256
		private rootUri: URI,
B
Benjamin Pasero 已提交
257 258
		id: string,
		label: string,
S
Sandeep Somavarapu 已提交
259
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService
B
Benjamin Pasero 已提交
260 261 262 263 264
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
S
Sandeep Somavarapu 已提交
265
		return this.workspaceEditingService.removeFolders([this.rootUri]);
B
Benjamin Pasero 已提交
266
	}
I
isidor 已提交
267
}
268

I
isidor 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
export class OpenFolderSettingsAction extends Action {

	static ID = 'workbench.action.openFolderSettings';
	static LABEL = nls.localize('openFolderSettings', "Open Folder Settings");

	constructor(
		private rootUri: URI,
		id: string,
		label: string,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@ICommandService private commandService: ICommandService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		const workspaceFolder = this.contextService.getWorkspaceFolder(this.rootUri);
286

I
isidor 已提交
287 288 289 290
		return this.commandService.executeCommand('_workbench.action.openFolderSettings', workspaceFolder);
	}
}

291
export class SaveWorkspaceAsAction extends BaseWorkspacesAction {
292

293 294
	static ID = 'workbench.action.saveWorkspaceAs';
	static LABEL = nls.localize('saveWorkspaceAsAction', "Save Workspace As...");
295 296 297 298

	constructor(
		id: string,
		label: string,
B
Benjamin Pasero 已提交
299 300
		@IWindowService windowService: IWindowService,
		@IEnvironmentService environmentService: IEnvironmentService,
B
Benjamin Pasero 已提交
301
		@IWorkspaceContextService contextService: IWorkspaceContextService,
302 303
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
		@IHistoryService historyService: IHistoryService
304
	) {
305
		super(id, label, windowService, environmentService, contextService, historyService);
306 307 308
	}

	public run(): TPromise<any> {
309 310 311 312 313 314 315 316 317 318 319
		return this.getNewWorkspaceConfigPath().then(configPath => {
			if (configPath) {
				switch (this.contextService.getWorkbenchState()) {
					case WorkbenchState.EMPTY:
					case WorkbenchState.FOLDER:
						const folders = this.contextService.getWorkspace().folders.map(folder => ({ uri: folder.uri }));
						return this.workspaceEditingService.createAndEnterWorkspace(folders, configPath);

					case WorkbenchState.WORKSPACE:
						return this.workspaceEditingService.saveAndEnterWorkspace(configPath);
				}
S
Sandeep Somavarapu 已提交
320
			}
S
Sandeep Somavarapu 已提交
321

322 323
			return null;
		});
S
Sandeep Somavarapu 已提交
324 325
	}

326
	private getNewWorkspaceConfigPath(): TPromise<string> {
S
Sandeep Somavarapu 已提交
327
		return this.windowService.showSaveDialog({
328
			buttonLabel: mnemonicButtonLabel(nls.localize({ key: 'save', comment: ['&& denotes a mnemonic'] }, "&&Save")),
S
Sandeep Somavarapu 已提交
329 330
			title: nls.localize('saveWorkspace', "Save Workspace"),
			filters: WORKSPACE_FILTER,
331
			defaultPath: defaultWorkspacePath(this.contextService, this.historyService, this.environmentService)
S
Sandeep Somavarapu 已提交
332
		});
333 334 335 336 337 338 339 340 341 342 343 344
	}
}

export class OpenWorkspaceAction extends Action {

	static ID = 'workbench.action.openWorkspace';
	static LABEL = nls.localize('openWorkspaceAction', "Open Workspace...");

	constructor(
		id: string,
		label: string,
		@IWindowService private windowService: IWindowService,
345 346 347
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IHistoryService private historyService: IHistoryService,
		@IEnvironmentService private environmentService: IEnvironmentService
348 349 350 351
	) {
		super(id, label);
	}

352 353
	public run(event?: any, data?: ITelemetryData): TPromise<any> {
		return this.windowService.pickWorkspaceAndOpen({ telemetryExtraData: data, dialogOptions: { defaultPath: defaultWorkspacePath(this.contextService, this.historyService, this.environmentService) } });
354
	}
355 356
}

357 358
export class OpenWorkspaceConfigFileAction extends Action {

M
Matt Bierner 已提交
359 360
	public static readonly ID = 'workbench.action.openWorkspaceConfigFile';
	public static readonly LABEL = nls.localize('openWorkspaceConfigFile', "Open Workspace Configuration File");
361 362 363 364 365 366 367 368

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(id, label);
B
Benjamin Pasero 已提交
369

370
		this.enabled = !!this.workspaceContextService.getWorkspace().configuration;
371 372 373 374 375
	}

	public run(): TPromise<any> {
		return this.editorService.openEditor({ resource: this.workspaceContextService.getWorkspace().configuration });
	}
376 377
}

378 379
export class OpenFolderAsWorkspaceInNewWindowAction extends Action {

M
Matt Bierner 已提交
380 381
	public static readonly ID = 'workbench.action.openFolderAsWorkspaceInNewWindow';
	public static readonly LABEL = nls.localize('openFolderAsWorkspaceInNewWindow', "Open Folder as Workspace in New Window");
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
		@IWindowsService private windowsService: IWindowsService,
		@ICommandService private commandService: ICommandService,
		@IWorkspacesService private workspacesService: IWorkspacesService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		const folders = this.workspaceContextService.getWorkspace().folders;

		let folderPromise: TPromise<IWorkspaceFolder>;
		if (folders.length === 0) {
			folderPromise = TPromise.as(null);
		} else if (folders.length === 1) {
			folderPromise = TPromise.as(folders[0]);
		} else {
			folderPromise = this.commandService.executeCommand<IWorkspaceFolder>(PICK_WORKSPACE_FOLDER_COMMAND);
		}

		return folderPromise.then(folder => {
			if (!folder) {
				return void 0; // need at least one folder
			}

412
			return this.workspacesService.createWorkspace([{ uri: folder.uri }]).then(newWorkspace => {
413 414 415 416 417 418 419 420
				return this.workspaceEditingService.copyWorkspaceSettings(newWorkspace).then(() => {
					return this.windowsService.openWindow([newWorkspace.configPath], { forceNewWindow: true });
				});
			});
		});
	}
}

421
export const PICK_WORKSPACE_FOLDER_COMMAND = '_workbench.pickWorkspaceFolder';
422

423
CommandsRegistry.registerCommand(PICK_WORKSPACE_FOLDER_COMMAND, function (accessor: ServicesAccessor, args?: [IPickOptions, CancellationToken]) {
424 425 426 427 428 429 430 431 432 433 434 435
	const contextService = accessor.get(IWorkspaceContextService);
	const quickOpenService = accessor.get(IQuickOpenService);
	const environmentService = accessor.get(IEnvironmentService);

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

	const folderPicks = folders.map(folder => {
		return {
			label: folder.name,
B
Benjamin Pasero 已提交
436
			description: getPathLabel(resources.dirname(folder.uri), void 0, environmentService),
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
			folder,
			resource: folder.uri,
			fileKind: FileKind.ROOT_FOLDER
		} as IFilePickOpenEntry;
	});

	let options: IPickOptions;
	if (args) {
		options = args[0];
	}

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

	if (!options.autoFocus) {
		options.autoFocus = { autoFocusFirstEntry: true };
	}

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

	return quickOpenService.pick(folderPicks, options, token).then(pick => {
		if (!pick) {
			return void 0;
		}

		return folders[folderPicks.indexOf(pick)];
	});
I
isidor 已提交
480
});