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

'use strict';

B
Benjamin Pasero 已提交
8
import nls = require('vs/nls');
9
import paths = require('vs/base/common/paths');
10
import { TPromise } from 'vs/base/common/winjs.base';
11
import * as labels from 'vs/base/common/labels';
12 13
import URI from 'vs/base/common/uri';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
14
import { toResource, IEditorCommandsContext } from 'vs/workbench/common/editor';
15
import { IWindowsService } from 'vs/platform/windows/common/windows';
I
isidor 已提交
16
import { ServicesAccessor, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
17 18
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
I
isidor 已提交
19
import { ExplorerFocusCondition, FileOnDiskContentProvider, VIEWLET_ID } from 'vs/workbench/parts/files/common/files';
20
import { ExplorerViewlet } from 'vs/workbench/parts/files/electron-browser/explorerViewlet';
21
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
B
Benjamin Pasero 已提交
22
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
23
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
I
isidor 已提交
24 25 26 27
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { basename } from 'vs/base/common/paths';
import { IListService } from 'vs/platform/list/browser/listService';
import { Tree } from 'vs/base/parts/tree/browser/treeImpl';
I
ups  
isidor 已提交
28
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
I
isidor 已提交
29
import { RawContextKey, IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
I
isidor 已提交
30
import { IResourceInput, Position } from 'vs/platform/editor/common/editor';
31 32 33 34
import { IFileService } from 'vs/platform/files/common/files';
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { IEditorViewState } from 'vs/editor/common/editorCommon';
import { getCodeEditor } from 'vs/editor/browser/services/codeEditorService';
35
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
36
import { KeyMod, KeyCode, KeyChord } from 'vs/base/common/keyCodes';
37
import { isWindows, isMacintosh } from 'vs/base/common/platform';
I
isidor 已提交
38
import { ITextModelService } from 'vs/editor/common/services/resolverService';
I
isidor 已提交
39
import { sequence } from 'vs/base/common/async';
40
import { getResourceForCommand, getMultiSelectedResources } from 'vs/workbench/parts/files/browser/files';
41
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
I
isidor 已提交
42
import { getMultiSelectedEditorContexts } from 'vs/workbench/browser/parts/editor/editorCommands';
43
import { Schemas } from 'vs/base/common/network';
44
import { INotificationService } from 'vs/platform/notification/common/notification';
45 46 47

// Commands

I
isidor 已提交
48
export const REVEAL_IN_OS_COMMAND_ID = 'revealFileInOS';
49
export const REVEAL_IN_OS_LABEL = isWindows ? nls.localize('revealInWindows', "Reveal in Explorer") : isMacintosh ? nls.localize('revealInMac', "Reveal in Finder") : nls.localize('openContainer', "Open Containing Folder");
I
isidor 已提交
50
export const REVEAL_IN_EXPLORER_COMMAND_ID = 'revealInExplorer';
51
export const REVERT_FILE_COMMAND_ID = 'workbench.action.files.revert';
52
export const OPEN_TO_SIDE_COMMAND_ID = 'explorer.openToSide';
I
isidor 已提交
53
export const SELECT_FOR_COMPARE_COMMAND_ID = 'selectForCompare';
I
isidor 已提交
54 55

export const COMPARE_SELECTED_COMMAND_ID = 'compareSelected';
I
isidor 已提交
56
export const COMPARE_RESOURCE_COMMAND_ID = 'compareFiles';
57
export const COMPARE_WITH_SAVED_COMMAND_ID = 'workbench.files.action.compareWithSaved';
58
export const COPY_PATH_COMMAND_ID = 'copyFilePath';
59

60
export const SAVE_FILE_AS_COMMAND_ID = 'workbench.action.files.saveAs';
61
export const SAVE_FILE_AS_LABEL = nls.localize('saveAs', "Save As...");
62
export const SAVE_FILE_COMMAND_ID = 'workbench.action.files.save';
63 64
export const SAVE_FILE_LABEL = nls.localize('save', "Save");

I
isidor 已提交
65
export const SAVE_ALL_COMMAND_ID = 'saveAll';
I
isidor 已提交
66 67
export const SAVE_ALL_LABEL = nls.localize('saveAll', "Save All");

68
export const SAVE_ALL_IN_GROUP_COMMAND_ID = 'workbench.files.action.saveAllInGroup';
I
isidor 已提交
69

I
isidor 已提交
70
export const SAVE_FILES_COMMAND_ID = 'workbench.action.files.saveFiles';
I
isidor 已提交
71

B
Benjamin Pasero 已提交
72
export const OpenEditorsGroupContext = new RawContextKey<boolean>('groupFocusedInOpenEditors', false);
73
export const DirtyEditorContext = new RawContextKey<boolean>('dirtyEditor', false);
I
isidor 已提交
74
export const ResourceSelectedForCompareContext = new RawContextKey<boolean>('resourceSelectedForCompare', false);
75

I
isidor 已提交
76
export const REMOVE_ROOT_FOLDER_COMMAND_ID = 'removeRootFolder';
77 78
export const REMOVE_ROOT_FOLDER_LABEL = nls.localize('removeFolderFromWorkspace', "Remove Folder from Workspace");

79 80 81 82 83
export const openWindowCommand = (accessor: ServicesAccessor, paths: string[], forceNewWindow: boolean) => {
	const windowsService = accessor.get(IWindowsService);
	windowsService.openWindow(paths, { forceNewWindow });
};

I
isidor 已提交
84 85 86
function save(resource: URI, isSaveAs: boolean, editorService: IWorkbenchEditorService, fileService: IFileService, untitledEditorService: IUntitledEditorService,
	textFileService: ITextFileService, editorGroupService: IEditorGroupService): TPromise<any> {

87
	if (resource && (fileService.canHandleResource(resource) || resource.scheme === Schemas.untitled)) {
I
isidor 已提交
88 89

		// Save As (or Save untitled with associated path)
90
		if (isSaveAs || resource.scheme === Schemas.untitled) {
I
isidor 已提交
91
			let encodingOfSource: string;
92
			if (resource.scheme === Schemas.untitled) {
I
isidor 已提交
93
				encodingOfSource = untitledEditorService.getEncoding(resource);
94
			} else if (fileService.canHandleResource(resource)) {
I
isidor 已提交
95
				const textModel = textFileService.models.get(resource);
I
isidor 已提交
96 97 98 99 100 101 102 103
				encodingOfSource = textModel && textModel.getEncoding(); // text model can be null e.g. if this is a binary file!
			}

			let viewStateOfSource: IEditorViewState;
			const activeEditor = editorService.getActiveEditor();
			const editor = getCodeEditor(activeEditor);
			if (editor) {
				const activeResource = toResource(activeEditor.input, { supportSideBySide: true });
104
				if (activeResource && (fileService.canHandleResource(activeResource) || resource.scheme === Schemas.untitled) && activeResource.toString() === resource.toString()) {
I
isidor 已提交
105 106 107 108 109 110
					viewStateOfSource = editor.saveViewState();
				}
			}

			// Special case: an untitled file with associated path gets saved directly unless "saveAs" is true
			let savePromise: TPromise<URI>;
111
			if (!isSaveAs && resource.scheme === Schemas.untitled && untitledEditorService.hasAssociatedFilePath(resource)) {
I
isidor 已提交
112
				savePromise = textFileService.save(resource).then((result) => {
I
isidor 已提交
113
					if (result) {
I
isidor 已提交
114
						return URI.file(resource.fsPath);
I
isidor 已提交
115 116 117 118 119 120 121 122
					}

					return null;
				});
			}

			// Otherwise, really "Save As..."
			else {
I
isidor 已提交
123
				savePromise = textFileService.saveAs(resource);
I
isidor 已提交
124 125 126
			}

			return savePromise.then((target) => {
I
isidor 已提交
127
				if (!target || target.toString() === resource.toString()) {
I
isidor 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
					return void 0; // save canceled or same resource used
				}

				const replaceWith: IResourceInput = {
					resource: target,
					encoding: encodingOfSource,
					options: {
						pinned: true,
						viewState: viewStateOfSource
					}
				};

				return editorService.replaceEditors([{
I
isidor 已提交
141
					toReplace: { resource: resource },
I
isidor 已提交
142 143 144 145 146 147
					replaceWith
				}]).then(() => true);
			});
		}

		// Pin the active editor if we are saving it
I
isidor 已提交
148
		const editor = editorService.getActiveEditor();
I
fix npe  
isidor 已提交
149 150
		const activeEditorResource = editor && editor.input && editor.input.getResource();
		if (activeEditorResource && activeEditorResource.toString() === resource.toString()) {
I
isidor 已提交
151
			editorGroupService.pinEditor(editor.position, editor.input);
I
isidor 已提交
152 153 154
		}

		// Just save
I
isidor 已提交
155
		return textFileService.save(resource, { force: true /* force a change to the file to trigger external watchers if any */ });
I
isidor 已提交
156 157 158 159 160
	}

	return TPromise.as(false);
}

I
isidor 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
function saveAll(saveAllArguments: any, editorService: IWorkbenchEditorService, untitledEditorService: IUntitledEditorService,
	textFileService: ITextFileService, editorGroupService: IEditorGroupService): TPromise<any> {

	const stacks = editorGroupService.getStacksModel();

	// Store some properties per untitled file to restore later after save is completed
	const mapUntitledToProperties: { [resource: string]: { encoding: string; indexInGroups: number[]; activeInGroups: boolean[] } } = Object.create(null);
	untitledEditorService.getDirty().forEach(resource => {
		const activeInGroups: boolean[] = [];
		const indexInGroups: number[] = [];
		const encoding = untitledEditorService.getEncoding(resource);

		// For each group
		stacks.groups.forEach((group, groupIndex) => {

			// Find out if editor is active in group
			const activeEditor = group.activeEditor;
			const activeResource = toResource(activeEditor, { supportSideBySide: true });
			activeInGroups[groupIndex] = (activeResource && activeResource.toString() === resource.toString());

			// Find index of editor in group
			indexInGroups[groupIndex] = -1;
			group.getEditors().forEach((editor, editorIndex) => {
				const editorResource = toResource(editor, { supportSideBySide: true });
				if (editorResource && editorResource.toString() === resource.toString()) {
					indexInGroups[groupIndex] = editorIndex;
					return;
				}
			});
		});

		mapUntitledToProperties[resource.toString()] = { encoding, indexInGroups, activeInGroups };
	});

	// Save all
	return textFileService.saveAll(saveAllArguments).then(results => {

		// Reopen saved untitled editors
		const untitledToReopen: { input: IResourceInput, position: Position }[] = [];

		results.results.forEach(result => {
202
			if (!result.success || result.source.scheme !== Schemas.untitled) {
I
isidor 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
				return;
			}

			const untitledProps = mapUntitledToProperties[result.source.toString()];
			if (!untitledProps) {
				return;
			}

			// For each position where the untitled file was opened
			untitledProps.indexInGroups.forEach((indexInGroup, index) => {
				if (indexInGroup >= 0) {
					untitledToReopen.push({
						input: {
							resource: result.target,
							encoding: untitledProps.encoding,
							options: {
								pinned: true,
								index: indexInGroup,
								preserveFocus: true,
								inactive: !untitledProps.activeInGroups[index]
							}
						},
						position: index
					});
				}
			});
		});

		if (untitledToReopen.length) {
			return editorService.openEditors(untitledToReopen).then(() => true);
		}

		return void 0;
	});
}
I
isidor 已提交
238

239
// Command registration
I
isidor 已提交
240

241 242
CommandsRegistry.registerCommand({
	id: REVERT_FILE_COMMAND_ID,
243
	handler: (accessor, resource: URI) => {
244 245
		const editorService = accessor.get(IWorkbenchEditorService);
		const textFileService = accessor.get(ITextFileService);
246
		const notificationService = accessor.get(INotificationService);
247 248
		const resources = getMultiSelectedResources(resource, accessor.get(IListService), editorService)
			.filter(resource => resource.scheme !== Schemas.untitled);
I
isidor 已提交
249

250
		if (resources.length) {
251
			return textFileService.revertAll(resources, { force: true }).then(null, error => {
I
isidor 已提交
252
				notificationService.error(nls.localize('genericRevertError', "Failed to revert '{0}': {1}", resources.map(r => basename(r.fsPath)).join(', '), toErrorMessage(error, false)));
253
			});
I
isidor 已提交
254 255
		}

256 257 258 259 260
		return TPromise.as(true);
	}
});

KeybindingsRegistry.registerCommandAndKeybindingRule({
261
	weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
262 263 264 265 266
	when: ExplorerFocusCondition,
	primary: KeyMod.CtrlCmd | KeyCode.Enter,
	mac: {
		primary: KeyMod.WinCtrl | KeyCode.Enter
	},
267
	id: OPEN_TO_SIDE_COMMAND_ID, handler: (accessor, resource: URI) => {
268
		const editorService = accessor.get(IWorkbenchEditorService);
B
Benjamin Pasero 已提交
269
		const editorGroupService = accessor.get(IEditorGroupService);
270
		const listService = accessor.get(IListService);
I
isidor 已提交
271
		const fileService = accessor.get(IFileService);
272
		const tree = listService.lastFocusedList;
273
		const resources = getMultiSelectedResources(resource, listService, editorService);
B
Benjamin Pasero 已提交
274 275
		const stacks = editorGroupService.getStacksModel();
		const activeGroup = stacks.activeGroup;
B
Benjamin Pasero 已提交
276

277 278 279
		// Remove highlight
		if (tree instanceof Tree) {
			tree.clearHighlight();
I
isidor 已提交
280 281
		}

282
		// Set side input
I
isidor 已提交
283
		if (resources.length) {
I
isidor 已提交
284 285
			return fileService.resolveFiles(resources.map(resource => ({ resource }))).then(resolved => {
				const editors = resolved.filter(r => r.success && !r.stat.isDirectory).map(r => ({
I
isidor 已提交
286
					input: {
I
isidor 已提交
287
						resource: r.stat.resource,
I
isidor 已提交
288
						options: { preserveFocus: false }
289
					}
I
isidor 已提交
290 291 292 293 294 295 296
				}));

				return editorService.openEditors(editors, true).then(() => {
					if (activeGroup) {
						editorGroupService.focusGroup(stacks.positionOfGroup(activeGroup) + 1);
					}
				});
B
Benjamin Pasero 已提交
297
			});
B
Benjamin Pasero 已提交
298 299 300
		}

		return TPromise.as(true);
301 302 303
	}
});

I
isidor 已提交
304 305
const COMPARE_WITH_SAVED_SCHEMA = 'showModifications';
let provider: FileOnDiskContentProvider;
306 307 308 309 310
KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: COMPARE_WITH_SAVED_COMMAND_ID,
	when: undefined,
	weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
	primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.KEY_D),
311
	handler: (accessor, resource: URI) => {
I
isidor 已提交
312 313 314 315 316 317 318
		if (!provider) {
			const instantiationService = accessor.get(IInstantiationService);
			const textModelService = accessor.get(ITextModelService);
			provider = instantiationService.createInstance(FileOnDiskContentProvider);
			textModelService.registerTextModelContentProvider(COMPARE_WITH_SAVED_SCHEMA, provider);
		}

319
		const editorService = accessor.get(IWorkbenchEditorService);
I
isidor 已提交
320
		resource = getResourceForCommand(resource, accessor.get(IListService), editorService);
I
isidor 已提交
321

322
		if (resource && resource.scheme === Schemas.file /* only files on disk supported for now */) {
323 324
			const name = paths.basename(resource.fsPath);
			const editorLabel = nls.localize('modifiedLabel', "{0} (on disk) ↔ {1}", name, name);
I
isidor 已提交
325

B
Benjamin Pasero 已提交
326
			return editorService.openEditor({ leftResource: URI.from({ scheme: COMPARE_WITH_SAVED_SCHEMA, path: resource.fsPath }), rightResource: resource, label: editorLabel }).then(() => void 0);
I
isidor 已提交
327 328
		}

329 330 331 332
		return TPromise.as(true);
	}
});

