saveErrorHandler.ts 14.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8 9 10 11
import nls = require('vs/nls');
import errors = require('vs/base/common/errors');
import paths = require('vs/base/common/paths');
import {Action} from 'vs/base/common/actions';
12
import URI from 'vs/base/common/uri';
13
import product from 'vs/platform/product';
B
Benjamin Pasero 已提交
14
import {EditorModel} from 'vs/workbench/common/editor';
15
import {guessMimeTypes} from 'vs/base/common/mime';
16
import {EditorInputAction} from 'vs/workbench/browser/parts/editor/baseEditor';
17 18 19
import {ResourceEditorInput} from 'vs/workbench/common/editor/resourceEditorInput';
import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput';
import {DiffEditorModel} from 'vs/workbench/common/editor/diffEditorModel';
20
import {FileEditorInput} from 'vs/workbench/parts/files/common/editors/fileEditorInput';
E
Erich Gamma 已提交
21
import {SaveFileAsAction, RevertFileAction, SaveFileAction} from 'vs/workbench/parts/files/browser/fileActions';
22
import {IFileService, IFileOperationResult, FileOperationResult} from 'vs/platform/files/common/files';
23
import {TextFileEditorModel, ISaveErrorHandler} from 'vs/workbench/parts/files/common/editors/textFileEditorModel';
E
Erich Gamma 已提交
24 25
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IEventService} from 'vs/platform/event/common/event';
A
Alex Dima 已提交
26
import {EventType as FileEventType, TextFileChangeEvent, ITextFileService} from 'vs/workbench/parts/files/common/files';
E
Erich Gamma 已提交
27 28
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IMessageService, IMessageWithAction, Severity, CancelAction} from 'vs/platform/message/common/message';
29 30
import {IModeService} from 'vs/editor/common/services/modeService';
import {IModelService} from 'vs/editor/common/services/modelService';
E
Erich Gamma 已提交
31 32 33

// A handler for save error happening with conflict resolution actions
export class SaveErrorHandler implements ISaveErrorHandler {
34
	private messages: { [resource: string]: () => void };
E
Erich Gamma 已提交
35 36 37

	constructor(
		@IMessageService private messageService: IMessageService,
38
		@IEventService private eventService: IEventService,
E
Erich Gamma 已提交
39 40
		@IInstantiationService private instantiationService: IInstantiationService
	) {
41 42 43 44 45 46
		this.messages = Object.create(null);

		this.registerListeners();
	}

	private registerListeners(): void {
A
Alex Dima 已提交
47 48
		this.eventService.addListener2(FileEventType.FILE_SAVED, (e: TextFileChangeEvent) => this.onFileSavedOrReverted(e.resource));
		this.eventService.addListener2(FileEventType.FILE_REVERTED, (e: TextFileChangeEvent) => this.onFileSavedOrReverted(e.resource));
49 50 51 52 53 54 55 56
	}

	private onFileSavedOrReverted(resource: URI): void {
		const hideMessage = this.messages[resource.toString()];
		if (hideMessage) {
			hideMessage();
			this.messages[resource.toString()] = void 0;
		}
E
Erich Gamma 已提交
57 58 59 60
	}

