workspaceActions.ts 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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';
10
import * as nls from 'vs/nls';
B
Benjamin Pasero 已提交
11
import { IWindowService } 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
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
15
import { WORKSPACE_FILTER, IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
I
isidor 已提交
16
import { mnemonicButtonLabel } from 'vs/base/common/labels';
17
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
I
isidor 已提交
18
import { ICommandService } from 'vs/platform/commands/common/commands';
M
Martin Aeschlimann 已提交
19
import { ADD_ROOT_FOLDER_COMMAND_ID, ADD_ROOT_FOLDER_LABEL, PICK_WORKSPACE_FOLDER_COMMAND_ID } from 'vs/workbench/browser/actions/workspaceCommands';
20
import { URI } from 'vs/base/common/uri';
21
import { Schemas } from 'vs/base/common/network';
M
Martin Aeschlimann 已提交
22
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
23 24 25

export class OpenFileAction extends Action {

26
	static readonly ID = 'workbench.action.files.openFile';
27 28 29 30 31
	static LABEL = nls.localize('openFile', "Open File...");

	constructor(
		id: string,
		label: string,
M
Martin Aeschlimann 已提交
32
		@IFileDialogService private dialogService: IFileDialogService
33 34 35 36 37
	) {
		super(id, label);
	}

	run(event?: any, data?: ITelemetryData): TPromise<any> {
M
Martin Aeschlimann 已提交
38
		return this.dialogService.pickFileAndOpen({ forceNewWindow: false, telemetryExtraData: data });
39 40
	}
}
41 42 43

export class OpenFolderAction extends Action {

44
	static readonly ID = 'workbench.action.files.openFolder';
45 46 47 48 49
	static LABEL = nls.localize('openFolder', "Open Folder...");

	constructor(
		id: string,
		label: string,
M
Martin Aeschlimann 已提交
50
		@IFileDialogService private dialogService: IFileDialogService
51 52 53 54
	) {
		super(id, label);
	}

55
	run(event?: any, data?: ITelemetryData): TPromise<any> {
M
Martin Aeschlimann 已提交
56
		return this.dialogService.pickFolderAndOpen({ forceNewWindow: false, telemetryExtraData: data });
57 58 59 60 61
	}
}

export class OpenFileFolderAction extends Action {

62
	static readonly ID = 'workbench.action.files.openFileFolder';
63 64 65 66 67
	static LABEL = nls.localize('openFileFolder', "Open...");

	constructor(
		id: string,
		label: string,
M
Martin Aeschlimann 已提交
68
		@IFileDialogService private dialogService: IFileDialogService
69 70 71 72
	) {
		super(id, label);
	}

73
	run(event?: any, data?: ITelemetryData): TPromise<any> {
M
Martin Aeschlimann 已提交
74
		return this.dialogService.pickFileFolderAndOpen({ forceNewWindow: false, telemetryExtraData: data });
75
	}
B
Benjamin Pasero 已提交
76 77
}

I
isidor 已提交
78
export class AddRootFolderAction extends Action {
B
Benjamin Pasero 已提交
79

80
	static readonly ID = 'workbench.action.addRootFolder';
I
isidor 已提交
81
	static LABEL = ADD_ROOT_FOLDER_LABEL;
B
Benjamin Pasero 已提交
82 83 84 85

	constructor(
		id: string,
		label: string,
I
isidor 已提交
86
		@ICommandService private commandService: ICommandService
B
Benjamin Pasero 已提交
87
	) {
I
isidor 已提交
88
		super(id, label);
B
Benjamin Pasero 已提交
89 90
	}

B
Benjamin Pasero 已提交
91
	run(): TPromise<any> {
I
isidor 已提交
92
		return this.commandService.executeCommand(ADD_ROOT_FOLDER_COMMAND_ID);
S
Sandeep Somavarapu 已提交
93
	}
B
Benjamin Pasero 已提交
94
}
S
Sandeep Somavarapu 已提交
95

I
isidor 已提交
96
export class GlobalRemoveRootFolderAction extends Action {
97

98
	static readonly ID = 'workbench.action.removeRootFolder';
99 100 101 102 103 104
	static LABEL = nls.localize('globalRemoveFolderFromWorkspace', "Remove Folder from Workspace...");

	constructor(
		id: string,
		label: string,
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
I
isidor 已提交
105 106
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@ICommandService private commandService: ICommandService
107
	) {
I
isidor 已提交
108
		super(id, label);
109 110
	}

B
Benjamin Pasero 已提交
111
	run(): TPromise<any> {
112 113 114 115
		const state = this.contextService.getWorkbenchState();

		// Workspace / Folder
		if (state === WorkbenchState.WORKSPACE || state === WorkbenchState.FOLDER) {
I
isidor 已提交
116
			return this.commandService.executeCommand<IWorkspaceFolder>(PICK_WORKSPACE_FOLDER_COMMAND_ID).then(folder => {
117
				if (folder) {
S
Sandeep Somavarapu 已提交
118
					return this.workspaceEditingService.removeFolders([folder.uri]).then(() => true);
119 120 121 122 123 124 125 126 127 128
				}

				return true;
			});
		}

		return TPromise.as(true);
	}
}

I
isidor 已提交
129
export class SaveWorkspaceAsAction extends Action {
130

131
	static readonly ID = 'workbench.action.saveWorkspaceAs';
132
	static LABEL = nls.localize('saveWorkspaceAsAction', "Save Workspace As...");
133 134 135 136

	constructor(
		id: string,
		label: string,
I
isidor 已提交
137
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
138
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
M
Martin Aeschlimann 已提交
139 140
		@IFileDialogService private dialogService: IFileDialogService

141
	) {
I
isidor 已提交
142
		super(id, label);
143 144
	}

B
Benjamin Pasero 已提交
145
	run(): TPromise<any> {
M
Martin Aeschlimann 已提交
146 147 148
		return this.getNewWorkspaceConfigPath().then(configPathUri => {
			if (configPathUri) {
				const configPath = configPathUri.fsPath;
149 150 151 152 153 154 155 156 157
				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 已提交
158
			}
S
Sandeep Somavarapu 已提交
159

160 161
			return null;
		});
S
Sandeep Somavarapu 已提交
162 163
	}

M
Martin Aeschlimann 已提交
164 165
	private getNewWorkspaceConfigPath(): TPromise<URI> {
		return this.dialogService.showSaveDialog({
166
			buttonLabel: mnemonicButtonLabel(nls.localize({ key: 'save', comment: ['&& denotes a mnemonic'] }, "&&Save")),
S
Sandeep Somavarapu 已提交
167 168
			title: nls.localize('saveWorkspace', "Save Workspace"),
			filters: WORKSPACE_FILTER,
M
Martin Aeschlimann 已提交
169
			defaultUri: this.dialogService.defaultWorkspacePath(Schemas.file)
S
Sandeep Somavarapu 已提交
170
		});
171 172 173 174 175
	}
}

export class OpenWorkspaceAction extends Action {

176
	static readonly ID = 'workbench.action.openWorkspace';
177 178 179 180 181
	static LABEL = nls.localize('openWorkspaceAction', "Open Workspace...");

	constructor(
		id: string,
		label: string,
M
Martin Aeschlimann 已提交
182
		@IFileDialogService private dialogService: IFileDialogService
183 184 185 186
	) {
		super(id, label);
	}

B
Benjamin Pasero 已提交
187
	run(event?: any, data?: ITelemetryData): TPromise<any> {
M
Martin Aeschlimann 已提交
188
		return this.dialogService.pickWorkspaceAndOpen({ telemetryExtraData: data });
189
	}
190 191
}

192 193
export class OpenWorkspaceConfigFileAction extends Action {

B
Benjamin Pasero 已提交
194 195
	static readonly ID = 'workbench.action.openWorkspaceConfigFile';
	static readonly LABEL = nls.localize('openWorkspaceConfigFile', "Open Workspace Configuration File");
196 197 198 199 200

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
201
		@IEditorService private editorService: IEditorService
202 203
	) {
		super(id, label);
B
Benjamin Pasero 已提交
204

205
		this.enabled = !!this.workspaceContextService.getWorkspace().configuration;
206 207
	}

B
Benjamin Pasero 已提交
208
	run(): TPromise<any> {
209 210
		return this.editorService.openEditor({ resource: this.workspaceContextService.getWorkspace().configuration });
	}
211 212
}

I
isidor 已提交
213
export class DuplicateWorkspaceInNewWindowAction extends Action {
214

B
Benjamin Pasero 已提交
215 216
	static readonly ID = 'workbench.action.duplicateWorkspaceInNewWindow';
	static readonly LABEL = nls.localize('duplicateWorkspaceInNewWindow', "Duplicate Workspace in New Window");
217 218 219 220 221 222

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
B
Benjamin Pasero 已提交
223
		@IWindowService private windowService: IWindowService,
224 225 226 227 228
		@IWorkspacesService private workspacesService: IWorkspacesService
	) {
		super(id, label);
	}

B
Benjamin Pasero 已提交
229
	run(): TPromise<any> {
230 231
		const folders = this.workspaceContextService.getWorkspace().folders;

I
isidor 已提交
232 233
		return this.workspacesService.createWorkspace(folders).then(newWorkspace => {
			return this.workspaceEditingService.copyWorkspaceSettings(newWorkspace).then(() => {
234
				return this.windowService.openWindow([URI.file(newWorkspace.configPath)], { forceNewWindow: true });
235 236 237
			});
		});
	}
I
isidor 已提交
238
}