bulkEdit.ts 8.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
/*---------------------------------------------------------------------------------------------
 *  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';
import URI from 'vs/base/common/uri';
import {merge} from 'vs/base/common/arrays';
import {IStringDictionary, forEach, values} from 'vs/base/common/collections';
import {TPromise} from 'vs/base/common/winjs.base';
import {IEventService} from 'vs/platform/event/common/event';
import {IEditorService} from 'vs/platform/editor/common/editor';
import {IRange, ISelection, IEditorSelection, IModel, IIdentifiedSingleEditOperation} from 'vs/editor/common/editorCommon';
import {ICodeEditor} from 'vs/editor/browser/editorBrowser';
import {Range} from 'vs/editor/common/core/range';
import {Selection} from 'vs/editor/common/core/selection';
import {IFileChange, EventType as FileEventType, FileChangesEvent} from 'vs/platform/files/common/files';
import {EditOperation} from 'vs/editor/common/core/editOperation';

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

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

class ChangeRecorder {

	private _eventService: IEventService;

	constructor(eventService: IEventService) {
		this._eventService = eventService;
	}

	public start(): IRecording {

		var changes: IStringDictionary<IFileChange[]> = Object.create(null);

		var stop = this._eventService.addListener(FileEventType.FILE_CHANGES,(event: FileChangesEvent) => {
			event.changes.forEach(change => {

				var key = String(change.resource),
					array = changes[key];

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

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

		return {
			stop,
			hasChanged: (resource: URI) => !!changes[resource.toString()],
			allChanges: () => merge(values(changes))
		};
	}
}

class EditTask {

	private _initialSelections: IEditorSelection[];
	private _endCursorSelection: IEditorSelection;
	private _model: IModel;
	private _edits: IIdentifiedSingleEditOperation[];

	constructor(model: IModel) {
		this._endCursorSelection = null;
		this._model = model;
		this._edits = [];
	}

	public addEdit(edit: IResourceEdit): void {
		var range: IRange;
		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));
	}

	protected _getInitialSelections(): IEditorSelection[] {
		var firstRange = this._edits[0].range;
		var initialSelection = Selection.createSelection(
			firstRange.startLineNumber,
			firstRange.startColumn,
			firstRange.endLineNumber,
			firstRange.endColumn
		);
		return [initialSelection];
	}

	private _getEndCursorSelections(inverseEditOperations:IIdentifiedSingleEditOperation[]): IEditorSelection[] {
		var relevantEditIndex = 0;
		for (var i = 0; i < inverseEditOperations.length; i++) {
			var editRange = inverseEditOperations[i].range;
			for (var j = 0; j < this._initialSelections.length; j++) {
				var selectionRange = this._initialSelections[j];
				if (Range.areIntersectingOrTouching(editRange, selectionRange)) {
					relevantEditIndex = i;
					break;
				}
			}
		}

		var srcRange = inverseEditOperations[relevantEditIndex].range;
		this._endCursorSelection = Selection.createSelection(
			srcRange.endLineNumber,
			srcRange.endColumn,
			srcRange.endLineNumber,
			srcRange.endColumn
		);
		return [this._endCursorSelection];
	}

	public getEndCursorSelection(): IEditorSelection {
		return this._endCursorSelection;
	}

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

class SourceModelEditTask extends EditTask {

	private _knownInitialSelections:IEditorSelection[];

	constructor(model: IModel, initialSelections:IEditorSelection[]) {
		super(model);
		this._knownInitialSelections = initialSelections;
	}

	protected _getInitialSelections(): IEditorSelection[] {
		return this._knownInitialSelections;
	}
}

class BulkEditModel {

	private _editorService: IEditorService;
	private _numberOfResourcesToModify: number = 0;
	private _numberOfChanges: number = 0;
	private _edits: IStringDictionary<IResourceEdit[]> = Object.create(null);
	private _tasks: EditTask[];
	private _sourceModel: URI;
	private _sourceSelections: IEditorSelection[];
	private _sourceModelTask: SourceModelEditTask;

	constructor(editorService: IEditorService, sourceModel: URI, sourceSelections: IEditorSelection[], edits: IResourceEdit[]) {
		this._editorService = editorService;
		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 {
		var array = this._edits[edit.resource.toString()];
		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 = [];
		var promises: TPromise<any>[] = [];

		forEach(this._edits, entry => {
			var promise = this._editorService.resolveEditorModel({ resource: URI.parse(entry.key) }).then(model => {
				if (!model || !model.textEditorModel) {
					throw new Error(`Cannot load file ${entry.key}`);
				}

				var textEditorModel = <IModel>model.textEditorModel,
					task: EditTask;

				if (textEditorModel.getAssociatedResource().equals(this._sourceModel)) {
					this._sourceModelTask = new SourceModelEditTask(textEditorModel, this._sourceSelections);
					task = this._sourceModelTask;
				} else {
					task = new EditTask(textEditorModel);
				}

				entry.value.forEach(edit => task.addEdit(edit));
				this._tasks.push(task);
			});
			promises.push(promise);
		});

		return TPromise.join(promises).then(_ => this);
	}

	public apply(): IEditorSelection {
		this._tasks.forEach(task => task.apply());
		var r: IEditorSelection = null;
		if (this._sourceModelTask) {
			r = this._sourceModelTask.getEndCursorSelection();
		}
		return r;
	}
}

export interface BulkEdit {
	add(edit: IResourceEdit[]): void;
	finish(): TPromise<ISelection>;
}

export function bulkEdit(eventService:IEventService, editorService:IEditorService, editor:ICodeEditor, edits:IResourceEdit[]):TPromise<any> {
	let bulk = createBulkEdit(eventService, editorService, editor);
	bulk.add(edits);
	return bulk.finish();
}

export function createBulkEdit(eventService: IEventService, editorService: IEditorService, editor: ICodeEditor): BulkEdit {

	let all: IResourceEdit[] = [];
	let recording = new ChangeRecorder(eventService).start();

	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) {
			return;
		}

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

		let uri: URI;
		let selections: IEditorSelection[];

		if (editor) {
			uri = editor.getModel().getAssociatedResource();
			selections = editor.getSelections();
		}

		let model = new BulkEditModel(editorService, uri, selections, all);

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

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

			recording.stop();
			return model.apply();
		});
	}

	return {
		add,
		finish
	}
}