fileActions.ts 54.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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 'vs/css!./media/fileactions';
J
Johannes Rieken 已提交
9
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
10
import nls = require('vs/nls');
J
Johannes Rieken 已提交
11
import { isWindows, isLinux, isMacintosh } from 'vs/base/common/platform';
B
Benjamin Pasero 已提交
12
import { sequence, ITask, always } from 'vs/base/common/async';
E
Erich Gamma 已提交
13
import paths = require('vs/base/common/paths');
I
isidor 已提交
14
import resources = require('vs/base/common/resources');
E
Erich Gamma 已提交
15 16
import URI from 'vs/base/common/uri';
import errors = require('vs/base/common/errors');
J
Johannes Rieken 已提交
17
import { toErrorMessage } from 'vs/base/common/errorMessage';
E
Erich Gamma 已提交
18
import strings = require('vs/base/common/strings');
19
import severity from 'vs/base/common/severity';
E
Erich Gamma 已提交
20
import diagnostics = require('vs/base/common/diagnostics');
J
Johannes Rieken 已提交
21 22
import { Action, IAction } from 'vs/base/common/actions';
import { MessageType, IInputValidator } from 'vs/base/browser/ui/inputbox/inputBox';
23
import { ITree, IHighlightEvent } from 'vs/base/parts/tree/browser/tree';
J
Johannes Rieken 已提交
24
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
25
import { VIEWLET_ID, FileOnDiskContentProvider } from 'vs/workbench/parts/files/common/files';
26
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
27
import { IFileService, IFileStat } from 'vs/platform/files/common/files';
I
isidor 已提交
28
import { toResource } from 'vs/workbench/common/editor';
29
import { FileStat, Model, NewStatPlaceholder } from 'vs/workbench/parts/files/common/explorerModel';
30 31
import { ExplorerView } from 'vs/workbench/parts/files/electron-browser/views/explorerView';
import { ExplorerViewlet } from 'vs/workbench/parts/files/electron-browser/explorerViewlet';
J
Johannes Rieken 已提交
32 33 34 35
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { CollapseAction } from 'vs/workbench/browser/viewlet';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
B
Benjamin Pasero 已提交
36
import { IQuickOpenService } from 'vs/platform/quickOpen/common/quickOpen';
B
Benjamin Pasero 已提交
37
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
38
import { IUntitledResourceInput } from 'vs/platform/editor/common/editor';
39
import { IInstantiationService, IConstructorSignature2, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
40
import { IMessageService, IMessageWithAction, IConfirmation, Severity, CancelAction, IConfirmationResult } from 'vs/platform/message/common/message';
41
import { IModel } from 'vs/editor/common/editorCommon';
42
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
43
import { IWindowsService } from 'vs/platform/windows/common/windows';
44
import { withFocusedFilesExplorer, REVERT_FILE_COMMAND_ID, COMPARE_WITH_SAVED_COMMAND_ID, REVEAL_IN_OS_COMMAND_ID, COPY_PATH_COMMAND_ID, REVEAL_IN_EXPLORER_COMMAND_ID, SAVE_FILE_AS_COMMAND_ID, SAVE_FILE_COMMAND_ID, SAVE_FILE_LABEL, SAVE_FILE_AS_LABEL, SAVE_ALL_COMMAND_ID, SAVE_ALL_LABEL, SAVE_ALL_IN_GROUP_COMMAND_ID, SAVE_FILES_COMMAND_ID, SAVE_FILES_LABEL, COMPARE_WITH_SAVED_SCHEMA } from 'vs/workbench/parts/files/electron-browser/fileCommands';
M
Max Furman 已提交
45
import { ITextModelService, ITextModelContentProvider } from 'vs/editor/common/services/resolverService';
46
import { IConfigurationService, ConfigurationTarget } from 'vs/platform/configuration/common/configuration';
B
Benjamin Pasero 已提交
47
import { once } from 'vs/base/common/event';
M
Max Furman 已提交
48 49 50
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { IModeService } from 'vs/editor/common/services/modeService';
import { IModelService } from 'vs/editor/common/services/modelService';
51
import { ICommandService } from 'vs/platform/commands/common/commands';
M
Max Furman 已提交
52

E
Erich Gamma 已提交
53 54 55 56 57 58 59 60 61 62 63
export interface IEditableData {
	action: IAction;
	validator: IInputValidator;
}

export interface IFileViewletState {
	getEditableData(stat: IFileStat): IEditableData;
	setEditable(stat: IFileStat, editableData: IEditableData): void;
	clearEditable(stat: IFileStat): void;
}

64
export class BaseErrorReportingAction extends Action {
E
Erich Gamma 已提交
65 66 67 68

	constructor(
		id: string,
		label: string,
69
		private _messageService: IMessageService
E
Erich Gamma 已提交
70 71 72 73 74 75 76 77
	) {
		super(id, label);
	}

	public get messageService() {
		return this._messageService;
	}

78
	protected onError(error: any): void {
79 80
		if (error.message === 'string') {
			error = error.message;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		}

		this._messageService.show(Severity.Error, toErrorMessage(error, false));
	}

	protected onErrorWithRetry(error: any, retry: () => TPromise<any>, extraAction?: Action): void {
		const actions = [
			new Action(this.id, nls.localize('retry', "Retry"), null, true, () => retry()),
			CancelAction
		];

		if (extraAction) {
			actions.unshift(extraAction);
		}

		const errorWithRetry: IMessageWithAction = {
			actions,
			message: toErrorMessage(error, false)
		};

		this._messageService.show(Severity.Error, errorWithRetry);
	}
}

export class BaseFileAction extends BaseErrorReportingAction {
	private _element: FileStat;

	constructor(
		id: string,
		label: string,
		@IFileService private _fileService: IFileService,
		@IMessageService _messageService: IMessageService,
		@ITextFileService private _textFileService: ITextFileService
	) {
		super(id, label, _messageService);

		this.enabled = false;
	}

E
Erich Gamma 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	public get fileService() {
		return this._fileService;
	}

	public get textFileService() {
		return this._textFileService;
	}

	public get element() {
		return this._element;
	}

	public set element(element: FileStat) {
		this._element = element;
	}

	_isEnabled(): boolean {
		return true;
	}

	_updateEnablement(): void {
B
Benjamin Pasero 已提交
141
		this.enabled = !!(this._fileService && this._isEnabled());
E
Erich Gamma 已提交
142 143 144 145 146
	}
}

export class TriggerRenameFileAction extends BaseFileAction {

M
Matt Bierner 已提交
147
	public static readonly ID = 'renameFile';
E
Erich Gamma 已提交
148 149 150 151 152 153 154 155 156 157 158 159

	private tree: ITree;
	private renameAction: BaseRenameAction;

	constructor(
		tree: ITree,
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
		@ITextFileService textFileService: ITextFileService,
		@IInstantiationService instantiationService: IInstantiationService
	) {
B
Benjamin Pasero 已提交
160
		super(TriggerRenameFileAction.ID, nls.localize('rename', "Rename"), fileService, messageService, textFileService);
E
Erich Gamma 已提交
161 162 163 164 165 166 167 168 169 170 171

		this.tree = tree;
		this.element = element;
		this.renameAction = instantiationService.createInstance(RenameFileAction, element);
		this._updateEnablement();
	}

	public validateFileName(parent: IFileStat, name: string): string {
		return this.renameAction.validateFileName(this.element.parent, name);
	}

172
	public run(context?: any): TPromise<any> {
E
Erich Gamma 已提交
173
		if (!context) {
174
			return TPromise.wrapError(new Error('No context provided to BaseEnableFileRenameAction.'));
E
Erich Gamma 已提交
175 176
		}

177
		const viewletState = <IFileViewletState>context.viewletState;
E
Erich Gamma 已提交
178
		if (!viewletState) {
179
			return TPromise.wrapError(new Error('Invalid viewlet state provided to BaseEnableFileRenameAction.'));
E
Erich Gamma 已提交
180 181
		}

182
		const stat = <IFileStat>context.stat;
E
Erich Gamma 已提交
183
		if (!stat) {
184
			return TPromise.wrapError(new Error('Invalid stat provided to BaseEnableFileRenameAction.'));
E
Erich Gamma 已提交
185 186 187 188 189
		}

		viewletState.setEditable(stat, {
			action: this.renameAction,
			validator: (value) => {
190
				const message = this.validateFileName(this.element.parent, value);
E
Erich Gamma 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

				if (!message) {
					return null;
				}

				return {
					content: message,
					formatContent: true,
					type: MessageType.ERROR
				};
			}
		});

		this.tree.refresh(stat, false).then(() => {
			this.tree.setHighlight(stat);

207
			const unbind = this.tree.onDidChangeHighlight((e: IHighlightEvent) => {
E
Erich Gamma 已提交
208 209 210
				if (!e.highlight) {
					viewletState.clearEditable(stat);
					this.tree.refresh(stat).done(null, errors.onUnexpectedError);
A
Alex Dima 已提交
211
					unbind.dispose();
E
Erich Gamma 已提交
212 213 214
				}
			});
		}).done(null, errors.onUnexpectedError);
M
Matt Bierner 已提交
215

216
		return void 0;
E
Erich Gamma 已提交
217 218 219 220 221 222 223 224 225 226 227
	}
}

export abstract class BaseRenameAction extends BaseFileAction {

	constructor(
		id: string,
		label: string,
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
228
		@ITextFileService textFileService: ITextFileService
E
Erich Gamma 已提交
229
	) {
B
Benjamin Pasero 已提交
230
		super(id, label, fileService, messageService, textFileService);
E
Erich Gamma 已提交
231 232 233 234

		this.element = element;
	}

235
	public run(context?: any): TPromise<any> {
E
Erich Gamma 已提交
236
		if (!context) {
237
			return TPromise.wrapError(new Error('No context provided to BaseRenameFileAction.'));
E
Erich Gamma 已提交
238 239 240 241
		}

		let name = <string>context.value;
		if (!name) {
242
			return TPromise.wrapError(new Error('No new name provided to BaseRenameFileAction.'));
E
Erich Gamma 已提交
243 244 245 246
		}

		// Automatically trim whitespaces and trailing dots to produce nice file names
		name = getWellFormedFileName(name);
247
		const existingName = getWellFormedFileName(this.element.name);
E
Erich Gamma 已提交
248

249
		// Return early if name is invalid or didn't change
E
Erich Gamma 已提交
250
		if (name === existingName || this.validateFileName(this.element.parent, name)) {
A
Alex Dima 已提交
251
			return TPromise.as(null);
E
Erich Gamma 已提交
252 253 254
		}

		// Call function and Emit Event through viewer
255
		const promise = this.runAction(name).then(null, (error: any) => {
E
Erich Gamma 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
			this.onError(error);
		});

		return promise;
	}

	public validateFileName(parent: IFileStat, name: string): string {
		let source = this.element.name;
		let target = name;

		if (!isLinux) { // allow rename of same file also when case differs (e.g. Game.js => game.js)
			source = source.toLowerCase();
			target = target.toLowerCase();
		}

		if (getWellFormedFileName(source) === getWellFormedFileName(target)) {
			return null;
		}

		return validateFileName(parent, name, false);
	}

278
	public abstract runAction(newName: string): TPromise<any>;
E
Erich Gamma 已提交
279 280
}

281
class RenameFileAction extends BaseRenameAction {
E
Erich Gamma 已提交
282

M
Matt Bierner 已提交
283
	public static readonly ID = 'workbench.files.action.renameFile';
E
Erich Gamma 已提交
284 285 286 287 288

	constructor(
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
289 290
		@ITextFileService textFileService: ITextFileService,
		@IBackupFileService private backupFileService: IBackupFileService
E
Erich Gamma 已提交
291
	) {
B
Benjamin Pasero 已提交
292
		super(RenameFileAction.ID, nls.localize('rename', "Rename"), element, fileService, messageService, textFileService);
E
Erich Gamma 已提交
293 294 295 296

		this._updateEnablement();
	}

297
	public runAction(newName: string): TPromise<any> {
I
isidor 已提交
298
		const dirty = this.textFileService.getDirty().filter(d => resources.isEqualOrParent(d, this.element.resource, !isLinux /* ignorecase */));
299 300
		const dirtyRenamed: URI[] = [];
		return TPromise.join(dirty.map(d => {
301 302 303
			let renamed: URI;

			// If the dirty file itself got moved, just reparent it to the target folder
B
Benjamin Pasero 已提交
304
			const targetPath = paths.join(this.element.parent.resource.path, newName);
I
isidor 已提交
305
			if (this.element.resource.toString() === d.toString()) {
B
Benjamin Pasero 已提交
306
				renamed = this.element.parent.resource.with({ path: targetPath });
E
Erich Gamma 已提交
307 308
			}

309 310
			// Otherwise, a parent of the dirty resource got moved, so we have to reparent more complicated. Example:
			else {
B
Benjamin Pasero 已提交
311
				renamed = this.element.parent.resource.with({ path: paths.join(targetPath, d.path.substr(this.element.resource.path.length + 1)) });
E
Erich Gamma 已提交
312 313
			}

314
			dirtyRenamed.push(renamed);
315

316
			const model = this.textFileService.models.get(d);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

			return this.backupFileService.backupResource(renamed, model.getValue(), model.getVersionId());
		}))

			// 2. soft revert all dirty since we have backed up their contents
			.then(() => this.textFileService.revertAll(dirty, { soft: true /* do not attempt to load content from disk */ }))

			// 3.) run the rename operation
			.then(() => this.fileService.rename(this.element.resource, newName).then(null, (error: Error) => {
				return TPromise.join(dirtyRenamed.map(d => this.backupFileService.discardResourceBackup(d))).then(() => {
					this.onErrorWithRetry(error, () => this.runAction(newName));
				});
			}))

			// 4.) resolve those that were dirty to load their previous dirty contents from disk
			.then(() => {
333
				return TPromise.join(dirtyRenamed.map(t => this.textFileService.models.loadOrCreate(t)));
E
Erich Gamma 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
			});
	}
}

/* Base New File/Folder Action */
export class BaseNewAction extends BaseFileAction {
	private presetFolder: FileStat;
	private tree: ITree;
	private isFile: boolean;
	private renameAction: BaseRenameAction;

	constructor(
		id: string,
		label: string,
		tree: ITree,
		isFile: boolean,
		editableAction: BaseRenameAction,
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
354
		@ITextFileService textFileService: ITextFileService
E
Erich Gamma 已提交
355
	) {
B
Benjamin Pasero 已提交
356
		super(id, label, fileService, messageService, textFileService);
E
Erich Gamma 已提交
357 358 359 360 361 362 363 364 365 366

		if (element) {
			this.presetFolder = element.isDirectory ? element : element.parent;
		}

		this.tree = tree;
		this.isFile = isFile;
		this.renameAction = editableAction;
	}

367
	public run(context?: any): TPromise<any> {
E
Erich Gamma 已提交
368
		if (!context) {
369
			return TPromise.wrapError(new Error('No context provided to BaseNewAction.'));
E
Erich Gamma 已提交
370 371
		}

372
		const viewletState = <IFileViewletState>context.viewletState;
E
Erich Gamma 已提交
373
		if (!viewletState) {
374
			return TPromise.wrapError(new Error('Invalid viewlet state provided to BaseNewAction.'));
E
Erich Gamma 已提交
375 376
		}

I
isidor 已提交
377
		let folder = this.presetFolder;
E
Erich Gamma 已提交
378
		if (!folder) {
379
			const focus = <FileStat>this.tree.getFocus();
E
Erich Gamma 已提交
380 381 382
			if (focus) {
				folder = focus.isDirectory ? focus : focus.parent;
			} else {
I
isidor 已提交
383
				const input: FileStat | Model = this.tree.getInput();
384
				folder = input instanceof Model ? input.roots[0] : input;
E
Erich Gamma 已提交
385 386 387 388
			}
		}

		if (!folder) {
389
			return TPromise.wrapError(new Error('Invalid parent folder to create.'));
E
Erich Gamma 已提交
390 391 392 393
		}

		return this.tree.reveal(folder, 0.5).then(() => {
			return this.tree.expand(folder).then(() => {
394
				const stat = NewStatPlaceholder.addNewStatPlaceholder(folder, !this.isFile);
E
Erich Gamma 已提交
395 396 397 398 399 400

				this.renameAction.element = stat;

				viewletState.setEditable(stat, {
					action: this.renameAction,
					validator: (value) => {
401
						const message = this.renameAction.validateFileName(folder, value);
E
Erich Gamma 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

						if (!message) {
							return null;
						}

						return {
							content: message,
							formatContent: true,
							type: MessageType.ERROR
						};
					}
				});

				return this.tree.refresh(folder).then(() => {
					return this.tree.expand(folder).then(() => {
						return this.tree.reveal(stat, 0.5).then(() => {
							this.tree.setHighlight(stat);

420
							const unbind = this.tree.onDidChangeHighlight((e: IHighlightEvent) => {
E
Erich Gamma 已提交
421 422 423
								if (!e.highlight) {
									stat.destroy();
									this.tree.refresh(folder).done(null, errors.onUnexpectedError);
A
Alex Dima 已提交
424
									unbind.dispose();
E
Erich Gamma 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
								}
							});
						});
					});
				});
			});
		});
	}
}

/* New File */
export class NewFileAction extends BaseNewAction {

	constructor(
		tree: ITree,
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
		@ITextFileService textFileService: ITextFileService,
		@IInstantiationService instantiationService: IInstantiationService
	) {
446
		super('explorer.newFile', nls.localize('newFile', "New File"), tree, true, instantiationService.createInstance(CreateFileAction, element), null, fileService, messageService, textFileService);
E
Erich Gamma 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

		this.class = 'explorer-action new-file';
		this._updateEnablement();
	}
}

/* New Folder */
export class NewFolderAction extends BaseNewAction {

	constructor(
		tree: ITree,
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
		@ITextFileService textFileService: ITextFileService,
		@IInstantiationService instantiationService: IInstantiationService
	) {
464
		super('explorer.newFolder', nls.localize('newFolder', "New Folder"), tree, false, instantiationService.createInstance(CreateFolderAction, element), null, fileService, messageService, textFileService);
E
Erich Gamma 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477

		this.class = 'explorer-action new-folder';
		this._updateEnablement();
	}
}

export abstract class BaseGlobalNewAction extends Action {
	private toDispose: Action;

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService,
B
fix npe  
Benjamin Pasero 已提交
478 479
		@IInstantiationService private instantiationService: IInstantiationService,
		@IMessageService private messageService: IMessageService
E
Erich Gamma 已提交
480 481 482 483
	) {
		super(id, label);
	}

484
	public run(): TPromise<any> {
485
		return this.viewletService.openViewlet(VIEWLET_ID, true).then((viewlet) => {
486
			return TPromise.timeout(100).then(() => { // use a timeout to prevent the explorer from revealing the active file
E
Erich Gamma 已提交
487 488
				viewlet.focus();

489 490
				const explorer = <ExplorerViewlet>viewlet;
				const explorerView = explorer.getExplorerView();
E
Erich Gamma 已提交
491

B
fix npe  
Benjamin Pasero 已提交
492 493 494 495 496
				// Not having a folder opened
				if (!explorerView) {
					return this.messageService.show(Severity.Info, nls.localize('openFolderFirst', "Open a folder first to create files or folders within."));
				}

E
Erich Gamma 已提交
497
				if (!explorerView.isExpanded()) {
498
					explorerView.setExpanded(true);
E
Erich Gamma 已提交
499 500
				}

501
				const action = this.toDispose = this.instantiationService.createInstance(this.getAction(), explorerView.getViewer(), null);
E
Erich Gamma 已提交
502 503 504 505 506 507

				return explorer.getActionRunner().run(action);
			});
		});
	}

508
	protected abstract getAction(): IConstructorSignature2<ITree, IFileStat, Action>;
E
Erich Gamma 已提交
509 510 511 512 513 514 515 516 517 518 519 520

	public dispose(): void {
		super.dispose();

		if (this.toDispose) {
			this.toDispose.dispose();
			this.toDispose = null;
		}
	}
}

