workspaceActions.ts 9.7 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';
B
Benjamin Pasero 已提交
13 14 15 16
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import URI from 'vs/base/common/uri';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
17
import { IInstantiationService } from "vs/platform/instantiation/common/instantiation";
18
import { IWorkspacesService, WORKSPACE_FILTER } from "vs/platform/workspaces/common/workspaces";
B
Benjamin Pasero 已提交
19 20
import { IEnvironmentService } from "vs/platform/environment/common/environment";
import { isWindows, isLinux } from "vs/base/common/platform";
B
Benjamin Pasero 已提交
21
import { dirname } from "vs/base/common/paths";
22 23 24 25 26 27 28 29 30 31 32 33 34 35

export class OpenFolderAction extends Action {

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

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

36
	run(event?: any, data?: ITelemetryData): TPromise<any> {
B
renames  
Benjamin Pasero 已提交
37
		return this.windowService.pickFolderAndOpen(undefined, data);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	}
}

export class OpenFileFolderAction extends Action {

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

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

54
	run(event?: any, data?: ITelemetryData): TPromise<any> {
B
renames  
Benjamin Pasero 已提交
55
		return this.windowService.pickFileFolderAndOpen(undefined, data);
56
	}
B
Benjamin Pasero 已提交
57 58
}

B
Benjamin Pasero 已提交
59
export abstract class BaseWorkspacesAction extends Action {
B
Benjamin Pasero 已提交
60 61 62 63 64 65

	constructor(
		id: string,
		label: string,
		protected windowService: IWindowService,
		protected instantiationService: IInstantiationService,
B
Benjamin Pasero 已提交
66 67
		protected environmentService: IEnvironmentService,
		protected contextService: IWorkspaceContextService
B
Benjamin Pasero 已提交
68 69 70 71
	) {
		super(id, label);
	}

S
Sandeep Somavarapu 已提交
72
	protected handleNotInMultiFolderWorkspaceCase(message: string, addExistingFolder: boolean): TPromise<any> {
B
Benjamin Pasero 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		const newWorkspace = { label: this.mnemonicLabel(nls.localize({ key: 'create', comment: ['&& denotes a mnemonic'] }, "&&New Workspace")), canceled: false };
		const cancel = { label: nls.localize('cancel', "Cancel"), canceled: true };

		const buttons: { label: string; canceled: boolean; }[] = [];
		if (isLinux) {
			buttons.push(cancel, newWorkspace);
		} else {
			buttons.push(newWorkspace, cancel);
		}

		const opts: Electron.ShowMessageBoxOptions = {
			title: this.environmentService.appNameLong,
			message,
			detail: nls.localize('workspaceDetail', "Workspaces allow to open multiple folders at once."),
			noLink: true,
			type: 'info',
			buttons: buttons.map(button => button.label),
			cancelId: buttons.indexOf(cancel)
		};

		if (isLinux) {
B
Benjamin Pasero 已提交
94
			opts.defaultId = 1;
B
Benjamin Pasero 已提交
95 96 97 98
		}

		const res = this.windowService.showMessageBox(opts);
		if (!buttons[res].canceled) {
S
Sandeep Somavarapu 已提交
99
			return this.instantiationService.createInstance(NewWorkspaceAction, NewWorkspaceAction.ID, NewWorkspaceAction.LABEL).run(addExistingFolder);
B
Benjamin Pasero 已提交
100 101 102 103 104 105 106 107 108 109 110 111
		}

		return TPromise.as(null);
	}

	private mnemonicLabel(label: string): string {
		if (!isWindows) {
			return label.replace(/\(&&\w\)|&&/g, ''); // no mnemonic support on mac/linux
		}

		return label.replace(/&&/g, '&');
	}
B
Benjamin Pasero 已提交
112 113 114 115 116 117 118 119 120

