bulkEdit.ts 10.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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 * as nls from 'vs/nls';
J
Johannes Rieken 已提交
8
import { flatten } from 'vs/base/common/arrays';
J
Johannes Rieken 已提交
9
import { IStringDictionary, forEach, values } from 'vs/base/common/collections';
J
Joao Moreno 已提交
10
import { IDisposable, dispose, IReference } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
11
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
12
import { TPromise } from 'vs/base/common/winjs.base';
J
Joao Moreno 已提交
13
import { ITextModelResolverService, ITextEditorModel } from 'vs/editor/common/services/resolverService';
14
import { IFileService, IFileChange } from 'vs/platform/files/common/files';
J
Johannes Rieken 已提交
15 16 17
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
18
import { IIdentifiedSingleEditOperation, IModel, IRange, ISelection, EndOfLineSequence, ICommonCodeEditor } from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
19
import { IProgressRunner } from 'vs/platform/progress/common/progress';
E
Erich Gamma 已提交
20

21
export interface IResourceTextEdit {
E
Erich Gamma 已提交
22 23 24 25 26
	resource: URI;
	range?: IRange;
	newText: string;
}

27 28 29 30 31 32 33 34 35 36 37
export interface IResourceEOLEdit {
	resource: URI;
	eol: EndOfLineSequence;
}

export type IResourceEdit = IResourceTextEdit | IResourceEOLEdit;

function isIEndOfLineSequenceEdit(thing: any): thing is IResourceEOLEdit {
	return thing && URI.isUri((<IResourceEOLEdit>thing).resource) && typeof (<IResourceEOLEdit>thing).eol === 'number';
}

E
Erich Gamma 已提交
38 39 40 41 42 43 44 45
interface IRecording {
	stop(): void;
	hasChanged(resource: URI): boolean;
	allChanges(): IFileChange[];
}

class ChangeRecorder {

46
	private _fileService: IFileService;
E
Erich Gamma 已提交
47

48
	constructor(fileService?: IFileService) {
49
		this._fileService = fileService;
E
Erich Gamma 已提交
50 51 52 53
	}

	public start(): IRecording {

54
		const changes: IStringDictionary<IFileChange[]> = Object.create(null);
E
Erich Gamma 已提交
55

56 57 58 59
		let stop: IDisposable;
		if (this._fileService) {
			stop = this._fileService.onFileChanges((event) => {
				event.changes.forEach(change => {
E
Erich Gamma 已提交
60

61 62
					const key = String(change.resource);
					let array = changes[key];
E
Erich Gamma 已提交
63

64 65 66
					if (!array) {
						changes[key] = array = [];
					}
E
Erich Gamma 已提交
67

68 69
					array.push(change);
				});
E
Erich Gamma 已提交
70
			});
71
		}
E
Erich Gamma 已提交
72 73

		return {
74
			stop: () => { return stop && stop.dispose(); },
E
Erich Gamma 已提交
75
			hasChanged: (resource: URI) => !!changes[resource.toString()],
J
Johannes Rieken 已提交
76
			allChanges: () => flatten(values(changes))
E
Erich Gamma 已提交
77 78 79 80
		};
	}
}

81
class EditTask implements IDisposable {
E
Erich Gamma 已提交
82

83 84
	private _initialSelections: Selection[];
	private _endCursorSelection: Selection;
J
Joao Moreno 已提交
85 86
	private get _model(): IModel { return this._modelReference.object.textEditorModel; }
	private _modelReference: IReference<ITextEditorModel>;
E
Erich Gamma 已提交
87
	private _edits: IIdentifiedSingleEditOperation[];
88
	private _newEol: EndOfLineSequence;
E
Erich Gamma 已提交
89

J
Joao Moreno 已提交
90
	constructor(modelReference: IReference<ITextEditorModel>) {
E
Erich Gamma 已提交
91
		this._endCursorSelection = null;
J
Joao Moreno 已提交
92
		this._modelReference = modelReference;
E
Erich Gamma 已提交
93 94 95 96
		this._edits = [];
	}

	public addEdit(edit: IResourceEdit): void {
97 98 99 100
		if (isIEndOfLineSequenceEdit(edit)) {
			// store new EOL-sequence, last wins
			this._newEol = edit.eol;

E
Erich Gamma 已提交
101
		} else {
102 103 104 105 106 107 108 109
			// create edit operation
			let range: IRange;
			if (!edit.range) {
				range = this._model.getFullModelRange();
			} else {
				range = edit.range;
			}
			this._edits.push(EditOperation.replaceMove(Range.lift(range), edit.newText));
E
Erich Gamma 已提交
110 111 112 113
		}
	}