/* Create new file from anywhere: Open untitled */
521
export class GlobalNewUntitledFileAction extends Action {
M
Matt Bierner 已提交
522 523
	public static readonly ID = 'workbench.action.files.newUntitledFile';
	public static readonly LABEL = nls.localize('newUntitledFile', "New Untitled File");
E
Erich Gamma 已提交
524 525 526 527

	constructor(
		id: string,
		label: string,
528
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
E
Erich Gamma 已提交
529 530 531 532
	) {
		super(id, label);
	}

533
	public run(): TPromise<any> {
534
		return this.editorService.openEditor({ options: { pinned: true } } as IUntitledResourceInput); // untitled are always pinned
E
Erich Gamma 已提交
535 536 537
	}
}

538 539
/* Create new file from anywhere */
export class GlobalNewFileAction extends BaseGlobalNewAction {
M
Matt Bierner 已提交
540 541
	public static readonly ID = 'explorer.newFile';
	public static readonly LABEL = nls.localize('newFile', "New File");
542 543 544 545 546 547

	protected getAction(): IConstructorSignature2<ITree, IFileStat, Action> {
		return NewFileAction;
	}
}

E
Erich Gamma 已提交
548 549
/* Create new folder from anywhere */
export class GlobalNewFolderAction extends BaseGlobalNewAction {
M
Matt Bierner 已提交
550 551
	public static readonly ID = 'explorer.newFolder';
	public static readonly LABEL = nls.localize('newFolder', "New Folder");
E
Erich Gamma 已提交
552

553
	protected getAction(): IConstructorSignature2<ITree, IFileStat, Action> {
E
Erich Gamma 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
		return NewFolderAction;
	}
}