I
isidor 已提交
333 334
let globalResourceToCompare: URI;
let resourceSelectedForCompareContext: IContextKey<boolean>;
335 336
CommandsRegistry.registerCommand({
	id: SELECT_FOR_COMPARE_COMMAND_ID,
337
	handler: (accessor, resource: URI) => {
338 339 340 341 342 343
		const listService = accessor.get(IListService);
		const tree = listService.lastFocusedList;
		// Remove highlight
		if (tree instanceof Tree) {
			tree.clearHighlight();
			tree.DOMFocus();
I
isidor 已提交
344 345
		}

I
isidor 已提交
346
		globalResourceToCompare = getResourceForCommand(resource, listService, accessor.get(IWorkbenchEditorService));
I
isidor 已提交
347 348 349 350
		if (!resourceSelectedForCompareContext) {
			resourceSelectedForCompareContext = ResourceSelectedForCompareContext.bindTo(accessor.get(IContextKeyService));
		}
		resourceSelectedForCompareContext.set(true);
351 352 353
	}
});

I
isidor 已提交
354 355 356 357
CommandsRegistry.registerCommand({
	id: COMPARE_SELECTED_COMMAND_ID,
	handler: (accessor, resource: URI) => {
		const editorService = accessor.get(IWorkbenchEditorService);
358
		const resources = getMultiSelectedResources(resource, accessor.get(IListService), editorService);
I
isidor 已提交
359

I
isidor 已提交
360 361 362 363 364 365 366 367
		if (resources.length === 2) {
			return editorService.openEditor({
				leftResource: resources[0],
				rightResource: resources[1]
			});
		}

		return TPromise.as(true);
I
isidor 已提交
368 369 370
	}
});

