cursor.test.ts 38.1 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
import * as assert from 'assert';
A
Alex Dima 已提交
8 9 10 11
import {Model} from 'vs/editor/common/model/model';
import {Cursor} from 'vs/editor/common/controller/cursor';
import {Position} from 'vs/editor/common/core/position';
import {Range} from 'vs/editor/common/core/range';
A
Alex Dima 已提交
12 13 14
import {BracketMode} from 'vs/editor/test/common/testModes';
import * as Modes from 'vs/editor/common/modes';
import {Handler, EventType, IPosition, ISelection, EndOfLinePreference} from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
15
import {MockConfiguration} from 'vs/editor/test/common/mocks/mockConfiguration';
E
Erich Gamma 已提交
16
import {EditOperation} from 'vs/editor/common/core/editOperation';
A
Alex Dima 已提交
17
import {AbstractState} from 'vs/editor/common/modes/abstractState';
18
import {RichEditSupport} from 'vs/editor/common/modes/supports/richEditSupport';
E
Erich Gamma 已提交
19

A
Alex Dima 已提交
20
let H = Handler;
E
Erich Gamma 已提交
21 22 23

// --------- utils

A
Alex Dima 已提交
24
function cursorCommand(cursor: Cursor, command: string, extraData?: any, sizeProvider?: { pageSize: number; }, overwriteSource?: string) {
E
Erich Gamma 已提交
25 26 27 28 29 30
	if (sizeProvider) {
		cursor.configuration.editor.pageSize = sizeProvider.pageSize;
	}
	cursor.configuration.handlerDispatcher.trigger(overwriteSource || 'tests', command, extraData);
}

A
Alex Dima 已提交
31 32
function moveTo(cursor: Cursor, lineNumber: number, column: number, inSelectionMode: boolean = false) {
	cursorCommand(cursor, inSelectionMode ? H.MoveToSelect : H.MoveTo, { position: new Position(lineNumber, column) });
E
Erich Gamma 已提交
33 34
}

A
Alex Dima 已提交
35
function moveLeft(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
36 37 38
	cursorCommand(cursor, inSelectionMode ? H.CursorLeftSelect : H.CursorLeft);
}

A
Alex Dima 已提交
39
function moveWordLeft(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
40 41 42
	cursorCommand(cursor, inSelectionMode ? H.CursorWordLeftSelect : H.CursorWordLeft);
}

A
Alex Dima 已提交
43
function moveRight(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
44 45 46
	cursorCommand(cursor, inSelectionMode ? H.CursorRightSelect : H.CursorRight);
}

A
Alex Dima 已提交
47
function moveWordRight(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
48 49 50
	cursorCommand(cursor, inSelectionMode ? H.CursorWordRightSelect : H.CursorWordRight);
}

A
Alex Dima 已提交
51
function moveDown(cursor: Cursor, linesCount: number, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
52 53 54 55 56 57 58
	if (linesCount === 1) {
		cursorCommand(cursor, inSelectionMode ? H.CursorDownSelect : H.CursorDown);
	} else {
		cursorCommand(cursor, inSelectionMode ? H.CursorPageDownSelect : H.CursorPageDown, null, { pageSize: linesCount });
	}
}

A
Alex Dima 已提交
59
function moveUp(cursor: Cursor, linesCount: number, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
60 61 62 63 64 65 66
	if (linesCount === 1) {
		cursorCommand(cursor, inSelectionMode ? H.CursorUpSelect : H.CursorUp);
	} else {
		cursorCommand(cursor, inSelectionMode ? H.CursorPageUpSelect : H.CursorPageUp, null, { pageSize: linesCount });
	}
}

A
Alex Dima 已提交
67
function moveToBeginningOfLine(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
68 69 70
	cursorCommand(cursor, inSelectionMode ? H.CursorHomeSelect : H.CursorHome);
}

A
Alex Dima 已提交
71
function moveToEndOfLine(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
72 73 74
	cursorCommand(cursor, inSelectionMode ?  H.CursorEndSelect : H.CursorEnd);
}

A
Alex Dima 已提交
75
function moveToBeginningOfBuffer(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
76 77 78
	cursorCommand(cursor, inSelectionMode ? H.CursorTopSelect : H.CursorTop);
}

A
Alex Dima 已提交
79
function moveToEndOfBuffer(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
80 81 82
	cursorCommand(cursor, inSelectionMode ? H.CursorBottomSelect : H.CursorBottom);
}

A
Alex Dima 已提交
83
function deleteWordLeft(cursor: Cursor) {
E
Erich Gamma 已提交
84 85 86
	cursorCommand(cursor, H.DeleteWordLeft);
}

A
Alex Dima 已提交
87
function deleteWordRight(cursor: Cursor) {
E
Erich Gamma 已提交
88 89 90
	cursorCommand(cursor, H.DeleteWordRight);
}

A
Alex Dima 已提交
91
function positionEqual(position:IPosition, lineNumber: number, column: number) {
E
Erich Gamma 已提交
92 93 94 95 96 97 98 99 100
	assert.deepEqual({
		lineNumber: position.lineNumber,
		column: position.column
	}, {
		lineNumber: lineNumber,
		column: column
	}, 'position equal');
}

A
Alex Dima 已提交
101
function selectionEqual(selection:ISelection, posLineNumber: number, posColumn: number, selLineNumber: number, selColumn: number) {
E
Erich Gamma 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114
	assert.deepEqual({
		selectionStartLineNumber: selection.selectionStartLineNumber,
		selectionStartColumn: selection.selectionStartColumn,
		positionLineNumber: selection.positionLineNumber,
		positionColumn: selection.positionColumn
	}, {
		selectionStartLineNumber: selLineNumber,
		selectionStartColumn: selColumn,
		positionLineNumber: posLineNumber,
		positionColumn: posColumn
	}, 'selection equal');
}

A
Alex Dima 已提交
115
function cursorEqual(cursor: Cursor, posLineNumber: number, posColumn: number, selLineNumber: number = posLineNumber, selColumn: number = posColumn) {
E
Erich Gamma 已提交
116 117 118 119 120 121
	positionEqual(cursor.getPosition(), posLineNumber, posColumn);
	selectionEqual(cursor.getSelection(), posLineNumber, posColumn, selLineNumber, selColumn);
}