/* Create New File/Folder (only used internally by explorerViewer) */
export abstract class BaseCreateAction extends BaseRenameAction {

	public validateFileName(parent: IFileStat, name: string): string {
		if (this.element instanceof NewStatPlaceholder) {
			return validateFileName(parent, name, false);
		}

		return super.validateFileName(parent, name);
	}
}

/* Create New File (only used internally by explorerViewer) */
export class CreateFileAction extends BaseCreateAction {

M
Matt Bierner 已提交
573 574
	public static readonly ID = 'workbench.files.action.createFileFromExplorer';
	public static readonly LABEL = nls.localize('createNewFile', "New File");
E
Erich Gamma 已提交
575 576 577 578

	constructor(
		element: FileStat,
		@IFileService fileService: IFileService,
579
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
E
Erich Gamma 已提交
580
		@IMessageService messageService: IMessageService,
581
		@ITextFileService textFileService: ITextFileService
E
Erich Gamma 已提交
582
	) {
B
Benjamin Pasero 已提交
583
		super(CreateFileAction.ID, CreateFileAction.LABEL, element, fileService, messageService, textFileService);
E
Erich Gamma 已提交
584 585 586 587

		this._updateEnablement();
	}

588
	public runAction(fileName: string): TPromise<any> {
589 590
		const resource = this.element.parent.resource;
		return this.fileService.createFile(resource.with({ path: paths.join(resource.path, fileName) })).then(stat => {
591 592
			return this.editorService.openEditor({ resource: stat.resource, options: { pinned: true } });
		}, (error) => {
E
Erich Gamma 已提交
593 594 595 596 597 598 599 600
			this.onErrorWithRetry(error, () => this.runAction(fileName));
		});
	}
}

/* Create New Folder (only used internally by explorerViewer) */
export class CreateFolderAction extends BaseCreateAction {

M
Matt Bierner 已提交
601 602
	public static readonly ID = 'workbench.files.action.createFolderFromExplorer';
	public static readonly LABEL = nls.localize('createNewFolder', "New Folder");
E
Erich Gamma 已提交
603 604 605 606 607

	constructor(
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
608
		@ITextFileService textFileService: ITextFileService
E
Erich Gamma 已提交
609
	) {
B
Benjamin Pasero 已提交
610
		super(CreateFolderAction.ID, CreateFolderAction.LABEL, null, fileService, messageService, textFileService);
E
Erich Gamma 已提交
611 612 613 614

		this._updateEnablement();
	}

615
	public runAction(fileName: string): TPromise<any> {
616 617
		const resource = this.element.parent.resource;
		return this.fileService.createFolder(resource.with({ path: paths.join(resource.path, fileName) })).then(null, (error) => {
E
Erich Gamma 已提交
618 619 620 621 622 623
			this.onErrorWithRetry(error, () => this.runAction(fileName));
		});
	}
}

export class BaseDeleteFileAction extends BaseFileAction {
624

625
	private static readonly CONFIRM_DELETE_SETTING_KEY = 'explorer.confirmDelete';
626

E
Erich Gamma 已提交
627 628
	private tree: ITree;
	private useTrash: boolean;
629
	private skipConfirm: boolean;
E
Erich Gamma 已提交
630 631 632 633 634 635 636 637 638

