cursorWordOperations.ts 16.7 KB
Newer Older
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';

7
import { SingleCursorState, CursorConfiguration, ICursorSimpleModel } from 'vs/editor/common/controller/cursorCommon';
8 9 10
import { Position } from 'vs/editor/common/core/position';
import { CharCode } from 'vs/base/common/charCode';
import { CharacterClassifier } from 'vs/editor/common/core/characterClassifier';
A
Alex Dima 已提交
11
import { SingleMoveOperationResult } from 'vs/editor/common/controller/cursorMoveOperations';
12
import { CursorChangeReason } from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
13 14
import * as strings from 'vs/base/common/strings';
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
15
import { Selection } from 'vs/editor/common/core/selection';
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

export interface IFindWordResult {
	/**
	 * The index where the word starts.
	 */
	start: number;
	/**
	 * The index where the word ends.
	 */
	end: number;
	/**
	 * The word type.
	 */
	wordType: WordType;
}

export const enum WordType {
	None = 0,
	Regular = 1,
	Separator = 2
}

38
export const enum WordCharacterClass {
39 40 41 42 43 44 45 46 47 48
	Regular = 0,
	Whitespace = 1,
	WordSeparator = 2
}

export const enum WordNavigationType {
	WordStart = 0,
	WordEnd = 1
}

49
export class WordCharacterClassifier extends CharacterClassifier<WordCharacterClass> {
50 51

	constructor(wordSeparators: string) {
52
		super(WordCharacterClass.Regular);
53 54

		for (let i = 0, len = wordSeparators.length; i < len; i++) {
55
			this.set(wordSeparators.charCodeAt(i), WordCharacterClass.WordSeparator);
56 57
		}

58 59
		this.set(CharCode.Space, WordCharacterClass.Whitespace);
		this.set(CharCode.Tab, WordCharacterClass.Whitespace);
60 61 62 63 64 65 66 67 68 69 70 71 72 73
	}

}

function once<R>(computeFn: (input: string) => R): (input: string) => R {
	let cache: { [key: string]: R; } = {}; // TODO@Alex unbounded cache
	return (input: string): R => {
		if (!cache.hasOwnProperty(input)) {
			cache[input] = computeFn(input);
		}
		return cache[input];
	};
}

74
export const getMapForWordSeparators = once<WordCharacterClassifier>(
75 76 77 78 79 80 81 82 83 84
	(input) => new WordCharacterClassifier(input)
);

export class WordOperations {

	private static _createWord(lineContent: string, wordType: WordType, start: number, end: number): IFindWordResult {
		// console.log('WORD ==> ' + start + ' => ' + end + ':::: <<<' + lineContent.substring(start, end) + '>>>');
		return { start: start, end: end, wordType: wordType };
	}

A
Alex Dima 已提交
85
	private static findPreviousWordOnLine(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position): IFindWordResult {
86 87 88 89 90 91 92 93 94 95
		let lineContent = model.getLineContent(position.lineNumber);
		return this._findPreviousWordOnLine(lineContent, wordSeparators, position);
	}

	private static _findPreviousWordOnLine(lineContent: string, wordSeparators: WordCharacterClassifier, position: Position): IFindWordResult {
		let wordType = WordType.None;
		for (let chIndex = position.column - 2; chIndex >= 0; chIndex--) {
			let chCode = lineContent.charCodeAt(chIndex);
			let chClass = wordSeparators.get(chCode);

96
			if (chClass === WordCharacterClass.Regular) {
97 98 99 100
				if (wordType === WordType.Separator) {
					return this._createWord(lineContent, wordType, chIndex + 1, this._findEndOfWord(lineContent, wordSeparators, wordType, chIndex + 1));
				}
				wordType = WordType.Regular;
101
			} else if (chClass === WordCharacterClass.WordSeparator) {
102 103 104 105
				if (wordType === WordType.Regular) {
					return this._createWord(lineContent, wordType, chIndex + 1, this._findEndOfWord(lineContent, wordSeparators, wordType, chIndex + 1));
				}
				wordType = WordType.Separator;
106
			} else if (chClass === WordCharacterClass.Whitespace) {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
				if (wordType !== WordType.None) {
					return this._createWord(lineContent, wordType, chIndex + 1, this._findEndOfWord(lineContent, wordSeparators, wordType, chIndex + 1));
				}
			}
		}

		if (wordType !== WordType.None) {
			return this._createWord(lineContent, wordType, 0, this._findEndOfWord(lineContent, wordSeparators, wordType, 0));
		}

		return null;
	}