suite('Editor Controller - Cursor', () => {
A
Alex Dima 已提交
122 123 124 125 126
	const LINE1 = '    \tMy First Line\t ';
	const LINE2 = '\tMy Second Line';
	const LINE3 = '    Third Line💩';
	const LINE4 = '';
	const LINE5 = '1';
E
Erich Gamma 已提交
127

A
Alex Dima 已提交
128 129 130
	let thisModel: Model;
	let thisConfiguration: MockConfiguration;
	let thisCursor: Cursor;
E
Erich Gamma 已提交
131 132

	setup(() => {
A
Alex Dima 已提交
133
		let text =
E
Erich Gamma 已提交
134 135 136 137 138 139
			LINE1 + '\r\n' +
			LINE2 + '\n' +
			LINE3 + '\n' +
			LINE4 + '\r\n' +
			LINE5;

A
Alex Dima 已提交
140
		thisModel = new Model(text, null);
E
Erich Gamma 已提交
141
		thisConfiguration = new MockConfiguration(null);
A
Alex Dima 已提交
142
		thisCursor = new Cursor(1, thisConfiguration, thisModel, null, false);
E
Erich Gamma 已提交
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
	});

	teardown(() => {
		thisCursor.dispose();
		thisModel.dispose();
		thisConfiguration.dispose();
	});

	test('cursor initialized', () => {
		cursorEqual(thisCursor, 1, 1);
	});

	// --------- absolute move

	test('no move', () => {
		moveTo(thisCursor, 1, 1);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move', () => {
		moveTo(thisCursor, 1, 2);
		cursorEqual(thisCursor, 1, 2);
	});

	test('move in selection mode', () => {
		moveTo(thisCursor, 1, 2, true);
		cursorEqual(thisCursor, 1, 2, 1, 1);
	});

	test('move beyond line end', () => {
		moveTo(thisCursor, 1, 25);
		cursorEqual(thisCursor, 1, LINE1.length + 1);
	});

	test('move empty line', () => {
		moveTo(thisCursor, 4, 20);
		cursorEqual(thisCursor, 4, 1);
	});

	test('move one char line', () => {
		moveTo(thisCursor, 5, 20);
		cursorEqual(thisCursor, 5, 2);
	});

	test('selection down', () => {
		moveTo(thisCursor, 2, 1, true);
		cursorEqual(thisCursor, 2, 1, 1, 1);
	});

	test('move and then select', () => {
		moveTo(thisCursor, 2, 3);
		cursorEqual(thisCursor, 2, 3);

		moveTo(thisCursor, 2, 15, true);
		cursorEqual(thisCursor, 2, 15, 2, 3);

		moveTo(thisCursor, 1, 2, true);
		cursorEqual(thisCursor, 1, 2, 2, 3);
	});

	// --------- move left

	test('move left on top left position', () => {
		moveLeft(thisCursor);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move left', () => {
		moveTo(thisCursor, 1, 3);
		cursorEqual(thisCursor, 1, 3);
		moveLeft(thisCursor);
		cursorEqual(thisCursor, 1, 2);
	});

217 218 219 220 221 222 223
	test('move left with surrogate pair', () => {
		moveTo(thisCursor, 3, 17);
		cursorEqual(thisCursor, 3, 17);
		moveLeft(thisCursor);
		cursorEqual(thisCursor, 3, 15);
	});

E
Erich Gamma 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	test('move left goes to previous row', () => {
		moveTo(thisCursor, 2, 1);
		cursorEqual(thisCursor, 2, 1);
		moveLeft(thisCursor);
		cursorEqual(thisCursor, 1, 21);
	});

	test('move left selection', () => {
		moveTo(thisCursor, 2, 1);
		cursorEqual(thisCursor, 2, 1);
		moveLeft(thisCursor, true);
		cursorEqual(thisCursor, 1, 21, 2, 1);
	});

	// --------- move word left

	test('move word left', () => {
		moveTo(thisCursor, 5, 2);
A
Alex Dima 已提交
242
		let expectedStops = [
E
Erich Gamma 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
			[5, 1],
			[4, 1],
			[3, 11],
			[3, 5],
			[3, 1],
			[2, 12],
			[2, 5],
			[2, 2],
			[2, 1],
			[1, 15],
			[1, 9],
			[1, 6],
			[1, 1],
			[1, 1],
		];

A
Alex Dima 已提交
259 260
		let actualStops:number[][] = [];
		for (let i = 0; i < expectedStops.length; i++) {
E
Erich Gamma 已提交
261
			moveWordLeft(thisCursor);
A
Alex Dima 已提交
262
			let pos = thisCursor.getPosition();
E
Erich Gamma 已提交
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
			actualStops.push([pos.lineNumber, pos.column]);
		}

		assert.deepEqual(actualStops, expectedStops);
	});

	test('move word left selection', () => {
		moveTo(thisCursor, 5, 2);
		cursorEqual(thisCursor, 5, 2);
		moveWordLeft(thisCursor, true);
		cursorEqual(thisCursor, 5, 1, 5, 2);
	});

	// --------- move right

	test('move right on bottom right position', () => {
		moveTo(thisCursor, 5, 2);
		cursorEqual(thisCursor, 5, 2);
		moveRight(thisCursor);
		cursorEqual(thisCursor, 5, 2);
	});

	test('move right', () => {
		moveTo(thisCursor, 1, 3);
		cursorEqual(thisCursor, 1, 3);
		moveRight(thisCursor);
		cursorEqual(thisCursor, 1, 4);
	});

292 293 294 295 296 297 298
	test('move right with surrogate pair', () => {
		moveTo(thisCursor, 3, 15);
		cursorEqual(thisCursor, 3, 15);
		moveRight(thisCursor);
		cursorEqual(thisCursor, 3, 17);
	});

E
Erich Gamma 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	test('move right goes to next row', () => {
		moveTo(thisCursor, 1, 21);
		cursorEqual(thisCursor, 1, 21);
		moveRight(thisCursor);
		cursorEqual(thisCursor, 2, 1);
	});

	test('move right selection', () => {
		moveTo(thisCursor, 1, 21);
		cursorEqual(thisCursor, 1, 21);
		moveRight(thisCursor, true);
		cursorEqual(thisCursor, 2, 1, 1, 21);
	});

	// --------- move word right

	test('move word right', () => {
		moveTo(thisCursor, 1, 1);
A
Alex Dima 已提交
317
		let expectedStops = [
E
Erich Gamma 已提交
318 319 320 321 322 323 324 325
			[1, 8],
			[1, 14],
			[1, 19],
			[1, 21],
			[2, 4],
			[2, 11],
			[2, 16],
			[3, 10],
326
			[3, 17],
E
Erich Gamma 已提交
327 328 329 330 331
			[4, 1],
			[5, 2],
			[5, 2],
		];

A
Alex Dima 已提交
332 333
		let actualStops:number[][] = [];
		for (let i = 0; i < expectedStops.length; i++) {
E
Erich Gamma 已提交
334
			moveWordRight(thisCursor);
A
Alex Dima 已提交
335
			let pos = thisCursor.getPosition();
E
Erich Gamma 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
			actualStops.push([pos.lineNumber, pos.column]);
		}

		assert.deepEqual(actualStops, expectedStops);
	});

	test('move word right selection', () => {
		moveTo(thisCursor, 1, 1);
		cursorEqual(thisCursor, 1, 1);
		moveWordRight(thisCursor, true);
		cursorEqual(thisCursor, 1, 8, 1, 1);
	});
	// --------- move down

	test('move down', () => {
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 2, 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 3, 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 4, 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 5, 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 5, 2);
	});

	test('move down with selection', () => {
		moveDown(thisCursor, 1, true);
		cursorEqual(thisCursor, 2, 1, 1, 1);
		moveDown(thisCursor, 1, true);
		cursorEqual(thisCursor, 3, 1, 1, 1);
		moveDown(thisCursor, 1, true);
		cursorEqual(thisCursor, 4, 1, 1, 1);
		moveDown(thisCursor, 1, true);
		cursorEqual(thisCursor, 5, 1, 1, 1);
		moveDown(thisCursor, 1, true);
		cursorEqual(thisCursor, 5, 2, 1, 1);
	});

	test('move down with tabs', () => {
		moveTo(thisCursor, 1, 5);
		cursorEqual(thisCursor, 1, 5);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 2, 2);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 3, 5);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 4, 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 5, 2);
	});

	// --------- move up

	test('move up', () => {
		moveTo(thisCursor, 3, 5);
		cursorEqual(thisCursor, 3, 5);

		moveUp(thisCursor, 1);
		cursorEqual(thisCursor, 2, 2);

		moveUp(thisCursor, 1);
		cursorEqual(thisCursor, 1, 5);
	});

	test('move up with selection', () => {
		moveTo(thisCursor, 3, 5);
		cursorEqual(thisCursor, 3, 5);

		moveUp(thisCursor, 1, true);
		cursorEqual(thisCursor, 2, 2, 3, 5);

		moveUp(thisCursor, 1, true);
		cursorEqual(thisCursor, 1, 5, 3, 5);
	});

	test('move up and down with tabs', () => {
		moveTo(thisCursor, 1, 5);
		cursorEqual(thisCursor, 1, 5);
		moveDown(thisCursor, 4);
		cursorEqual(thisCursor, 5, 2);
		moveUp(thisCursor, 1);
		cursorEqual(thisCursor, 4, 1);
		moveUp(thisCursor, 1);
		cursorEqual(thisCursor, 3, 5);
		moveUp(thisCursor, 1);
		cursorEqual(thisCursor, 2, 2);
		moveUp(thisCursor, 1);
		cursorEqual(thisCursor, 1, 5);
	});

	test('move up and down with end of lines starting from a long one', () => {
		moveToEndOfLine(thisCursor);
		cursorEqual(thisCursor, 1, LINE1.length - 1);
		moveToEndOfLine(thisCursor);
		cursorEqual(thisCursor, 1, LINE1.length + 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 2, LINE2.length + 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 3, LINE3.length + 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 4, LINE4.length + 1);
		moveDown(thisCursor, 1);
		cursorEqual(thisCursor, 5, LINE5.length + 1);
		moveUp(thisCursor, 4);
		cursorEqual(thisCursor, 1, LINE1.length + 1);
	});

	// --------- move to beginning of line

	test('move to beginning of line', () => {
		moveToBeginningOfLine(thisCursor);
		cursorEqual(thisCursor, 1, 6);
		moveToBeginningOfLine(thisCursor);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move to beginning of line from within line', () => {
		moveTo(thisCursor, 1, 8);
		moveToBeginningOfLine(thisCursor);
		cursorEqual(thisCursor, 1, 6);
		moveToBeginningOfLine(thisCursor);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move to beginning of line from whitespace at beginning of line', () => {
		moveTo(thisCursor, 1, 2);
		moveToBeginningOfLine(thisCursor);
		cursorEqual(thisCursor, 1, 1);
		moveToBeginningOfLine(thisCursor);
		cursorEqual(thisCursor, 1, 6);
	});

	test('move to beginning of line from within line selection', () => {
		moveTo(thisCursor, 1, 8);
		moveToBeginningOfLine(thisCursor, true);
		cursorEqual(thisCursor, 1, 6, 1, 8);
		moveToBeginningOfLine(thisCursor, true);
		cursorEqual(thisCursor, 1, 1, 1, 8);
	});

	// --------- move to end of line

	test('move to end of line', () => {
		moveToEndOfLine(thisCursor);
		cursorEqual(thisCursor, 1, LINE1.length - 1);
		moveToEndOfLine(thisCursor);
		cursorEqual(thisCursor, 1, LINE1.length + 1);
	});

	test('move to end of line from within line', () => {
		moveTo(thisCursor, 1, 6);
		moveToEndOfLine(thisCursor);
		cursorEqual(thisCursor, 1, LINE1.length - 1);
		moveToEndOfLine(thisCursor);
		cursorEqual(thisCursor, 1, LINE1.length + 1);
	});

	test('move to end of line from whitespace at end of line', () => {
		moveTo(thisCursor, 1, 20);
		moveToEndOfLine(thisCursor);
		cursorEqual(thisCursor, 1, LINE1.length + 1);
		moveToEndOfLine(thisCursor);
		cursorEqual(thisCursor, 1, LINE1.length - 1);
	});

	test('move to end of line from within line selection', () => {
		moveTo(thisCursor, 1, 6);
		moveToEndOfLine(thisCursor, true);
		cursorEqual(thisCursor, 1, LINE1.length - 1, 1, 6);
		moveToEndOfLine(thisCursor, true);
		cursorEqual(thisCursor, 1, LINE1.length + 1, 1, 6);
	});

	// --------- move to beginning of buffer

	test('move to beginning of buffer', () => {
		moveToBeginningOfBuffer(thisCursor);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move to beginning of buffer from within first line', () => {
		moveTo(thisCursor, 1, 3);
		moveToBeginningOfBuffer(thisCursor);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move to beginning of buffer from within another line', () => {
		moveTo(thisCursor, 3, 3);
		moveToBeginningOfBuffer(thisCursor);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move to beginning of buffer from within first line selection', () => {
		moveTo(thisCursor, 1, 3);
		moveToBeginningOfBuffer(thisCursor, true);
		cursorEqual(thisCursor, 1, 1, 1, 3);
	});

	test('move to beginning of buffer from within another line selection', () => {
		moveTo(thisCursor, 3, 3);
		moveToBeginningOfBuffer(thisCursor, true);
		cursorEqual(thisCursor, 1, 1, 3, 3);
	});

	// --------- move to end of buffer

	test('move to end of buffer', () => {
		moveToEndOfBuffer(thisCursor);
		cursorEqual(thisCursor, 5, LINE5.length + 1);
	});

	test('move to end of buffer from within last line', () => {
		moveTo(thisCursor, 5, 1);
		moveToEndOfBuffer(thisCursor);
		cursorEqual(thisCursor, 5, LINE5.length + 1);
	});

	test('move to end of buffer from within another line', () => {
		moveTo(thisCursor, 3, 3);
		moveToEndOfBuffer(thisCursor);
		cursorEqual(thisCursor, 5, LINE5.length + 1);
	});

	test('move to end of buffer from within last line selection', () => {
		moveTo(thisCursor, 5, 1);
		moveToEndOfBuffer(thisCursor, true);
		cursorEqual(thisCursor, 5, LINE5.length + 1, 5, 1);
	});

	test('move to end of buffer from within another line selection', () => {
		moveTo(thisCursor, 3, 3);
		moveToEndOfBuffer(thisCursor, true);
		cursorEqual(thisCursor, 5, LINE5.length + 1, 3, 3);
	});

	// --------- delete word left/right

	test('delete word left for non-empty selection', () => {
		moveTo(thisCursor, 3, 7);
		moveRight(thisCursor, true);
		moveRight(thisCursor, true);
		deleteWordLeft(thisCursor);
580
		assert.equal(thisModel.getLineContent(3), '    Thd Line💩');
E
Erich Gamma 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593
		cursorEqual(thisCursor, 3, 7);
	});

	test('delete word left for caret at beginning of document', () => {
		moveTo(thisCursor, 1, 1);
		deleteWordLeft(thisCursor);
		assert.equal(thisModel.getLineContent(1), '    \tMy First Line\t ');
		cursorEqual(thisCursor, 1, 1);
	});

	test('delete word left for caret at end of whitespace', () => {
		moveTo(thisCursor, 3, 11);
		deleteWordLeft(thisCursor);
594
		assert.equal(thisModel.getLineContent(3), '    ThirdLine💩');
E
Erich Gamma 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
		cursorEqual(thisCursor, 3, 10);
	});

	test('delete word left for caret just behind a word', () => {
		moveTo(thisCursor, 2, 11);
		deleteWordLeft(thisCursor);
		assert.equal(thisModel.getLineContent(2), '\tMy  Line');
		cursorEqual(thisCursor, 2, 5);
	});

	test('delete word left for caret inside of a word', () => {
		moveTo(thisCursor, 1, 12);
		deleteWordLeft(thisCursor);
		assert.equal(thisModel.getLineContent(1), '    \tMy st Line\t ');
		cursorEqual(thisCursor, 1, 9);
	});

	test('delete word right for non-empty selection', () => {
		moveTo(thisCursor, 3, 7);
		moveRight(thisCursor, true);
		moveRight(thisCursor, true);
		deleteWordRight(thisCursor);
617
		assert.equal(thisModel.getLineContent(3), '    Thd Line💩');
E
Erich Gamma 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630
		cursorEqual(thisCursor, 3, 7);
	});

	test('delete word right for caret at end of document', () => {
		moveTo(thisCursor, 5, 3);
		deleteWordRight(thisCursor);
		assert.equal(thisModel.getLineContent(5), '1');
		cursorEqual(thisCursor, 5, 2);
	});

	test('delete word right for caret at beggining of whitespace', () => {
		moveTo(thisCursor, 3, 1);
		deleteWordRight(thisCursor);
631
		assert.equal(thisModel.getLineContent(3), 'Third Line💩');
E
Erich Gamma 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
		cursorEqual(thisCursor, 3, 1);
	});

	test('delete word right for caret just before a word', () => {
		moveTo(thisCursor, 2, 5);
		deleteWordRight(thisCursor);
		assert.equal(thisModel.getLineContent(2), '\tMy  Line');
		cursorEqual(thisCursor, 2, 5);
	});

	test('delete word right for caret inside of a word', () => {
		moveTo(thisCursor, 1, 11);
		deleteWordRight(thisCursor);
		assert.equal(thisModel.getLineContent(1), '    \tMy Fi Line\t ');
		cursorEqual(thisCursor, 1, 11);
	});

	// --------- misc

	test('select all', () => {
		cursorCommand(thisCursor, H.SelectAll);
		cursorEqual(thisCursor, 5, LINE5.length + 1, 1, 1);
	});