371 372
CommandsRegistry.registerCommand({
	id: COMPARE_RESOURCE_COMMAND_ID,
373
	handler: (accessor, resource: URI) => {
374 375 376
		const editorService = accessor.get(IWorkbenchEditorService);
		const listService = accessor.get(IListService);
		const tree = listService.lastFocusedList;
B
Benjamin Pasero 已提交
377

378 379 380
		// Remove highlight
		if (tree instanceof Tree) {
			tree.clearHighlight();
I
isidor 已提交
381 382
		}

383 384
		return editorService.openEditor({
			leftResource: globalResourceToCompare,
I
isidor 已提交
385
			rightResource: getResourceForCommand(resource, listService, editorService)
B
Benjamin Pasero 已提交
386
		}).then(() => void 0);
387 388 389
	}
});

390
function revealResourcesInOS(resources: URI[], windowsService: IWindowsService, notificationService: INotificationService): void {
I
isidor 已提交
391 392
	if (resources.length) {
		sequence(resources.map(r => () => windowsService.showItemInFolder(paths.normalize(r.fsPath, true))));
I
isidor 已提交
393
	} else {
394
		notificationService.info(nls.localize('openFileToReveal', "Open a file first to reveal"));
I
isidor 已提交
395
	}
I
isidor 已提交
396
}
397 398 399
KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: REVEAL_IN_OS_COMMAND_ID,
	weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