	private static _findEndOfWord(lineContent: string, wordSeparators: WordCharacterClassifier, wordType: WordType, startIndex: number): number {
		let len = lineContent.length;
		for (let chIndex = startIndex; chIndex < len; chIndex++) {
			let chCode = lineContent.charCodeAt(chIndex);
			let chClass = wordSeparators.get(chCode);

126
			if (chClass === WordCharacterClass.Whitespace) {
127 128
				return chIndex;
			}
129
			if (wordType === WordType.Regular && chClass === WordCharacterClass.WordSeparator) {
130 131
				return chIndex;
			}
132
			if (wordType === WordType.Separator && chClass === WordCharacterClass.Regular) {
133 134 135 136 137 138
				return chIndex;
			}
		}
		return len;
	}

A
Alex Dima 已提交
139
	private static findNextWordOnLine(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position): IFindWordResult {
140 141 142 143 144 145 146 147 148 149 150 151
		let lineContent = model.getLineContent(position.lineNumber);
		return this._findNextWordOnLine(lineContent, wordSeparators, position);
	}

	private static _findNextWordOnLine(lineContent: string, wordSeparators: WordCharacterClassifier, position: Position): IFindWordResult {
		let wordType = WordType.None;
		let len = lineContent.length;

		for (let chIndex = position.column - 1; chIndex < len; chIndex++) {
			let chCode = lineContent.charCodeAt(chIndex);
			let chClass = wordSeparators.get(chCode);

152
			if (chClass === WordCharacterClass.Regular) {
153 154 155 156
				if (wordType === WordType.Separator) {
					return this._createWord(lineContent, wordType, this._findStartOfWord(lineContent, wordSeparators, wordType, chIndex - 1), chIndex);
				}
				wordType = WordType.Regular;
157
			} else if (chClass === WordCharacterClass.WordSeparator) {
158 159 160 161
				if (wordType === WordType.Regular) {
					return this._createWord(lineContent, wordType, this._findStartOfWord(lineContent, wordSeparators, wordType, chIndex - 1), chIndex);
				}
				wordType = WordType.Separator;
162
			} else if (chClass === WordCharacterClass.Whitespace) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
				if (wordType !== WordType.None) {
					return this._createWord(lineContent, wordType, this._findStartOfWord(lineContent, wordSeparators, wordType, chIndex - 1), chIndex);
				}
			}
		}

		if (wordType !== WordType.None) {
			return this._createWord(lineContent, wordType, this._findStartOfWord(lineContent, wordSeparators, wordType, len - 1), len);
		}

		return null;
	}

	private static _findStartOfWord(lineContent: string, wordSeparators: WordCharacterClassifier, wordType: WordType, startIndex: number): number {
		for (let chIndex = startIndex; chIndex >= 0; chIndex--) {
			let chCode = lineContent.charCodeAt(chIndex);
			let chClass = wordSeparators.get(chCode);

181
			if (chClass === WordCharacterClass.Whitespace) {
182 183
				return chIndex + 1;
			}
184
			if (wordType === WordType.Regular && chClass === WordCharacterClass.WordSeparator) {
185 186
				return chIndex + 1;
			}
187
			if (wordType === WordType.Separator && chClass === WordCharacterClass.Regular) {
188 189 190 191 192 193
				return chIndex + 1;
			}
		}
		return 0;
	}

194
	public static moveWordLeft(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position, wordNavigationType: WordNavigationType): Position {
195 196 197 198 199 200 201 202 203 204
		let lineNumber = position.lineNumber;
		let column = position.column;

		if (column === 1) {
			if (lineNumber > 1) {
				lineNumber = lineNumber - 1;
				column = model.getLineMaxColumn(lineNumber);
			}
		}

A
Alex Dima 已提交
205
		let prevWordOnLine = WordOperations.findPreviousWordOnLine(wordSeparators, model, new Position(lineNumber, column));
206 207 208 209 210 211 212 213 214

		if (wordNavigationType === WordNavigationType.WordStart) {
			if (prevWordOnLine) {
				column = prevWordOnLine.start + 1;
			} else {
				column = 1;
			}
		} else {
			if (prevWordOnLine && column <= prevWordOnLine.end + 1) {
A
Alex Dima 已提交
215
				prevWordOnLine = WordOperations.findPreviousWordOnLine(wordSeparators, model, new Position(lineNumber, prevWordOnLine.start + 1));
216 217 218 219 220 221 222 223
			}
			if (prevWordOnLine) {
				column = prevWordOnLine.end + 1;
			} else {
				column = 1;
			}
		}

A
Alex Dima 已提交
224
		return new Position(lineNumber, column);
225 226
	}

