workspaceActions.ts 8.9 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';
18
import { Schemas } from 'vs/base/common/network';
M
Martin Aeschlimann 已提交
19
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
20
import { INotificationService } from 'vs/platform/notification/common/notification';
21 22 23

export class OpenFileAction extends Action {

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

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

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

export class OpenFolderAction extends Action {

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

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

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

export class OpenFileFolderAction extends Action {

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

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

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

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

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

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

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

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

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

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

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

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

				return true;
			});
		}

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

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

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

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

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

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

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

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

export class OpenWorkspaceAction extends Action {

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

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

J
Johannes Rieken 已提交
182
	run(event?: any, data?: ITelemetryData): Promise<any> {
183
		return this.dialogService.pickWorkspaceAndOpen({ telemetryExtraData: data, allowLocalPaths: true });
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 212
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();
	}
}

213 214
export class OpenWorkspaceConfigFileAction extends Action {

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

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

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

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

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

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

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

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

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