A
Alex Dima 已提交
656 657 658
	test('expandLineSelection', () => {
		//              0          1         2
		//              01234 56789012345678 0
A
Alex Dima 已提交
659
		// let LINE1 = '    \tMy First Line\t ';
A
Alex Dima 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		moveTo(thisCursor, 1, 1);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 1, LINE1.length + 1, 1, 1);

		moveTo(thisCursor, 1, 2);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 1, LINE1.length + 1, 1, 1);

		moveTo(thisCursor, 1, 5);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 1, LINE1.length + 1, 1, 1);

		moveTo(thisCursor, 1, 19);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 1, LINE1.length + 1, 1, 1);

		moveTo(thisCursor, 1, 20);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 1, LINE1.length + 1, 1, 1);

		moveTo(thisCursor, 1, 21);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 1, LINE1.length + 1, 1, 1);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 2, LINE2.length + 1, 1, 1);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 3, LINE3.length + 1, 1, 1);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 4, LINE4.length + 1, 1, 1);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 5, LINE5.length + 1, 1, 1);
		cursorCommand(thisCursor, H.ExpandLineSelection);
		cursorEqual(thisCursor, 5, LINE5.length + 1, 1, 1);
	});

E
Erich Gamma 已提交
695 696 697
	// --------- eventing

	test('no move doesn\'t trigger event', () => {
A
Alex Dima 已提交
698
		thisCursor.addListener(EventType.CursorPositionChanged, (e) => {
E
Erich Gamma 已提交
699 700
			assert.ok(false, 'was not expecting event');
		});
A
Alex Dima 已提交
701
		thisCursor.addListener(EventType.CursorSelectionChanged, (e) => {
E
Erich Gamma 已提交
702 703 704 705 706 707
			assert.ok(false, 'was not expecting event');
		});
		moveTo(thisCursor, 1, 1);
	});

	test('move eventing', () => {
A
Alex Dima 已提交
708
		let events = 0;
A
Alex Dima 已提交
709
		thisCursor.addListener(EventType.CursorPositionChanged, (e) => {
E
Erich Gamma 已提交
710 711 712
			events++;
			positionEqual(e.position, 1, 2);
		});
A
Alex Dima 已提交
713
		thisCursor.addListener(EventType.CursorSelectionChanged, (e) => {
E
Erich Gamma 已提交
714 715 716 717 718 719 720 721
			events++;
			selectionEqual(e.selection, 1, 2, 1, 2);
		});
		moveTo(thisCursor, 1, 2);
		assert.equal(events, 2, 'receives 2 events');
	});

	test('move in selection mode eventing', () => {
A
Alex Dima 已提交
722
		let events = 0;
A
Alex Dima 已提交
723
		thisCursor.addListener(EventType.CursorPositionChanged, (e) => {
E
Erich Gamma 已提交
724 725 726
			events++;
			positionEqual(e.position, 1, 2);
		});
A
Alex Dima 已提交
727
		thisCursor.addListener(EventType.CursorSelectionChanged, (e) => {
E
Erich Gamma 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740
			events++;
			selectionEqual(e.selection, 1, 2, 1, 1);
		});
		moveTo(thisCursor, 1, 2, true);
		assert.equal(events, 2, 'receives 2 events');
	});

	// --------- state save & restore

	test('saveState & restoreState', () => {
		moveTo(thisCursor, 2, 1, true);
		cursorEqual(thisCursor, 2, 1, 1, 1);

A
Alex Dima 已提交
741
		let savedState = JSON.stringify(thisCursor.saveState());
E
Erich Gamma 已提交
742 743 744 745 746 747 748 749 750 751 752 753 754

		moveTo(thisCursor, 1, 1, false);
		cursorEqual(thisCursor, 1, 1);

		thisCursor.restoreState(JSON.parse(savedState));
		cursorEqual(thisCursor, 2, 1, 1, 1);
	});

	// --------- updating cursor

	test('Independent model edit 1', () => {
		moveTo(thisCursor, 2, 16, true);

A
Alex Dima 已提交
755
		thisModel.applyEdits([EditOperation.delete(new Range(2, 1, 2, 2))]);
E
Erich Gamma 已提交
756 757
		cursorEqual(thisCursor, 2, 15, 1, 1);
	});
