cursorCollection.ts 12.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

A
Alex Dima 已提交
7 8
import {onUnexpectedError} from 'vs/base/common/errors';
import {IModeConfiguration, IOneCursorState, IViewModelHelper, OneCursor} from 'vs/editor/common/controller/oneCursor';
E
Erich Gamma 已提交
9
import {Selection} from 'vs/editor/common/core/selection';
10
import {IConfiguration, IModel, ISelection} from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
11
import {IAutoClosingPair} from 'vs/editor/common/modes';
A
Alex Dima 已提交
12
import {Position} from 'vs/editor/common/core/position';
13
import {LanguageConfigurationRegistry} from 'vs/editor/common/modes/languageConfigurationRegistry';
E
Erich Gamma 已提交
14 15 16 17 18 19 20 21 22

export interface ICursorCollectionState {
	primary: IOneCursorState;
	secondary: IOneCursorState[];
}

export class CursorCollection {

	private editorId: number;
A
Alex Dima 已提交
23 24
	private model: IModel;
	private configuration: IConfiguration;
E
Erich Gamma 已提交
25 26 27 28 29 30 31 32 33 34
	private modeConfiguration: IModeConfiguration;

	private primaryCursor: OneCursor;
	private secondaryCursors: OneCursor[];

	// An index which identifies the last cursor that was added / moved (think Ctrl+drag)
	private lastAddedCursorIndex: number;

	private viewModelHelper:IViewModelHelper;

A
Alex Dima 已提交
35
	constructor(editorId: number, model: IModel, configuration: IConfiguration, viewModelHelper:IViewModelHelper) {
E
Erich Gamma 已提交
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
		this.editorId = editorId;
		this.model = model;
		this.configuration = configuration;
		this.viewModelHelper = viewModelHelper;
		this.modeConfiguration = this.getModeConfiguration();

		this.primaryCursor = new OneCursor(this.editorId, this.model, this.configuration, this.modeConfiguration, this.viewModelHelper);
		this.secondaryCursors = [];
		this.lastAddedCursorIndex = 0;
	}

	public dispose(): void {
		this.primaryCursor.dispose();
		this.killSecondaryCursors();
	}

	public saveState(): ICursorCollectionState {
		return {
			primary: this.primaryCursor.saveState(),
			secondary: this.secondaryCursors.map(c => c.saveState())
		};
	}

	public restoreState(state: ICursorCollectionState): void {
		this.primaryCursor.restoreState(state.primary);
		this.killSecondaryCursors();
		for (var i = 0; i < state.secondary.length; i++) {
			this.addSecondaryCursor(null);
			this.secondaryCursors[i].restoreState(state.secondary[i]);
		}
	}


	public updateMode(): void {
		this.modeConfiguration = this.getModeConfiguration();
		this.getAll().forEach((cursor) => {
			cursor.updateModeConfiguration(this.modeConfiguration);
		});
	}