400 401
	when: ExplorerFocusCondition,
	primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_R,
402 403 404
	win: {
		primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_R
	},
I
isidor 已提交
405 406
	handler: (accessor: ServicesAccessor, resource: URI) => {
		const resources = getMultiSelectedResources(resource, accessor.get(IListService), accessor.get(IWorkbenchEditorService));
407
		revealResourcesInOS(resources, accessor.get(IWindowsService), accessor.get(INotificationService));
I
isidor 已提交
408
	}
I
isidor 已提交
409
});
410 411 412 413
KeybindingsRegistry.registerCommandAndKeybindingRule({
	weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
	when: undefined,
	primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.KEY_R),
I
isidor 已提交
414
	id: 'workbench.action.files.revealActiveFileInWindows',
I
isidor 已提交
415 416 417 418
	handler: (accessor: ServicesAccessor, resource: URI) => {
		const editorService = accessor.get(IWorkbenchEditorService);
		const activeInput = editorService.getActiveEditorInput();
		const resources = activeInput && activeInput.getResource() ? [activeInput.getResource()] : [];
419
		revealResourcesInOS(resources, accessor.get(IWindowsService), accessor.get(INotificationService));
I
isidor 已提交
420
	}
421 422
});

423
function resourcesToClipboard(resources: URI[], clipboardService: IClipboardService, notificationService: INotificationService): void {
424
	if (resources.length) {
I
isidor 已提交
425
		const lineDelimiter = isWindows ? '\r\n' : '\n';
B
Benjamin Pasero 已提交
426
		const text = resources.map(r => r.scheme === Schemas.file ? labels.getPathLabel(r) : r.toString()).join(lineDelimiter);
427 428
		clipboardService.writeText(text);
	} else {
429
		notificationService.info(nls.localize('openFileToCopy', "Open a file first to copy its path"));
430
	}
I
isidor 已提交
431
}
432 433 434 435 436 437 438 439
KeybindingsRegistry.registerCommandAndKeybindingRule({
	weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
	when: ExplorerFocusCondition,
	primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_C,
	win: {
		primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_C
	},
	id: COPY_PATH_COMMAND_ID,
I
isidor 已提交
440 441
	handler: (accessor, resource: URI) => {
		const resources = getMultiSelectedResources(resource, accessor.get(IListService), accessor.get(IWorkbenchEditorService));
442
		resourcesToClipboard(resources, accessor.get(IClipboardService), accessor.get(INotificationService));
I
isidor 已提交
443
	}
444 445
});