	public onSaveError(error: any, model: TextFileEditorModel): void {
		let message: IMessageWithAction;
61
		const resource = model.getResource();
E
Erich Gamma 已提交
62 63 64 65 66 67 68 69

		// Dirty write prevention
		if ((<IFileOperationResult>error).fileOperationResult === FileOperationResult.FILE_MODIFIED_SINCE) {
			message = this.instantiationService.createInstance(ResolveSaveConflictMessage, model, null);
		}

		// Any other save error
		else {
70 71
			const isReadonly = (<IFileOperationResult>error).fileOperationResult === FileOperationResult.FILE_READ_ONLY;
			const actions: Action[] = [];
E
Erich Gamma 已提交
72

73 74 75 76
			// Save As
			actions.push(new Action('workbench.files.action.saveAs', SaveFileAsAction.LABEL, null, true, () => {
				const saveAsAction = this.instantiationService.createInstance(SaveFileAsAction, SaveFileAsAction.ID, SaveFileAsAction.LABEL);
				saveAsAction.setResource(resource);
77
				saveAsAction.run().done(() => saveAsAction.dispose(), errors.onUnexpectedError);
78

79
				return TPromise.as(true);
80 81 82 83 84 85
			}));

			// Discard
			actions.push(new Action('workbench.files.action.discard', nls.localize('discard', "Discard"), null, true, () => {
				const revertFileAction = this.instantiationService.createInstance(RevertFileAction, RevertFileAction.ID, RevertFileAction.LABEL);
				revertFileAction.setResource(resource);
86
				revertFileAction.run().done(() => revertFileAction.dispose(), errors.onUnexpectedError);
87

88
				return TPromise.as(true);
89
			}));
E
Erich Gamma 已提交
90 91 92 93 94

			// Retry
			if (isReadonly) {
				actions.push(new Action('workbench.files.action.overwrite', nls.localize('overwrite', "Overwrite"), null, true, () => {
					if (!model.isDisposed()) {
95
						model.save(true /* overwrite readonly */).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
96 97
					}

A
Alex Dima 已提交
98
					return TPromise.as(true);
E
Erich Gamma 已提交
99 100 101
				}));
			} else {
				actions.push(new Action('workbench.files.action.retry', nls.localize('retry', "Retry"), null, true, () => {
102
					const saveFileAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL);
103
					saveFileAction.setResource(resource);
104
					saveFileAction.run().done(() => saveFileAction.dispose(), errors.onUnexpectedError);
E
Erich Gamma 已提交
105

106
					return TPromise.as(true);
E
Erich Gamma 已提交
107 108 109
				}));
			}

110 111
			// Cancel
			actions.push(CancelAction);
E
Erich Gamma 已提交
112 113 114

			let errorMessage: string;
			if (isReadonly) {
115
				errorMessage = nls.localize('readonlySaveError', "Failed to save '{0}': File is write protected. Select 'Overwrite' to remove protection.", paths.basename(resource.fsPath));
E
Erich Gamma 已提交
116
			} else {
117
				errorMessage = nls.localize('genericSaveError', "Failed to save '{0}': {1}", paths.basename(resource.fsPath), errors.toErrorMessage(error, false));
E
Erich Gamma 已提交
118 119 120 121
			}

			message = {
				message: errorMessage,
122
				actions
E
Erich Gamma 已提交
123 124 125
			};
		}

126 127
		// Show message and keep function to hide in case the file gets saved/reverted
		this.messages[model.getResource().toString()] = this.messageService.show(Severity.Error, message);
E
Erich Gamma 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141
	}
}

// Save conflict resolution editor input
export class ConflictResolutionDiffEditorInput extends DiffEditorInput {

	public static ID = 'workbench.editors.files.conflictResolutionDiffEditorInput';

	private model: TextFileEditorModel;

	constructor(
		model: TextFileEditorModel,
		name: string,
		description: string,
142
		originalInput: FileOnDiskEditorInput,
143
		modifiedInput: FileEditorInput
E
Erich Gamma 已提交
144 145 146 147 148 149 150 151 152 153
	) {
		super(name, description, originalInput, modifiedInput);

		this.model = model;
	}

	public getModel(): TextFileEditorModel {
		return this.model;
	}

154
	public getTypeId(): string {
E
Erich Gamma 已提交
155 156 157 158
		return ConflictResolutionDiffEditorInput.ID;
	}
}

159
export class FileOnDiskEditorInput extends ResourceEditorInput {
160 161 162
	private fileResource: URI;
	private lastModified: number;
	private mime: string;
163
	private createdEditorModel: boolean;
164 165 166 167 168 169 170

	constructor(
		fileResource: URI,
		mime: string,
		name: string,
		description: string,
		@IModelService modelService: IModelService,
J
ops  
Johannes Rieken 已提交
171
		@IModeService private modeService: IModeService,
172
		@IInstantiationService instantiationService: IInstantiationService,
A
Alex Dima 已提交
173 174
		@IFileService private fileService: IFileService,
		@ITextFileService private textFileService: ITextFileService
175 176 177 178
	) {
		// We create a new resource URI here that is different from the file resource because we represent the state of
		// the file as it is on disk and not as it is (potentially cached) in Code. That allows us to have a different
		// model for the left-hand comparision compared to the conflicting one in Code to the right.
179
		super(name, description, URI.from({ scheme: 'disk', path: fileResource.fsPath }), modelService, instantiationService);
180 181 182 183 184 185 186 187 188 189 190 191

		this.fileResource = fileResource;
		this.mime = mime;
	}