	public getAll(): OneCursor[] {
		var result: OneCursor[] = [];
		result.push(this.primaryCursor);
		result = result.concat(this.secondaryCursors);
		return result;
	}

A
Alex Dima 已提交
83
	public getPosition(index: number): Position {
E
Erich Gamma 已提交
84 85 86 87 88 89 90
		if (index === 0) {
			return this.primaryCursor.getPosition();
		} else {
			return this.secondaryCursors[index - 1].getPosition();
		}
	}

A
Alex Dima 已提交
91
	public getViewPosition(index: number): Position {
E
Erich Gamma 已提交
92 93 94 95 96 97 98
		if (index === 0) {
			return this.primaryCursor.getViewPosition();
		} else {
			return this.secondaryCursors[index - 1].getViewPosition();
		}
	}

A
Alex Dima 已提交
99 100
	public getPositions(): Position[] {
		var result: Position[] = [];
E
Erich Gamma 已提交
101 102 103 104 105 106 107
		result.push(this.primaryCursor.getPosition());
		for (var i = 0, len = this.secondaryCursors.length; i < len; i++) {
			result.push(this.secondaryCursors[i].getPosition());
		}
		return result;
	}

A
Alex Dima 已提交
108 109
	public getViewPositions(): Position[] {
		var result: Position[] = [];
E
Erich Gamma 已提交
110 111 112 113 114 115 116
		result.push(this.primaryCursor.getViewPosition());
		for (var i = 0, len = this.secondaryCursors.length; i < len; i++) {
			result.push(this.secondaryCursors[i].getViewPosition());
		}
		return result;
	}

117
	public getSelection(index: number): Selection {
E
Erich Gamma 已提交
118 119 120 121 122 123 124
		if (index === 0) {
			return this.primaryCursor.getSelection();
		} else {
			return this.secondaryCursors[index - 1].getSelection();
		}
	}

125 126
	public getSelections(): Selection[] {
		var result: Selection[] = [];
E
Erich Gamma 已提交
127 128 129 130 131 132 133
		result.push(this.primaryCursor.getSelection());
		for (var i = 0, len = this.secondaryCursors.length; i < len; i++) {
			result.push(this.secondaryCursors[i].getSelection());
		}
		return result;
	}

134 135
	public getViewSelections(): Selection[] {
		var result: Selection[] = [];
E
Erich Gamma 已提交
136 137 138 139 140 141 142
		result.push(this.primaryCursor.getViewSelection());
		for (var i = 0, len = this.secondaryCursors.length; i < len; i++) {
			result.push(this.secondaryCursors[i].getViewSelection());
		}
		return result;
	}

A
Alex Dima 已提交
143
	public setSelections(selections: ISelection[], viewSelections?: ISelection[]): void {
E
Erich Gamma 已提交
144 145
		this.primaryCursor.setSelection(selections[0]);
		this._setSecondarySelections(selections.slice(1));
A
Alex Dima 已提交
146 147 148 149 150 151 152

		if (viewSelections) {
			this.primaryCursor.setViewSelection(viewSelections[0]);
			for (let i = 0; i < this.secondaryCursors.length; i++) {
				this.secondaryCursors[i].setViewSelection(viewSelections[i + 1]);
			}
		}
E
Erich Gamma 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	}

	public killSecondaryCursors(): boolean {
		return (this._setSecondarySelections([]) > 0);
	}

	public normalize(): void {
		this._mergeCursorsIfNecessary();

		this.primaryCursor.adjustBracketDecorations();
		for (var i = 0, len = this.secondaryCursors.length; i < len; i++) {
			this.secondaryCursors[i].adjustBracketDecorations();
		}
	}

A
Alex Dima 已提交
168
	public addSecondaryCursor(selection: ISelection): void {
E
Erich Gamma 已提交
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
		var newCursor = new OneCursor(this.editorId, this.model, this.configuration, this.modeConfiguration, this.viewModelHelper);
		if (selection) {
			newCursor.setSelection(selection);
		}
		this.secondaryCursors.push(newCursor);
		this.lastAddedCursorIndex = this.secondaryCursors.length;
	}

	public duplicateCursors(): void {
		var newCursors:OneCursor[] = [];

		newCursors.push(this.primaryCursor.duplicate());
		for (var i = 0, len = this.secondaryCursors.length; i < len; i++) {
			newCursors.push(this.secondaryCursors[i].duplicate());
		}

		this.secondaryCursors = this.secondaryCursors.concat(newCursors);
		this.lastAddedCursorIndex = this.secondaryCursors.length;
	}

	public getLastAddedCursor(): OneCursor {
		if (this.secondaryCursors.length === 0 || this.lastAddedCursorIndex === 0) {
			return this.primaryCursor;
		}
		return this.secondaryCursors[this.lastAddedCursorIndex - 1];
	}