758 759 760 761 762 763 764 765 766 767 768
});

class TestMode {
	public getId():string {
		return 'testing';
	}

	public toSimplifiedMode(): Modes.IMode {
		return this;
	}
}
E
Erich Gamma 已提交
769

770
class SurroundingMode extends TestMode {
771
	public richEditSupport: Modes.IRichEditSupport;
E
Erich Gamma 已提交
772

773 774
	constructor() {
		super();
775 776 777 778
		this.richEditSupport = new RichEditSupport(this.getId(), {
			__characterPairSupport: {
				autoClosingPairs: [{ open: '(', close: ')' }]
			}
779 780 781 782 783
		});
	}
}

class OnEnterMode extends TestMode {
784
	public richEditSupport: Modes.IRichEditSupport;
785 786 787

	constructor(indentAction: Modes.IndentAction) {
		super();
788 789 790 791 792 793 794 795 796
		this.richEditSupport = {
			electricCharacter: {
				getElectricCharacters: ():string[] => null,
				onElectricCharacter: (context:Modes.ILineContext, offset:number): Modes.IElectricAction => null,
				onEnter: (context:Modes.ILineContext, offset:number): Modes.IEnterAction => {
					return {
						indentAction: indentAction
					};
				}
797 798 799 800 801 802
			}
		};
	}
}