	protected pickFolders(button: string, title: string): string[] {
		return this.windowService.showOpenDialog({
			buttonLabel: nls.localize('add', "Add"),
			title: nls.localize('addFolderToWorkspaceTitle', "Add Folder to Workspace"),
			properties: ['multiSelections', 'openDirectory', 'createDirectory'],
			defaultPath: this.contextService.hasWorkspace() ? dirname(this.contextService.getWorkspace().roots[0].fsPath) : void 0 // pick the parent of the first root by default
		});
	}
B
Benjamin Pasero 已提交
121 122
}

B
Benjamin Pasero 已提交
123
export class AddRootFolderAction extends BaseWorkspacesAction {
B
Benjamin Pasero 已提交
124

B
Benjamin Pasero 已提交
125
	static ID = 'workbench.action.addRootFolder';
126
	static LABEL = nls.localize('addFolderToWorkspace', "Add Folder to Workspace...");
B
Benjamin Pasero 已提交
127 128 129 130

	constructor(
		id: string,
		label: string,
B
Benjamin Pasero 已提交
131 132
		@IWindowService windowService: IWindowService,
		@IInstantiationService instantiationService: IInstantiationService,
B
Benjamin Pasero 已提交
133
		@IWorkspaceContextService contextService: IWorkspaceContextService,
134
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
B
Benjamin Pasero 已提交
135 136
		@IViewletService private viewletService: IViewletService,
		@IEnvironmentService environmentService: IEnvironmentService
B
Benjamin Pasero 已提交
137
	) {
B
Benjamin Pasero 已提交
138
		super(id, label, windowService, instantiationService, environmentService, contextService);
B
Benjamin Pasero 已提交
139 140 141
	}

	public run(): TPromise<any> {
142
		if (!this.contextService.hasMultiFolderWorkspace()) {
S
Sandeep Somavarapu 已提交
143
			return super.handleNotInMultiFolderWorkspaceCase(nls.localize('addSupported', "You can only add folders to workspaces. Do you want to create a new workspace with the current folder and add?"), true);
B
Benjamin Pasero 已提交
144 145
		}

B
Benjamin Pasero 已提交
146 147 148 149
		const folders = super.pickFolders(nls.localize('add', "Add"), nls.localize('addFolderToWorkspaceTitle', "Add Folder to Workspace"));
		if (!folders || !folders.length) {
			return TPromise.as(null);
		}
150

B
Benjamin Pasero 已提交
151 152
		return this.workspaceEditingService.addRoots(folders.map(folder => URI.file(folder))).then(() => {
			return this.viewletService.openViewlet(this.viewletService.getDefaultViewletId(), true);
B
Benjamin Pasero 已提交
153 154 155 156
		});
	}
}

B
Benjamin Pasero 已提交
157
export class NewWorkspaceAction extends BaseWorkspacesAction {
158

S
Sandeep Somavarapu 已提交
159 160
	static ID = 'workbench.action.newWorkspace';
	static LABEL = nls.localize('newWorkspace', "New Workspace...");
161 162 163 164

	constructor(
		id: string,
		label: string,
B
Benjamin Pasero 已提交
165 166
		@IWindowService windowService: IWindowService,
		@IWorkspaceContextService contextService: IWorkspaceContextService,
167
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
B
Benjamin Pasero 已提交
168 169
		@IInstantiationService instantiationService: IInstantiationService,
		@IEnvironmentService environmentService: IEnvironmentService
170
	) {
B
Benjamin Pasero 已提交
171
		super(id, label, windowService, instantiationService, environmentService, contextService);
172 173
	}

S
Sandeep Somavarapu 已提交
174 175 176 177 178 179 180
	public run(addExistingFolder: boolean = false): TPromise<any> {
		let folders = this.pickFolders(nls.localize('select', "Select"), nls.localize('selectWorkspace', "Select Folders for Workspace"));
		if (folders && folders.length) {
			if (addExistingFolder && this.contextService.hasWorkspace()) {
				folders = [this.contextService.getWorkspace().roots[0].fsPath, ...folders];
			}
			return this.workspaceEditingService.createAndOpenWorkspace(folders.map(folder => URI.file(folder)));
B
Benjamin Pasero 已提交
181
		}
B
Benjamin Pasero 已提交
182

S
Sandeep Somavarapu 已提交
183
		return TPromise.as(null);
184 185 186
	}
}

