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';
B
Benjamin Pasero 已提交
13
import {EditorModel} from 'vs/workbench/common/editor';
14
import {guessMimeTypes} from 'vs/base/common/mime';
E
Erich Gamma 已提交
15
import {EditorInputAction} from 'vs/workbench/browser/parts/editor/baseEditor';
16 17 18
import {ResourceEditorInput} from 'vs/workbench/common/editor/resourceEditorInput';
import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput';
import {DiffEditorModel} from 'vs/workbench/common/editor/diffEditorModel';
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';
25
import {EventType as FileEventType, TextFileChangeEvent} 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 {
47 48
		this.eventService.addListener(FileEventType.FILE_SAVED, (e: TextFileChangeEvent) => this.onFileSavedOrReverted(e.resource));
		this.eventService.addListener(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 70 71 72 73 74 75 76 77 78 79 80 81 82

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

		// Any other save error
		else {
			let isReadonly = (<IFileOperationResult>error).fileOperationResult === FileOperationResult.FILE_READ_ONLY;
			let actions: Action[] = [];

			// 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 87
				}));
			} else {
				actions.push(new Action('workbench.files.action.retry', nls.localize('retry', "Retry"), null, true, () => {
					let saveFileAction = this.instantiationService.createInstance(SaveFileAction, SaveFileAction.ID, SaveFileAction.LABEL);
88
					saveFileAction.setResource(resource);
E
Erich Gamma 已提交
89 90 91 92 93 94 95 96

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

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

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

			// Save As
			actions.push(new Action('workbench.files.action.saveAs', SaveFileAsAction.LABEL, null, true, () => {
				let 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 151 152 153 154 155
	) {
		super(name, description, originalInput, modifiedInput);

		this.model = model;
	}

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

	public getId(): string {
		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 170 171 172 173 174
		@IInstantiationService instantiationService: IInstantiationService,
		@IFileService private fileService: IFileService
	) {
		// 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.
J
ops  
Johannes Rieken 已提交
175
		super(name, description, URI.create('disk', null, fileResource.fsPath), modelService, instantiationService);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

		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
		return this.fileService.resolveContent(this.fileResource).then(content => {
			this.lastModified = content.mtime;

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

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

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

		super.dispose();
	}
211 212
}

E
Erich Gamma 已提交
213 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,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@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
					let mime = guessMimeTypes(resource.fsPath).join(', ');
					let originalInput = this.instantiationService.createInstance(FileOnDiskEditorInput, resource, mime, paths.basename(resource.fsPath), resource.fsPath);
					let modifiedInput = this.instantiationService.createInstance(FileEditorInput, resource, mime, void 0);
E
Erich Gamma 已提交
243 244 245 246 247 248 249 250 251 252 253 254
					let 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);

					return this.editorService.openEditor(conflictInput).then(() => {

						// 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
						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"));
					});
				}

A
Alex Dima 已提交
255
				return TPromise.as(true);
E
Erich Gamma 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
			})
		];
	}
}

// Accept changes to resolve a conflicting edit
export class AcceptLocalChangesAction extends EditorInputAction {
	private messagesToHide: { (): void; }[];

	constructor(
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super('workbench.files.action.acceptLocalChanges', nls.localize('acceptLocalChanges', "Use local changes and overwrite disk contents"), 'conflict-editor-action accept-changes');

		this.messagesToHide = [];
	}

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

		// 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
			let knownLastModified = (<FileOnDiskEditorInput>conflictInput.originalInput).getLastModified();
E
Erich Gamma 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

			// 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(() => {
				let diskLastModified = model.getLastModifiedTime();

				// 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
						let 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 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
export class RevertLocalChangesAction extends EditorInputAction {

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

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

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

			// Reopen file input
349
			let 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();
			});
		});
	}
}