227
	public static moveWordRight(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position, wordNavigationType: WordNavigationType): Position {
228 229 230 231 232 233 234 235 236 237
		let lineNumber = position.lineNumber;
		let column = position.column;

		if (column === model.getLineMaxColumn(lineNumber)) {
			if (lineNumber < model.getLineCount()) {
				lineNumber = lineNumber + 1;
				column = 1;
			}
		}

A
Alex Dima 已提交
238
		let nextWordOnLine = WordOperations.findNextWordOnLine(wordSeparators, model, new Position(lineNumber, column));
239 240 241 242 243 244 245 246 247

		if (wordNavigationType === WordNavigationType.WordEnd) {
			if (nextWordOnLine) {
				column = nextWordOnLine.end + 1;
			} else {
				column = model.getLineMaxColumn(lineNumber);
			}
		} else {
			if (nextWordOnLine && column >= nextWordOnLine.start + 1) {
A
Alex Dima 已提交
248
				nextWordOnLine = WordOperations.findNextWordOnLine(wordSeparators, model, new Position(lineNumber, nextWordOnLine.end + 1));
249 250 251 252 253 254 255 256
			}
			if (nextWordOnLine) {
				column = nextWordOnLine.start + 1;
			} else {
				column = model.getLineMaxColumn(lineNumber);
			}
		}

A
Alex Dima 已提交
257
		return new Position(lineNumber, column);
258
	}
A
Alex Dima 已提交
259

A
Alex Dima 已提交
260 261 262 263
	private static _deleteWordLeftWhitespace(model: ICursorSimpleModel, position: Position): Range {
		const lineContent = model.getLineContent(position.lineNumber);
		const startIndex = position.column - 2;
		const lastNonWhitespace = strings.lastNonWhitespaceIndex(lineContent, startIndex);
A
Alex Dima 已提交
264
		if (lastNonWhitespace + 1 < startIndex) {
A
Alex Dima 已提交
265
			return new Range(position.lineNumber, lastNonWhitespace + 2, position.lineNumber, position.column);
A
Alex Dima 已提交
266 267 268 269
		}
		return null;
	}

270
	public static _deleteWordLeft(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, selection: Selection, whitespaceHeuristics: boolean, wordNavigationType: WordNavigationType): Range {
A
Alex Dima 已提交
271 272
		if (!selection.isEmpty()) {
			return selection;
A
Alex Dima 已提交
273 274
		}

A
Alex Dima 已提交
275
		const position = new Position(selection.positionLineNumber, selection.positionColumn);
A
Alex Dima 已提交
276

A
Alex Dima 已提交
277 278
		let lineNumber = position.lineNumber;
		let column = position.column;
A
Alex Dima 已提交
279

A
Alex Dima 已提交
280 281 282 283
		if (lineNumber === 1 && column === 1) {
			// Ignore deleting at beginning of file
			return null;
		}
A
Alex Dima 已提交
284

A
Alex Dima 已提交
285 286 287 288
		if (whitespaceHeuristics) {
			let r = this._deleteWordLeftWhitespace(model, position);
			if (r) {
				return r;
A
Alex Dima 已提交
289
			}
A
Alex Dima 已提交
290
		}
A
Alex Dima 已提交
291

A
Alex Dima 已提交
292
		let prevWordOnLine = WordOperations.findPreviousWordOnLine(wordSeparators, model, position);
A
Alex Dima 已提交
293

A
Alex Dima 已提交
294 295 296 297 298
		if (wordNavigationType === WordNavigationType.WordStart) {
			if (prevWordOnLine) {
				column = prevWordOnLine.start + 1;
			} else {
				if (column > 1 || lineNumber === 1) {
A
Alex Dima 已提交
299
					column = 1;
A
Alex Dima 已提交
300 301 302 303 304 305 306 307
				} else {
					lineNumber--;
					prevWordOnLine = WordOperations.findPreviousWordOnLine(wordSeparators, model, new Position(lineNumber, model.getLineMaxColumn(lineNumber)));
					if (prevWordOnLine) {
						column = prevWordOnLine.start + 1;
					} else {
						column = 1;
					}
A
Alex Dima 已提交
308
				}
A
Alex Dima 已提交
309 310 311 312 313 314 315
			}
		} else {
			if (prevWordOnLine && column <= prevWordOnLine.end + 1) {
				prevWordOnLine = WordOperations.findPreviousWordOnLine(wordSeparators, model, new Position(lineNumber, prevWordOnLine.start + 1));
			}
			if (prevWordOnLine) {
				column = prevWordOnLine.end + 1;
A
Alex Dima 已提交
316
			} else {
A
Alex Dima 已提交
317
				if (column > 1 || lineNumber === 1) {
A
Alex Dima 已提交
318
					column = 1;
A
Alex Dima 已提交
319 320 321 322 323 324 325 326
				} else {
					lineNumber--;
					prevWordOnLine = WordOperations.findPreviousWordOnLine(wordSeparators, model, new Position(lineNumber, model.getLineMaxColumn(lineNumber)));
					if (prevWordOnLine) {
						column = prevWordOnLine.end + 1;
					} else {
						column = 1;
					}
A
Alex Dima 已提交
327 328 329 330
				}
			}
		}

A
Alex Dima 已提交
331 332 333
		return new Range(lineNumber, column, position.lineNumber, position.column);
	}