	constructor(
		id: string,
		label: string,
		tree: ITree,
		element: FileStat,
		useTrash: boolean,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
639
		@ITextFileService textFileService: ITextFileService,
640
		@IConfigurationService private configurationService: IConfigurationService
E
Erich Gamma 已提交
641
	) {
B
Benjamin Pasero 已提交
642
		super(id, label, fileService, messageService, textFileService);
E
Erich Gamma 已提交
643 644 645 646 647 648 649 650

		this.tree = tree;
		this.element = element;
		this.useTrash = useTrash && !paths.isUNC(element.resource.fsPath); // on UNC shares there is no trash

		this._updateEnablement();
	}

651
	public run(context?: any): TPromise<any> {
E
Erich Gamma 已提交
652 653 654 655 656 657

		// Remove highlight
		if (this.tree) {
			this.tree.clearHighlight();
		}

658
		// Read context
659 660 661 662 663 664 665 666
		if (context) {
			if (context.event) {
				const bypassTrash = (isMacintosh && context.event.altKey) || (!isMacintosh && context.event.shiftKey);
				if (bypassTrash) {
					this.useTrash = false;
				}
			} else if (typeof context.useTrash === 'boolean') {
				this.useTrash = context.useTrash;
667 668 669
			}
		}

670 671 672 673 674 675 676 677
		let primaryButton: string;
		if (this.useTrash) {
			primaryButton = isWindows ? nls.localize('deleteButtonLabelRecycleBin', "&&Move to Recycle Bin") : nls.localize({ key: 'deleteButtonLabelTrash', comment: ['&& denotes a mnemonic'] }, "&&Move to Trash");
		} else {
			primaryButton = nls.localize({ key: 'deleteButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Delete");
		}

		// Handle dirty
678
		let confirmDirtyPromise: TPromise<boolean> = TPromise.as(true);
I
isidor 已提交
679
		const dirty = this.textFileService.getDirty().filter(d => resources.isEqualOrParent(d, this.element.resource, !isLinux /* ignorecase */));
680 681 682 683 684 685 686 687
		if (dirty.length) {
			let message: string;
			if (this.element.isDirectory) {
				if (dirty.length === 1) {
					message = nls.localize('dirtyMessageFolderOneDelete', "You are deleting a folder with unsaved changes in 1 file. Do you want to continue?");
				} else {
					message = nls.localize('dirtyMessageFolderDelete', "You are deleting a folder with unsaved changes in {0} files. Do you want to continue?", dirty.length);
				}
688
			} else {
689
				message = nls.localize('dirtyMessageFileDelete', "You are deleting a file with unsaved changes. Do you want to continue?");
690
			}
E
Erich Gamma 已提交
691

692
			confirmDirtyPromise = this.messageService.confirm({
693 694 695 696
				message,
				type: 'warning',
				detail: nls.localize('dirtyWarning', "Your changes will be lost if you don't save them."),
				primaryButton
697 698 699 700
			}).then(confirmed => {
				if (!confirmed) {
					return false;
				}
701

702 703 704
				this.skipConfirm = true; // since we already asked for confirmation
				return this.textFileService.revertAll(dirty).then(() => true);
			});
E
Erich Gamma 已提交
705 706
		}

707
		// Check if file is dirty in editor and save it to avoid data loss
708 709 710 711 712 713
		return confirmDirtyPromise.then(confirmed => {
			if (!confirmed) {
				return null;
			}

			let confirmDeletePromise: TPromise<IConfirmationResult>;
714

715
			// Check if we need to ask for confirmation at all
S
Sandeep Somavarapu 已提交
716
			if (this.skipConfirm || (this.useTrash && this.configurationService.getValue<boolean>(BaseDeleteFileAction.CONFIRM_DELETE_SETTING_KEY) === false)) {
717
				confirmDeletePromise = TPromise.as({ confirmed: true } as IConfirmationResult);
718
			}
B
Benjamin Pasero 已提交
719

720 721
			// Confirm for moving to trash
			else if (this.useTrash) {
722
				confirmDeletePromise = this.messageService.confirmWithCheckbox({
723 724 725 726 727 728 729 730
					message: this.element.isDirectory ? nls.localize('confirmMoveTrashMessageFolder', "Are you sure you want to delete '{0}' and its contents?", this.element.name) : nls.localize('confirmMoveTrashMessageFile', "Are you sure you want to delete '{0}'?", this.element.name),
					detail: isWindows ? nls.localize('undoBin', "You can restore from the recycle bin.") : nls.localize('undoTrash', "You can restore from the trash."),
					primaryButton,
					checkbox: {
						label: nls.localize('doNotAskAgain', "Do not ask me again")
					},
					type: 'question'
				});
E
Erich Gamma 已提交
731 732
			}

733 734
			// Confirm for deleting permanently
			else {
735
				confirmDeletePromise = this.messageService.confirmWithCheckbox({
736 737 738 739 740 741 742
					message: this.element.isDirectory ? nls.localize('confirmDeleteMessageFolder', "Are you sure you want to permanently delete '{0}' and its contents?", this.element.name) : nls.localize('confirmDeleteMessageFile', "Are you sure you want to permanently delete '{0}'?", this.element.name),
					detail: nls.localize('irreversible', "This action is irreversible!"),
					primaryButton,
					type: 'warning'
				});
			}

743
			return confirmDeletePromise.then(confirmation => {
E
Erich Gamma 已提交
744

745 746
				// Check for confirmation checkbox
				let updateConfirmSettingsPromise: TPromise<void> = TPromise.as(void 0);
747
				if (confirmation.confirmed && confirmation.checkboxChecked === true) {
748
					updateConfirmSettingsPromise = this.configurationService.updateValue(BaseDeleteFileAction.CONFIRM_DELETE_SETTING_KEY, false, ConfigurationTarget.USER);
749
				}
E
Erich Gamma 已提交
750

751
				return updateConfirmSettingsPromise.then(() => {
B
Benjamin Pasero 已提交
752

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
					// Check for confirmation
					if (!confirmation.confirmed) {
						return TPromise.as(null);
					}

					// Call function
					const servicePromise = this.fileService.del(this.element.resource, this.useTrash).then(() => {
						if (this.element.parent) {
							this.tree.setFocus(this.element.parent); // move focus to parent
						}
					}, (error: any) => {

						// Allow to retry
						let extraAction: Action;
						if (this.useTrash) {
							extraAction = new Action('permanentDelete', nls.localize('permDelete', "Delete Permanently"), null, true, () => { this.useTrash = false; this.skipConfirm = true; return this.run(); });
						}

						this.onErrorWithRetry(error, () => this.run(), extraAction);
772

773 774 775 776 777 778 779
						// Focus back to tree
						this.tree.DOMFocus();
					});

					return servicePromise;
				});
			});
780
		});
E
Erich Gamma 已提交
781 782 783 784 785
	}
}

/* Move File/Folder to trash */
export class MoveFileToTrashAction extends BaseDeleteFileAction {
M
Matt Bierner 已提交
786
	public static readonly ID = 'moveFileToTrash';
E
Erich Gamma 已提交
787 788 789 790 791 792

	constructor(
		tree: ITree,
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
793
		@ITextFileService textFileService: ITextFileService,
794
		@IConfigurationService configurationService: IConfigurationService
E
Erich Gamma 已提交
795
	) {
796
		super(MoveFileToTrashAction.ID, nls.localize('delete', "Delete"), tree, element, true, fileService, messageService, textFileService, configurationService);
E
Erich Gamma 已提交
797 798 799 800 801 802
	}
}

/* Import File */
export class ImportFileAction extends BaseFileAction {

M
Matt Bierner 已提交
803
	public static readonly ID = 'workbench.files.action.importFile';
E
Erich Gamma 已提交
804 805 806 807 808 809 810
	private tree: ITree;

	constructor(
		tree: ITree,
		element: FileStat,
		clazz: string,
		@IFileService fileService: IFileService,
811
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
E
Erich Gamma 已提交
812
		@IMessageService messageService: IMessageService,
813
		@ITextFileService textFileService: ITextFileService
E
Erich Gamma 已提交
814
	) {
B
Benjamin Pasero 已提交
815
		super(ImportFileAction.ID, nls.localize('importFiles', "Import Files"), fileService, messageService, textFileService);
E
Erich Gamma 已提交
816 817 818 819 820 821 822 823 824 825 826

		this.tree = tree;
		this.element = element;

		if (clazz) {
			this.class = clazz;
		}

		this._updateEnablement();
	}

827
	public run(resources: URI[]): TPromise<any> {
828
		const importPromise = TPromise.as(null).then(() => {
829
			if (resources && resources.length > 0) {
E
Erich Gamma 已提交
830 831 832 833 834 835

				// Find parent for import
				let targetElement: FileStat;
				if (this.element) {
					targetElement = this.element;
				} else {
I
isidor 已提交
836 837
					const input: FileStat | Model = this.tree.getInput();
					targetElement = this.tree.getFocus() || (input instanceof Model ? input.roots[0] : input);
E
Erich Gamma 已提交
838 839 840 841 842 843 844 845 846 847
				}

				if (!targetElement.isDirectory) {
					targetElement = targetElement.parent;
				}

				// Resolve target to check for name collisions and ask user
				return this.fileService.resolveFile(targetElement.resource).then((targetStat: IFileStat) => {

					// Check for name collisions
848
					const targetNames: { [name: string]: IFileStat } = {};
E
Erich Gamma 已提交
849 850 851 852
					targetStat.children.forEach((child) => {
						targetNames[isLinux ? child.name : child.name.toLowerCase()] = child;
					});

853
					let overwritePromise = TPromise.as(true);
854 855
					if (resources.some(resource => {
						return !!targetNames[isLinux ? paths.basename(resource.fsPath) : paths.basename(resource.fsPath).toLowerCase()];
E
Erich Gamma 已提交
856
					})) {
857
						const confirm: IConfirmation = {
E
Erich Gamma 已提交
858 859
							message: nls.localize('confirmOverwrite', "A file or folder with the same name already exists in the destination folder. Do you want to replace it?"),
							detail: nls.localize('irreversible', "This action is irreversible!"),
B
Benjamin Pasero 已提交
860 861
							primaryButton: nls.localize({ key: 'replaceButtonLabel', comment: ['&& denotes a mnemonic'] }, "&&Replace"),
							type: 'warning'
E
Erich Gamma 已提交
862 863
						};

864
						overwritePromise = this.messageService.confirm(confirm);
E
Erich Gamma 已提交
865 866
					}

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
					return overwritePromise.then(overwrite => {
						if (!overwrite) {
							return void 0;
						}

						// Run import in sequence
						const importPromisesFactory: ITask<TPromise<void>>[] = [];
						resources.forEach(resource => {
							importPromisesFactory.push(() => {
								const sourceFile = resource;
								const targetFile = targetElement.resource.with({ path: paths.join(targetElement.resource.path, paths.basename(sourceFile.path)) });

								// if the target exists and is dirty, make sure to revert it. otherwise the dirty contents
								// of the target file would replace the contents of the imported file. since we already
								// confirmed the overwrite before, this is OK.
								let revertPromise = TPromise.wrap(null);
								if (this.textFileService.isDirty(targetFile)) {
									revertPromise = this.textFileService.revertAll([targetFile], { soft: true });
								}
E
Erich Gamma 已提交
886

887 888 889 890 891 892 893 894 895
								return revertPromise.then(() => {
									return this.fileService.importFile(sourceFile, targetElement.resource).then(res => {

										// if we only import one file, just open it directly
										if (resources.length === 1) {
											this.editorService.openEditor({ resource: res.stat.resource, options: { pinned: true } }).done(null, errors.onUnexpectedError);
										}
									}, error => this.onError(error));
								});
E
Erich Gamma 已提交
896 897 898
							});
						});

899 900
						return sequence(importPromisesFactory);
					});
E
Erich Gamma 已提交
901 902
				});
			}
903 904

			return void 0;
E
Erich Gamma 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
		});

		return importPromise.then(() => {
			this.tree.clearHighlight();
		}, (error: any) => {
			this.onError(error);
			this.tree.clearHighlight();
		});
	}
}

// Copy File/Folder
let fileToCopy: FileStat;
export class CopyFileAction extends BaseFileAction {

M
Matt Bierner 已提交
920
	public static readonly ID = 'filesExplorer.copy';
E
Erich Gamma 已提交
921 922 923 924 925 926 927

	private tree: ITree;
	constructor(
		tree: ITree,
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
928
		@ITextFileService textFileService: ITextFileService
E
Erich Gamma 已提交
929
	) {
B
Benjamin Pasero 已提交
930
		super(CopyFileAction.ID, nls.localize('copyFile', "Copy"), fileService, messageService, textFileService);
E
Erich Gamma 已提交
931 932 933 934 935 936

		this.tree = tree;
		this.element = element;
		this._updateEnablement();
	}

937
	public run(): TPromise<any> {
E
Erich Gamma 已提交
938 939 940 941 942 943 944 945 946 947 948

		// Remember as file/folder to copy
		fileToCopy = this.element;

		// Remove highlight
		if (this.tree) {
			this.tree.clearHighlight();
		}

		this.tree.DOMFocus();

A
Alex Dima 已提交
949
		return TPromise.as(null);
E
Erich Gamma 已提交
950 951 952 953 954 955
	}
}

// Paste File/Folder
export class PasteFileAction extends BaseFileAction {

M
Matt Bierner 已提交
956
	public static readonly ID = 'filesExplorer.paste';
E
Erich Gamma 已提交
957 958 959 960 961 962 963 964 965 966 967

	private tree: ITree;

	constructor(
		tree: ITree,
		element: FileStat,
		@IFileService fileService: IFileService,
		@IMessageService messageService: IMessageService,
		@ITextFileService textFileService: ITextFileService,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
B
Benjamin Pasero 已提交
968
		super(PasteFileAction.ID, nls.localize('pasteFile', "Paste"), fileService, messageService, textFileService);
E
Erich Gamma 已提交
969 970

		this.tree = tree;
I
isidor 已提交
971 972 973 974 975
		this.element = element;
		if (!this.element) {
			const input: FileStat | Model = this.tree.getInput();
			this.element = input instanceof Model ? input.roots[0] : input;
		}
E
Erich Gamma 已提交
976 977 978 979 980 981 982 983 984 985 986
		this._updateEnablement();
	}

	_isEnabled(): boolean {

		// Need at least a file to copy
		if (!fileToCopy) {
			return false;
		}

		// Check if file was deleted or moved meanwhile
I
isidor 已提交
987
		const exists = fileToCopy.root.find(fileToCopy.resource);
E
Erich Gamma 已提交
988 989 990 991 992 993
		if (!exists) {
			fileToCopy = null;
			return false;
		}

		// Check if target is ancestor of pasted folder
I
isidor 已提交
994
		if (this.element.resource.toString() !== fileToCopy.resource.toString() && resources.isEqualOrParent(this.element.resource, fileToCopy.resource, !isLinux /* ignorecase */)) {
E
Erich Gamma 已提交
995 996 997 998 999 1000
			return false;
		}

		return true;
	}

1001
	public run(): TPromise<any> {
E
Erich Gamma 已提交
1002 1003 1004

		// Find target
		let target: FileStat;
1005
		if (this.element.resource.toString() === fileToCopy.resource.toString()) {
E
Erich Gamma 已提交
1006 1007 1008 1009 1010 1011
			target = this.element.parent;
		} else {
			target = this.element.isDirectory ? this.element : this.element.parent;
		}

		// Reuse duplicate action
1012
		const pasteAction = this.instantiationService.createInstance(DuplicateFileAction, this.tree, fileToCopy, target);
E
Erich Gamma 已提交
1013 1014 1015 1016 1017 1018 1019

		return pasteAction.run().then(() => {
			this.tree.DOMFocus();
		});
	}
}

1020 1021 1022
export const pasteIntoFocusedFilesExplorerViewItem = (accessor: ServicesAccessor) => {
	const instantiationService = accessor.get(IInstantiationService);

1023
	withFocusedFilesExplorer(accessor).then(res => {
1024 1025
		if (res) {
			const pasteAction = instantiationService.createInstance(PasteFileAction, res.tree, res.tree.getFocus());
1026 1027 1028 1029 1030 1031 1032
			if (pasteAction._isEnabled()) {
				pasteAction.run().done(null, errors.onUnexpectedError);
			}
		}
	});
};

E
Erich Gamma 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
// Duplicate File/Folder
export class DuplicateFileAction extends BaseFileAction {
	private tree: ITree;
	private target: IFileStat;

	constructor(
		tree: ITree,
		element: FileStat,
		target: FileStat,
		@IFileService fileService: IFileService,
1043
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
E
Erich Gamma 已提交
1044
		@IMessageService messageService: IMessageService,
1045
		@ITextFileService textFileService: ITextFileService
E
Erich Gamma 已提交
1046
	) {
B
Benjamin Pasero 已提交
1047
		super('workbench.files.action.duplicateFile', nls.localize('duplicateFile', "Duplicate"), fileService, messageService, textFileService);
E
Erich Gamma 已提交
1048 1049 1050 1051 1052 1053 1054

		this.tree = tree;
		this.element = element;
		this.target = (target && target.isDirectory) ? target : element.parent;
		this._updateEnablement();
	}

1055
	public run(): TPromise<any> {
E
Erich Gamma 已提交
1056 1057 1058 1059 1060 1061

		// Remove highlight
		if (this.tree) {
			this.tree.clearHighlight();
		}

1062
		// Copy File
1063 1064 1065 1066
		const result = this.fileService.copyFile(this.element.resource, this.findTarget()).then(stat => {
			if (!stat.isDirectory) {
				return this.editorService.openEditor({ resource: stat.resource, options: { pinned: true } });
			}
1067 1068

			return void 0;
1069
		}, error => this.onError(error));
E
Erich Gamma 已提交
1070 1071 1072 1073 1074 1075 1076

		return result;
	}

	private findTarget(): URI {
		let name = this.element.name;

I
isidor 已提交
1077
		let candidate = this.target.resource.with({ path: paths.join(this.target.resource.path, name) });
E
Erich Gamma 已提交
1078
		while (true) {
I
isidor 已提交
1079
			if (!this.element.root.find(candidate)) {
E
Erich Gamma 已提交
1080 1081 1082 1083
				break;
			}

			name = this.toCopyName(name, this.element.isDirectory);
I
isidor 已提交
1084
			candidate = this.target.resource.with({ path: paths.join(this.target.resource.path, name) });
E
Erich Gamma 已提交
1085 1086 1087 1088 1089 1090 1091 1092
		}

		return candidate;
	}

	private toCopyName(name: string, isFolder: boolean): string {

		// file.1.txt=>file.2.txt
1093 1094
		if (!isFolder && name.match(/(.*\.)(\d+)(\..*)$/)) {
			return name.replace(/(.*\.)(\d+)(\..*)$/, (match, g1?, g2?, g3?) => { return g1 + (parseInt(g2) + 1) + g3; });
E
Erich Gamma 已提交
1095 1096 1097
		}

		// file.txt=>file.1.txt
1098
		const lastIndexOfDot = name.lastIndexOf('.');
E
Erich Gamma 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
		if (!isFolder && lastIndexOfDot >= 0) {
			return strings.format('{0}.1{1}', name.substr(0, lastIndexOfDot), name.substr(lastIndexOfDot));
		}

		// folder.1=>folder.2
		if (isFolder && name.match(/(\d+)$/)) {
			return name.replace(/(\d+)$/, (match: string, ...groups: any[]) => { return String(parseInt(groups[0]) + 1); });
		}

		// file/folder=>file.1/folder.1
		return strings.format('{0}.1', name);
	}
}

// Global Compare with
export class GlobalCompareResourcesAction extends Action {

M
Matt Bierner 已提交
1116 1117
	public static readonly ID = 'workbench.files.action.compareFileWith';
	public static readonly LABEL = nls.localize('globalCompareFile', "Compare Active File With...");
E
Erich Gamma 已提交
1118 1119 1120 1121 1122 1123

	constructor(
		id: string,
		label: string,
		@IQuickOpenService private quickOpenService: IQuickOpenService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
1124
		@IMessageService private messageService: IMessageService,
B
Benjamin Pasero 已提交
1125
		@IEditorGroupService private editorGroupService: IEditorGroupService
E
Erich Gamma 已提交
1126 1127 1128 1129
	) {
		super(id, label);
	}

1130
	public run(): TPromise<any> {
B
Benjamin Pasero 已提交
1131 1132
		const activeInput = this.editorService.getActiveEditorInput();
		const activeResource = activeInput ? activeInput.getResource() : void 0;
1133
		if (activeResource) {
E
Erich Gamma 已提交
1134

B
Benjamin Pasero 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
			// Compare with next editor that opens
			const unbind = once(this.editorGroupService.onEditorOpening)(e => {
				const resource = e.input.getResource();
				if (resource) {
					e.prevent(() => {
						return this.editorService.openEditor({
							leftResource: activeResource,
							rightResource: resource
						});
					});
1145
				}
B
Benjamin Pasero 已提交
1146
			});
1147

B
Benjamin Pasero 已提交
1148 1149 1150
			// Bring up quick open
			this.quickOpenService.show('', { autoFocus: { autoFocusSecondEntry: true } }).then(() => {
				unbind.dispose(); // make sure to unbind if quick open is closing
E
Erich Gamma 已提交
1151 1152 1153 1154 1155
			});
		} else {
			this.messageService.show(Severity.Info, nls.localize('openFileToCompare', "Open a file first to compare it with another file."));
		}

A
Alex Dima 已提交
1156
		return TPromise.as(true);
E
Erich Gamma 已提交
1157 1158 1159 1160 1161 1162
	}
}

// Refresh Explorer Viewer
export class RefreshViewExplorerAction extends Action {

1163
	constructor(explorerView: ExplorerView, clazz: string) {
B
Benjamin Pasero 已提交
1164
		super('workbench.files.action.refreshFilesExplorer', nls.localize('refresh', "Refresh"), clazz, true, (context: any) => explorerView.refresh());
E
Erich Gamma 已提交
1165 1166 1167
	}
}

1168 1169 1170 1171 1172 1173 1174
export class SaveFileAction extends BaseErrorReportingAction {

	public static readonly ID = 'workbench.action.files.save';
	public static readonly LABEL = SAVE_FILE_LABEL;

	private resource: URI;

E
Erich Gamma 已提交
1175 1176 1177
	constructor(
		id: string,
		label: string,
1178 1179
		@ICommandService private commandService: ICommandService,
		@IMessageService messageService: IMessageService
E
Erich Gamma 已提交
1180
	) {
1181
		super(id, label, messageService);
E
Erich Gamma 已提交
1182 1183
	}

1184 1185 1186 1187
	public setResource(resource: URI): void {
		this.resource = resource;
	}

1188
	public run(context?: any): TPromise<boolean> {
1189
		return this.commandService.executeCommand(SAVE_FILE_COMMAND_ID, { resource: this.resource }).then(() => true, error => {
R
Ron Buckton 已提交
1190 1191 1192
			this.onError(error);
			return null;
		});
E
Erich Gamma 已提交
1193 1194 1195
	}
}

1196 1197 1198 1199 1200
export class SaveFileAsAction extends BaseErrorReportingAction {

	public static readonly ID = 'workbench.action.files.saveAs';
	public static readonly LABEL = SAVE_FILE_AS_LABEL;

E
Erich Gamma 已提交
1201 1202 1203 1204 1205
	private resource: URI;

	constructor(
		id: string,
		label: string,
1206 1207
		@ICommandService private commandService: ICommandService,
		@IMessageService messageService: IMessageService
E
Erich Gamma 已提交
1208 1209 1210 1211 1212 1213 1214 1215
	) {
		super(id, label, messageService);
	}

	public setResource(resource: URI): void {
		this.resource = resource;
	}

1216 1217 1218 1219 1220
	public run(context?: any): TPromise<boolean> {
		return this.commandService.executeCommand(SAVE_FILE_AS_COMMAND_ID, { resource: this.resource }).then(() => true, error => {
			this.onError(error);
			return null;
		});
E
Erich Gamma 已提交
1221 1222 1223
	}
}

1224
export abstract class BaseSaveAllAction extends BaseErrorReportingAction {
E
Erich Gamma 已提交
1225 1226 1227 1228 1229 1230 1231 1232
	private toDispose: IDisposable[];
	private lastIsDirty: boolean;

	constructor(
		id: string,
		label: string,
		@ITextFileService private textFileService: ITextFileService,
		@IUntitledEditorService private untitledEditorService: IUntitledEditorService,
I
isidor 已提交
1233
		@ICommandService protected commandService: ICommandService,
1234
		@IMessageService messageService: IMessageService,
E
Erich Gamma 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	) {
		super(id, label, messageService);

		this.toDispose = [];
		this.lastIsDirty = this.textFileService.isDirty();
		this.enabled = this.lastIsDirty;

		this.registerListeners();
	}

	protected abstract includeUntitled(): boolean;
I
isidor 已提交
1246
	protected abstract doRun(context: any): TPromise<any>;
E
Erich Gamma 已提交
1247 1248 1249 1250

	private registerListeners(): void {

		// listen to files being changed locally
1251 1252 1253 1254
		this.toDispose.push(this.textFileService.models.onModelsDirty(e => this.updateEnablement(true)));
		this.toDispose.push(this.textFileService.models.onModelsSaved(e => this.updateEnablement(false)));
		this.toDispose.push(this.textFileService.models.onModelsReverted(e => this.updateEnablement(false)));
		this.toDispose.push(this.textFileService.models.onModelsSaveError(e => this.updateEnablement(true)));
E
Erich Gamma 已提交
1255 1256

		if (this.includeUntitled()) {
B
Benjamin Pasero 已提交
1257
			this.toDispose.push(this.untitledEditorService.onDidChangeDirty(resource => this.updateEnablement(this.untitledEditorService.isDirty(resource))));
E
Erich Gamma 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
		}
	}

	private updateEnablement(isDirty: boolean): void {
		if (this.lastIsDirty !== isDirty) {
			this.enabled = this.textFileService.isDirty();
			this.lastIsDirty = this.enabled;
		}
	}

1268 1269 1270 1271 1272 1273 1274
	public run(context?: any): TPromise<boolean> {
		return this.doRun(context).then(() => true, error => {
			this.onError(error);
			return null;
		});
	}

E
Erich Gamma 已提交
1275
	public dispose(): void {
J
Joao Moreno 已提交
1276
		this.toDispose = dispose(this.toDispose);
E
Erich Gamma 已提交
1277 1278 1279 1280 1281 1282 1283

		super.dispose();
	}
}

export class SaveAllAction extends BaseSaveAllAction {

M
Matt Bierner 已提交
1284
	public static readonly ID = 'workbench.action.files.saveAll';
I
isidor 已提交
1285
	public static readonly LABEL = SAVE_ALL_LABEL;
E
Erich Gamma 已提交
1286 1287 1288 1289 1290

	public get class(): string {
		return 'explorer-action save-all';
	}

I
isidor 已提交
1291 1292
	protected doRun(context: any): TPromise<any> {
		return this.commandService.executeCommand(SAVE_ALL_COMMAND_ID);
1293 1294 1295 1296 1297 1298 1299 1300 1301
	}

	protected includeUntitled(): boolean {
		return true;
	}
}

export class SaveAllInGroupAction extends BaseSaveAllAction {

M
Matt Bierner 已提交
1302
	public static readonly ID = 'workbench.files.action.saveAllInGroup';
1303
	public static readonly LABEL = nls.localize('saveAllInGroup', "Save All in Group");
1304 1305 1306 1307 1308

	public get class(): string {
		return 'explorer-action save-all';
	}

I
isidor 已提交
1309 1310
	protected doRun(context: any): TPromise<any> {
		return this.commandService.executeCommand(SAVE_ALL_IN_GROUP_COMMAND_ID);
1311 1312
	}

E
Erich Gamma 已提交
1313 1314 1315 1316 1317 1318 1319
	protected includeUntitled(): boolean {
		return true;
	}
}

export class SaveFilesAction extends BaseSaveAllAction {

M
Matt Bierner 已提交
1320
	public static readonly ID = 'workbench.action.files.saveFiles';
I
isidor 已提交
1321
	public static readonly LABEL = SAVE_FILES_LABEL;
E
Erich Gamma 已提交
1322

I
isidor 已提交
1323 1324
	protected doRun(context: any): TPromise<any> {
		return this.commandService.executeCommand(SAVE_FILES_COMMAND_ID, false);
1325 1326
	}

E
Erich Gamma 已提交
1327 1328 1329 1330 1331 1332 1333
	protected includeUntitled(): boolean {
		return false;
	}
}

export class RevertFileAction extends Action {

M
Matt Bierner 已提交
1334 1335
	public static readonly ID = 'workbench.action.files.revert';
	public static readonly LABEL = nls.localize('revert', "Revert File");
E
Erich Gamma 已提交
1336 1337 1338 1339 1340 1341

	private resource: URI;

	constructor(
		id: string,
		label: string,
1342
		@ICommandService private commandService: ICommandService
E
Erich Gamma 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	) {
		super(id, label);

		this.enabled = true;
	}

	public setResource(resource: URI): void {
		this.resource = resource;
	}

1353
	public run(): TPromise<any> {
I
isidor 已提交
1354
		return this.commandService.executeCommand(REVERT_FILE_COMMAND_ID, { resource: this.resource });
E
Erich Gamma 已提交
1355 1356 1357
	}
}

1358
export class FocusOpenEditorsView extends Action {
1359

M
Matt Bierner 已提交
1360 1361
	public static readonly ID = 'workbench.files.action.focusOpenEditorsView';
	public static readonly LABEL = nls.localize({ key: 'focusOpenEditors', comment: ['Open is an adjective'] }, "Focus on Open Editors View");
1362 1363 1364 1365 1366 1367 1368 1369 1370

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
		super(id, label);
	}

1371
	public run(): TPromise<any> {
1372
		return this.viewletService.openViewlet(VIEWLET_ID, true).then((viewlet: ExplorerViewlet) => {
I
isidor 已提交
1373 1374
			const openEditorsView = viewlet.getOpenEditorsView();
			if (openEditorsView) {
1375
				openEditorsView.setExpanded(true);
I
isidor 已提交
1376
				openEditorsView.getList().domFocus();
I
isidor 已提交
1377
			}
1378 1379 1380 1381
		});
	}
}

1382 1383
export class FocusFilesExplorer extends Action {

M
Matt Bierner 已提交
1384 1385
	public static readonly ID = 'workbench.files.action.focusFilesExplorer';
	public static readonly LABEL = nls.localize('focusFilesExplorer', "Focus on Files Explorer");
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
1396
		return this.viewletService.openViewlet(VIEWLET_ID, true).then((viewlet: ExplorerViewlet) => {
1397 1398
			const view = viewlet.getExplorerView();
			if (view) {
1399
				view.setExpanded(true);
1400 1401 1402 1403 1404 1405
				view.getViewer().DOMFocus();
			}
		});
	}
}

