bulkEdit.ts 9.5 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 9
import { merge } from 'vs/base/common/arrays';
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';
J
Joao Moreno 已提交
18
import { IIdentifiedSingleEditOperation, IModel, IRange, ISelection, ICommonCodeEditor } from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
19
import { IProgressRunner } from 'vs/platform/progress/common/progress';
E
Erich Gamma 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

export interface IResourceEdit {
	resource: URI;
	range?: IRange;
	newText: string;
}

interface IRecording {
	stop(): void;
	hasChanged(resource: URI): boolean;
	allChanges(): IFileChange[];
}

class ChangeRecorder {

35
	private _fileService: IFileService;
E
Erich Gamma 已提交
36

37 38
	constructor(fileService: IFileService) {
		this._fileService = fileService;
E
Erich Gamma 已提交
39 40 41 42
	}

	public start(): IRecording {

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

45
		const stop = this._fileService.onFileChanges((event) => {
E
Erich Gamma 已提交
46 47
			event.changes.forEach(change => {

48 49
				const key = String(change.resource);
				let array = changes[key];
E
Erich Gamma 已提交
50 51 52 53 54 55 56 57 58 59

				if (!array) {
					changes[key] = array = [];
				}

				array.push(change);
			});
		});

		return {
A
Alex Dima 已提交
60
			stop: () => { stop.dispose(); },
E
Erich Gamma 已提交
61 62 63 64 65 66
			hasChanged: (resource: URI) => !!changes[resource.toString()],
			allChanges: () => merge(values(changes))
		};
	}
}

67
class EditTask implements IDisposable {
E
Erich Gamma 已提交
68

69 70
	private _initialSelections: Selection[];
	private _endCursorSelection: Selection;
J
Joao Moreno 已提交
71 72
	private get _model(): IModel { return this._modelReference.object.textEditorModel; }
	private _modelReference: IReference<ITextEditorModel>;
E
Erich Gamma 已提交
73 74
	private _edits: IIdentifiedSingleEditOperation[];

J
Joao Moreno 已提交
75
	constructor(modelReference: IReference<ITextEditorModel>) {
E
Erich Gamma 已提交
76
		this._endCursorSelection = null;
J
Joao Moreno 已提交
77
		this._modelReference = modelReference;
E
Erich Gamma 已提交
78 79 80 81
		this._edits = [];
	}

	public addEdit(edit: IResourceEdit): void {
82
		let range: IRange;
E
Erich Gamma 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		if (!edit.range) {
			range = this._model.getFullModelRange();
		} else {
			range = edit.range;
		}
		this._edits.push(EditOperation.replace(Range.lift(range), edit.newText));
	}

	public apply(): void {
		if (this._edits.length === 0) {
			return;
		}
		this._edits.sort(EditTask._editCompare);

		this._initialSelections = this._getInitialSelections();
		this._model.pushEditOperations(this._initialSelections, this._edits, (edits) => this._getEndCursorSelections(edits));
	}

101
	protected _getInitialSelections(): Selection[] {
102 103
		const firstRange = this._edits[0].range;
		const initialSelection = new Selection(
E
Erich Gamma 已提交
104 105 106 107 108 109 110 111
			firstRange.startLineNumber,
			firstRange.startColumn,
			firstRange.endLineNumber,
			firstRange.endColumn
		);
		return [initialSelection];
	}

J
Johannes Rieken 已提交
112
	private _getEndCursorSelections(inverseEditOperations: IIdentifiedSingleEditOperation[]): Selection[] {
113 114 115 116 117
		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 已提交
118 119 120 121 122 123 124
				if (Range.areIntersectingOrTouching(editRange, selectionRange)) {
					relevantEditIndex = i;
					break;
				}
			}
		}

125
		const srcRange = inverseEditOperations[relevantEditIndex].range;
A
Alex Dima 已提交
126
		this._endCursorSelection = new Selection(
E
Erich Gamma 已提交
127 128 129 130 131 132 133 134
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
		return [this._endCursorSelection];
	}

135
	public getEndCursorSelection(): Selection {
E
Erich Gamma 已提交
136 137 138 139 140 141
		return this._endCursorSelection;
	}

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

	dispose() {
J
Joao Moreno 已提交
144 145 146 147
		if (this._model) {
			this._modelReference.dispose();
			this._modelReference = null;
		}
148
	}
E
Erich Gamma 已提交
149 150 151 152
}

class SourceModelEditTask extends EditTask {

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

J
Joao Moreno 已提交
155 156
	constructor(modelReference: IReference<ITextEditorModel>, initialSelections: Selection[]) {
		super(modelReference);
E
Erich Gamma 已提交
157 158 159
		this._knownInitialSelections = initialSelections;
	}

160
	protected _getInitialSelections(): Selection[] {
E
Erich Gamma 已提交
161 162 163 164
		return this._knownInitialSelections;
	}
}

