bulkEdit.ts 9.9 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 IResourceEdit {
E
Erich Gamma 已提交
22 23 24
	resource: URI;
	range?: IRange;
	newText: string;
25
	newEol?: EndOfLineSequence;
26 27
}

E
Erich Gamma 已提交
28 29 30 31 32 33 34 35
interface IRecording {
	stop(): void;
	hasChanged(resource: URI): boolean;
	allChanges(): IFileChange[];
}

class ChangeRecorder {

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

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

	public start(): IRecording {

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

46 47 48 49
		let stop: IDisposable;
		if (this._fileService) {
			stop = this._fileService.onFileChanges((event) => {
				event.changes.forEach(change => {
E
Erich Gamma 已提交
50

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

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

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

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

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

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

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

	public addEdit(edit: IResourceEdit): void {
87

88 89 90 91
		// create edit operation
		let range: IRange;
		if (!edit.range) {
			range = this._model.getFullModelRange();
E
Erich Gamma 已提交
92
		} else {
93 94 95 96 97 98 99
			range = edit.range;
		}
		this._edits.push(EditOperation.replaceMove(Range.lift(range), edit.newText));

		// honor eol-change
		if (typeof edit.newEol === 'number') {
			this._newEol = edit.newEol;
E
Erich Gamma 已提交
100 101 102 103
		}
	}

	public apply(): void {
104 105 106 107 108 109 110
		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 已提交
111 112 113
		}
	}

114
	protected _getInitialSelections(): Selection[] {
115 116
		const firstRange = this._edits[0].range;
		const initialSelection = new Selection(
E
Erich Gamma 已提交
117 118 119 120 121 122 123 124
			firstRange.startLineNumber,
			firstRange.startColumn,
			firstRange.endLineNumber,
			firstRange.endColumn
		);
		return [initialSelection];
	}

J
Johannes Rieken 已提交
125
	private _getEndCursorSelections(inverseEditOperations: IIdentifiedSingleEditOperation[]): Selection[] {
126 127 128 129 130
		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 已提交
131 132 133 134 135 136 137
				if (Range.areIntersectingOrTouching(editRange, selectionRange)) {
					relevantEditIndex = i;
					break;
				}
			}
		}

138
		const srcRange = inverseEditOperations[relevantEditIndex].range;
A
Alex Dima 已提交
139
		this._endCursorSelection = new Selection(
E
Erich Gamma 已提交
140 141 142 143 144 145 146 147
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
		return [this._endCursorSelection];
	}

148
	public getEndCursorSelection(): Selection {
E
Erich Gamma 已提交
149 150 151 152 153 154
		return this._endCursorSelection;
	}

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

	dispose() {
J
Joao Moreno 已提交
157 158 159 160
		if (this._model) {
			this._modelReference.dispose();
			this._modelReference = null;
		}
161
	}
E
Erich Gamma 已提交
162 163 164 165
}

class SourceModelEditTask extends EditTask {

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

J
Joao Moreno 已提交
168 169
	constructor(modelReference: IReference<ITextEditorModel>, initialSelections: Selection[]) {
		super(modelReference);
E
Erich Gamma 已提交
170 171 172
		this._knownInitialSelections = initialSelections;
	}

173
	protected _getInitialSelections(): Selection[] {
E
Erich Gamma 已提交
174 175 176 177
		return this._knownInitialSelections;
	}
}

178
class BulkEditModel implements IDisposable {
E
Erich Gamma 已提交
179

180
	private _textModelResolverService: ITextModelResolverService;
E
Erich Gamma 已提交
181 182 183 184 185
	private _numberOfResourcesToModify: number = 0;
	private _numberOfChanges: number = 0;
	private _edits: IStringDictionary<IResourceEdit[]> = Object.create(null);
	private _tasks: EditTask[];
	private _sourceModel: URI;
186
	private _sourceSelections: Selection[];
E
Erich Gamma 已提交
187 188
	private _sourceModelTask: SourceModelEditTask;

189 190
	constructor(textModelResolverService: ITextModelResolverService, sourceModel: URI, sourceSelections: Selection[], edits: IResourceEdit[], private progress: IProgressRunner = null) {
		this._textModelResolverService = textModelResolverService;
E
Erich Gamma 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
		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 {
209
		let array = this._edits[edit.resource.toString()];
E
Erich Gamma 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
		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 = [];
225
		const promises: TPromise<any>[] = [];
E
Erich Gamma 已提交
226

227 228 229 230
		if (this.progress) {
			this.progress.total(this._numberOfResourcesToModify * 2);
		}

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

E
Erich Gamma 已提交
235 236 237 238
				if (!model || !model.textEditorModel) {
					throw new Error(`Cannot load file ${entry.key}`);
				}

239 240
				const textEditorModel = model.textEditorModel;
				let task: EditTask;
E
Erich Gamma 已提交
241

J
Johannes Rieken 已提交
242
				if (this._sourceModel && textEditorModel.uri.toString() === this._sourceModel.toString()) {
J
Joao Moreno 已提交
243
					this._sourceModelTask = new SourceModelEditTask(ref, this._sourceSelections);
E
Erich Gamma 已提交
244 245
					task = this._sourceModelTask;
				} else {
J
Joao Moreno 已提交
246
					task = new EditTask(ref);
E
Erich Gamma 已提交
247 248 249 250
				}

				entry.value.forEach(edit => task.addEdit(edit));
				this._tasks.push(task);
251 252 253
				if (this.progress) {
					this.progress.worked(1);
				}
E
Erich Gamma 已提交
254 255 256 257
			});
			promises.push(promise);
		});

258

E
Erich Gamma 已提交
259 260 261
		return TPromise.join(promises).then(_ => this);
	}

262
	public apply(): Selection {
263
		this._tasks.forEach(task => this.applyTask(task));
264
		let r: Selection = null;
E
Erich Gamma 已提交
265 266 267 268 269
		if (this._sourceModelTask) {
			r = this._sourceModelTask.getEndCursorSelection();
		}
		return r;
	}
270 271 272 273 274 275 276

	private applyTask(task): void {
		task.apply();
		if (this.progress) {
			this.progress.worked(1);
		}
	}
277 278 279 280

	dispose(): void {
		this._tasks = dispose(this._tasks);
	}
E
Erich Gamma 已提交
281 282 283
}

export interface BulkEdit {
284
	progress(progress: IProgressRunner);
E
Erich Gamma 已提交
285 286 287 288
	add(edit: IResourceEdit[]): void;
	finish(): TPromise<ISelection>;
}

289 290
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 已提交
291
	bulk.add(edits);
292
	bulk.progress(progress);
E
Erich Gamma 已提交
293 294 295
	return bulk.finish();
}