1406 1407
export class ShowActiveFileInExplorer extends Action {

M
Matt Bierner 已提交
1408 1409
	public static readonly ID = 'workbench.files.action.showActiveFileInExplorer';
	public static readonly LABEL = nls.localize('showInExplorer', "Reveal Active File in Side Bar");
1410 1411 1412 1413 1414

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
I
isidor 已提交
1415 1416
		@IMessageService private messageService: IMessageService,
		@ICommandService private commandService: ICommandService
1417 1418 1419 1420 1421
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
1422 1423
		const resource = toResource(this.editorService.getActiveEditorInput(), { supportSideBySide: true });
		if (resource) {
I
isidor 已提交
1424
			this.commandService.executeCommand(REVEAL_IN_EXPLORER_COMMAND_ID, { resource });
1425 1426 1427 1428 1429 1430 1431 1432
		} else {
			this.messageService.show(severity.Info, nls.localize('openFileToShow', "Open a file first to show it in the explorer"));
		}

		return TPromise.as(true);
	}
}

1433 1434
export class CollapseExplorerView extends Action {

M
Matt Bierner 已提交
1435 1436
	public static readonly ID = 'workbench.files.action.collapseExplorerFolders';
	public static readonly LABEL = nls.localize('collapseExplorerFolders', "Collapse Folders in Explorer");
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		return this.viewletService.openViewlet(VIEWLET_ID, true).then((viewlet: ExplorerViewlet) => {
			const explorerView = viewlet.getExplorerView();
			if (explorerView) {
				const viewer = explorerView.getViewer();
				if (viewer) {
					const action = new CollapseAction(viewer, true, null);
					action.run().done();
					action.dispose();
				}
			}
		});
	}
}