446 447 448 449
KeybindingsRegistry.registerCommandAndKeybindingRule({
	weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
	when: undefined,
	primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.KEY_P),
450
	id: 'workbench.action.files.copyPathOfActiveFile',
I
isidor 已提交
451 452 453 454
	handler: (accessor, resource: URI) => {
		const editorService = accessor.get(IWorkbenchEditorService);
		const activeInput = editorService.getActiveEditorInput();
		const resources = activeInput && activeInput.getResource() ? [activeInput.getResource()] : [];
455
		resourcesToClipboard(resources, accessor.get(IClipboardService), accessor.get(INotificationService));
I
isidor 已提交
456
	}
457 458 459 460
});

CommandsRegistry.registerCommand({
	id: REVEAL_IN_EXPLORER_COMMAND_ID,
461
	handler: (accessor, resource: URI) => {
462 463
		const viewletService = accessor.get(IViewletService);
		const contextService = accessor.get(IWorkspaceContextService);
I
isidor 已提交
464
		resource = getResourceForCommand(resource, accessor.get(IListService), accessor.get(IWorkbenchEditorService));
465 466

		viewletService.openViewlet(VIEWLET_ID, false).then((viewlet: ExplorerViewlet) => {
467
			const isInsideWorkspace = contextService.isInsideWorkspace(resource);
468 469 470 471
			if (isInsideWorkspace) {
				const explorerView = viewlet.getExplorerView();
				if (explorerView) {
					explorerView.setExpanded(true);
472
					explorerView.select(resource, true);
473
				}
I
isidor 已提交
474
			} else {
475 476 477 478
				const openEditorsView = viewlet.getOpenEditorsView();
				if (openEditorsView) {
					openEditorsView.setExpanded(true);
				}
I
isidor 已提交
479
			}
480 481 482
		});
	}
});
I
isidor 已提交
483

