saveErrorHandler.ts 14.2 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 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
// 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,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		this.model = model;

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

		this.actions = [
			new Action('workbench.files.action.resolveConflict', nls.localize('compareChanges', "Compare"), null, true, () => {
				if (!this.model.isDisposed()) {
240 241 242
					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);
243
					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 已提交
244

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

						// 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
251
						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 已提交
252 253 254
					});
				}

A
Alex Dima 已提交
255
				return TPromise.as(true);
E
Erich Gamma 已提交
256 257 258 259 260 261
			})
		];
	}
}

// Accept changes to resolve a conflicting edit
262
export class AcceptLocalChangesAction extends EditorInputAction {
E
Erich Gamma 已提交
263 264 265 266 267 268 269
	private messagesToHide: { (): void; }[];

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

		this.messagesToHide = [];
	}

275
	public run(): TPromise<void> {
276
		const conflictInput = <ConflictResolutionDiffEditorInput>this.input;
277 278
		const model = conflictInput.getModel();
		const localModelValue = model.getValue();
E
Erich Gamma 已提交
279 280 281

		// 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) => {
282
			const knownLastModified = (<FileOnDiskEditorInput>conflictInput.originalInput).getLastModified();
E
Erich Gamma 已提交
283 284 285

			// 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(() => {
286
				const diskLastModified = model.getLastModifiedTime();
E
Erich Gamma 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

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

	constructor(
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
338
		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 已提交
339 340
	}

341
	public run(): TPromise<void> {
342
		const conflictInput = <ConflictResolutionDiffEditorInput>this.input;
343
		const model = conflictInput.getModel();
E
Erich Gamma 已提交
344 345 346 347 348

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

			// Reopen file input
349
			const input = this.instantiationService.createInstance(FileEditorInput, model.getResource(), guessMimeTypes(model.getResource().fsPath).join(', '), void 0);
E
Erich Gamma 已提交
350 351 352 353 354 355 356 357
			return this.editorService.openEditor(input, null, this.position).then(() => {

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