export class RefreshExplorerView extends Action {

M
Matt Bierner 已提交
1463 1464
	public static readonly ID = 'workbench.files.action.refreshFilesExplorer';
	public static readonly LABEL = nls.localize('refreshExplorer', "Refresh Explorer");
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		return this.viewletService.openViewlet(VIEWLET_ID, true).then((viewlet: ExplorerViewlet) => {
			const explorerView = viewlet.getExplorerView();
			if (explorerView) {
				explorerView.refresh();
			}
		});
	}
}

1484 1485
export class ShowOpenedFileInNewWindow extends Action {

M
Matt Bierner 已提交
1486 1487
	public static readonly ID = 'workbench.action.files.showOpenedFileInNewWindow';
	public static readonly LABEL = nls.localize('openFileInNewWindow', "Open Active File in New Window");
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

	constructor(
		id: string,
		label: string,
		@IWindowsService private windowsService: IWindowsService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IMessageService private messageService: IMessageService
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
		const fileResource = toResource(this.editorService.getActiveEditorInput(), { supportSideBySide: true, filter: 'file' });
		if (fileResource) {
1502
			this.windowsService.openWindow([fileResource.fsPath], { forceNewWindow: true, forceOpenWorkspaceAsFile: true });
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
		} else {
			this.messageService.show(severity.Info, nls.localize('openFileToShowInNewWindow', "Open a file first to open in new window"));
		}

		return TPromise.as(true);
	}
}