A
Alex Dima 已提交
334 335 336 337 338 339 340 341 342 343 344
	private static _findFirstNonWhitespaceChar(str: string, startIndex: number): number {
		let len = str.length;
		for (let chIndex = startIndex; chIndex < len; chIndex++) {
			let ch = str.charAt(chIndex);
			if (ch !== ' ' && ch !== '\t') {
				return chIndex;
			}
		}
		return len;
	}

A
Alex Dima 已提交
345 346 347 348
	private static _deleteWordRightWhitespace(model: ICursorSimpleModel, position: Position): Range {
		const lineContent = model.getLineContent(position.lineNumber);
		const startIndex = position.column - 1;
		const firstNonWhitespace = this._findFirstNonWhitespaceChar(lineContent, startIndex);
A
Alex Dima 已提交
349 350
		if (startIndex + 1 < firstNonWhitespace) {
			// bingo
A
Alex Dima 已提交
351
			return new Range(position.lineNumber, position.column, position.lineNumber, firstNonWhitespace + 1);
A
Alex Dima 已提交
352 353 354 355
		}
		return null;
	}

356
	public static _deleteWordRight(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, selection: Selection, whitespaceHeuristics: boolean, wordNavigationType: WordNavigationType): Range {
A
Alex Dima 已提交
357 358 359
		if (!selection.isEmpty()) {
			return selection;
		}
A
Alex Dima 已提交
360

A
Alex Dima 已提交
361
		const position = new Position(selection.positionLineNumber, selection.positionColumn);
A
Alex Dima 已提交
362

A
Alex Dima 已提交
363 364
		let lineNumber = position.lineNumber;
		let column = position.column;
A
Alex Dima 已提交
365

A
Alex Dima 已提交
366 367 368 369 370 371
		const lineCount = model.getLineCount();
		const maxColumn = model.getLineMaxColumn(lineNumber);
		if (lineNumber === lineCount && column === maxColumn) {
			// Ignore deleting at end of file
			return null;
		}
A
Alex Dima 已提交
372

A
Alex Dima 已提交
373 374 375 376
		if (whitespaceHeuristics) {
			let r = this._deleteWordRightWhitespace(model, position);
			if (r) {
				return r;
A
Alex Dima 已提交
377
			}
A
Alex Dima 已提交
378
		}
A
Alex Dima 已提交
379

A
Alex Dima 已提交
380
		let nextWordOnLine = WordOperations.findNextWordOnLine(wordSeparators, model, position);
A
Alex Dima 已提交
381

A
Alex Dima 已提交
382 383 384 385 386 387
		if (wordNavigationType === WordNavigationType.WordEnd) {
			if (nextWordOnLine) {
				column = nextWordOnLine.end + 1;
			} else {
				if (column < maxColumn || lineNumber === lineCount) {
					column = maxColumn;
A
Alex Dima 已提交
388
				} else {
A
Alex Dima 已提交
389 390 391 392
					lineNumber++;
					nextWordOnLine = WordOperations.findNextWordOnLine(wordSeparators, model, new Position(lineNumber, 1));
					if (nextWordOnLine) {
						column = nextWordOnLine.start + 1;
A
Alex Dima 已提交
393
					} else {
A
Alex Dima 已提交
394
						column = model.getLineMaxColumn(lineNumber);
A
Alex Dima 已提交
395 396
					}
				}
A
Alex Dima 已提交
397 398 399 400 401 402 403
			}
		} else {
			if (nextWordOnLine && column >= nextWordOnLine.start + 1) {
				nextWordOnLine = WordOperations.findNextWordOnLine(wordSeparators, model, new Position(lineNumber, nextWordOnLine.end + 1));
			}
			if (nextWordOnLine) {
				column = nextWordOnLine.start + 1;
A
Alex Dima 已提交
404
			} else {
A
Alex Dima 已提交
405 406
				if (column < maxColumn || lineNumber === lineCount) {
					column = maxColumn;
A
Alex Dima 已提交
407
				} else {
A
Alex Dima 已提交
408 409 410 411
					lineNumber++;
					nextWordOnLine = WordOperations.findNextWordOnLine(wordSeparators, model, new Position(lineNumber, 1));
					if (nextWordOnLine) {
						column = nextWordOnLine.start + 1;
A
Alex Dima 已提交
412
					} else {
A
Alex Dima 已提交
413
						column = model.getLineMaxColumn(lineNumber);
A
Alex Dima 已提交
414 415 416 417 418
					}
				}
			}
		}

A
Alex Dima 已提交
419 420 421
		return new Range(lineNumber, column, position.lineNumber, position.column);
	}