	public getLastModified(): number {
		return this.lastModified;
	}

	public resolve(refresh?: boolean): TPromise<EditorModel> {

		// Make sure our file from disk is resolved up to date
A
Alex Dima 已提交
192
		return this.textFileService.resolveTextContent(this.fileResource).then(content => {
193 194
			this.lastModified = content.mtime;

195
			const codeEditorModel = this.modelService.getModel(this.resource);
196
			if (!codeEditorModel) {
J
Johannes Rieken 已提交
197
				this.modelService.createModel(content.value, this.modeService.getOrCreateMode(this.mime), this.resource);
198
				this.createdEditorModel = true;
199
			} else {
A
Alex Dima 已提交
200
				codeEditorModel.setValueFromRawText(content.value);
201 202 203 204 205
			}

			return super.resolve(refresh);
		});
	}
206 207 208 209 210 211 212 213 214

	public dispose(): void {
		if (this.createdEditorModel) {
			this.modelService.destroyModel(this.resource);
			this.createdEditorModel = false;
		}

		super.dispose();
	}
215 216
}

217 218 219 220 221 222 223 224
const pendingResolveSaveConflictMessages: Function[] = [];
function clearPendingResolveSaveConflictMessages(): void {
	while (pendingResolveSaveConflictMessages.length > 0) {
		pendingResolveSaveConflictMessages.pop()();
	}
}

// A message with action to resolve a save conflict
E
Erich Gamma 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
class ResolveSaveConflictMessage implements IMessageWithAction {
	public message: string;
	public actions: Action[];

	private model: TextFileEditorModel;

	constructor(
		model: TextFileEditorModel,
		message: string,
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		this.model = model;

240
		const resource = model.getResource();
E
Erich Gamma 已提交
241 242 243
		if (message) {
			this.message = message;
		} else {
244
			this.message = nls.localize('staleSaveError', "Failed to save '{0}': The content on disk is newer. Click on **Compare** to compare your version with the one on disk.", paths.basename(resource.fsPath));
E
Erich Gamma 已提交
245 246 247 248 249
		}

		this.actions = [
			new Action('workbench.files.action.resolveConflict', nls.localize('compareChanges', "Compare"), null, true, () => {
				if (!this.model.isDisposed()) {
250 251 252
					const mime = guessMimeTypes(resource.fsPath).join(', ');
					const originalInput = this.instantiationService.createInstance(FileOnDiskEditorInput, resource, mime, paths.basename(resource.fsPath), resource.fsPath);
					const modifiedInput = this.instantiationService.createInstance(FileEditorInput, resource, mime, void 0);
253
					const conflictInput = this.instantiationService.createInstance(ConflictResolutionDiffEditorInput, this.model, nls.localize('saveConflictDiffLabel', "{0} (on disk) ↔ {1} (in {2})", modifiedInput.getName(), modifiedInput.getName(), product.nameLong), nls.localize('resolveSaveConflict', "Resolve save conflict"), originalInput, modifiedInput);
E
Erich Gamma 已提交
254

255
					return this.editorService.openEditor(conflictInput).then(() => {
E
Erich Gamma 已提交
256 257 258 259 260

						// We have to bring the model into conflict resolution mode to prevent subsequent save erros when the user makes edits
						this.model.setConflictResolutionMode();

						// Inform user
261
						pendingResolveSaveConflictMessages.push(this.messageService.show(Severity.Info, nls.localize('userGuide', "Use the actions in the editor tool bar to either **undo** your changes or **overwrite** the content on disk with your changes")));
E
Erich Gamma 已提交
262 263 264
					});
				}

A
Alex Dima 已提交
265
				return TPromise.as(true);
E
Erich Gamma 已提交
266 267 268 269 270 271
			})
		];
	}
}