484
KeybindingsRegistry.registerCommandAndKeybindingRule({
485
	id: SAVE_FILE_AS_COMMAND_ID,
486 487 488
	weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
	when: undefined,
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_S,
489
	handler: (accessor, resource: URI) => {
I
isidor 已提交
490 491 492
		const editorService = accessor.get(IWorkbenchEditorService);
		resource = getResourceForCommand(resource, accessor.get(IListService), editorService);
		return save(resource, true, editorService, accessor.get(IFileService), accessor.get(IUntitledEditorService), accessor.get(ITextFileService), accessor.get(IEditorGroupService));
493 494 495 496 497 498 499 500
	}
});

KeybindingsRegistry.registerCommandAndKeybindingRule({
	when: undefined,
	weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
	primary: KeyMod.CtrlCmd | KeyCode.KEY_S,
	id: SAVE_FILE_COMMAND_ID,
501
	handler: (accessor, resource: URI) => {
I
isidor 已提交
502
		const editorService = accessor.get(IWorkbenchEditorService);
503
		const resources = getMultiSelectedResources(resource, accessor.get(IListService), editorService);
504 505 506 507 508

		if (resources.length === 1) {
			// If only one resource is selected explictly call save since the behavior is a bit different than save all #41841
			return save(resources[0], false, editorService, accessor.get(IFileService), accessor.get(IUntitledEditorService), accessor.get(ITextFileService), accessor.get(IEditorGroupService));
		}
509
		return saveAll(resources, editorService, accessor.get(IUntitledEditorService), accessor.get(ITextFileService), accessor.get(IEditorGroupService));
510 511
	}
});
I
isidor 已提交
512