	public apply(): void {
114 115 116 117 118 119 120
		if (this._edits.length > 0) {
			this._edits.sort(EditTask._editCompare);
			this._initialSelections = this._getInitialSelections();
			this._model.pushEditOperations(this._initialSelections, this._edits, (edits) => this._getEndCursorSelections(edits));
		}
		if (this._newEol !== undefined) {
			this._model.setEOL(this._newEol);
E
Erich Gamma 已提交
121 122 123
		}
	}

124
	protected _getInitialSelections(): Selection[] {
125 126
		const firstRange = this._edits[0].range;
		const initialSelection = new Selection(
E
Erich Gamma 已提交
127 128 129 130 131 132 133 134
			firstRange.startLineNumber,
			firstRange.startColumn,
			firstRange.endLineNumber,
			firstRange.endColumn
		);
		return [initialSelection];
	}

J
Johannes Rieken 已提交
135
	private _getEndCursorSelections(inverseEditOperations: IIdentifiedSingleEditOperation[]): Selection[] {
136 137 138 139 140
		let relevantEditIndex = 0;
		for (let i = 0; i < inverseEditOperations.length; i++) {
			const editRange = inverseEditOperations[i].range;
			for (let j = 0; j < this._initialSelections.length; j++) {
				const selectionRange = this._initialSelections[j];
E
Erich Gamma 已提交
141 142 143 144 145 146 147
				if (Range.areIntersectingOrTouching(editRange, selectionRange)) {
					relevantEditIndex = i;
					break;
				}
			}
		}

148
		const srcRange = inverseEditOperations[relevantEditIndex].range;
A
Alex Dima 已提交
149
		this._endCursorSelection = new Selection(
E
Erich Gamma 已提交
150 151 152 153 154 155 156 157
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
		return [this._endCursorSelection];
	}

158
	public getEndCursorSelection(): Selection {
E
Erich Gamma 已提交
159 160 161 162 163 164
		return this._endCursorSelection;
	}

	private static _editCompare(a: IIdentifiedSingleEditOperation, b: IIdentifiedSingleEditOperation): number {
		return Range.compareRangesUsingStarts(a.range, b.range);
	}
165 166

	dispose() {
J
Joao Moreno 已提交
167 168 169 170
		if (this._model) {
			this._modelReference.dispose();
			this._modelReference = null;
		}
171
	}
E
Erich Gamma 已提交
172 173 174 175
}

class SourceModelEditTask extends EditTask {

J
Johannes Rieken 已提交
176
	private _knownInitialSelections: Selection[];
E
Erich Gamma 已提交
177

J
Joao Moreno 已提交
178 179
	constructor(modelReference: IReference<ITextEditorModel>, initialSelections: Selection[]) {
		super(modelReference);
E
Erich Gamma 已提交
180 181 182
		this._knownInitialSelections = initialSelections;
	}

183
	protected _getInitialSelections(): Selection[] {
E
Erich Gamma 已提交
184 185 186 187
		return this._knownInitialSelections;
	}
}

188
class BulkEditModel implements IDisposable {
E
Erich Gamma 已提交
189

190
	private _textModelResolverService: ITextModelResolverService;
E
Erich Gamma 已提交
191 192 193 194 195
	private _numberOfResourcesToModify: number = 0;
	private _numberOfChanges: number = 0;
	private _edits: IStringDictionary<IResourceEdit[]> = Object.create(null);
	private _tasks: EditTask[];
	private _sourceModel: URI;
196
	private _sourceSelections: Selection[];
E
Erich Gamma 已提交
197 198
	private _sourceModelTask: SourceModelEditTask;

199 200
	constructor(textModelResolverService: ITextModelResolverService, sourceModel: URI, sourceSelections: Selection[], edits: IResourceEdit[], private progress: IProgressRunner = null) {
		this._textModelResolverService = textModelResolverService;
E
Erich Gamma 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
		this._sourceModel = sourceModel;
		this._sourceSelections = sourceSelections;
		this._sourceModelTask = null;

		for (let edit of edits) {
			this._addEdit(edit);
		}
	}

	public resourcesCount(): number {
		return this._numberOfResourcesToModify;
	}

	public changeCount(): number {
		return this._numberOfChanges;
	}

	private _addEdit(edit: IResourceEdit): void {
219
		let array = this._edits[edit.resource.toString()];
E
Erich Gamma 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		if (!array) {
			this._edits[edit.resource.toString()] = array = [];
			this._numberOfResourcesToModify += 1;
		}
		this._numberOfChanges += 1;
		array.push(edit);
	}

