saveErrorHandler.ts 14.6 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';
B
Benjamin Pasero 已提交
13
import {EditorModel} from 'vs/workbench/common/editor';
14
import {guessMimeTypes} from 'vs/base/common/mime';
15 16 17
import {ResourceEditorInput} from 'vs/workbench/common/editor/resourceEditorInput';
import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput';
import {DiffEditorModel} from 'vs/workbench/common/editor/diffEditorModel';
18
import {Position} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
19 20
import {FileEditorInput} from 'vs/workbench/parts/files/browser/editors/fileEditorInput';
import {SaveFileAsAction, RevertFileAction, SaveFileAction} from 'vs/workbench/parts/files/browser/fileActions';
21
import {IFileService, IFileOperationResult, FileOperationResult} from 'vs/platform/files/common/files';
22
import {TextFileEditorModel, ISaveErrorHandler} from 'vs/workbench/parts/files/common/editors/textFileEditorModel';
E
Erich Gamma 已提交
23 24
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IEventService} from 'vs/platform/event/common/event';
A
Alex Dima 已提交
25
import {EventType as FileEventType, TextFileChangeEvent, ITextFileService} from 'vs/workbench/parts/files/common/files';
E
Erich Gamma 已提交
26 27 28
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import {IMessageService, IMessageWithAction, Severity, CancelAction} from 'vs/platform/message/common/message';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
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 77 78 79 80 81 82

			// Cancel
			actions.push(CancelAction);

			// Retry
			if (isReadonly) {
				actions.push(new Action('workbench.files.action.overwrite', nls.localize('overwrite', "Overwrite"), null, true, () => {
					if (!model.isDisposed()) {
						return model.save(true /* overwrite readonly */).then(() => true);
					}

A
Alex Dima 已提交
83
					return TPromise.as(true);
E
Erich Gamma 已提交
84 85 86
				}));
			} else {
				actions.push(new Action('workbench.files.action.retry', nls.localize('retry', "Retry"), null, true, () => {
87
					const saveFileAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL);
88
					saveFileAction.setResource(resource);
E
Erich Gamma 已提交
89 90 91 92 93 94 95

					return saveFileAction.run().then(() => { saveFileAction.dispose(); return true; });
				}));
			}

			// Discard
			actions.push(new Action('workbench.files.action.discard', nls.localize('discard', "Discard"), null, true, () => {
96
				const revertFileAction = this.instantiationService.createInstance(RevertFileAction, RevertFileAction.ID, RevertFileAction.LABEL);
97
				revertFileAction.setResource(resource);
E
Erich Gamma 已提交
98 99 100 101 102 103

				return revertFileAction.run().then(() => { revertFileAction.dispose(); return true; });
			}));

			// Save As
			actions.push(new Action('workbench.files.action.saveAs', SaveFileAsAction.LABEL, null, true, () => {
104
				const saveAsAction = this.instantiationService.createInstance(SaveFileAsAction, SaveFileAsAction.ID, SaveFileAsAction.LABEL);
105
				saveAsAction.setResource(resource);
E
Erich Gamma 已提交
106 107 108 109 110 111

				return saveAsAction.run().then(() => { saveAsAction.dispose(); return true; });
			}));

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

			message = {
				message: errorMessage,
				actions: actions
			};
		}

123 124
		// 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 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
	}
}

// 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,
139
		originalInput: FileOnDiskEditorInput,
140
		modifiedInput: FileEditorInput
E
Erich Gamma 已提交
141 142 143 144 145 146 147 148 149 150
	) {
		super(name, description, originalInput, modifiedInput);

		this.model = model;
	}

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

151
	public getTypeId(): string {
E
Erich Gamma 已提交
152 153 154 155
		return ConflictResolutionDiffEditorInput.ID;
	}
}

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

	constructor(
		fileResource: URI,
		mime: string,
		name: string,
		description: string,
		@IModelService modelService: IModelService,
J
ops  
Johannes Rieken 已提交
168
		@IModeService private modeService: IModeService,
169
		@IInstantiationService instantiationService: IInstantiationService,
A
Alex Dima 已提交
170 171
		@IFileService private fileService: IFileService,
		@ITextFileService private textFileService: ITextFileService
172 173 174 175
	) {
		// 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.
176
		super(name, description, URI.from({ scheme: 'disk', path: fileResource.fsPath }), modelService, instantiationService);
177 178 179 180 181 182 183 184 185 186 187 188

		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 已提交
189
		return this.textFileService.resolveTextContent(this.fileResource).then(content => {
190 191
			this.lastModified = content.mtime;

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

			return super.resolve(refresh);
		});
	}