	/**
	 * Creates or disposes secondary cursors as necessary to match the number of `secondarySelections`.
	 * Return value:
	 * 		- a positive number indicates the number of secondary cursors added
	 * 		- a negative number indicates the number of secondary cursors removed
	 * 		- 0 indicates that no changes have been done to the secondary cursors list
	 */
A
Alex Dima 已提交
203
	private _setSecondarySelections(secondarySelections: ISelection[]): number {
E
Erich Gamma 已提交
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
		var secondaryCursorsLength = this.secondaryCursors.length;
		var secondarySelectionsLength = secondarySelections.length;
		var returnValue = secondarySelectionsLength - secondaryCursorsLength;

		if (secondaryCursorsLength < secondarySelectionsLength) {
			var createCnt = secondarySelectionsLength - secondaryCursorsLength;
			for (var i = 0; i < createCnt; i++) {
				this.addSecondaryCursor(null);
			}
		} else if (secondaryCursorsLength > secondarySelectionsLength) {
			var removeCnt = secondaryCursorsLength - secondarySelectionsLength;
			for (var i = 0; i < removeCnt; i++) {
				this._removeSecondaryCursor(this.secondaryCursors.length - 1);
			}
		}

		for (var i = 0; i < secondarySelectionsLength; i++) {
			if (secondarySelections[i]) {
				this.secondaryCursors[i].setSelection(secondarySelections[i]);
			}
		}

		return returnValue;
	}

	private _removeSecondaryCursor(removeIndex: number): void {
		if (this.lastAddedCursorIndex >= removeIndex + 1) {
			this.lastAddedCursorIndex--;
		}
		this.secondaryCursors[removeIndex].dispose();
		this.secondaryCursors.splice(removeIndex, 1);
	}

	private _mergeCursorsIfNecessary(): void {
		if (this.secondaryCursors.length === 0) {
			return;
		}
		var cursors = this.getAll();
		var sortedCursors:{
			index: number;
244 245
			selection: Selection;
			viewSelection: Selection;
E
Erich Gamma 已提交
246 247 248 249
		}[] = [];
		for (var i = 0; i < cursors.length; i++) {
			sortedCursors.push({
				index: i,
A
Alex Dima 已提交
250 251
				selection: cursors[i].getSelection(),
				viewSelection: cursors[i].getViewSelection()
E
Erich Gamma 已提交
252 253 254 255
			});
		}

		sortedCursors.sort((a, b) => {
A
Alex Dima 已提交
256 257
			if (a.viewSelection.startLineNumber === b.viewSelection.startLineNumber) {
				return a.viewSelection.startColumn - b.viewSelection.startColumn;
E
Erich Gamma 已提交
258
			}
A
Alex Dima 已提交
259
			return a.viewSelection.startLineNumber - b.viewSelection.startLineNumber;
E
Erich Gamma 已提交
260 261 262 263 264 265
		});

		for (var sortedCursorIndex = 0; sortedCursorIndex < sortedCursors.length - 1; sortedCursorIndex++) {
			var current = sortedCursors[sortedCursorIndex];
			var next = sortedCursors[sortedCursorIndex + 1];

A
Alex Dima 已提交
266 267
			var currentViewSelection = current.viewSelection;
			var nextViewSelection = next.viewSelection;
E
Erich Gamma 已提交
268

A
Alex Dima 已提交
269
			if (nextViewSelection.getStartPosition().isBeforeOrEqual(currentViewSelection.getEndPosition())) {
E
Erich Gamma 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
				var winnerSortedCursorIndex = current.index < next.index ? sortedCursorIndex : sortedCursorIndex + 1;
				var looserSortedCursorIndex = current.index < next.index ? sortedCursorIndex + 1 : sortedCursorIndex;

				var looserIndex = sortedCursors[looserSortedCursorIndex].index;
				var winnerIndex = sortedCursors[winnerSortedCursorIndex].index;

				var looserSelection = sortedCursors[looserSortedCursorIndex].selection;
				var winnerSelection = sortedCursors[winnerSortedCursorIndex].selection;

				if (!looserSelection.equalsSelection(winnerSelection)) {
					var resultingRange = looserSelection.plusRange(winnerSelection);
					var looserSelectionIsLTR = (looserSelection.selectionStartLineNumber === looserSelection.startLineNumber && looserSelection.selectionStartColumn === looserSelection.startColumn);
					var winnerSelectionIsLTR = (winnerSelection.selectionStartLineNumber === winnerSelection.startLineNumber && winnerSelection.selectionStartColumn === winnerSelection.startColumn);

					// Give more importance to the last added cursor (think Ctrl-dragging + hitting another cursor)
					var resultingSelectionIsLTR:boolean;
					if (looserIndex === this.lastAddedCursorIndex) {
						resultingSelectionIsLTR = looserSelectionIsLTR;
						this.lastAddedCursorIndex = winnerIndex;
					} else {
						// Winner takes it all
						resultingSelectionIsLTR = winnerSelectionIsLTR;
					}

294
					var resultingSelection: Selection;
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 318 319 320
					if (resultingSelectionIsLTR) {
						resultingSelection = new Selection(resultingRange.startLineNumber, resultingRange.startColumn, resultingRange.endLineNumber, resultingRange.endColumn);
					} else {
						resultingSelection = new Selection(resultingRange.endLineNumber, resultingRange.endColumn, resultingRange.startLineNumber, resultingRange.startColumn);
					}

					sortedCursors[winnerSortedCursorIndex].selection = resultingSelection;
					cursors[winnerIndex].setSelection(resultingSelection);
				}

				for (var j = 0; j < sortedCursors.length; j++) {
					if (sortedCursors[j].index > looserIndex) {
						sortedCursors[j].index--;
					}
				}

				cursors.splice(looserIndex, 1);
				sortedCursors.splice(looserSortedCursorIndex, 1);
				this._removeSecondaryCursor(looserIndex - 1);

				sortedCursorIndex--;
			}
		}
	}

