cursorMoveCommand.test.ts 13.8 KB
Newer Older
S
tests  
Sandeep Somavarapu 已提交
1 2 3 4
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
A
Alex Dima 已提交
5

S
tests  
Sandeep Somavarapu 已提交
6
import * as assert from 'assert';
A
Alex Dima 已提交
7
import { CoreNavigationCommands } from 'vs/editor/browser/controller/coreCommands';
J
Johannes Rieken 已提交
8
import { Cursor } from 'vs/editor/common/controller/cursor';
9
import { CursorMove } from 'vs/editor/common/controller/cursorMoveCommands';
A
Alex Dima 已提交
10
import { Position } from 'vs/editor/common/core/position';
J
Johannes Rieken 已提交
11
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
12
import { Selection } from 'vs/editor/common/core/selection';
A
Alex Dima 已提交
13
import { TextModel } from 'vs/editor/common/model/textModel';
14
import { ViewModel } from 'vs/editor/common/viewModel/viewModelImpl';
A
Alex Dima 已提交
15
import { TestConfiguration } from 'vs/editor/test/common/mocks/testConfiguration';
S
tests  
Sandeep Somavarapu 已提交
16 17 18

suite('Cursor move command test', () => {

A
Alex Dima 已提交
19
	let thisModel: TextModel;
20
	let thisConfiguration: TestConfiguration;
21
	let thisViewModel: ViewModel;
S
tests  
Sandeep Somavarapu 已提交
22 23 24
	let thisCursor: Cursor;

	setup(() => {
A
Alex Dima 已提交
25 26 27 28 29 30 31
		let text = [
			'    \tMy First Line\t ',
			'\tMy Second Line',
			'    Third Line🐶',
			'',
			'1'
		].join('\n');
S
tests  
Sandeep Somavarapu 已提交
32

A
Alex Dima 已提交
33
		thisModel = TextModel.createFromString(text);
A
Alex Dima 已提交
34 35
		thisConfiguration = new TestConfiguration({});
		thisViewModel = new ViewModel(0, thisConfiguration, thisModel, null!);
36
		thisCursor = new Cursor(thisConfiguration, thisModel, thisViewModel);
S
tests  
Sandeep Somavarapu 已提交
37 38 39 40
	});

	teardown(() => {
		thisCursor.dispose();
41
		thisViewModel.dispose();
S
tests  
Sandeep Somavarapu 已提交
42 43 44 45
		thisModel.dispose();
		thisConfiguration.dispose();
	});

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	test('move left should move to left character', () => {
		moveTo(thisCursor, 1, 8);

		moveLeft(thisCursor);

		cursorEqual(thisCursor, 1, 7);
	});

	test('move left should move to left by n characters', () => {
		moveTo(thisCursor, 1, 8);

		moveLeft(thisCursor, 3);

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

	test('move left should move to left by half line', () => {
		moveTo(thisCursor, 1, 8);

A
Alex Dima 已提交
65
		moveLeft(thisCursor, 1, CursorMove.RawUnit.HalfLine);
66 67 68 69 70 71 72 73 74

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

	test('move left moves to previous line', () => {
		moveTo(thisCursor, 2, 3);

		moveLeft(thisCursor, 10);

A
Alex Dima 已提交
75
		cursorEqual(thisCursor, 1, 21);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	});

	test('move right should move to right character', () => {
		moveTo(thisCursor, 1, 5);

		moveRight(thisCursor);

		cursorEqual(thisCursor, 1, 6);
	});

	test('move right should move to right by n characters', () => {
		moveTo(thisCursor, 1, 2);

		moveRight(thisCursor, 6);

		cursorEqual(thisCursor, 1, 8);
	});

	test('move right should move to right by half line', () => {
		moveTo(thisCursor, 1, 4);

A
Alex Dima 已提交
97
		moveRight(thisCursor, 1, CursorMove.RawUnit.HalfLine);
98 99 100 101 102 103 104 105 106 107 108 109

		cursorEqual(thisCursor, 1, 14);
	});

	test('move right moves to next line', () => {
		moveTo(thisCursor, 1, 8);

		moveRight(thisCursor, 100);

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

S
tests  
Sandeep Somavarapu 已提交
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
	test('move to first character of line from middle', () => {
		moveTo(thisCursor, 1, 8);
		moveToLineStart(thisCursor);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move to first character of line from first non white space character', () => {
		moveTo(thisCursor, 1, 6);

		moveToLineStart(thisCursor);

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

	test('move to first character of line from first character', () => {
		moveTo(thisCursor, 1, 1);

		moveToLineStart(thisCursor);

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

	test('move to first non white space character of line from middle', () => {
		moveTo(thisCursor, 1, 8);

135
		moveToLineFirstNonWhitespaceCharacter(thisCursor);
S
tests  
Sandeep Somavarapu 已提交
136 137 138 139 140 141 142

		cursorEqual(thisCursor, 1, 6);
	});

	test('move to first non white space character of line from first non white space character', () => {
		moveTo(thisCursor, 1, 6);

143
		moveToLineFirstNonWhitespaceCharacter(thisCursor);
S
tests  
Sandeep Somavarapu 已提交
144 145 146 147 148 149 150

		cursorEqual(thisCursor, 1, 6);
	});

	test('move to first non white space character of line from first character', () => {
		moveTo(thisCursor, 1, 1);

151
		moveToLineFirstNonWhitespaceCharacter(thisCursor);
S
tests  
Sandeep Somavarapu 已提交
152 153 154 155 156 157 158 159 160

		cursorEqual(thisCursor, 1, 6);
	});

	test('move to end of line from middle', () => {
		moveTo(thisCursor, 1, 8);

		moveToLineEnd(thisCursor);

A
Alex Dima 已提交
161
		cursorEqual(thisCursor, 1, 21);
S
tests  
Sandeep Somavarapu 已提交
162 163 164
	});

	test('move to end of line from last non white space character', () => {
A
Alex Dima 已提交
165
		moveTo(thisCursor, 1, 19);
S
tests  
Sandeep Somavarapu 已提交
166 167 168

		moveToLineEnd(thisCursor);

A
Alex Dima 已提交
169
		cursorEqual(thisCursor, 1, 21);
S
tests  
Sandeep Somavarapu 已提交
170 171 172
	});

	test('move to end of line from line end', () => {
A
Alex Dima 已提交
173
		moveTo(thisCursor, 1, 21);
S
tests  
Sandeep Somavarapu 已提交
174 175 176

		moveToLineEnd(thisCursor);

A
Alex Dima 已提交
177
		cursorEqual(thisCursor, 1, 21);
S
tests  
Sandeep Somavarapu 已提交
178 179 180 181 182
	});

	test('move to last non white space character from middle', () => {
		moveTo(thisCursor, 1, 8);

183
		moveToLineLastNonWhitespaceCharacter(thisCursor);
S
tests  
Sandeep Somavarapu 已提交
184

A
Alex Dima 已提交
185
		cursorEqual(thisCursor, 1, 19);
S
tests  
Sandeep Somavarapu 已提交
186 187 188
	});

	test('move to last non white space character from last non white space character', () => {
A
Alex Dima 已提交
189
		moveTo(thisCursor, 1, 19);
S
tests  
Sandeep Somavarapu 已提交
190

191
		moveToLineLastNonWhitespaceCharacter(thisCursor);
S
tests  
Sandeep Somavarapu 已提交
192

A
Alex Dima 已提交
193
		cursorEqual(thisCursor, 1, 19);
S
tests  
Sandeep Somavarapu 已提交
194 195 196
	});

	test('move to last non white space character from line end', () => {
A
Alex Dima 已提交
197
		moveTo(thisCursor, 1, 21);
S
tests  
Sandeep Somavarapu 已提交
198

199
		moveToLineLastNonWhitespaceCharacter(thisCursor);
S
tests  
Sandeep Somavarapu 已提交
200

A
Alex Dima 已提交
201
		cursorEqual(thisCursor, 1, 19);
S
tests  
Sandeep Somavarapu 已提交
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
	});

	test('move to center of line not from center', () => {
		moveTo(thisCursor, 1, 8);

		moveToLineCenter(thisCursor);

		cursorEqual(thisCursor, 1, 11);
	});

	test('move to center of line from center', () => {
		moveTo(thisCursor, 1, 11);

		moveToLineCenter(thisCursor);

		cursorEqual(thisCursor, 1, 11);
	});

	test('move to center of line from start', () => {
		moveToLineStart(thisCursor);

		moveToLineCenter(thisCursor);

		cursorEqual(thisCursor, 1, 11);
	});

	test('move to center of line from end', () => {
		moveToLineEnd(thisCursor);

		moveToLineCenter(thisCursor);

		cursorEqual(thisCursor, 1, 11);
	});

	test('move up by cursor move command', () => {

		moveTo(thisCursor, 3, 5);
		cursorEqual(thisCursor, 3, 5);

241
		moveUp(thisCursor, 2);
S
tests  
Sandeep Somavarapu 已提交
242 243
		cursorEqual(thisCursor, 1, 5);

244
		moveUp(thisCursor, 1);
S
tests  
Sandeep Somavarapu 已提交
245 246 247
		cursorEqual(thisCursor, 1, 1);
	});

S
Sandeep Somavarapu 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	test('move up by model line cursor move command', () => {

		moveTo(thisCursor, 3, 5);
		cursorEqual(thisCursor, 3, 5);

		moveUpByModelLine(thisCursor, 2);
		cursorEqual(thisCursor, 1, 5);

		moveUpByModelLine(thisCursor, 1);
		cursorEqual(thisCursor, 1, 1);
	});

	test('move down by model line cursor move command', () => {

		moveTo(thisCursor, 3, 5);
		cursorEqual(thisCursor, 3, 5);

		moveDownByModelLine(thisCursor, 2);
		cursorEqual(thisCursor, 5, 2);

		moveDownByModelLine(thisCursor, 1);
		cursorEqual(thisCursor, 5, 2);
	});

S
tests  
Sandeep Somavarapu 已提交
272 273 274 275 276
	test('move up with selection by cursor move command', () => {

		moveTo(thisCursor, 3, 5);
		cursorEqual(thisCursor, 3, 5);

277
		moveUp(thisCursor, 1, true);
S
tests  
Sandeep Somavarapu 已提交
278 279
		cursorEqual(thisCursor, 2, 2, 3, 5);

280
		moveUp(thisCursor, 1, true);
S
tests  
Sandeep Somavarapu 已提交
281 282 283 284 285 286 287 288
		cursorEqual(thisCursor, 1, 5, 3, 5);
	});

	test('move up and down with tabs by cursor move command', () => {

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

289
		moveDown(thisCursor, 4);
S
tests  
Sandeep Somavarapu 已提交
290 291
		cursorEqual(thisCursor, 5, 2);

292
		moveUp(thisCursor, 1);
S
tests  
Sandeep Somavarapu 已提交
293 294
		cursorEqual(thisCursor, 4, 1);

295
		moveUp(thisCursor, 1);
S
tests  
Sandeep Somavarapu 已提交
296 297
		cursorEqual(thisCursor, 3, 5);

298
		moveUp(thisCursor, 1);
S
tests  
Sandeep Somavarapu 已提交
299 300
		cursorEqual(thisCursor, 2, 2);

301
		moveUp(thisCursor, 1);
S
tests  
Sandeep Somavarapu 已提交
302 303 304 305 306 307
		cursorEqual(thisCursor, 1, 5);
	});

	test('move up and down with end of lines starting from a long one by cursor move command', () => {

		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
308
		cursorEqual(thisCursor, 1, 21);
S
tests  
Sandeep Somavarapu 已提交
309 310

		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
311
		cursorEqual(thisCursor, 1, 21);
S
tests  
Sandeep Somavarapu 已提交
312

313
		moveDown(thisCursor, 2);
A
Alex Dima 已提交
314
		cursorEqual(thisCursor, 3, 17);
S
tests  
Sandeep Somavarapu 已提交
315

316
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
317
		cursorEqual(thisCursor, 4, 1);
S
tests  
Sandeep Somavarapu 已提交
318

319
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
320
		cursorEqual(thisCursor, 5, 2);
S
tests  
Sandeep Somavarapu 已提交
321

322
		moveUp(thisCursor, 4);
A
Alex Dima 已提交
323
		cursorEqual(thisCursor, 1, 21);
S
tests  
Sandeep Somavarapu 已提交
324 325 326
	});

	test('move to view top line moves to first visible line if it is first line', () => {
327
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(1, 1, 10, 1);
S
tests  
Sandeep Somavarapu 已提交
328 329 330 331 332 333 334 335

		moveTo(thisCursor, 2, 2);
		moveToTop(thisCursor);

		cursorEqual(thisCursor, 1, 6);
	});

	test('move to view top line moves to top visible line when first line is not visible', () => {
336
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(2, 1, 10, 1);
S
tests  
Sandeep Somavarapu 已提交
337 338 339 340 341 342 343 344

		moveTo(thisCursor, 4, 1);
		moveToTop(thisCursor);

		cursorEqual(thisCursor, 2, 2);
	});

	test('move to view top line moves to nth line from top', () => {
345
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(1, 1, 10, 1);
S
tests  
Sandeep Somavarapu 已提交
346 347 348 349 350 351 352 353

		moveTo(thisCursor, 4, 1);
		moveToTop(thisCursor, 3);

		cursorEqual(thisCursor, 3, 5);
	});

	test('move to view top line moves to last line if n is greater than last visible line number', () => {
354
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(1, 1, 3, 1);
S
tests  
Sandeep Somavarapu 已提交
355 356 357 358 359 360 361 362

		moveTo(thisCursor, 2, 2);
		moveToTop(thisCursor, 4);

		cursorEqual(thisCursor, 3, 5);
	});

	test('move to view center line moves to the center line', () => {
363
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(3, 1, 3, 1);
S
tests  
Sandeep Somavarapu 已提交
364 365 366 367 368 369 370 371

		moveTo(thisCursor, 2, 2);
		moveToCenter(thisCursor);

		cursorEqual(thisCursor, 3, 5);
	});

	test('move to view bottom line moves to last visible line if it is last line', () => {
372
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(1, 1, 5, 1);
S
tests  
Sandeep Somavarapu 已提交
373 374 375 376 377 378 379 380

		moveTo(thisCursor, 2, 2);
		moveToBottom(thisCursor);

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

	test('move to view bottom line moves to last visible line when last line is not visible', () => {
381
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(2, 1, 3, 1);
S
tests  
Sandeep Somavarapu 已提交
382 383 384 385 386 387 388 389

		moveTo(thisCursor, 2, 2);
		moveToBottom(thisCursor);

		cursorEqual(thisCursor, 3, 5);
	});

	test('move to view bottom line moves to nth line from bottom', () => {
390
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(1, 1, 5, 1);
S
tests  
Sandeep Somavarapu 已提交
391 392 393 394 395 396 397 398

		moveTo(thisCursor, 4, 1);
		moveToBottom(thisCursor, 3);

		cursorEqual(thisCursor, 3, 5);
	});

	test('move to view bottom line moves to first line if n is lesser than first visible line number', () => {
399
		thisViewModel.getCompletelyVisibleViewRange = () => new Range(2, 1, 5, 1);
S
tests  
Sandeep Somavarapu 已提交
400 401 402 403 404 405 406 407 408 409 410

		moveTo(thisCursor, 4, 1);
		moveToBottom(thisCursor, 5);

		cursorEqual(thisCursor, 2, 2);
	});
});

// Move command

function move(cursor: Cursor, args: any) {
A
Alex Dima 已提交
411
	CoreNavigationCommands.CursorMove.runCoreEditorCommand(cursor, args);
S
tests  
Sandeep Somavarapu 已提交
412 413 414
}

function moveToLineStart(cursor: Cursor) {
A
Alex Dima 已提交
415
	move(cursor, { to: CursorMove.RawDirection.WrappedLineStart });
S
tests  
Sandeep Somavarapu 已提交
416 417
}

418
function moveToLineFirstNonWhitespaceCharacter(cursor: Cursor) {
A
Alex Dima 已提交
419
	move(cursor, { to: CursorMove.RawDirection.WrappedLineFirstNonWhitespaceCharacter });
S
tests  
Sandeep Somavarapu 已提交
420 421 422
}

function moveToLineCenter(cursor: Cursor) {
A
Alex Dima 已提交
423
	move(cursor, { to: CursorMove.RawDirection.WrappedLineColumnCenter });
S
tests  
Sandeep Somavarapu 已提交
424 425 426
}

function moveToLineEnd(cursor: Cursor) {
A
Alex Dima 已提交
427
	move(cursor, { to: CursorMove.RawDirection.WrappedLineEnd });
S
tests  
Sandeep Somavarapu 已提交
428 429
}

430
function moveToLineLastNonWhitespaceCharacter(cursor: Cursor) {
A
Alex Dima 已提交
431
	move(cursor, { to: CursorMove.RawDirection.WrappedLineLastNonWhitespaceCharacter });
S
tests  
Sandeep Somavarapu 已提交
432 433
}

434
function moveLeft(cursor: Cursor, value?: number, by?: string, select?: boolean) {
A
Alex Dima 已提交
435
	move(cursor, { to: CursorMove.RawDirection.Left, by: by, value: value, select: select });
436 437
}

438
function moveRight(cursor: Cursor, value?: number, by?: string, select?: boolean) {
A
Alex Dima 已提交
439
	move(cursor, { to: CursorMove.RawDirection.Right, by: by, value: value, select: select });
440 441
}

J
Johannes Rieken 已提交
442
function moveUp(cursor: Cursor, noOfLines: number = 1, select?: boolean) {
A
Alex Dima 已提交
443
	move(cursor, { to: CursorMove.RawDirection.Up, by: CursorMove.RawUnit.WrappedLine, value: noOfLines, select: select });
S
tests  
Sandeep Somavarapu 已提交
444 445
}

J
Johannes Rieken 已提交
446
function moveUpByModelLine(cursor: Cursor, noOfLines: number = 1, select?: boolean) {
A
Alex Dima 已提交
447
	move(cursor, { to: CursorMove.RawDirection.Up, value: noOfLines, select: select });
S
Sandeep Somavarapu 已提交
448 449
}

J
Johannes Rieken 已提交
450
function moveDown(cursor: Cursor, noOfLines: number = 1, select?: boolean) {
A
Alex Dima 已提交
451
	move(cursor, { to: CursorMove.RawDirection.Down, by: CursorMove.RawUnit.WrappedLine, value: noOfLines, select: select });
S
tests  
Sandeep Somavarapu 已提交
452 453
}

J
Johannes Rieken 已提交
454
function moveDownByModelLine(cursor: Cursor, noOfLines: number = 1, select?: boolean) {
A
Alex Dima 已提交
455
	move(cursor, { to: CursorMove.RawDirection.Down, value: noOfLines, select: select });
S
Sandeep Somavarapu 已提交
456 457
}

J
Johannes Rieken 已提交
458
function moveToTop(cursor: Cursor, noOfLines: number = 1, select?: boolean) {
A
Alex Dima 已提交
459
	move(cursor, { to: CursorMove.RawDirection.ViewPortTop, value: noOfLines, select: select });
S
tests  
Sandeep Somavarapu 已提交
460 461
}

462
function moveToCenter(cursor: Cursor, select?: boolean) {
A
Alex Dima 已提交
463
	move(cursor, { to: CursorMove.RawDirection.ViewPortCenter, select: select });
S
tests  
Sandeep Somavarapu 已提交
464 465
}

J
Johannes Rieken 已提交
466
function moveToBottom(cursor: Cursor, noOfLines: number = 1, select?: boolean) {
A
Alex Dima 已提交
467
	move(cursor, { to: CursorMove.RawDirection.ViewPortBottom, value: noOfLines, select: select });
S
tests  
Sandeep Somavarapu 已提交
468 469 470 471 472 473 474
}

function cursorEqual(cursor: Cursor, posLineNumber: number, posColumn: number, selLineNumber: number = posLineNumber, selColumn: number = posColumn) {
	positionEqual(cursor.getPosition(), posLineNumber, posColumn);
	selectionEqual(cursor.getSelection(), posLineNumber, posColumn, selLineNumber, selColumn);
}

475 476
function positionEqual(position: Position, lineNumber: number, column: number) {
	assert.deepEqual(position, new Position(lineNumber, column), 'position equal');
S
tests  
Sandeep Somavarapu 已提交
477 478
}

A
Alex Dima 已提交
479
function selectionEqual(selection: Selection, posLineNumber: number, posColumn: number, selLineNumber: number, selColumn: number) {
S
tests  
Sandeep Somavarapu 已提交
480 481 482 483 484 485
	assert.deepEqual({
		selectionStartLineNumber: selection.selectionStartLineNumber,
		selectionStartColumn: selection.selectionStartColumn,
		positionLineNumber: selection.positionLineNumber,
		positionColumn: selection.positionColumn
	}, {
J
Johannes Rieken 已提交
486 487 488 489 490
			selectionStartLineNumber: selLineNumber,
			selectionStartColumn: selColumn,
			positionLineNumber: posLineNumber,
			positionColumn: posColumn
		}, 'selection equal');
S
tests  
Sandeep Somavarapu 已提交
491 492 493
}

function moveTo(cursor: Cursor, lineNumber: number, column: number, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
494
	if (inSelectionMode) {
A
Alex Dima 已提交
495
		CoreNavigationCommands.MoveToSelect.runCoreEditorCommand(cursor, {
A
Alex Dima 已提交
496 497 498
			position: new Position(lineNumber, column)
		});
	} else {
A
Alex Dima 已提交
499
		CoreNavigationCommands.MoveTo.runCoreEditorCommand(cursor, {
A
Alex Dima 已提交
500 501 502
			position: new Position(lineNumber, column)
		});
	}
S
tests  
Sandeep Somavarapu 已提交
503 504 505
}

function moveToEndOfLine(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
506
	if (inSelectionMode) {
A
Alex Dima 已提交
507
		CoreNavigationCommands.CursorEndSelect.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
508
	} else {
A
Alex Dima 已提交
509
		CoreNavigationCommands.CursorEnd.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
510 511
	}
}