export class RevealInOSAction extends Action {

M
Matt Bierner 已提交
1513
	public static readonly LABEL = isWindows ? nls.localize('revealInWindows', "Reveal in Explorer") : isMacintosh ? nls.localize('revealInMac', "Reveal in Finder") : nls.localize('openContainer', "Open Containing Folder");
1514 1515 1516

	constructor(
		private resource: URI,
I
isidor 已提交
1517
		@ICommandService private commandService: ICommandService
1518
	) {
1519
		super('revealFileInOS', RevealInOSAction.LABEL);
1520 1521 1522 1523 1524

		this.order = 45;
	}

	public run(): TPromise<any> {
I
isidor 已提交
1525
		return this.commandService.executeCommand(REVEAL_IN_OS_COMMAND_ID, { resource: this.resource });
1526 1527 1528 1529 1530
	}
}

export class GlobalRevealInOSAction extends Action {

M
Matt Bierner 已提交
1531 1532
	public static readonly ID = 'workbench.action.files.revealActiveFileInWindows';
	public static readonly LABEL = isWindows ? nls.localize('revealActiveFileInWindows', "Reveal Active File in Windows Explorer") : (isMacintosh ? nls.localize('revealActiveFileInMac', "Reveal Active File in Finder") : nls.localize('openActiveFileContainer', "Open Containing Folder of Active File"));
1533 1534 1535 1536