203 204 205 206 207 208 209 210 211

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

		super.dispose();
	}
212 213
}

E
Erich Gamma 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
// A message with action to resolve a 412 save conflict
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,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		this.model = model;

231
		const resource = model.getResource();
E
Erich Gamma 已提交
232 233 234
		if (message) {
			this.message = message;
		} else {
235
			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 已提交
236 237 238 239 240
		}

		this.actions = [
			new Action('workbench.files.action.resolveConflict', nls.localize('compareChanges', "Compare"), null, true, () => {
				if (!this.model.isDisposed()) {
241 242 243 244
					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);
					const conflictInput = this.instantiationService.createInstance(ConflictResolutionDiffEditorInput, this.model, nls.localize('saveConflictDiffLabel', "{0} - on disk ↔ in {1}", modifiedInput.getName(), this.contextService.getConfiguration().env.appName), nls.localize('resolveSaveConflict', "{0} - Resolve save conflict", modifiedInput.getDescription()), originalInput, modifiedInput);
E
Erich Gamma 已提交
245

246
					return this.editorService.openEditor(conflictInput).then(editor => {
E
Erich Gamma 已提交
247 248 249 250 251

						// 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
252 253 254 255 256 257 258
						this.messageService.show(Severity.Info, {
							message: nls.localize('userGuide', "Please either select **Revert** to discard your changes or **Overwrite** to replace the content on disk with your changes"),
							actions: [
								this.instantiationService.createInstance(AcceptLocalChangesAction, conflictInput, editor.position),
								this.instantiationService.createInstance(RevertLocalChangesAction, conflictInput, editor.position)
							]
						});
E
Erich Gamma 已提交
259 260 261
					});
				}

A
Alex Dima 已提交
262
				return TPromise.as(true);
E
Erich Gamma 已提交
263 264 265 266 267 268
			})
		];
	}
}

// Accept changes to resolve a conflicting edit
269
export class AcceptLocalChangesAction extends Action {
E
Erich Gamma 已提交
270 271 272
	private messagesToHide: { (): void; }[];

	constructor(
273 274
		private input: ConflictResolutionDiffEditorInput,
		private position: Position,
E
Erich Gamma 已提交
275 276 277 278
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
279
		super('workbench.files.action.acceptLocalChanges', nls.localize('acceptLocalChanges', "Overwrite"), 'conflict-editor-action accept-changes');
E
Erich Gamma 已提交
280 281 282 283

		this.messagesToHide = [];
	}

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

		// 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) => {
291
			const knownLastModified = (<FileOnDiskEditorInput>conflictInput.originalInput).getLastModified();
E
Erich Gamma 已提交
292 293 294

			// 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(() => {
295
				const diskLastModified = model.getLastModifiedTime();
E
Erich Gamma 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

				// 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
312
						const input = this.instantiationService.createInstance(FileEditorInput, model.getResource(), guessMimeTypes(model.getResource().fsPath).join(', '), void 0);
E
Erich Gamma 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
						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
331
					return conflictInput.originalInput.resolve(true).then(() => {
E
Erich Gamma 已提交
332 333 334 335 336 337 338 339 340
						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
341
export class RevertLocalChangesAction extends Action {
E
Erich Gamma 已提交
342 343

	constructor(
344 345
		private input: ConflictResolutionDiffEditorInput,
		private position: Position,
E
Erich Gamma 已提交
346 347 348
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
349
		super('workbench.action.files.revert', nls.localize('revertLocalChanges', "Revert"), 'conflict-editor-action revert-changes');
E
Erich Gamma 已提交
350 351
	}

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

		// Revert on model
		return model.revert().then(() => {

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

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