296
export function createBulkEdit(textModelResolverService: ITextModelResolverService, editor?: ICommonCodeEditor, fileService?: IFileService): BulkEdit {
E
Erich Gamma 已提交
297 298

	let all: IResourceEdit[] = [];
299
	let recording = new ChangeRecorder(fileService).start();
300 301 302
	let progressRunner: IProgressRunner;

	function progress(progress: IProgressRunner) {
J
Johannes Rieken 已提交
303
		progressRunner = progress;
304
	}
E
Erich Gamma 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

	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 已提交
323
		return undefined;
E
Erich Gamma 已提交
324 325 326 327 328
	}

	function finish(): TPromise<ISelection> {

		if (all.length === 0) {
329
			return TPromise.as(undefined);
E
Erich Gamma 已提交
330 331 332 333 334 335 336 337
		}

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

		let uri: URI;
338
		let selections: Selection[];
E
Erich Gamma 已提交
339

340
		if (editor && editor.getModel()) {
341
			uri = editor.getModel().uri;
E
Erich Gamma 已提交
342 343 344
			selections = editor.getSelections();
		}

345
		const model = new BulkEditModel(textModelResolverService, uri, selections, all, progressRunner);
E
Erich Gamma 已提交
346 347 348 349 350 351 352 353 354

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

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

			recording.stop();
355 356 357 358

			const result = model.apply();
			model.dispose();
			return result;
E
Erich Gamma 已提交
359 360 361 362
		});
	}

	return {
363
		progress,
E
Erich Gamma 已提交
364 365
		add,
		finish
A
tslint  
Alex Dima 已提交
366
	};
E
Erich Gamma 已提交
367
}