workspaceActions.ts 8.9 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';
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
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
15
import { WORKSPACE_FILTER, IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
B
Benjamin Pasero 已提交
16
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
I
isidor 已提交
17
import { mnemonicButtonLabel } from 'vs/base/common/labels';
18
import { INextEditorService } from 'vs/workbench/services/editor/common/editorService';
I
isidor 已提交
19
import { ICommandService } from 'vs/platform/commands/common/commands';
20
import { IHistoryService } from 'vs/workbench/services/history/common/history';
I
isidor 已提交
21
import { ADD_ROOT_FOLDER_COMMAND_ID, ADD_ROOT_FOLDER_LABEL, PICK_WORKSPACE_FOLDER_COMMAND_ID, defaultWorkspacePath, defaultFilePath, defaultFolderPath } from 'vs/workbench/browser/actions/workspaceCommands';
22 23 24

export class OpenFileAction extends Action {

25
	static readonly ID = 'workbench.action.files.openFile';
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	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) } });
	}
}
42 43 44

export class OpenFolderAction extends Action {

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

	constructor(
		id: string,
		label: string,
51 52 53
		@IWindowService private windowService: IWindowService,
		@IHistoryService private historyService: IHistoryService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
54 55 56 57
	) {
		super(id, label);
	}

58
	run(event?: any, data?: ITelemetryData): TPromise<any> {
59
		return this.windowService.pickFolderAndOpen({ telemetryExtraData: data, dialogOptions: { defaultPath: defaultFolderPath(this.contextService, this.historyService) } });
60 61 62 63 64
	}
}

export class OpenFileFolderAction extends Action {

65
	static readonly ID = 'workbench.action.files.openFileFolder';
66 67 68 69 70
	static LABEL = nls.localize('openFileFolder', "Open...");

	constructor(
		id: string,
		label: string,
71 72 73
		@IWindowService private windowService: IWindowService,
		@IHistoryService private historyService: IHistoryService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService
74 75 76 77
	) {
		super(id, label);
	}

78
	run(event?: any, data?: ITelemetryData): TPromise<any> {
79
		return this.windowService.pickFileFolderAndOpen({ telemetryExtraData: data, dialogOptions: { defaultPath: defaultFilePath(this.contextService, this.historyService) } });
80
	}
B
Benjamin Pasero 已提交
81 82
}

I
isidor 已提交
83
export class AddRootFolderAction extends Action {
B
Benjamin Pasero 已提交
84

85
	static readonly ID = 'workbench.action.addRootFolder';
I
isidor 已提交
86
	static LABEL = ADD_ROOT_FOLDER_LABEL;
B
Benjamin Pasero 已提交
87 88 89 90

	constructor(
		id: string,
		label: string,
I
isidor 已提交
91
		@ICommandService private commandService: ICommandService
B
Benjamin Pasero 已提交
92
	) {
I
isidor 已提交
93
		super(id, label);
B
Benjamin Pasero 已提交
94 95 96
	}

	public run(): TPromise<any> {
I
isidor 已提交
97
		return this.commandService.executeCommand(ADD_ROOT_FOLDER_COMMAND_ID);
S
Sandeep Somavarapu 已提交
98
	}
B
Benjamin Pasero 已提交
99
}
S
Sandeep Somavarapu 已提交
100

I
isidor 已提交
101
export class GlobalRemoveRootFolderAction extends Action {
102

103
	static readonly ID = 'workbench.action.removeRootFolder';
104 105 106 107 108 109
	static LABEL = nls.localize('globalRemoveFolderFromWorkspace', "Remove Folder from Workspace...");

	constructor(
		id: string,
		label: string,
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
I
isidor 已提交
110 111
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@ICommandService private commandService: ICommandService
112
	) {
I
isidor 已提交
113
		super(id, label);
114 115 116 117 118 119 120
	}

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

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

				return true;
			});
		}

		return TPromise.as(true);
	}
}