	constructor(
		id: string,
		label: string,
I
isidor 已提交
1537
		@ICommandService private commandService: ICommandService
1538 1539 1540 1541 1542
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
I
isidor 已提交
1543
		return this.commandService.executeCommand(REVEAL_IN_OS_COMMAND_ID);
1544 1545 1546
	}
}

1547 1548
export class CopyPathAction extends Action {

M
Matt Bierner 已提交
1549
	public static readonly LABEL = nls.localize('copyPath', "Copy Path");
1550 1551 1552

	constructor(
		private resource: URI,
I
isidor 已提交
1553
		@ICommandService private commandService: ICommandService
1554
	) {
1555
		super('copyFilePath', CopyPathAction.LABEL);
1556 1557 1558 1559 1560

		this.order = 140;
	}

	public run(): TPromise<any> {
I
isidor 已提交
1561
		return this.commandService.executeCommand(COPY_PATH_COMMAND_ID, { resource: this.resource });
1562 1563 1564 1565 1566
	}
}

export class GlobalCopyPathAction extends Action {

M
Matt Bierner 已提交
1567 1568
	public static readonly ID = 'workbench.action.files.copyPathOfActiveFile';
	public static readonly LABEL = nls.localize('copyPathOfActive', "Copy Path of Active File");
1569 1570 1571 1572

	constructor(
		id: string,
		label: string,
I
isidor 已提交
1573
		@ICommandService private commandService: ICommandService
1574 1575 1576 1577 1578
	) {
		super(id, label);
	}

	public run(): TPromise<any> {
I
isidor 已提交
1579
		return this.commandService.executeCommand(COPY_PATH_COMMAND_ID);
1580 1581 1582
	}
}

E
Erich Gamma 已提交
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
export function validateFileName(parent: IFileStat, name: string, allowOverwriting: boolean = false): string {

	// Produce a well formed file name
	name = getWellFormedFileName(name);

	// Name not provided
	if (!name || name.length === 0 || /^\s+$/.test(name)) {
		return nls.localize('emptyFileNameError', "A file or folder name must be provided.");
	}

	// Do not allow to overwrite existing file
	if (!allowOverwriting) {
		if (parent.children && parent.children.some((c) => {
			if (isLinux) {
				return c.name === name;
			}

			return c.name.toLowerCase() === name.toLowerCase();
		})) {
			return nls.localize('fileNameExistsError', "A file or folder **{0}** already exists at this location. Please choose a different name.", name);
		}
	}

1606 1607
	// Invalid File name
	if (!paths.isValidBasename(name)) {
1608
		return nls.localize('invalidFileNameError', "The name **{0}** is not valid as a file or folder name. Please choose a different name.", trimLongName(name));
E
Erich Gamma 已提交
1609 1610 1611 1612
	}

	// Max length restriction (on Windows)
	if (isWindows) {
1613
		const fullPathLength = name.length + parent.resource.fsPath.length + 1 /* path segment */;
E
Erich Gamma 已提交
1614
		if (fullPathLength > 255) {
1615
			return nls.localize('filePathTooLongError', "The name **{0}** results in a path that is too long. Please choose a shorter name.", trimLongName(name));
E
Erich Gamma 已提交
1616 1617 1618 1619 1620 1621
		}
	}

	return null;
}

1622 1623 1624 1625 1626 1627 1628 1629
function trimLongName(name: string): string {
	if (name && name.length > 255) {
		return `${name.substr(0, 255)}...`;
	}

	return name;
}

E
Erich Gamma 已提交
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
export function getWellFormedFileName(filename: string): string {
	if (!filename) {
		return filename;
	}

	// Trim whitespaces
	filename = strings.trim(strings.trim(filename, ' '), '\t');

	// Remove trailing dots
	filename = strings.rtrim(filename, '.');

	return filename;
}

1644
export class CompareWithSavedAction extends Action {
B
Benjamin Pasero 已提交
1645

M
Matt Bierner 已提交
1646 1647
	public static readonly ID = 'workbench.files.action.compareWithSaved';
	public static readonly LABEL = nls.localize('compareWithSaved', "Compare Active File with Saved");
B
Benjamin Pasero 已提交
1648

1649
	private resource: URI;
1650
	private toDispose: IDisposable[];
1651 1652 1653 1654

	constructor(
		id: string,
		label: string,
I
isidor 已提交
1655
		@ICommandService private commandService: ICommandService,
1656
		@IInstantiationService instantiationService: IInstantiationService,
1657 1658 1659 1660 1661
		@ITextModelService textModelService: ITextModelService
	) {
		super(id, label);

		this.enabled = true;
1662 1663 1664 1665 1666
		this.toDispose = [];

		const provider = instantiationService.createInstance(FileOnDiskContentProvider);
		this.toDispose.push(provider);

I
isidor 已提交
1667
		const registrationDisposal = textModelService.registerTextModelContentProvider(COMPARE_WITH_SAVED_SCHEMA, provider);
1668
		this.toDispose.push(registrationDisposal);
1669 1670 1671 1672 1673 1674 1675
	}

	public setResource(resource: URI) {
		this.resource = resource;
	}

	public run(): TPromise<any> {
I
isidor 已提交
1676
		return this.commandService.executeCommand(COMPARE_WITH_SAVED_COMMAND_ID, { resource: this.resource });
1677 1678
	}

1679 1680 1681
	public dispose(): void {
		super.dispose();

1682
		this.toDispose = dispose(this.toDispose);
1683
	}
1684 1685
}

M
Max Furman 已提交
1686 1687
export class CompareWithClipboardAction extends Action {

M
Matt Bierner 已提交
1688 1689
	public static readonly ID = 'workbench.files.action.compareWithClipboard';
	public static readonly LABEL = nls.localize('compareWithClipboard', "Compare Active File with Clipboard");
M
Max Furman 已提交
1690

1691
	private static readonly SCHEME = 'clipboardCompare';
M
Max Furman 已提交
1692

B
Benjamin Pasero 已提交
1693
	private registrationDisposal: IDisposable;
M
Max Furman 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707

	constructor(
		id: string,
		label: string,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@ITextModelService private textModelService: ITextModelService,
	) {
		super(id, label);

		this.enabled = true;
	}

	public run(): TPromise<any> {
B
Benjamin Pasero 已提交
1708
		const resource: URI = toResource(this.editorService.getActiveEditorInput(), { supportSideBySide: true, filter: 'file' });
M
Max Furman 已提交
1709 1710 1711
		const provider = this.instantiationService.createInstance(ClipboardContentProvider);

		if (resource) {
B
Benjamin Pasero 已提交
1712 1713 1714 1715
			if (!this.registrationDisposal) {
				this.registrationDisposal = this.textModelService.registerTextModelContentProvider(CompareWithClipboardAction.SCHEME, provider);
			}

M
Max Furman 已提交
1716 1717 1718
			const name = paths.basename(resource.fsPath);
			const editorLabel = nls.localize('clipboardComparisonLabel', "Clipboard ↔ {0}", name);

B
Benjamin Pasero 已提交
1719 1720 1721 1722 1723
			const cleanUp = () => {
				this.registrationDisposal = dispose(this.registrationDisposal);
			};

			return always(this.editorService.openEditor({ leftResource: URI.from({ scheme: CompareWithClipboardAction.SCHEME, path: resource.fsPath }), rightResource: resource, label: editorLabel }), cleanUp);
M
Max Furman 已提交
1724 1725 1726 1727 1728 1729 1730 1731
		}

		return TPromise.as(true);
	}

	public dispose(): void {
		super.dispose();

B
Benjamin Pasero 已提交
1732
		this.registrationDisposal = dispose(this.registrationDisposal);
M
Max Furman 已提交
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
	}
}

class ClipboardContentProvider implements ITextModelContentProvider {
	constructor(
		@IClipboardService private clipboardService: IClipboardService,
		@IModeService private modeService: IModeService,
		@IModelService private modelService: IModelService
	) { }

	provideTextContent(resource: URI): TPromise<IModel> {
		const model = this.modelService.createModel(this.clipboardService.readText(), this.modeService.getOrCreateMode('text/plain'), resource);
B
Benjamin Pasero 已提交
1745

M
Max Furman 已提交
1746 1747 1748 1749
		return TPromise.as(model);
	}
}

E
Erich Gamma 已提交
1750 1751 1752
// Diagnostics support
let diag: (...args: any[]) => void;
if (!diag) {
1753
	diag = diagnostics.register('FileActionsDiagnostics', function (...args: any[]) {
E
Erich Gamma 已提交
1754 1755
		console.log(args[1] + ' - ' + args[0] + ' (time: ' + args[2].getTime() + ' [' + args[2].toUTCString() + '])');
	});
J
Johannes Rieken 已提交
1756
}