165
class BulkEditModel implements IDisposable {
E
Erich Gamma 已提交
166

167
	private _textModelResolverService: ITextModelResolverService;
E
Erich Gamma 已提交
168 169 170 171 172
	private _numberOfResourcesToModify: number = 0;
	private _numberOfChanges: number = 0;
	private _edits: IStringDictionary<IResourceEdit[]> = Object.create(null);
	private _tasks: EditTask[];
	private _sourceModel: URI;
173
	private _sourceSelections: Selection[];
E
Erich Gamma 已提交
174 175
	private _sourceModelTask: SourceModelEditTask;

176 177
	constructor(textModelResolverService: ITextModelResolverService, sourceModel: URI, sourceSelections: Selection[], edits: IResourceEdit[], private progress: IProgressRunner = null) {
		this._textModelResolverService = textModelResolverService;
E
Erich Gamma 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
		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 {
196
		let array = this._edits[edit.resource.toString()];
E
Erich Gamma 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		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 = [];
212
		const promises: TPromise<any>[] = [];
E
Erich Gamma 已提交
213

214 215 216 217
		if (this.progress) {
			this.progress.total(this._numberOfResourcesToModify * 2);
		}

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

E
Erich Gamma 已提交
222 223 224 225
				if (!model || !model.textEditorModel) {
					throw new Error(`Cannot load file ${entry.key}`);
				}

226 227
				const textEditorModel = model.textEditorModel;
				let task: EditTask;
E
Erich Gamma 已提交
228

J
Johannes Rieken 已提交
229
				if (this._sourceModel && textEditorModel.uri.toString() === this._sourceModel.toString()) {
J
Joao Moreno 已提交
230
					this._sourceModelTask = new SourceModelEditTask(ref, this._sourceSelections);
E
Erich Gamma 已提交
231 232
					task = this._sourceModelTask;
				} else {
J
Joao Moreno 已提交
233
					task = new EditTask(ref);
E
Erich Gamma 已提交
234 235 236 237
				}

				entry.value.forEach(edit => task.addEdit(edit));
				this._tasks.push(task);
238 239 240
				if (this.progress) {
					this.progress.worked(1);
				}
E
Erich Gamma 已提交
241 242 243 244
			});
			promises.push(promise);
		});

245

E
Erich Gamma 已提交
246 247 248
		return TPromise.join(promises).then(_ => this);
	}

249
	public apply(): Selection {
250
		this._tasks.forEach(task => this.applyTask(task));
251
		let r: Selection = null;
E
Erich Gamma 已提交
252 253 254 255 256
		if (this._sourceModelTask) {
			r = this._sourceModelTask.getEndCursorSelection();
		}
		return r;
	}
257 258 259 260 261 262 263

	private applyTask(task): void {
		task.apply();
		if (this.progress) {
			this.progress.worked(1);
		}
	}
264 265 266 267

	dispose(): void {
		this._tasks = dispose(this._tasks);
	}
E
Erich Gamma 已提交
268 269 270
}

export interface BulkEdit {
271
	progress(progress: IProgressRunner);
E
Erich Gamma 已提交
272 273 274 275
	add(edit: IResourceEdit[]): void;
	finish(): TPromise<ISelection>;
}

276 277
export function bulkEdit(fileService: IFileService, textModelResolverService: ITextModelResolverService, editor: ICommonCodeEditor, edits: IResourceEdit[], progress: IProgressRunner = null): TPromise<any> {
	let bulk = createBulkEdit(fileService, textModelResolverService, editor);
E
Erich Gamma 已提交
278
	bulk.add(edits);
279
	bulk.progress(progress);
E
Erich Gamma 已提交
280 281 282
	return bulk.finish();
}

283
export function createBulkEdit(fileService: IFileService, textModelResolverService: ITextModelResolverService, editor: ICommonCodeEditor): BulkEdit {
E
Erich Gamma 已提交
284 285

	let all: IResourceEdit[] = [];
286
	let recording = new ChangeRecorder(fileService).start();
287 288 289
	let progressRunner: IProgressRunner;

	function progress(progress: IProgressRunner) {
J
Johannes Rieken 已提交
290
		progressRunner = progress;
291
	}
E
Erich Gamma 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

	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(', '));
		}
	}

	function finish(): TPromise<ISelection> {

		if (all.length === 0) {
315
			return TPromise.as(undefined);
E
Erich Gamma 已提交
316 317 318 319 320 321 322 323
		}

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

		let uri: URI;
324
		let selections: Selection[];
E
Erich Gamma 已提交
325

326
		if (editor && editor.getModel()) {
327
			uri = editor.getModel().uri;
E
Erich Gamma 已提交
328 329 330
			selections = editor.getSelections();
		}

331
		const model = new BulkEditModel(textModelResolverService, uri, selections, all, progressRunner);
E
Erich Gamma 已提交
332 333 334 335 336 337 338 339 340

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

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

			recording.stop();
341 342 343 344

			const result = model.apply();
			model.dispose();
			return result;
E
Erich Gamma 已提交
345 346 347 348
		});
	}

	return {
349
		progress,
E
Erich Gamma 已提交
350 351
		add,
		finish
A
tslint  
Alex Dima 已提交
352
	};
E
Erich Gamma 已提交
353
}