bulkEdit.ts 9.6 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';
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
	constructor(fileService?: IFileService) {
38
		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 46 47 48
		let stop: IDisposable;
		if (this._fileService) {
			stop = this._fileService.onFileChanges((event) => {
				event.changes.forEach(change => {
E
Erich Gamma 已提交
49

50 51
					const key = String(change.resource);
					let array = changes[key];
E
Erich Gamma 已提交
52

53 54 55
					if (!array) {
						changes[key] = array = [];
					}
E
Erich Gamma 已提交
56

57 58
					array.push(change);
				});
E
Erich Gamma 已提交
59
			});
60
		}
E
Erich Gamma 已提交
61 62

		return {
63
			stop: () => { return stop && stop.dispose(); },
E
Erich Gamma 已提交
64
			hasChanged: (resource: URI) => !!changes[resource.toString()],
J
Johannes Rieken 已提交
65
			allChanges: () => flatten(values(changes))
E
Erich Gamma 已提交
66 67 68 69
		};
	}
}

70
class EditTask implements IDisposable {
E
Erich Gamma 已提交
71

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

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

	public addEdit(edit: IResourceEdit): void {
85
		let range: IRange;
E
Erich Gamma 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		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));
	}

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

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

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

138
	public getEndCursorSelection(): Selection {
E
Erich Gamma 已提交
139 140 141 142 143 144
		return this._endCursorSelection;
	}

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

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

class SourceModelEditTask extends EditTask {

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

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

163
	protected _getInitialSelections(): Selection[] {
E
Erich Gamma 已提交
164 165 166 167
		return this._knownInitialSelections;
	}
}

168
class BulkEditModel implements IDisposable {
E
Erich Gamma 已提交
169

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

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

217 218 219 220
		if (this.progress) {
			this.progress.total(this._numberOfResourcesToModify * 2);
		}

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

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

229 230
				const textEditorModel = model.textEditorModel;
				let task: EditTask;
E
Erich Gamma 已提交
231

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

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

248

E
Erich Gamma 已提交
249 250 251
		return TPromise.join(promises).then(_ => this);
	}

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

	private applyTask(task): void {
		task.apply();
		if (this.progress) {
			this.progress.worked(1);
		}
	}
267 268 269 270

	dispose(): void {
		this._tasks = dispose(this._tasks);
	}
E
Erich Gamma 已提交
271 272 273
}

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

279 280
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 已提交
281
	bulk.add(edits);
282
	bulk.progress(progress);
E
Erich Gamma 已提交
283 284 285
	return bulk.finish();
}

286
export function createBulkEdit(textModelResolverService: ITextModelResolverService, editor?: ICommonCodeEditor, fileService?: IFileService): BulkEdit {
E
Erich Gamma 已提交
287 288

	let all: IResourceEdit[] = [];
289
	let recording = new ChangeRecorder(fileService).start();
290 291 292
	let progressRunner: IProgressRunner;

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

	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) {
318
			return TPromise.as(undefined);
E
Erich Gamma 已提交
319 320 321 322 323 324 325 326
		}

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

		let uri: URI;
327
		let selections: Selection[];
E
Erich Gamma 已提交
328

329
		if (editor && editor.getModel()) {
330
			uri = editor.getModel().uri;
E
Erich Gamma 已提交
331 332 333
			selections = editor.getSelections();
		}

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

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

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

			recording.stop();
344 345 346 347

			const result = model.apply();
			model.dispose();
			return result;
E
Erich Gamma 已提交
348 349 350 351
		});
	}

	return {
352
		progress,
E
Erich Gamma 已提交
353 354
		add,
		finish
A
tslint  
Alex Dima 已提交
355
	};
E
Erich Gamma 已提交
356
}