I
isidor 已提交
134
export class SaveWorkspaceAsAction extends Action {
135

136
	static readonly ID = 'workbench.action.saveWorkspaceAs';
137
	static LABEL = nls.localize('saveWorkspaceAsAction', "Save Workspace As...");
138 139 140 141

	constructor(
		id: string,
		label: string,
I
isidor 已提交
142 143 144
		@IWindowService private windowService: IWindowService,
		@IEnvironmentService private environmentService: IEnvironmentService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
145
		@IWorkspaceEditingService private workspaceEditingService: IWorkspaceEditingService,
I
isidor 已提交
146
		@IHistoryService private historyService: IHistoryService
147
	) {
I
isidor 已提交
148
		super(id, label);
149 150 151
	}

	public run(): TPromise<any> {
152 153 154 155 156 157 158 159 160 161 162
		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 已提交
163
			}
S
Sandeep Somavarapu 已提交
164

165 166
			return null;
		});
S
Sandeep Somavarapu 已提交
167 168
	}

169
	private getNewWorkspaceConfigPath(): TPromise<string> {
S
Sandeep Somavarapu 已提交
170
		return this.windowService.showSaveDialog({
171
			buttonLabel: mnemonicButtonLabel(nls.localize({ key: 'save', comment: ['&& denotes a mnemonic'] }, "&&Save")),
S
Sandeep Somavarapu 已提交
172 173
			title: nls.localize('saveWorkspace', "Save Workspace"),
			filters: WORKSPACE_FILTER,
174
			defaultPath: defaultWorkspacePath(this.contextService, this.historyService, this.environmentService)
S
Sandeep Somavarapu 已提交
175
		});
176 177 178 179 180
	}
}

export class OpenWorkspaceAction extends Action {

181
	static readonly ID = 'workbench.action.openWorkspace';
182 183 184 185 186 187
	static LABEL = nls.localize('openWorkspaceAction', "Open Workspace...");

	constructor(
		id: string,
		label: string,
		@IWindowService private windowService: IWindowService,
188 189 190
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IHistoryService private historyService: IHistoryService,
		@IEnvironmentService private environmentService: IEnvironmentService
191 192 193 194
	) {
		super(id, label);
	}

195 196
	public run(event?: any, data?: ITelemetryData): TPromise<any> {
		return this.windowService.pickWorkspaceAndOpen({ telemetryExtraData: data, dialogOptions: { defaultPath: defaultWorkspacePath(this.contextService, this.historyService, this.environmentService) } });
197
	}
198 199
}

200 201
export class OpenWorkspaceConfigFileAction extends Action {

M
Matt Bierner 已提交
202 203
	public static readonly ID = 'workbench.action.openWorkspaceConfigFile';
	public static readonly LABEL = nls.localize('openWorkspaceConfigFile', "Open Workspace Configuration File");
204 205 206 207 208

	constructor(
		id: string,
		label: string,
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
209
		@INextEditorService private editorService: INextEditorService
210 211
	) {
		super(id, label);
B
Benjamin Pasero 已提交
212

213
		this.enabled = !!this.workspaceContextService.getWorkspace().configuration;
214 215 216 217 218
	}

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

I
isidor 已提交
221
export class DuplicateWorkspaceInNewWindowAction extends Action {
222

I
isidor 已提交
223 224
	public static readonly ID = 'workbench.action.duplicateWorkspaceInNewWindow';
	public static readonly LABEL = nls.localize('duplicateWorkspaceInNewWindow', "Duplicate Workspace in New Window");
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

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

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

I
isidor 已提交
240 241 242
		return this.workspacesService.createWorkspace(folders).then(newWorkspace => {
			return this.workspaceEditingService.copyWorkspaceSettings(newWorkspace).then(() => {
				return this.windowsService.openWindow([newWorkspace.configPath], { forceNewWindow: true });
243 244 245
			});
		});
	}
I
isidor 已提交
246
}