	private getModeConfiguration(): IModeConfiguration {
321
		let i: number;
E
Erich Gamma 已提交
322

323
		let result: IModeConfiguration = {
E
Erich Gamma 已提交
324 325 326 327 328 329
			electricChars: {},
			autoClosingPairsOpen: {},
			autoClosingPairsClose: {},
			surroundingPairs: {}
		};

330

A
Alex Dima 已提交
331
		let electricCharSupport = LanguageConfigurationRegistry.getElectricCharacterSupport(this.model.getMode().getId());
332 333
		if (electricCharSupport) {
			let electricChars: string[] = null;
E
Erich Gamma 已提交
334
			try {
335
				electricChars = electricCharSupport.getElectricCharacters();
E
Erich Gamma 已提交
336
			} catch(e) {
A
Alex Dima 已提交
337
				onUnexpectedError(e);
E
Erich Gamma 已提交
338 339
				electricChars = null;
			}
340 341 342 343
			if (electricChars) {
				for (i = 0; i < electricChars.length; i++) {
					result.electricChars[electricChars[i]] = true;
				}
E
Erich Gamma 已提交
344 345 346
			}
		}

A
Alex Dima 已提交
347
		let characterPairSupport = LanguageConfigurationRegistry.getCharacterPairSupport(this.model.getMode().getId());
348 349
		if (characterPairSupport) {
			let autoClosingPairs: IAutoClosingPair[];
E
Erich Gamma 已提交
350
			try {
351
				autoClosingPairs = characterPairSupport.getAutoClosingPairs();
E
Erich Gamma 已提交
352
			} catch(e) {
A
Alex Dima 已提交
353
				onUnexpectedError(e);
E
Erich Gamma 已提交
354 355
				autoClosingPairs = null;
			}
356 357 358 359 360
			if (autoClosingPairs) {
				for (i = 0; i < autoClosingPairs.length; i++) {
					result.autoClosingPairsOpen[autoClosingPairs[i].open] = autoClosingPairs[i].close;
					result.autoClosingPairsClose[autoClosingPairs[i].close] = autoClosingPairs[i].open;
				}
E
Erich Gamma 已提交
361 362
			}

363
			let surroundingPairs: IAutoClosingPair[];
E
Erich Gamma 已提交
364
			try {
365
				surroundingPairs = characterPairSupport.getSurroundingPairs();
E
Erich Gamma 已提交
366
			} catch(e) {
A
Alex Dima 已提交
367
				onUnexpectedError(e);
E
Erich Gamma 已提交
368 369
				surroundingPairs = null;
			}
370 371 372 373
			if (surroundingPairs) {
				for (i = 0; i < surroundingPairs.length; i++) {
					result.surroundingPairs[surroundingPairs[i].open] = surroundingPairs[i].close;
				}
E
Erich Gamma 已提交
374 375 376 377 378 379
			}
		}

		return result;
	}
}