suite('Editor Controller - Regression tests', () => {
E
Erich Gamma 已提交
803
	test('Bug 9121: Auto indent + undo + redo is funky', () => {
A
Alex Dima 已提交
804 805 806 807 808 809 810 811 812
		usingCursor({
			text: [
				''
			],
			mode: null,
			config: { insertSpaces: false, tabSize: 4 }
		}, (model, cursor) => {
			cursorCommand(cursor, H.Type, { text: '\n' }, null, 'keyboard');
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n', 'assert1');
E
Erich Gamma 已提交
813

A
Alex Dima 已提交
814 815
			cursorCommand(cursor, H.Tab, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t', 'assert2');
E
Erich Gamma 已提交
816

A
Alex Dima 已提交
817 818
			cursorCommand(cursor, H.Type, { text: '\n'}, null, 'keyboard');
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\n\t', 'assert3');
E
Erich Gamma 已提交
819

A
Alex Dima 已提交
820 821
			cursorCommand(cursor, H.Type, { text: 'x' });
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\n\tx', 'assert4');
E
Erich Gamma 已提交
822

A
Alex Dima 已提交
823 824
			cursorCommand(cursor, H.CursorLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\n\tx', 'assert5');
E
Erich Gamma 已提交
825

A
Alex Dima 已提交
826 827
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\nx', 'assert6');
E
Erich Gamma 已提交
828

A
Alex Dima 已提交
829 830
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\tx', 'assert7');
E
Erich Gamma 已提交
831

A
Alex Dima 已提交
832 833
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\nx', 'assert8');
E
Erich Gamma 已提交
834

A
Alex Dima 已提交
835 836
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), 'x', 'assert9');
E
Erich Gamma 已提交
837

A
Alex Dima 已提交
838 839
			cursorCommand(cursor, H.Undo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\nx', 'assert10');
E
Erich Gamma 已提交
840

A
Alex Dima 已提交
841 842
			cursorCommand(cursor, H.Undo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\nx', 'assert11');
E
Erich Gamma 已提交
843

A
Alex Dima 已提交
844 845
			cursorCommand(cursor, H.Undo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\n\tx', 'assert12');
E
Erich Gamma 已提交
846

A
Alex Dima 已提交
847 848
			cursorCommand(cursor, H.Redo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\nx', 'assert13');
E
Erich Gamma 已提交
849

A
Alex Dima 已提交
850 851
			cursorCommand(cursor, H.Redo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\nx', 'assert14');
E
Erich Gamma 已提交
852

A
Alex Dima 已提交
853 854 855
			cursorCommand(cursor, H.Redo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), 'x', 'assert15');
		});
E
Erich Gamma 已提交
856
	});
857

A
Alex Dima 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
	test('issue #183: jump to matching bracket position', () => {
		usingCursor({
			text: [
				'var x = (3 + (5-7));'
			],
			mode: new BracketMode(),
			config: null
		}, (model, cursor) => {
			// ensure is tokenized
			model.getLineContext(1);

			moveTo(cursor, 1, 20);

			cursorCommand(cursor, H.JumpToBracket, null, null, 'keyboard');
			cursorEqual(cursor, 1, 10);

			cursorCommand(cursor, H.JumpToBracket, null, null, 'keyboard');
			cursorEqual(cursor, 1, 20);

			cursorCommand(cursor, H.JumpToBracket, null, null, 'keyboard');
			cursorEqual(cursor, 1, 10);
		});
880 881
	});

E
Erich Gamma 已提交
882
	test('bug #16543: Tab should indent to correct indentation spot immediately', () => {
A
Alex Dima 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896
		usingCursor({
			text: [
				'function baz() {',
				'\tfunction hello() { // something here',
				'\t',
				'',
				'\t}',
				'}'
			],
			mode: new OnEnterMode(Modes.IndentAction.Indent),
			config: { insertSpaces: false, tabSize: 4 }
		}, (model, cursor) => {
			moveTo(cursor, 4, 1, false);
			cursorEqual(cursor, 4, 1, 4, 1);
E
Erich Gamma 已提交
897

A
Alex Dima 已提交
898 899 900
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t');
		});
E
Erich Gamma 已提交
901 902 903
	});

	test('Bug 18276:[editor] Indentation broken when selection is empty', () => {
A
Alex Dima 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
		usingCursor({
			text: [
				'function baz() {'
			],
			mode: null,
			config: { insertSpaces: false, tabSize: 4 }
		}, (model, cursor) => {
			moveTo(cursor, 1, 2, false);
			cursorEqual(cursor, 1, 2, 1, 2);

			cursorCommand(cursor, H.Indent, null, null, 'keyboard');
			assert.equal(model.getLineContent(1), '\tfunction baz() {');

			cursorEqual(cursor, 1, 3, 1, 3);
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(1), '\tf\tunction baz() {');
		});
E
Erich Gamma 已提交
921 922 923
	});

	test('bug #16815:Shift+Tab doesn\'t go back to tabstop', () => {
A
Alex Dima 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937
		usingCursor({
			text: [
				'     function baz() {'
			],
			mode: new OnEnterMode(Modes.IndentAction.IndentOutdent),
			config: { insertSpaces: true, tabSize: 4 }
		}, (model, cursor) => {
			moveTo(cursor, 1, 6, false);
			cursorEqual(cursor, 1, 6, 1, 6);

			cursorCommand(cursor, H.Outdent, null, null, 'keyboard');
			assert.equal(model.getLineContent(1), '    function baz() {');
			cursorEqual(cursor, 1, 5, 1, 5);
		});
E
Erich Gamma 已提交
938 939 940
	});

	test('Bug #18293:[regression][editor] Can\'t outdent whitespace line', () => {
A
Alex Dima 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954
		usingCursor({
			text: [
				'      '
			],
			mode: null,
			config: { insertSpaces: true, tabSize: 4 }
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
			cursorEqual(cursor, 1, 7, 1, 7);

			cursorCommand(cursor, H.Outdent, null, null, 'keyboard');
			assert.equal(model.getLineContent(1), '    ');
			cursorEqual(cursor, 1, 5, 1, 5);
		});
E
Erich Gamma 已提交
955 956 957
	});

	test('Bug #16657: [editor] Tab on empty line of zero indentation moves cursor to position (1,1)', () => {
A
Alex Dima 已提交
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
		usingCursor({
			text: [
				'function baz() {',
				'\tfunction hello() { // something here',
				'\t',
				'',
				'\t}',
				'}',
				''
			],
			mode: null,
			config: { insertSpaces: false, tabSize: 4 }
		}, (model, cursor) => {
			moveTo(cursor, 7, 1, false);
			cursorEqual(cursor, 7, 1, 7, 1);

			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(7), '\t');
			cursorEqual(cursor, 7, 2, 7, 2);
		});
E
Erich Gamma 已提交
978 979 980 981
	});

	test('bug #16740: [editor] Cut line doesn\'t quite cut the last line', () => {
		// Part 1 => there is text on the last line
A
Alex Dima 已提交
982
		let text = [
E
Erich Gamma 已提交
983 984 985
			'asdasd',
			'qwerty'
		];
A
Alex Dima 已提交
986 987
		let model = new Model(text.join('\n'), null);
		let cursor = new Cursor(1, new MockConfiguration({ insertSpaces: false, tabSize: 4 }), model, null, true);
E
Erich Gamma 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003

		moveTo(cursor, 2, 1, false);
		cursorEqual(cursor, 2, 1, 2, 1);

		cursorCommand(cursor, H.Cut, null, null, 'keyboard');
		assert.equal(model.getLineCount(), 1);
		assert.equal(model.getLineContent(1), 'asdasd');

		cursor.dispose();
		model.dispose();

		// Part 2 => there is no text on the last line
		text = [
			'asdasd',
			''
		];
A
Alex Dima 已提交
1004 1005
		model = new Model(text.join('\n'), null);
		cursor = new Cursor(1, new MockConfiguration({ insertSpaces: false, tabSize: 4 }), model, null, true);
E
Erich Gamma 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

		moveTo(cursor, 2, 1, false);
		cursorEqual(cursor, 2, 1, 2, 1);

		cursorCommand(cursor, H.Cut, null, null, 'keyboard');
		assert.equal(model.getLineCount(), 1);
		assert.equal(model.getLineContent(1), 'asdasd');

		cursorCommand(cursor, H.Cut, null, null, 'keyboard');
		assert.equal(model.getLineCount(), 1);
		assert.equal(model.getLineContent(1), '');

		cursor.dispose();
		model.dispose();
	});

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	test('Bug #11476: Double bracket surrounding + undo is broken', () => {
		usingCursor({
			text: [
				'hello'
			],
			mode: new SurroundingMode(),
			config: { insertSpaces: true }
		}, (model, cursor) => {
			moveTo(cursor, 1, 3, false);
			moveTo(cursor, 1, 5, true);
			cursorEqual(cursor, 1, 5, 1, 3);

			cursorCommand(cursor, H.Type, { text: '(' }, null, 'keyboard');
			cursorEqual(cursor, 1, 6, 1, 4);

			cursorCommand(cursor, H.Type, { text: '(' }, null, 'keyboard');
			cursorEqual(cursor, 1, 7, 1, 5);
		});
	});

	test('issue #1140: Backspace stops prematurely', () => {
		usingCursor({
			text: [
				'function baz() {',
				'  return 1;',
				'};'
			],
			mode: new SurroundingMode(),
			config: { insertSpaces: true }
		}, (model, cursor) => {
			moveTo(cursor, 3, 2, false);
			moveTo(cursor, 1, 14, true);
			cursorEqual(cursor, 1, 14, 3, 2);

			cursorCommand(cursor, H.DeleteLeft);
			cursorEqual(cursor, 1, 14, 1, 14);
			assert.equal(model.getLineCount(), 1);
			assert.equal(model.getLineContent(1), 'function baz(;');
		});
	});
});

suite('Editor Controller - Cursor Configuration', () => {

	test('Cursor honors insertSpaces configuration on new line', () => {
		usingCursor({
			text: [
				'    \tMy First Line\t ',
				'\tMy Second Line',
				'    Third Line',
				'',
				'1'
			],
			mode: null,
			config: { insertSpaces: true, tabSize: 4 }
		}, (model, cursor) => {
			cursorCommand(cursor, H.MoveTo, { position: new Position(1, 21) }, null, 'keyboard');
			cursorCommand(cursor, H.Type, { text: '\n' }, null, 'keyboard');
			assert.equal(model.getLineContent(1), '    \tMy First Line\t ');
			assert.equal(model.getLineContent(2), '        ');
		});
	});

E
Erich Gamma 已提交
1085
	test('Cursor honors insertSpaces configuration on tab', () => {
A
Alex Dima 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
		usingCursor({
			text: [
				'    \tMy First Line\t ',
				'My Second Line123',
				'    Third Line',
				'',
				'1'
			],
			mode: null,
			config: { insertSpaces: true, tabSize: 13 }
		}, (model, cursor) => {
			// Tab on column 1
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 1) }, null, 'keyboard');
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(2), '             My Second Line123');
			cursorCommand(cursor, H.Undo, null, null, 'keyboard');

			// Tab on column 2
			assert.equal(model.getLineContent(2), 'My Second Line123');
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 2) }, null, 'keyboard');
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(2), 'M            y Second Line123');
			cursorCommand(cursor, H.Undo, null, null, 'keyboard');

			// Tab on column 3
			assert.equal(model.getLineContent(2), 'My Second Line123');
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 3) }, null, 'keyboard');
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(2), 'My            Second Line123');
			cursorCommand(cursor, H.Undo, null, null, 'keyboard');

			// Tab on column 4
			assert.equal(model.getLineContent(2), 'My Second Line123');
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 4) }, null, 'keyboard');
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(2), 'My           Second Line123');
			cursorCommand(cursor, H.Undo, null, null, 'keyboard');

			// Tab on column 5
			assert.equal(model.getLineContent(2), 'My Second Line123');
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 5) }, null, 'keyboard');
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(2), 'My S         econd Line123');
			cursorCommand(cursor, H.Undo, null, null, 'keyboard');

			// Tab on column 5
			assert.equal(model.getLineContent(2), 'My Second Line123');
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 5) }, null, 'keyboard');
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(2), 'My S         econd Line123');
			cursorCommand(cursor, H.Undo, null, null, 'keyboard');

			// Tab on column 13
			assert.equal(model.getLineContent(2), 'My Second Line123');
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 13) }, null, 'keyboard');
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(2), 'My Second Li ne123');
			cursorCommand(cursor, H.Undo, null, null, 'keyboard');

			// Tab on column 14
			assert.equal(model.getLineContent(2), 'My Second Line123');
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 14) }, null, 'keyboard');
			cursorCommand(cursor, H.Tab, null, null, 'keyboard');
			assert.equal(model.getLineContent(2), 'My Second Lin             e123');
		});