513 514
CommandsRegistry.registerCommand({
	id: SAVE_ALL_COMMAND_ID,
515
	handler: (accessor) => {
516 517 518 519 520 521
		return saveAll(true, accessor.get(IWorkbenchEditorService), accessor.get(IUntitledEditorService), accessor.get(ITextFileService), accessor.get(IEditorGroupService));
	}
});

CommandsRegistry.registerCommand({
	id: SAVE_ALL_IN_GROUP_COMMAND_ID,
522
	handler: (accessor, resource: URI, editorContext: IEditorCommandsContext) => {
I
isidor 已提交
523
		const contexts = getMultiSelectedEditorContexts(editorContext, accessor.get(IListService));
524
		const editorGroupService = accessor.get(IEditorGroupService);
525
		let saveAllArg: any;
I
isidor 已提交
526
		if (!contexts.length) {
527 528 529
			saveAllArg = true;
		} else {
			const fileService = accessor.get(IFileService);
I
isidor 已提交
530
			saveAllArg = [];
I
isidor 已提交
531
			contexts.forEach(context => {
532
				const editorGroup = editorGroupService.getStacksModel().getGroup(context.groupId);
I
isidor 已提交
533 534
				editorGroup.getEditors().forEach(editor => {
					const resource = toResource(editor, { supportSideBySide: true });
535
					if (resource && (resource.scheme === Schemas.untitled || fileService.canHandleResource(resource))) {
I
isidor 已提交
536 537 538
						saveAllArg.push(resource);
					}
				});
539
			});
540 541
		}

542 543 544
		return saveAll(saveAllArg, accessor.get(IWorkbenchEditorService), accessor.get(IUntitledEditorService), accessor.get(ITextFileService), accessor.get(IEditorGroupService));
	}
});
I
isidor 已提交
545

546 547
CommandsRegistry.registerCommand({
	id: SAVE_FILES_COMMAND_ID,
548
	handler: (accessor) => {
549 550 551
		return saveAll(false, accessor.get(IWorkbenchEditorService), accessor.get(IUntitledEditorService), accessor.get(ITextFileService), accessor.get(IEditorGroupService));
	}
});
552 553 554 555 556 557 558

CommandsRegistry.registerCommand({
	id: REMOVE_ROOT_FOLDER_COMMAND_ID,
	handler: (accessor, resource: URI) => {
		const workspaceEditingService = accessor.get(IWorkspaceEditingService);
		const contextService = accessor.get(IWorkspaceContextService);
		const workspace = contextService.getWorkspace();
559
		const resources = getMultiSelectedResources(resource, accessor.get(IListService), accessor.get(IWorkbenchEditorService)).filter(r =>
560 561 562 563 564 565 566
			// Need to verify resources are workspaces since multi selection can trigger this command on some non workspace resources
			workspace.folders.some(f => f.uri.toString() === r.toString())
		);

		return workspaceEditingService.removeFolders(resources);
	}
});