I
isidor 已提交
187
export class RemoveRootFolderAction extends Action {
B
Benjamin Pasero 已提交
188

I
isidor 已提交
189
	static ID = 'workbench.action.removeRootFolder';
190
	static LABEL = nls.localize('removeFolderFromWorkspace', "Remove Folder from Workspace");
B
Benjamin Pasero 已提交
191 192

	constructor(
I
isidor 已提交
193
		private rootUri: URI,
B
Benjamin Pasero 已提交
194 195 196 197 198 199 200 201
		id: string,
		label: string,
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
I
isidor 已提交
202
		return this.workspaceEditingService.removeRoots([this.rootUri]);
B
Benjamin Pasero 已提交
203
	}
I
isidor 已提交
204
}
205

B
Benjamin Pasero 已提交
206
export class SaveWorkspaceAction extends BaseWorkspacesAction {
207 208 209 210 211 212 213

	static ID = 'workbench.action.saveWorkspace';
	static LABEL = nls.localize('saveWorkspaceAction', "Save Workspace...");

	constructor(
		id: string,
		label: string,
B
Benjamin Pasero 已提交
214 215
		@IWindowService windowService: IWindowService,
		@IEnvironmentService environmentService: IEnvironmentService,
B
Benjamin Pasero 已提交
216
		@IWorkspaceContextService contextService: IWorkspaceContextService,
217
		@IWorkspacesService private workspacesService: IWorkspacesService,
B
Benjamin Pasero 已提交
218
		@IInstantiationService instantiationService: IInstantiationService,
219 220
		@IWindowsService private windowsService: IWindowsService
	) {
B
Benjamin Pasero 已提交
221
		super(id, label, windowService, instantiationService, environmentService, contextService);
222 223 224 225
	}

	public run(): TPromise<any> {
		if (!this.contextService.hasMultiFolderWorkspace()) {
S
Sandeep Somavarapu 已提交
226
			return super.handleNotInMultiFolderWorkspaceCase(nls.localize('saveNotSupported', "You need to open a workspace first to save it. Do you want to create a new workspace with the existing folder and add?"), true);
227 228 229 230 231
		}

		const target = this.windowService.showSaveDialog({
			buttonLabel: nls.localize('save', "Save"),
			title: nls.localize('saveWorkspace', "Save Workspace"),
232
			filters: WORKSPACE_FILTER,
B
Benjamin Pasero 已提交
233
			defaultPath: dirname(this.contextService.getWorkspace().roots[0].fsPath) // pick the parent of the first root by default
234 235 236
		});

		if (target) {
237
			return this.contextService.saveWorkspace(URI.file(target));
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
		}

		return TPromise.as(false);
	}
}

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,
B
Benjamin Pasero 已提交
253 254
		@IWindowsService private windowsService: IWindowsService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
255 256 257 258 259 260 261 262
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		const files = this.windowService.showOpenDialog({
			buttonLabel: nls.localize('open', "Open"),
			title: nls.localize('openWorkspace', "Open Workspace"),
263
			filters: WORKSPACE_FILTER,
B
Benjamin Pasero 已提交
264 265
			properties: ['openFile'],
			defaultPath: this.contextService.hasWorkspace() ? dirname(this.contextService.getWorkspace().roots[0].fsPath) : void 0 // pick the parent of the first root by default
266 267 268 269 270 271 272 273 274
		});

		if (!files || !files.length) {
			return TPromise.as(null);
		}

		return this.windowsService.openWindow([files[0]]);
	}
}