E
Erich Gamma 已提交
1151 1152 1153
	});

	test('Enter auto-indents with insertSpaces setting 1', () => {
A
Alex Dima 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
		usingCursor({
			text: [
				'\thello'
			],
			mode: new OnEnterMode(Modes.IndentAction.Indent),
			config: { insertSpaces: true, tabSize: 4 }
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
			cursorEqual(cursor, 1, 7, 1, 7);

			cursorCommand(cursor, H.Type, { text: '\n' }, null, 'keyboard');
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thello\r\n        ');
		});
E
Erich Gamma 已提交
1167 1168 1169
	});

	test('Enter auto-indents with insertSpaces setting 2', () => {
A
Alex Dima 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
		usingCursor({
			text: [
				'\thello'
			],
			mode: new OnEnterMode(Modes.IndentAction.None),
			config: { insertSpaces: true, tabSize: 4 }
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
			cursorEqual(cursor, 1, 7, 1, 7);

			cursorCommand(cursor, H.Type, { text: '\n' }, null, 'keyboard');
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thello\r\n    ');
		});
E
Erich Gamma 已提交
1183 1184 1185
	});

	test('Enter auto-indents with insertSpaces setting 3', () => {
A
Alex Dima 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
		usingCursor({
			text: [
				'\thell()'
			],
			mode: new OnEnterMode(Modes.IndentAction.IndentOutdent),
			config: { insertSpaces: true, tabSize: 4 }
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
			cursorEqual(cursor, 1, 7, 1, 7);

			cursorCommand(cursor, H.Type, { text: '\n' }, null, 'keyboard');
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thell(\r\n        \r\n    )');
		});
E
Erich Gamma 已提交
1199 1200 1201
	});

	test('Insert line before', () => {
A
Alex Dima 已提交
1202
		let testInsertLineBefore = (lineNumber:number, column:number, callback:(model:Model, cursor:Cursor) => void) => {
A
Alex Dima 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211
			usingCursor({
				text: [
					'First line',
					'Second line',
					'Third line'
				],
				mode: null,
				config: null
			}, (model, cursor) => {
E
Erich Gamma 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
				moveTo(cursor, lineNumber, column, false);
				cursorEqual(cursor, lineNumber, column, lineNumber, column);

				cursorCommand(cursor, H.LineInsertBefore, null, null, 'keyboard');
				callback(model, cursor);
			});
		};

		testInsertLineBefore(1, 3, (model, cursor) => {
			cursorEqual(cursor, 1, 1, 1, 1);
			assert.equal(model.getLineContent(1), '');
			assert.equal(model.getLineContent(2), 'First line');
			assert.equal(model.getLineContent(3), 'Second line');
			assert.equal(model.getLineContent(4), 'Third line');
		});

		testInsertLineBefore(2, 3, (model, cursor) => {
			cursorEqual(cursor, 2, 1, 2, 1);
			assert.equal(model.getLineContent(1), 'First line');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), 'Second line');
			assert.equal(model.getLineContent(4), 'Third line');
		});

		testInsertLineBefore(3, 3, (model, cursor) => {
			cursorEqual(cursor, 3, 1, 3, 1);
			assert.equal(model.getLineContent(1), 'First line');
			assert.equal(model.getLineContent(2), 'Second line');
			assert.equal(model.getLineContent(3), '');
			assert.equal(model.getLineContent(4), 'Third line');
		});
	});

	test('Insert line after', () => {
A
Alex Dima 已提交
1246
		let testInsertLineAfter = (lineNumber:number, column:number, callback:(model:Model, cursor:Cursor) => void) => {
A
Alex Dima 已提交
1247 1248 1249 1250 1251 1252 1253 1254 1255
			usingCursor({
				text: [
					'First line',
					'Second line',
					'Third line'
				],
				mode: null,
				config: null
			}, (model, cursor) => {
E
Erich Gamma 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
				moveTo(cursor, lineNumber, column, false);
				cursorEqual(cursor, lineNumber, column, lineNumber, column);

				cursorCommand(cursor, H.LineInsertAfter, null, null, 'keyboard');
				callback(model, cursor);
			});
		};

		testInsertLineAfter(1, 3, (model, cursor) => {
			cursorEqual(cursor, 2, 1, 2, 1);
			assert.equal(model.getLineContent(1), 'First line');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), 'Second line');
			assert.equal(model.getLineContent(4), 'Third line');
		});

		testInsertLineAfter(2, 3, (model, cursor) => {
			cursorEqual(cursor, 3, 1, 3, 1);
			assert.equal(model.getLineContent(1), 'First line');
			assert.equal(model.getLineContent(2), 'Second line');
			assert.equal(model.getLineContent(3), '');
			assert.equal(model.getLineContent(4), 'Third line');
		});

		testInsertLineAfter(3, 3, (model, cursor) => {
			cursorEqual(cursor, 4, 1, 4, 1);
			assert.equal(model.getLineContent(1), 'First line');
			assert.equal(model.getLineContent(2), 'Second line');
			assert.equal(model.getLineContent(3), 'Third line');
			assert.equal(model.getLineContent(4), '');
		});
	});
});

interface ICursorOpts {
A
Alex Dima 已提交
1291 1292 1293
	text: string[];
	mode?: Modes.IMode;
	config?: any;
E
Erich Gamma 已提交
1294 1295
}

A
Alex Dima 已提交
1296 1297
function usingCursor(opts:ICursorOpts, callback:(model:Model, cursor:Cursor)=>void): void {
	let model = new Model(opts.text.join('\n'), opts.mode);
A
Alex Dima 已提交
1298 1299
	let config = new MockConfiguration(opts.config);
	let cursor = new Cursor(1, config, model, null, false);
E
Erich Gamma 已提交
1300 1301 1302 1303 1304 1305

	callback(model, cursor);

	cursor.dispose();
	config.dispose();
	model.dispose();
1306
}