	public prepare(): TPromise<BulkEditModel> {

		if (this._tasks) {
			throw new Error('illegal state - already prepared');
		}

		this._tasks = [];
235
		const promises: TPromise<any>[] = [];
E
Erich Gamma 已提交
236

237 238 239 240
		if (this.progress) {
			this.progress.total(this._numberOfResourcesToModify * 2);
		}

E
Erich Gamma 已提交
241
		forEach(this._edits, entry => {
J
Joao Moreno 已提交
242
			const promise = this._textModelResolverService.createModelReference(URI.parse(entry.key)).then(ref => {
J
Joao Moreno 已提交
243
				const model = ref.object;
244

E
Erich Gamma 已提交
245 246 247 248
				if (!model || !model.textEditorModel) {
					throw new Error(`Cannot load file ${entry.key}`);
				}

249 250
				const textEditorModel = model.textEditorModel;
				let task: EditTask;
E
Erich Gamma 已提交
251

J
Johannes Rieken 已提交
252
				if (this._sourceModel && textEditorModel.uri.toString() === this._sourceModel.toString()) {
J
Joao Moreno 已提交
253
					this._sourceModelTask = new SourceModelEditTask(ref, this._sourceSelections);
E
Erich Gamma 已提交
254 255
					task = this._sourceModelTask;
				} else {
J
Joao Moreno 已提交
256
					task = new EditTask(ref);
E
Erich Gamma 已提交
257 258 259 260
				}

				entry.value.forEach(edit => task.addEdit(edit));
				this._tasks.push(task);
261 262 263
				if (this.progress) {
					this.progress.worked(1);
				}
E
Erich Gamma 已提交
264 265 266 267
			});
			promises.push(promise);
		});

268

E
Erich Gamma 已提交
269 270 271
		return TPromise.join(promises).then(_ => this);
	}

272
	public apply(): Selection {
273
		this._tasks.forEach(task => this.applyTask(task));
274
		let r: Selection = null;
E
Erich Gamma 已提交
275 276 277 278 279
		if (this._sourceModelTask) {
			r = this._sourceModelTask.getEndCursorSelection();
		}
		return r;
	}
280 281 282 283 284 285 286

	private applyTask(task): void {
		task.apply();
		if (this.progress) {
			this.progress.worked(1);
		}
	}
287 288 289 290

	dispose(): void {
		this._tasks = dispose(this._tasks);
	}
E
Erich Gamma 已提交
291 292 293
}

export interface BulkEdit {
294
	progress(progress: IProgressRunner);
E
Erich Gamma 已提交
295 296 297 298
	add(edit: IResourceEdit[]): void;
	finish(): TPromise<ISelection>;
}

299 300
export function bulkEdit(textModelResolverService: ITextModelResolverService, editor: ICommonCodeEditor, edits: IResourceEdit[], fileService?: IFileService, progress: IProgressRunner = null): TPromise<any> {
	let bulk = createBulkEdit(textModelResolverService, editor, fileService);
E
Erich Gamma 已提交
301
	bulk.add(edits);
302
	bulk.progress(progress);
E
Erich Gamma 已提交
303 304 305
	return bulk.finish();
}

306
export function createBulkEdit(textModelResolverService: ITextModelResolverService, editor?: ICommonCodeEditor, fileService?: IFileService): BulkEdit {
E
Erich Gamma 已提交
307 308

	let all: IResourceEdit[] = [];
309
	let recording = new ChangeRecorder(fileService).start();
310 311 312
	let progressRunner: IProgressRunner;

	function progress(progress: IProgressRunner) {
J
Johannes Rieken 已提交
313
		progressRunner = progress;
314
	}
E
Erich Gamma 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

	function add(edits: IResourceEdit[]): void {
		all.push(...edits);
	}

	function getConcurrentEdits() {
		let names: string[];
		for (let edit of all) {
			if (recording.hasChanged(edit.resource)) {
				if (!names) {
					names = [];
				}
				names.push(edit.resource.fsPath);
			}
		}
		if (names) {
			return nls.localize('conflict', "These files have changed in the meantime: {0}", names.join(', '));
		}
M
Matt Bierner 已提交
333
		return undefined;
E
Erich Gamma 已提交
334 335 336 337 338
	}

	function finish(): TPromise<ISelection> {

		if (all.length === 0) {
339
			return TPromise.as(undefined);
E
Erich Gamma 已提交
340 341 342 343 344 345 346 347
		}

		let concurrentEdits = getConcurrentEdits();
		if (concurrentEdits) {
			return TPromise.wrapError(concurrentEdits);
		}

		let uri: URI;
348
		let selections: Selection[];
E
Erich Gamma 已提交
349

350
		if (editor && editor.getModel()) {
351
			uri = editor.getModel().uri;
E
Erich Gamma 已提交
352 353 354
			selections = editor.getSelections();
		}

355
		const model = new BulkEditModel(textModelResolverService, uri, selections, all, progressRunner);
E
Erich Gamma 已提交
356 357 358 359 360 361 362 363 364

		return model.prepare().then(_ => {

			let concurrentEdits = getConcurrentEdits();
			if (concurrentEdits) {
				throw new Error(concurrentEdits);
			}

			recording.stop();
365 366 367 368

			const result = model.apply();
			model.dispose();
			return result;
E
Erich Gamma 已提交
369 370 371 372
		});
	}

	return {
373
		progress,
E
Erich Gamma 已提交
374 375
		add,
		finish
A
tslint  
Alex Dima 已提交
376
	};
E
Erich Gamma 已提交
377
}