// Accept changes to resolve a conflicting edit
272
export class AcceptLocalChangesAction extends EditorInputAction {
E
Erich Gamma 已提交
273 274 275 276 277 278 279
	private messagesToHide: { (): void; }[];

	constructor(
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
280
		super('workbench.files.action.acceptLocalChanges', nls.localize('acceptLocalChanges', "Use local changes and overwrite disk contents"), 'conflict-editor-action accept-changes');
E
Erich Gamma 已提交
281 282 283 284

		this.messagesToHide = [];
	}

285
	public run(): TPromise<void> {
286
		const conflictInput = <ConflictResolutionDiffEditorInput>this.input;
287 288
		const model = conflictInput.getModel();
		const localModelValue = model.getValue();
E
Erich Gamma 已提交
289

290 291
		clearPendingResolveSaveConflictMessages(); // hide any previously shown message about how to use these actions

E
Erich Gamma 已提交
292 293
		// 1.) Get the diff editor model from cache (resolve(false)) to have access to the mtime of the file we currently show to the left
		return conflictInput.resolve(false).then((diffModel: DiffEditorModel) => {
294
			const knownLastModified = (<FileOnDiskEditorInput>conflictInput.originalInput).getLastModified();
E
Erich Gamma 已提交
295 296 297

			// 2.) Revert the model to get the latest copy from disk and to have access to the mtime of the file now
			return model.revert().then(() => {
298
				const diskLastModified = model.getLastModifiedTime();
E
Erich Gamma 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

				// 3. a) If we know that the file on the left hand side was not modified meanwhile, restore the user value and trigger a save
				if (diskLastModified <= knownLastModified) {

					// Restore user value
					model.textEditorModel.setValue(localModelValue);

					// Trigger save
					return model.save().then(() => {

						// Hide any previously shown messages
						while (this.messagesToHide.length) {
							this.messagesToHide.pop()();
						}

						// Reopen file input
315
						const input = this.instantiationService.createInstance(FileEditorInput, model.getResource(), guessMimeTypes(model.getResource().fsPath).join(', '), void 0);
E
Erich Gamma 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
						return this.editorService.openEditor(input, null, this.position).then(() => {

							// Dispose conflict input
							conflictInput.dispose();
						});
					});
				}

				// 3. b) The file was changed on disk while it was shown in the conflict editor
				else {

					// Again, we have to bring the model into conflict resolution because revert() would have cleared it
					model.setConflictResolutionMode();

					// Restore user value
					model.textEditorModel.setValue(localModelValue);

					// Reload the left hand side of the diff editor to show the up to date version and inform the user that he has to redo the action
334
					return conflictInput.originalInput.resolve(true).then(() => {
E
Erich Gamma 已提交
335 336 337 338 339 340 341 342 343
						this.messagesToHide.push(this.messageService.show(Severity.Info, nls.localize('conflictingFileHasChanged', "The content of the file on disk has changed and the left hand side of the compare editor was refreshed. Please review and resolve again.")));
					});
				}
			});
		});
	}
}

// Revert changes to resolve a conflicting edit
344
export class RevertLocalChangesAction extends EditorInputAction {
E
Erich Gamma 已提交
345 346 347 348 349

	constructor(
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
350
		super('workbench.action.files.revert', nls.localize('revertLocalChanges', "Discard local changes and revert to content on disk"), 'conflict-editor-action revert-changes');
E
Erich Gamma 已提交
351 352
	}

353
	public run(): TPromise<void> {
354
		const conflictInput = <ConflictResolutionDiffEditorInput>this.input;
355
		const model = conflictInput.getModel();
E
Erich Gamma 已提交
356

357 358
		clearPendingResolveSaveConflictMessages(); // hide any previously shown message about how to use these actions

E
Erich Gamma 已提交
359 360 361 362
		// Revert on model
		return model.revert().then(() => {

			// Reopen file input
363
			const input = this.instantiationService.createInstance(FileEditorInput, model.getResource(), guessMimeTypes(model.getResource().fsPath).join(', '), void 0);
E
Erich Gamma 已提交
364 365 366 367 368 369 370 371
			return this.editorService.openEditor(input, null, this.position).then(() => {

				// Dispose conflict input
				conflictInput.dispose();
			});
		});
	}
}