A
Alex Dima 已提交
422
	public static word(config: CursorConfiguration, model: ICursorSimpleModel, cursor: SingleCursorState, inSelectionMode: boolean, position: Position): SingleMoveOperationResult {
A
Alex Dima 已提交
423 424
		const wordSeparators = getMapForWordSeparators(config.wordSeparators);
		let prevWord = WordOperations.findPreviousWordOnLine(wordSeparators, model, position);
A
Alex Dima 已提交
425
		let isInPrevWord = (prevWord && prevWord.wordType === WordType.Regular && prevWord.start < position.column - 1 && position.column - 1 <= prevWord.end);
A
Alex Dima 已提交
426
		let nextWord = WordOperations.findNextWordOnLine(wordSeparators, model, position);
A
Alex Dima 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
		let isInNextWord = (nextWord && nextWord.wordType === WordType.Regular && nextWord.start < position.column - 1 && position.column - 1 <= nextWord.end);

		if (!inSelectionMode || !cursor.hasSelection()) {

			let startColumn: number;
			let endColumn: number;

			if (isInPrevWord) {
				startColumn = prevWord.start + 1;
				endColumn = prevWord.end + 1;
			} else if (isInNextWord) {
				startColumn = nextWord.start + 1;
				endColumn = nextWord.end + 1;
			} else {
				if (prevWord) {
					startColumn = prevWord.end + 1;
				} else {
					startColumn = 1;
				}
				if (nextWord) {
					endColumn = nextWord.start + 1;
				} else {
					endColumn = model.getLineMaxColumn(position.lineNumber);
				}
			}

			return new SingleMoveOperationResult(
				new SingleCursorState(
					new Range(position.lineNumber, startColumn, position.lineNumber, endColumn), 0,
					new Position(position.lineNumber, endColumn), 0
				),
				false,
				CursorChangeReason.Explicit
			);
		}

		let startColumn: number;
		let endColumn: number;

		if (isInPrevWord) {
			startColumn = prevWord.start + 1;
			endColumn = prevWord.end + 1;
		} else if (isInNextWord) {
			startColumn = nextWord.start + 1;
			endColumn = nextWord.end + 1;
		} else {
			startColumn = position.column;
			endColumn = position.column;
		}

		let lineNumber = position.lineNumber;
		let column: number;
		if (position.isBeforeOrEqual(cursor.selectionStart.getStartPosition())) {
			column = startColumn;
			let possiblePosition = new Position(lineNumber, column);
			if (cursor.selectionStart.containsPosition(possiblePosition)) {
				column = cursor.selectionStart.endColumn;
			}
		} else {
			column = endColumn;
			let possiblePosition = new Position(lineNumber, column);
			if (cursor.selectionStart.containsPosition(possiblePosition)) {
				column = cursor.selectionStart.startColumn;
			}
		}

		return SingleMoveOperationResult.fromMove(cursor, cursor.hasSelection(), lineNumber, column, 0, false, CursorChangeReason.Explicit);
	}
495
}