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

import { Action } from 'vs/base/common/actions';
7
import * as nls from 'vs/nls';
B
Benjamin Pasero 已提交
8
import { IWindowService } from 'vs/platform/windows/common/windows';
9
import { ITelemetryData } from 'vs/platform/telemetry/common/telemetry';
10
import { IWorkspaceContextService, WorkbenchState, IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
B
Benjamin Pasero 已提交
11
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
12
import { WORKSPACE_FILTER, IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
I
isidor 已提交
13
import { mnemonicButtonLabel } from 'vs/base/common/labels';
14
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
I
isidor 已提交
15
import { ICommandService } from 'vs/platform/commands/common/commands';
M
Martin Aeschlimann 已提交
16
import { ADD_ROOT_FOLDER_COMMAND_ID, ADD_ROOT_FOLDER_LABEL, PICK_WORKSPACE_FOLDER_COMMAND_ID } from 'vs/workbench/browser/actions/workspaceCommands';
17
import { URI } from 'vs/base/common/uri';
M
Martin Aeschlimann 已提交
18
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
19
import { INotificationService } from 'vs/platform/notification/common/notification';
20 21 22

export class OpenFileAction extends Action {

23
	static readonly ID = 'workbench.action.files.openFile';
24 25 26 27 28
	static LABEL = nls.localize('openFile', "Open File...");

	constructor(
		id: string,
		label: string,
29
		@IFileDialogService private readonly dialogService: IFileDialogService
30 31 32 33
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
34
	run(event?: any, data?: ITelemetryData): Promise<any> {
M
Martin Aeschlimann 已提交
35
		return this.dialogService.pickFileAndOpen({ forceNewWindow: false, telemetryExtraData: data });
36 37
	}
}
38 39 40

export class OpenFolderAction extends Action {

41
	static readonly ID = 'workbench.action.files.openFolder';
42 43 44 45 46
	static LABEL = nls.localize('openFolder', "Open Folder...");

	constructor(
		id: string,
		label: string,
47
		@IFileDialogService private readonly dialogService: IFileDialogService
48 49 50 51
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
52
	run(event?: any, data?: ITelemetryData): Promise<any> {
M
Martin Aeschlimann 已提交
53
		return this.dialogService.pickFolderAndOpen({ forceNewWindow: false, telemetryExtraData: data });
54 55 56 57 58
	}
}

export class OpenFileFolderAction extends Action {

59
	static readonly ID = 'workbench.action.files.openFileFolder';
60 61 62 63 64
	static LABEL = nls.localize('openFileFolder', "Open...");

	constructor(
		id: string,
		label: string,
65
		@IFileDialogService private readonly dialogService: IFileDialogService
66 67 68 69
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
70
	run(event?: any, data?: ITelemetryData): Promise<any> {
M
Martin Aeschlimann 已提交
71
		return this.dialogService.pickFileFolderAndOpen({ forceNewWindow: false, telemetryExtraData: data });
72
	}
B
Benjamin Pasero 已提交
73 74
}

I
isidor 已提交
75
export class AddRootFolderAction extends Action {
B
Benjamin Pasero 已提交
76

77
	static readonly ID = 'workbench.action.addRootFolder';
I
isidor 已提交
78
	static LABEL = ADD_ROOT_FOLDER_LABEL;
B
Benjamin Pasero 已提交
79 80 81 82

	constructor(
		id: string,
		label: string,
83
		@ICommandService private readonly commandService: ICommandService
B
Benjamin Pasero 已提交
84
	) {
I
isidor 已提交
85
		super(id, label);
B
Benjamin Pasero 已提交
86 87
	}

J
Johannes Rieken 已提交
88
	run(): Promise<any> {
I
isidor 已提交
89
		return this.commandService.executeCommand(ADD_ROOT_FOLDER_COMMAND_ID);
S
Sandeep Somavarapu 已提交
90
	}
B
Benjamin Pasero 已提交
91
}
S
Sandeep Somavarapu 已提交
92

I
isidor 已提交
93
export class GlobalRemoveRootFolderAction extends Action {
94

95
	static readonly ID = 'workbench.action.removeRootFolder';
96 97 98 99 100
	static LABEL = nls.localize('globalRemoveFolderFromWorkspace', "Remove Folder from Workspace...");

	constructor(
		id: string,
		label: string,
101 102 103
		@IWorkspaceEditingService private readonly workspaceEditingService: IWorkspaceEditingService,
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
		@ICommandService private readonly commandService: ICommandService
104
	) {
I
isidor 已提交
105
		super(id, label);
106 107
	}

J
Johannes Rieken 已提交
108
	run(): Promise<any> {
109 110 111 112
		const state = this.contextService.getWorkbenchState();

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

				return true;
			});
		}

122
		return Promise.resolve(true);
123 124 125
	}
}

I
isidor 已提交
126
export class SaveWorkspaceAsAction extends Action {
127

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

	constructor(
		id: string,
		label: string,
134 135 136
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
		@IWorkspaceEditingService private readonly workspaceEditingService: IWorkspaceEditingService,
		@IFileDialogService private readonly dialogService: IFileDialogService
M
Martin Aeschlimann 已提交
137

138
	) {
I
isidor 已提交
139
		super(id, label);
140 141
	}

J
Johannes Rieken 已提交
142
	run(): Promise<any> {
143
		return this.getNewWorkspaceConfigPath().then((configPathUri): Promise<void> | void => {
M
Martin Aeschlimann 已提交
144
			if (configPathUri) {
145 146 147 148
				switch (this.contextService.getWorkbenchState()) {
					case WorkbenchState.EMPTY:
					case WorkbenchState.FOLDER:
						const folders = this.contextService.getWorkspace().folders.map(folder => ({ uri: folder.uri }));
M
Martin Aeschlimann 已提交
149
						return this.workspaceEditingService.createAndEnterWorkspace(folders, configPathUri);
150 151

					case WorkbenchState.WORKSPACE:
M
Martin Aeschlimann 已提交
152
						return this.workspaceEditingService.saveAndEnterWorkspace(configPathUri);
153
				}
S
Sandeep Somavarapu 已提交
154
			}
155
		});
S
Sandeep Somavarapu 已提交
156 157
	}

158
	private getNewWorkspaceConfigPath(): Promise<URI | undefined> {
M
Martin Aeschlimann 已提交
159
		return this.dialogService.showSaveDialog({
160
			saveLabel: mnemonicButtonLabel(nls.localize({ key: 'save', comment: ['&& denotes a mnemonic'] }, "&&Save")),
S
Sandeep Somavarapu 已提交
161 162
			title: nls.localize('saveWorkspace', "Save Workspace"),
			filters: WORKSPACE_FILTER,
163
			defaultUri: this.dialogService.defaultWorkspacePath()
S
Sandeep Somavarapu 已提交
164
		});
165 166 167 168 169
	}
}

export class OpenWorkspaceAction extends Action {

170
	static readonly ID = 'workbench.action.openWorkspace';
171 172 173 174 175
	static LABEL = nls.localize('openWorkspaceAction', "Open Workspace...");

	constructor(
		id: string,
		label: string,
176
		@IFileDialogService private readonly dialogService: IFileDialogService
177 178 179 180
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
181
	run(event?: any, data?: ITelemetryData): Promise<any> {
M
Martin Aeschlimann 已提交
182
		return this.dialogService.pickWorkspaceAndOpen({ telemetryExtraData: data });
183
	}
184 185
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
export class CloseWorkspaceAction extends Action {

	static readonly ID = 'workbench.action.closeFolder';
	static LABEL = nls.localize('closeWorkspace', "Close Workspace");

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
		@INotificationService private readonly notificationService: INotificationService,
		@IWindowService private readonly windowService: IWindowService
	) {
		super(id, label);
	}

	run(): Promise<void> {
		if (this.contextService.getWorkbenchState() === WorkbenchState.EMPTY) {
			this.notificationService.info(nls.localize('noWorkspaceOpened', "There is currently no workspace opened in this instance to close."));

			return Promise.resolve(undefined);
		}

		return this.windowService.closeWorkspace();
	}
}

212 213
export class OpenWorkspaceConfigFileAction extends Action {

B
Benjamin Pasero 已提交
214 215
	static readonly ID = 'workbench.action.openWorkspaceConfigFile';
	static readonly LABEL = nls.localize('openWorkspaceConfigFile', "Open Workspace Configuration File");
216 217 218 219

	constructor(
		id: string,
		label: string,
220 221
		@IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService,
		@IEditorService private readonly editorService: IEditorService
222 223
	) {
		super(id, label);
B
Benjamin Pasero 已提交
224

225
		this.enabled = !!this.workspaceContextService.getWorkspace().configuration;
226 227
	}

J
Johannes Rieken 已提交
228
	run(): Promise<any> {
229 230 231 232 233
		const configuration = this.workspaceContextService.getWorkspace().configuration;
		if (configuration) {
			return this.editorService.openEditor({ resource: configuration });
		}
		return Promise.resolve();
234
	}
235 236
}

I
isidor 已提交
237
export class DuplicateWorkspaceInNewWindowAction extends Action {
238

B
Benjamin Pasero 已提交
239 240
	static readonly ID = 'workbench.action.duplicateWorkspaceInNewWindow';
	static readonly LABEL = nls.localize('duplicateWorkspaceInNewWindow', "Duplicate Workspace in New Window");
241 242 243 244

	constructor(
		id: string,
		label: string,
245 246 247 248
		@IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService,
		@IWorkspaceEditingService private readonly workspaceEditingService: IWorkspaceEditingService,
		@IWindowService private readonly windowService: IWindowService,
		@IWorkspacesService private readonly workspacesService: IWorkspacesService
249 250 251 252
	) {
		super(id, label);
	}

J
Johannes Rieken 已提交
253
	run(): Promise<any> {
254 255
		const folders = this.workspaceContextService.getWorkspace().folders;

256
		return this.workspacesService.createUntitledWorkspace(folders).then(newWorkspace => {
I
isidor 已提交
257
			return this.workspaceEditingService.copyWorkspaceSettings(newWorkspace).then(() => {
258
				return this.windowService.openWindow([{ uri: newWorkspace.configPath, typeHint: 'file' }], { forceNewWindow: true });
259 260 261
			});
		});
	}
I
isidor 已提交
262
}