cursor.test.ts 103.0 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';
J
Johannes Rieken 已提交
8 9 10 11 12
import { Cursor } from 'vs/editor/common/controller/cursor';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
13
import {
A
Alex Dima 已提交
14
	EndOfLinePreference, EventType, Handler, IEditorOptions,
15
	DefaultEndOfLine, ITextModelCreationOptions, ICommand,
A
Alex Dima 已提交
16 17
	ITokenizedModel, IEditOperationBuilder, ICursorStateComputerData,
	ICursorPositionChangedEvent, ICursorSelectionChangedEvent
18
} from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
19
import { Model } from 'vs/editor/common/model/model';
20
import { IndentAction } from 'vs/editor/common/modes/languageConfiguration';
J
Johannes Rieken 已提交
21 22 23 24
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
import { MockConfiguration } from 'vs/editor/test/common/mocks/mockConfiguration';
import { MockMode } from 'vs/editor/test/common/mocks/mockMode';
import { viewModelHelper } from 'vs/editor/test/common/editorTestUtils';
E
Erich Gamma 已提交
25

A
Alex Dima 已提交
26
let H = Handler;
E
Erich Gamma 已提交
27 28 29

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

30
function cursorCommand(cursor: Cursor, command: string, extraData?: any, overwriteSource?: string) {
A
Alex Dima 已提交
31
	cursor.trigger(overwriteSource || 'tests', command, extraData);
E
Erich Gamma 已提交
32 33
}

A
Alex Dima 已提交
34 35
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 已提交
36 37
}

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

A
Alex Dima 已提交
42
function moveWordLeft(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
43 44
	cursorCommand(cursor, inSelectionMode ? H.CursorWordLeftSelect : H.CursorWordLeft);
}
45 46 47 48 49 50
function moveWordStartLeft(cursor: Cursor, inSelectionMode: boolean = false) {
	cursorCommand(cursor, inSelectionMode ? H.CursorWordStartLeftSelect : H.CursorWordStartLeft);
}
function moveWordEndLeft(cursor: Cursor, inSelectionMode: boolean = false) {
	cursorCommand(cursor, inSelectionMode ? H.CursorWordEndLeftSelect : H.CursorWordEndLeft);
}
E
Erich Gamma 已提交
51

A
Alex Dima 已提交
52
function moveRight(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
53 54 55
	cursorCommand(cursor, inSelectionMode ? H.CursorRightSelect : H.CursorRight);
}

A
Alex Dima 已提交
56
function moveWordRight(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
57 58
	cursorCommand(cursor, inSelectionMode ? H.CursorWordRightSelect : H.CursorWordRight);
}
59 60 61 62 63 64
function moveWordEndRight(cursor: Cursor, inSelectionMode: boolean = false) {
	cursorCommand(cursor, inSelectionMode ? H.CursorWordEndRightSelect : H.CursorWordEndRight);
}
function moveWordStartRight(cursor: Cursor, inSelectionMode: boolean = false) {
	cursorCommand(cursor, inSelectionMode ? H.CursorWordStartRightSelect : H.CursorWordStartRight);
}
E
Erich Gamma 已提交
65

A
Alex Dima 已提交
66
function moveDown(cursor: Cursor, linesCount: number, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
67 68 69
	if (linesCount === 1) {
		cursorCommand(cursor, inSelectionMode ? H.CursorDownSelect : H.CursorDown);
	} else {
70
		cursorCommand(cursor, inSelectionMode ? H.CursorPageDownSelect : H.CursorPageDown, { pageSize: linesCount });
E
Erich Gamma 已提交
71 72 73
	}
}

A
Alex Dima 已提交
74
function moveUp(cursor: Cursor, linesCount: number, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
75 76 77
	if (linesCount === 1) {
		cursorCommand(cursor, inSelectionMode ? H.CursorUpSelect : H.CursorUp);
	} else {
78
		cursorCommand(cursor, inSelectionMode ? H.CursorPageUpSelect : H.CursorPageUp, { pageSize: linesCount });
E
Erich Gamma 已提交
79 80 81
	}
}

A
Alex Dima 已提交
82
function moveToBeginningOfLine(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
83 84 85
	cursorCommand(cursor, inSelectionMode ? H.CursorHomeSelect : H.CursorHome);
}

A
Alex Dima 已提交
86
function moveToEndOfLine(cursor: Cursor, inSelectionMode: boolean = false) {
J
Johannes Rieken 已提交
87
	cursorCommand(cursor, inSelectionMode ? H.CursorEndSelect : H.CursorEnd);
E
Erich Gamma 已提交
88 89
}

A
Alex Dima 已提交
90
function moveToBeginningOfBuffer(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
91 92 93
	cursorCommand(cursor, inSelectionMode ? H.CursorTopSelect : H.CursorTop);
}

A
Alex Dima 已提交
94
function moveToEndOfBuffer(cursor: Cursor, inSelectionMode: boolean = false) {
E
Erich Gamma 已提交
95 96 97
	cursorCommand(cursor, inSelectionMode ? H.CursorBottomSelect : H.CursorBottom);
}

A
Alex Dima 已提交
98
function deleteWordLeft(cursor: Cursor) {
E
Erich Gamma 已提交
99 100
	cursorCommand(cursor, H.DeleteWordLeft);
}
101 102 103 104 105 106
function deleteWordStartLeft(cursor: Cursor) {
	cursorCommand(cursor, H.DeleteWordStartLeft);
}
function deleteWordEndLeft(cursor: Cursor) {
	cursorCommand(cursor, H.DeleteWordEndLeft);
}
E
Erich Gamma 已提交
107

A
Alex Dima 已提交
108
function deleteWordRight(cursor: Cursor) {
E
Erich Gamma 已提交
109 110
	cursorCommand(cursor, H.DeleteWordRight);
}
111 112 113 114 115 116
function deleteWordStartRight(cursor: Cursor) {
	cursorCommand(cursor, H.DeleteWordStartRight);
}
function deleteWordEndRight(cursor: Cursor) {
	cursorCommand(cursor, H.DeleteWordEndRight);
}
E
Erich Gamma 已提交
117

J
Johannes Rieken 已提交
118 119
function assertCursor(cursor: Cursor, what: Position | Selection | Selection[]): void {
	let selections: Selection[];
A
Alex Dima 已提交
120 121 122 123 124 125 126
	if (what instanceof Position) {
		selections = [new Selection(what.lineNumber, what.column, what.lineNumber, what.column)];
	} else if (what instanceof Selection) {
		selections = [what];
	} else {
		selections = what;
	}
A
Alex Dima 已提交
127 128 129 130 131
	let actual = cursor.getSelections().map(s => s.toString());
	let expected = selections.map(s => s.toString());

	assert.deepEqual(actual, expected);
}
E
Erich Gamma 已提交
132 133

suite('Editor Controller - Cursor', () => {
A
Alex Dima 已提交
134 135 136 137 138
	const LINE1 = '    \tMy First Line\t ';
	const LINE2 = '\tMy Second Line';
	const LINE3 = '    Third Line💩';
	const LINE4 = '';
	const LINE5 = '1';
E
Erich Gamma 已提交
139

A
Alex Dima 已提交
140 141 142
	let thisModel: Model;
	let thisConfiguration: MockConfiguration;
	let thisCursor: Cursor;
E
Erich Gamma 已提交
143 144

	setup(() => {
A
Alex Dima 已提交
145
		let text =
E
Erich Gamma 已提交
146 147 148 149 150 151
			LINE1 + '\r\n' +
			LINE2 + '\n' +
			LINE3 + '\n' +
			LINE4 + '\r\n' +
			LINE5;

152
		thisModel = Model.createFromString(text);
E
Erich Gamma 已提交
153
		thisConfiguration = new MockConfiguration(null);
154
		thisCursor = new Cursor(1, thisConfiguration, thisModel, viewModelHelper(thisModel), false);
E
Erich Gamma 已提交
155 156 157 158 159 160 161 162 163
	});

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

	test('cursor initialized', () => {
A
Alex Dima 已提交
164
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
165 166 167 168 169 170
	});

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

	test('no move', () => {
		moveTo(thisCursor, 1, 1);
A
Alex Dima 已提交
171
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
172 173 174 175
	});

	test('move', () => {
		moveTo(thisCursor, 1, 2);
A
Alex Dima 已提交
176
		assertCursor(thisCursor, new Position(1, 2));
E
Erich Gamma 已提交
177 178 179 180
	});

	test('move in selection mode', () => {
		moveTo(thisCursor, 1, 2, true);
A
Alex Dima 已提交
181
		assertCursor(thisCursor, new Selection(1, 1, 1, 2));
E
Erich Gamma 已提交
182 183 184 185
	});

	test('move beyond line end', () => {
		moveTo(thisCursor, 1, 25);
A
Alex Dima 已提交
186
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
187 188 189 190
	});

	test('move empty line', () => {
		moveTo(thisCursor, 4, 20);
A
Alex Dima 已提交
191
		assertCursor(thisCursor, new Position(4, 1));
E
Erich Gamma 已提交
192 193 194 195
	});

	test('move one char line', () => {
		moveTo(thisCursor, 5, 20);
A
Alex Dima 已提交
196
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
197 198 199 200
	});

	test('selection down', () => {
		moveTo(thisCursor, 2, 1, true);
A
Alex Dima 已提交
201
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
E
Erich Gamma 已提交
202 203 204 205
	});

	test('move and then select', () => {
		moveTo(thisCursor, 2, 3);
A
Alex Dima 已提交
206
		assertCursor(thisCursor, new Position(2, 3));
E
Erich Gamma 已提交
207 208

		moveTo(thisCursor, 2, 15, true);
A
Alex Dima 已提交
209
		assertCursor(thisCursor, new Selection(2, 3, 2, 15));
E
Erich Gamma 已提交
210 211

		moveTo(thisCursor, 1, 2, true);
A
Alex Dima 已提交
212
		assertCursor(thisCursor, new Selection(2, 3, 1, 2));
E
Erich Gamma 已提交
213 214 215 216 217 218
	});

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

	test('move left on top left position', () => {
		moveLeft(thisCursor);
A
Alex Dima 已提交
219
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
220 221 222 223
	});

	test('move left', () => {
		moveTo(thisCursor, 1, 3);
A
Alex Dima 已提交
224
		assertCursor(thisCursor, new Position(1, 3));
E
Erich Gamma 已提交
225
		moveLeft(thisCursor);
A
Alex Dima 已提交
226
		assertCursor(thisCursor, new Position(1, 2));
E
Erich Gamma 已提交
227 228
	});

229 230
	test('move left with surrogate pair', () => {
		moveTo(thisCursor, 3, 17);
A
Alex Dima 已提交
231
		assertCursor(thisCursor, new Position(3, 17));
232
		moveLeft(thisCursor);
A
Alex Dima 已提交
233
		assertCursor(thisCursor, new Position(3, 15));
234 235
	});

E
Erich Gamma 已提交
236 237
	test('move left goes to previous row', () => {
		moveTo(thisCursor, 2, 1);
A
Alex Dima 已提交
238
		assertCursor(thisCursor, new Position(2, 1));
E
Erich Gamma 已提交
239
		moveLeft(thisCursor);
A
Alex Dima 已提交
240
		assertCursor(thisCursor, new Position(1, 21));
E
Erich Gamma 已提交
241 242 243 244
	});

	test('move left selection', () => {
		moveTo(thisCursor, 2, 1);
A
Alex Dima 已提交
245
		assertCursor(thisCursor, new Position(2, 1));
E
Erich Gamma 已提交
246
		moveLeft(thisCursor, true);
A
Alex Dima 已提交
247
		assertCursor(thisCursor, new Selection(2, 1, 1, 21));
E
Erich Gamma 已提交
248 249 250 251 252 253
	});

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

	test('move word left', () => {
		moveTo(thisCursor, 5, 2);
A
Alex Dima 已提交
254
		let expectedStops = [
E
Erich Gamma 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
			[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],
		];

J
Johannes Rieken 已提交
271
		let actualStops: number[][] = [];
A
Alex Dima 已提交
272
		for (let i = 0; i < expectedStops.length; i++) {
E
Erich Gamma 已提交
273
			moveWordLeft(thisCursor);
A
Alex Dima 已提交
274
			let pos = thisCursor.getPosition();
E
Erich Gamma 已提交
275 276 277 278 279 280 281 282
			actualStops.push([pos.lineNumber, pos.column]);
		}

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

	test('move word left selection', () => {
		moveTo(thisCursor, 5, 2);
A
Alex Dima 已提交
283
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
284
		moveWordLeft(thisCursor, true);
A
Alex Dima 已提交
285
		assertCursor(thisCursor, new Selection(5, 2, 5, 1));
E
Erich Gamma 已提交
286 287 288 289 290 291
	});

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

	test('move right on bottom right position', () => {
		moveTo(thisCursor, 5, 2);
A
Alex Dima 已提交
292
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
293
		moveRight(thisCursor);
A
Alex Dima 已提交
294
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
295 296 297 298
	});

	test('move right', () => {
		moveTo(thisCursor, 1, 3);
A
Alex Dima 已提交
299
		assertCursor(thisCursor, new Position(1, 3));
E
Erich Gamma 已提交
300
		moveRight(thisCursor);
A
Alex Dima 已提交
301
		assertCursor(thisCursor, new Position(1, 4));
E
Erich Gamma 已提交
302 303
	});

304 305
	test('move right with surrogate pair', () => {
		moveTo(thisCursor, 3, 15);
A
Alex Dima 已提交
306
		assertCursor(thisCursor, new Position(3, 15));
307
		moveRight(thisCursor);
A
Alex Dima 已提交
308
		assertCursor(thisCursor, new Position(3, 17));
309 310
	});

E
Erich Gamma 已提交
311 312
	test('move right goes to next row', () => {
		moveTo(thisCursor, 1, 21);
A
Alex Dima 已提交
313
		assertCursor(thisCursor, new Position(1, 21));
E
Erich Gamma 已提交
314
		moveRight(thisCursor);
A
Alex Dima 已提交
315
		assertCursor(thisCursor, new Position(2, 1));
E
Erich Gamma 已提交
316 317 318 319
	});

	test('move right selection', () => {
		moveTo(thisCursor, 1, 21);
A
Alex Dima 已提交
320
		assertCursor(thisCursor, new Position(1, 21));
E
Erich Gamma 已提交
321
		moveRight(thisCursor, true);
A
Alex Dima 已提交
322
		assertCursor(thisCursor, new Selection(1, 21, 2, 1));
E
Erich Gamma 已提交
323 324 325 326 327 328
	});

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

	test('move word right', () => {
		moveTo(thisCursor, 1, 1);
A
Alex Dima 已提交
329
		let expectedStops = [
E
Erich Gamma 已提交
330 331 332 333 334 335 336 337
			[1, 8],
			[1, 14],
			[1, 19],
			[1, 21],
			[2, 4],
			[2, 11],
			[2, 16],
			[3, 10],
338
			[3, 17],
E
Erich Gamma 已提交
339 340 341 342 343
			[4, 1],
			[5, 2],
			[5, 2],
		];

J
Johannes Rieken 已提交
344
		let actualStops: number[][] = [];
A
Alex Dima 已提交
345
		for (let i = 0; i < expectedStops.length; i++) {
E
Erich Gamma 已提交
346
			moveWordRight(thisCursor);
A
Alex Dima 已提交
347
			let pos = thisCursor.getPosition();
E
Erich Gamma 已提交
348 349 350 351 352 353 354 355
			actualStops.push([pos.lineNumber, pos.column]);
		}

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

	test('move word right selection', () => {
		moveTo(thisCursor, 1, 1);
A
Alex Dima 已提交
356
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
357
		moveWordRight(thisCursor, true);
A
Alex Dima 已提交
358
		assertCursor(thisCursor, new Selection(1, 1, 1, 8));
E
Erich Gamma 已提交
359 360 361 362 363
	});
	// --------- move down

	test('move down', () => {
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
364
		assertCursor(thisCursor, new Position(2, 1));
E
Erich Gamma 已提交
365
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
366
		assertCursor(thisCursor, new Position(3, 1));
E
Erich Gamma 已提交
367
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
368
		assertCursor(thisCursor, new Position(4, 1));
E
Erich Gamma 已提交
369
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
370
		assertCursor(thisCursor, new Position(5, 1));
E
Erich Gamma 已提交
371
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
372
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
373 374 375 376
	});

	test('move down with selection', () => {
		moveDown(thisCursor, 1, true);
A
Alex Dima 已提交
377
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
E
Erich Gamma 已提交
378
		moveDown(thisCursor, 1, true);
A
Alex Dima 已提交
379
		assertCursor(thisCursor, new Selection(1, 1, 3, 1));
E
Erich Gamma 已提交
380
		moveDown(thisCursor, 1, true);
A
Alex Dima 已提交
381
		assertCursor(thisCursor, new Selection(1, 1, 4, 1));
E
Erich Gamma 已提交
382
		moveDown(thisCursor, 1, true);
A
Alex Dima 已提交
383
		assertCursor(thisCursor, new Selection(1, 1, 5, 1));
E
Erich Gamma 已提交
384
		moveDown(thisCursor, 1, true);
A
Alex Dima 已提交
385
		assertCursor(thisCursor, new Selection(1, 1, 5, 2));
E
Erich Gamma 已提交
386 387 388 389
	});

	test('move down with tabs', () => {
		moveTo(thisCursor, 1, 5);
A
Alex Dima 已提交
390
		assertCursor(thisCursor, new Position(1, 5));
E
Erich Gamma 已提交
391
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
392
		assertCursor(thisCursor, new Position(2, 2));
E
Erich Gamma 已提交
393
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
394
		assertCursor(thisCursor, new Position(3, 5));
E
Erich Gamma 已提交
395
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
396
		assertCursor(thisCursor, new Position(4, 1));
E
Erich Gamma 已提交
397
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
398
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
399 400 401 402 403 404
	});

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

	test('move up', () => {
		moveTo(thisCursor, 3, 5);
A
Alex Dima 已提交
405
		assertCursor(thisCursor, new Position(3, 5));
E
Erich Gamma 已提交
406 407

		moveUp(thisCursor, 1);
A
Alex Dima 已提交
408
		assertCursor(thisCursor, new Position(2, 2));
E
Erich Gamma 已提交
409 410

		moveUp(thisCursor, 1);
A
Alex Dima 已提交
411
		assertCursor(thisCursor, new Position(1, 5));
E
Erich Gamma 已提交
412 413 414 415
	});

	test('move up with selection', () => {
		moveTo(thisCursor, 3, 5);
A
Alex Dima 已提交
416
		assertCursor(thisCursor, new Position(3, 5));
E
Erich Gamma 已提交
417 418

		moveUp(thisCursor, 1, true);
A
Alex Dima 已提交
419
		assertCursor(thisCursor, new Selection(3, 5, 2, 2));
E
Erich Gamma 已提交
420 421

		moveUp(thisCursor, 1, true);
A
Alex Dima 已提交
422
		assertCursor(thisCursor, new Selection(3, 5, 1, 5));
E
Erich Gamma 已提交
423 424 425 426
	});

	test('move up and down with tabs', () => {
		moveTo(thisCursor, 1, 5);
A
Alex Dima 已提交
427
		assertCursor(thisCursor, new Position(1, 5));
E
Erich Gamma 已提交
428
		moveDown(thisCursor, 4);
A
Alex Dima 已提交
429
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
430
		moveUp(thisCursor, 1);
A
Alex Dima 已提交
431
		assertCursor(thisCursor, new Position(4, 1));
E
Erich Gamma 已提交
432
		moveUp(thisCursor, 1);
A
Alex Dima 已提交
433
		assertCursor(thisCursor, new Position(3, 5));
E
Erich Gamma 已提交
434
		moveUp(thisCursor, 1);
A
Alex Dima 已提交
435
		assertCursor(thisCursor, new Position(2, 2));
E
Erich Gamma 已提交
436
		moveUp(thisCursor, 1);
A
Alex Dima 已提交
437
		assertCursor(thisCursor, new Position(1, 5));
E
Erich Gamma 已提交
438 439 440 441
	});

	test('move up and down with end of lines starting from a long one', () => {
		moveToEndOfLine(thisCursor);
442
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
443
		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
444
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
445
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
446
		assertCursor(thisCursor, new Position(2, LINE2.length + 1));
E
Erich Gamma 已提交
447
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
448
		assertCursor(thisCursor, new Position(3, LINE3.length + 1));
E
Erich Gamma 已提交
449
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
450
		assertCursor(thisCursor, new Position(4, LINE4.length + 1));
E
Erich Gamma 已提交
451
		moveDown(thisCursor, 1);
A
Alex Dima 已提交
452
		assertCursor(thisCursor, new Position(5, LINE5.length + 1));
E
Erich Gamma 已提交
453
		moveUp(thisCursor, 4);
A
Alex Dima 已提交
454
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
455 456 457 458 459 460
	});

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

	test('move to beginning of line', () => {
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
461
		assertCursor(thisCursor, new Position(1, 6));
E
Erich Gamma 已提交
462
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
463
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
464 465 466 467 468
	});

	test('move to beginning of line from within line', () => {
		moveTo(thisCursor, 1, 8);
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
469
		assertCursor(thisCursor, new Position(1, 6));
E
Erich Gamma 已提交
470
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
471
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
472 473 474 475 476
	});

	test('move to beginning of line from whitespace at beginning of line', () => {
		moveTo(thisCursor, 1, 2);
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
477
		assertCursor(thisCursor, new Position(1, 6));
478 479
		moveToBeginningOfLine(thisCursor);
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
480 481 482 483 484
	});

	test('move to beginning of line from within line selection', () => {
		moveTo(thisCursor, 1, 8);
		moveToBeginningOfLine(thisCursor, true);
A
Alex Dima 已提交
485
		assertCursor(thisCursor, new Selection(1, 8, 1, 6));
E
Erich Gamma 已提交
486
		moveToBeginningOfLine(thisCursor, true);
A
Alex Dima 已提交
487
		assertCursor(thisCursor, new Selection(1, 8, 1, 1));
E
Erich Gamma 已提交
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
	test('move to beginning of line with selection multiline forward', () => {
		moveTo(thisCursor, 1, 8);
		moveTo(thisCursor, 3, 9, true);
		moveToBeginningOfLine(thisCursor, false);
		assertCursor(thisCursor, new Selection(1, 6, 1, 6));
	});

	test('move to beginning of line with selection multiline backward', () => {
		moveTo(thisCursor, 3, 9);
		moveTo(thisCursor, 1, 8, true);
		moveToBeginningOfLine(thisCursor, false);
		assertCursor(thisCursor, new Selection(1, 6, 1, 6));
	});

	test('move to beginning of line with selection single line forward', () => {
		moveTo(thisCursor, 3, 2);
		moveTo(thisCursor, 3, 9, true);
		moveToBeginningOfLine(thisCursor, false);
		assertCursor(thisCursor, new Selection(3, 5, 3, 5));
	});

	test('move to beginning of line with selection single line backward', () => {
		moveTo(thisCursor, 3, 9);
		moveTo(thisCursor, 3, 2, true);
		moveToBeginningOfLine(thisCursor, false);
		assertCursor(thisCursor, new Selection(3, 5, 3, 5));
	});

518
	test('issue #15401: "End" key is behaving weird when text is selected part 1', () => {
519
		moveTo(thisCursor, 1, 8);
520 521
		moveTo(thisCursor, 3, 9, true);
		moveToBeginningOfLine(thisCursor, false);
522
		assertCursor(thisCursor, new Selection(1, 6, 1, 6));
523 524
	});

E
Erich Gamma 已提交
525 526 527 528
	// --------- move to end of line

	test('move to end of line', () => {
		moveToEndOfLine(thisCursor);
529
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
530
		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
531
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
532 533 534 535 536
	});

	test('move to end of line from within line', () => {
		moveTo(thisCursor, 1, 6);
		moveToEndOfLine(thisCursor);
537
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
538
		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
539
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
540 541 542 543 544
	});

	test('move to end of line from whitespace at end of line', () => {
		moveTo(thisCursor, 1, 20);
		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
545
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
546
		moveToEndOfLine(thisCursor);
547
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
548 549 550 551 552
	});

	test('move to end of line from within line selection', () => {
		moveTo(thisCursor, 1, 6);
		moveToEndOfLine(thisCursor, true);
553
		assertCursor(thisCursor, new Selection(1, 6, 1, LINE1.length + 1));
E
Erich Gamma 已提交
554
		moveToEndOfLine(thisCursor, true);
A
Alex Dima 已提交
555
		assertCursor(thisCursor, new Selection(1, 6, 1, LINE1.length + 1));
E
Erich Gamma 已提交
556 557
	});

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	test('move to end of line with selection multiline forward', () => {
		moveTo(thisCursor, 1, 1);
		moveTo(thisCursor, 3, 9, true);
		moveToEndOfLine(thisCursor, false);
		assertCursor(thisCursor, new Selection(3, 17, 3, 17));
	});

	test('move to end of line with selection multiline backward', () => {
		moveTo(thisCursor, 3, 9);
		moveTo(thisCursor, 1, 1, true);
		moveToEndOfLine(thisCursor, false);
		assertCursor(thisCursor, new Selection(3, 17, 3, 17));
	});

	test('move to end of line with selection single line forward', () => {
		moveTo(thisCursor, 3, 1);
		moveTo(thisCursor, 3, 9, true);
		moveToEndOfLine(thisCursor, false);
		assertCursor(thisCursor, new Selection(3, 17, 3, 17));
	});

	test('move to end of line with selection single line backward', () => {
		moveTo(thisCursor, 3, 9);
		moveTo(thisCursor, 3, 1, true);
		moveToEndOfLine(thisCursor, false);
		assertCursor(thisCursor, new Selection(3, 17, 3, 17));
	});

586 587 588 589
	test('issue #15401: "End" key is behaving weird when text is selected part 2', () => {
		moveTo(thisCursor, 1, 1);
		moveTo(thisCursor, 3, 9, true);
		moveToEndOfLine(thisCursor, false);
590
		assertCursor(thisCursor, new Selection(3, 17, 3, 17));
591 592
	});

E
Erich Gamma 已提交
593 594 595 596
	// --------- move to beginning of buffer

	test('move to beginning of buffer', () => {
		moveToBeginningOfBuffer(thisCursor);
A
Alex Dima 已提交
597
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
598 599 600 601 602
	});

	test('move to beginning of buffer from within first line', () => {
		moveTo(thisCursor, 1, 3);
		moveToBeginningOfBuffer(thisCursor);
A
Alex Dima 已提交
603
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
604 605 606 607 608
	});

	test('move to beginning of buffer from within another line', () => {
		moveTo(thisCursor, 3, 3);
		moveToBeginningOfBuffer(thisCursor);
A
Alex Dima 已提交
609
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
610 611 612 613 614
	});

	test('move to beginning of buffer from within first line selection', () => {
		moveTo(thisCursor, 1, 3);
		moveToBeginningOfBuffer(thisCursor, true);
A
Alex Dima 已提交
615
		assertCursor(thisCursor, new Selection(1, 3, 1, 1));
E
Erich Gamma 已提交
616 617 618 619 620
	});

	test('move to beginning of buffer from within another line selection', () => {
		moveTo(thisCursor, 3, 3);
		moveToBeginningOfBuffer(thisCursor, true);
A
Alex Dima 已提交
621
		assertCursor(thisCursor, new Selection(3, 3, 1, 1));
E
Erich Gamma 已提交
622 623 624 625 626 627
	});

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

	test('move to end of buffer', () => {
		moveToEndOfBuffer(thisCursor);
A
Alex Dima 已提交
628
		assertCursor(thisCursor, new Position(5, LINE5.length + 1));
E
Erich Gamma 已提交
629 630 631 632 633
	});

	test('move to end of buffer from within last line', () => {
		moveTo(thisCursor, 5, 1);
		moveToEndOfBuffer(thisCursor);
A
Alex Dima 已提交
634
		assertCursor(thisCursor, new Position(5, LINE5.length + 1));
E
Erich Gamma 已提交
635 636 637 638 639
	});

	test('move to end of buffer from within another line', () => {
		moveTo(thisCursor, 3, 3);
		moveToEndOfBuffer(thisCursor);
A
Alex Dima 已提交
640
		assertCursor(thisCursor, new Position(5, LINE5.length + 1));
E
Erich Gamma 已提交
641 642 643 644 645
	});

	test('move to end of buffer from within last line selection', () => {
		moveTo(thisCursor, 5, 1);
		moveToEndOfBuffer(thisCursor, true);
A
Alex Dima 已提交
646
		assertCursor(thisCursor, new Selection(5, 1, 5, LINE5.length + 1));
E
Erich Gamma 已提交
647 648 649 650 651
	});

	test('move to end of buffer from within another line selection', () => {
		moveTo(thisCursor, 3, 3);
		moveToEndOfBuffer(thisCursor, true);
A
Alex Dima 已提交
652
		assertCursor(thisCursor, new Selection(3, 3, 5, LINE5.length + 1));
E
Erich Gamma 已提交
653 654 655 656 657 658 659 660 661
	});

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

	test('delete word left for non-empty selection', () => {
		moveTo(thisCursor, 3, 7);
		moveRight(thisCursor, true);
		moveRight(thisCursor, true);
		deleteWordLeft(thisCursor);
662
		assert.equal(thisModel.getLineContent(3), '    Thd Line💩');
A
Alex Dima 已提交
663
		assertCursor(thisCursor, new Position(3, 7));
E
Erich Gamma 已提交
664 665 666 667 668 669
	});

	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 ');
A
Alex Dima 已提交
670
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
671 672 673 674 675
	});

	test('delete word left for caret at end of whitespace', () => {
		moveTo(thisCursor, 3, 11);
		deleteWordLeft(thisCursor);
676
		assert.equal(thisModel.getLineContent(3), '    Line💩');
A
Alex Dima 已提交
677
		assertCursor(thisCursor, new Position(3, 5));
E
Erich Gamma 已提交
678 679 680 681 682 683
	});

	test('delete word left for caret just behind a word', () => {
		moveTo(thisCursor, 2, 11);
		deleteWordLeft(thisCursor);
		assert.equal(thisModel.getLineContent(2), '\tMy  Line');
A
Alex Dima 已提交
684
		assertCursor(thisCursor, new Position(2, 5));
E
Erich Gamma 已提交
685 686 687 688 689 690
	});

	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 ');
A
Alex Dima 已提交
691
		assertCursor(thisCursor, new Position(1, 9));
E
Erich Gamma 已提交
692 693 694 695 696 697 698
	});

	test('delete word right for non-empty selection', () => {
		moveTo(thisCursor, 3, 7);
		moveRight(thisCursor, true);
		moveRight(thisCursor, true);
		deleteWordRight(thisCursor);
699
		assert.equal(thisModel.getLineContent(3), '    Thd Line💩');
A
Alex Dima 已提交
700
		assertCursor(thisCursor, new Position(3, 7));
E
Erich Gamma 已提交
701 702 703 704 705 706
	});

	test('delete word right for caret at end of document', () => {
		moveTo(thisCursor, 5, 3);
		deleteWordRight(thisCursor);
		assert.equal(thisModel.getLineContent(5), '1');
A
Alex Dima 已提交
707
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
708 709 710 711 712
	});

	test('delete word right for caret at beggining of whitespace', () => {
		moveTo(thisCursor, 3, 1);
		deleteWordRight(thisCursor);
713
		assert.equal(thisModel.getLineContent(3), 'Third Line💩');
A
Alex Dima 已提交
714
		assertCursor(thisCursor, new Position(3, 1));
E
Erich Gamma 已提交
715 716 717 718 719 720
	});

	test('delete word right for caret just before a word', () => {
		moveTo(thisCursor, 2, 5);
		deleteWordRight(thisCursor);
		assert.equal(thisModel.getLineContent(2), '\tMy  Line');
A
Alex Dima 已提交
721
		assertCursor(thisCursor, new Position(2, 5));
E
Erich Gamma 已提交
722 723 724 725 726 727
	});

	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 ');
A
Alex Dima 已提交
728
		assertCursor(thisCursor, new Position(1, 11));
E
Erich Gamma 已提交
729 730 731 732 733 734
	});

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

	test('select all', () => {
		cursorCommand(thisCursor, H.SelectAll);
A
Alex Dima 已提交
735
		assertCursor(thisCursor, new Selection(1, 1, 5, LINE5.length + 1));
E
Erich Gamma 已提交
736 737
	});

A
Alex Dima 已提交
738 739 740
	test('expandLineSelection', () => {
		//              0          1         2
		//              01234 56789012345678 0
A
Alex Dima 已提交
741
		// let LINE1 = '    \tMy First Line\t ';
A
Alex Dima 已提交
742 743
		moveTo(thisCursor, 1, 1);
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
744
		assertCursor(thisCursor, new Selection(1, 1, 1, LINE1.length + 1));
A
Alex Dima 已提交
745 746 747

		moveTo(thisCursor, 1, 2);
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
748
		assertCursor(thisCursor, new Selection(1, 1, 1, LINE1.length + 1));
A
Alex Dima 已提交
749 750 751

		moveTo(thisCursor, 1, 5);
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
752
		assertCursor(thisCursor, new Selection(1, 1, 1, LINE1.length + 1));
A
Alex Dima 已提交
753 754 755

		moveTo(thisCursor, 1, 19);
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
756
		assertCursor(thisCursor, new Selection(1, 1, 1, LINE1.length + 1));
A
Alex Dima 已提交
757 758 759

		moveTo(thisCursor, 1, 20);
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
760
		assertCursor(thisCursor, new Selection(1, 1, 1, LINE1.length + 1));
A
Alex Dima 已提交
761 762 763

		moveTo(thisCursor, 1, 21);
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
764
		assertCursor(thisCursor, new Selection(1, 1, 1, LINE1.length + 1));
A
Alex Dima 已提交
765
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
766
		assertCursor(thisCursor, new Selection(1, 1, 2, LINE2.length + 1));
A
Alex Dima 已提交
767
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
768
		assertCursor(thisCursor, new Selection(1, 1, 3, LINE3.length + 1));
A
Alex Dima 已提交
769
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
770
		assertCursor(thisCursor, new Selection(1, 1, 4, LINE4.length + 1));
A
Alex Dima 已提交
771
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
772
		assertCursor(thisCursor, new Selection(1, 1, 5, LINE5.length + 1));
A
Alex Dima 已提交
773
		cursorCommand(thisCursor, H.ExpandLineSelection);
A
Alex Dima 已提交
774
		assertCursor(thisCursor, new Selection(1, 1, 5, LINE5.length + 1));
A
Alex Dima 已提交
775 776
	});

E
Erich Gamma 已提交
777 778 779
	// --------- eventing

	test('no move doesn\'t trigger event', () => {
A
Alex Dima 已提交
780
		thisCursor.addListener2(EventType.CursorPositionChanged, (e) => {
E
Erich Gamma 已提交
781 782
			assert.ok(false, 'was not expecting event');
		});
A
Alex Dima 已提交
783
		thisCursor.addListener2(EventType.CursorSelectionChanged, (e) => {
E
Erich Gamma 已提交
784 785 786 787 788 789
			assert.ok(false, 'was not expecting event');
		});
		moveTo(thisCursor, 1, 1);
	});

	test('move eventing', () => {
A
Alex Dima 已提交
790
		let events = 0;
J
Johannes Rieken 已提交
791
		thisCursor.addListener2(EventType.CursorPositionChanged, (e: ICursorPositionChangedEvent) => {
E
Erich Gamma 已提交
792
			events++;
A
Alex Dima 已提交
793
			assert.deepEqual(e.position, new Position(1, 2));
E
Erich Gamma 已提交
794
		});
J
Johannes Rieken 已提交
795
		thisCursor.addListener2(EventType.CursorSelectionChanged, (e: ICursorSelectionChangedEvent) => {
E
Erich Gamma 已提交
796
			events++;
A
Alex Dima 已提交
797
			assert.deepEqual(e.selection, new Selection(1, 2, 1, 2));
E
Erich Gamma 已提交
798 799 800 801 802 803
		});
		moveTo(thisCursor, 1, 2);
		assert.equal(events, 2, 'receives 2 events');
	});

	test('move in selection mode eventing', () => {
A
Alex Dima 已提交
804
		let events = 0;
J
Johannes Rieken 已提交
805
		thisCursor.addListener2(EventType.CursorPositionChanged, (e: ICursorPositionChangedEvent) => {
E
Erich Gamma 已提交
806
			events++;
A
Alex Dima 已提交
807
			assert.deepEqual(e.position, new Position(1, 2));
E
Erich Gamma 已提交
808
		});
J
Johannes Rieken 已提交
809
		thisCursor.addListener2(EventType.CursorSelectionChanged, (e: ICursorSelectionChangedEvent) => {
E
Erich Gamma 已提交
810
			events++;
811
			assert.deepEqual(e.selection, new Selection(1, 1, 1, 2));
E
Erich Gamma 已提交
812 813 814 815 816 817 818 819 820
		});
		moveTo(thisCursor, 1, 2, true);
		assert.equal(events, 2, 'receives 2 events');
	});

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

	test('saveState & restoreState', () => {
		moveTo(thisCursor, 2, 1, true);
A
Alex Dima 已提交
821
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
E
Erich Gamma 已提交
822

A
Alex Dima 已提交
823
		let savedState = JSON.stringify(thisCursor.saveState());
E
Erich Gamma 已提交
824 825

		moveTo(thisCursor, 1, 1, false);
A
Alex Dima 已提交
826
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
827 828

		thisCursor.restoreState(JSON.parse(savedState));
A
Alex Dima 已提交
829
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
E
Erich Gamma 已提交
830 831 832 833 834 835 836
	});

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

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

A
Alex Dima 已提交
837
		thisModel.applyEdits([EditOperation.delete(new Range(2, 1, 2, 2))]);
A
Alex Dima 已提交
838
		assertCursor(thisCursor, new Selection(1, 1, 2, 15));
E
Erich Gamma 已提交
839
	});
A
Alex Dima 已提交
840 841

	test('column select 1', () => {
842
		let model = Model.createFromString([
A
Alex Dima 已提交
843 844 845 846 847
			'\tprivate compute(a:number): boolean {',
			'\t\tif (a + 3 === 0 || a + 5 === 0) {',
			'\t\t\treturn false;',
			'\t\t}',
			'\t}'
848
		].join('\n'));
849
		let cursor = new Cursor(1, new MockConfiguration(null), model, viewModelHelper(model), true);
A
Alex Dima 已提交
850 851

		moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
852
		assertCursor(cursor, new Position(1, 7));
A
Alex Dima 已提交
853 854 855 856 857 858 859 860

		cursorCommand(cursor, H.ColumnSelect, {
			position: new Position(4, 4),
			viewPosition: new Position(4, 4),
			mouseColumn: 15
		});

		let expectedSelections = [
A
Alex Dima 已提交
861 862 863
			new Selection(1, 7, 1, 12),
			new Selection(2, 4, 2, 9),
			new Selection(3, 3, 3, 6),
A
Alex Dima 已提交
864 865 866
			new Selection(4, 4, 4, 4),
		];

A
Alex Dima 已提交
867
		assertCursor(cursor, expectedSelections);
A
Alex Dima 已提交
868 869 870 871

		cursor.dispose();
		model.dispose();
	});
A
Alex Dima 已提交
872 873

	test('issue #4905 - column select is biased to the right', () => {
874
		let model = Model.createFromString([
A
Alex Dima 已提交
875 876 877 878 879 880 881
			'var gulp = require("gulp");',
			'var path = require("path");',
			'var rimraf = require("rimraf");',
			'var isarray = require("isarray");',
			'var merge = require("merge-stream");',
			'var concat = require("gulp-concat");',
			'var newer = require("gulp-newer");',
882
		].join('\n'));
883
		let cursor = new Cursor(1, new MockConfiguration(null), model, viewModelHelper(model), true);
A
Alex Dima 已提交
884 885

		moveTo(cursor, 1, 4, false);
A
Alex Dima 已提交
886
		assertCursor(cursor, new Position(1, 4));
A
Alex Dima 已提交
887 888 889 890 891 892 893

		cursorCommand(cursor, H.ColumnSelect, {
			position: new Position(4, 1),
			viewPosition: new Position(4, 1),
			mouseColumn: 1
		});

A
Alex Dima 已提交
894
		assertCursor(cursor, [
A
Alex Dima 已提交
895 896 897 898 899 900 901 902 903 904 905
			new Selection(1, 4, 1, 1),
			new Selection(2, 4, 2, 1),
			new Selection(3, 4, 3, 1),
			new Selection(4, 4, 4, 1),
		]);

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

	test('column select with keyboard', () => {
906
		let model = Model.createFromString([
A
Alex Dima 已提交
907 908 909 910 911 912 913
			'var gulp = require("gulp");',
			'var path = require("path");',
			'var rimraf = require("rimraf");',
			'var isarray = require("isarray");',
			'var merge = require("merge-stream");',
			'var concat = require("gulp-concat");',
			'var newer = require("gulp-newer");',
914
		].join('\n'));
915
		let cursor = new Cursor(1, new MockConfiguration(null), model, viewModelHelper(model), true);
A
Alex Dima 已提交
916 917

		moveTo(cursor, 1, 4, false);
A
Alex Dima 已提交
918
		assertCursor(cursor, new Position(1, 4));
A
Alex Dima 已提交
919 920

		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
921
		assertCursor(cursor, [
A
Alex Dima 已提交
922 923 924 925
			new Selection(1, 4, 1, 5)
		]);

		cursorCommand(cursor, H.CursorColumnSelectDown);
A
Alex Dima 已提交
926
		assertCursor(cursor, [
A
Alex Dima 已提交
927 928 929 930 931
			new Selection(1, 4, 1, 5),
			new Selection(2, 4, 2, 5)
		]);

		cursorCommand(cursor, H.CursorColumnSelectDown);
A
Alex Dima 已提交
932
		assertCursor(cursor, [
A
Alex Dima 已提交
933 934 935 936 937 938 939 940 941
			new Selection(1, 4, 1, 5),
			new Selection(2, 4, 2, 5),
			new Selection(3, 4, 3, 5),
		]);

		cursorCommand(cursor, H.CursorColumnSelectDown);
		cursorCommand(cursor, H.CursorColumnSelectDown);
		cursorCommand(cursor, H.CursorColumnSelectDown);
		cursorCommand(cursor, H.CursorColumnSelectDown);
A
Alex Dima 已提交
942
		assertCursor(cursor, [
A
Alex Dima 已提交
943 944 945 946 947 948 949 950 951 952
			new Selection(1, 4, 1, 5),
			new Selection(2, 4, 2, 5),
			new Selection(3, 4, 3, 5),
			new Selection(4, 4, 4, 5),
			new Selection(5, 4, 5, 5),
			new Selection(6, 4, 6, 5),
			new Selection(7, 4, 7, 5),
		]);

		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
953
		assertCursor(cursor, [
A
Alex Dima 已提交
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
			new Selection(1, 4, 1, 6),
			new Selection(2, 4, 2, 6),
			new Selection(3, 4, 3, 6),
			new Selection(4, 4, 4, 6),
			new Selection(5, 4, 5, 6),
			new Selection(6, 4, 6, 6),
			new Selection(7, 4, 7, 6),
		]);

		// 10 times
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
974
		assertCursor(cursor, [
A
Alex Dima 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
			new Selection(1, 4, 1, 16),
			new Selection(2, 4, 2, 16),
			new Selection(3, 4, 3, 16),
			new Selection(4, 4, 4, 16),
			new Selection(5, 4, 5, 16),
			new Selection(6, 4, 6, 16),
			new Selection(7, 4, 7, 16),
		]);

		// 10 times
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
995
		assertCursor(cursor, [
A
Alex Dima 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
			new Selection(1, 4, 1, 26),
			new Selection(2, 4, 2, 26),
			new Selection(3, 4, 3, 26),
			new Selection(4, 4, 4, 26),
			new Selection(5, 4, 5, 26),
			new Selection(6, 4, 6, 26),
			new Selection(7, 4, 7, 26),
		]);

		// 2 times => reaching the ending of lines 1 and 2
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
1008
		assertCursor(cursor, [
A
Alex Dima 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
			new Selection(1, 4, 1, 28),
			new Selection(2, 4, 2, 28),
			new Selection(3, 4, 3, 28),
			new Selection(4, 4, 4, 28),
			new Selection(5, 4, 5, 28),
			new Selection(6, 4, 6, 28),
			new Selection(7, 4, 7, 28),
		]);

		// 4 times => reaching the ending of line 3
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
1023
		assertCursor(cursor, [
A
Alex Dima 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
			new Selection(1, 4, 1, 28),
			new Selection(2, 4, 2, 28),
			new Selection(3, 4, 3, 32),
			new Selection(4, 4, 4, 32),
			new Selection(5, 4, 5, 32),
			new Selection(6, 4, 6, 32),
			new Selection(7, 4, 7, 32),
		]);

		// 2 times => reaching the ending of line 4
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
1036
		assertCursor(cursor, [
A
Alex Dima 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
			new Selection(1, 4, 1, 28),
			new Selection(2, 4, 2, 28),
			new Selection(3, 4, 3, 32),
			new Selection(4, 4, 4, 34),
			new Selection(5, 4, 5, 34),
			new Selection(6, 4, 6, 34),
			new Selection(7, 4, 7, 34),
		]);

		// 1 time => reaching the ending of line 7
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
1048
		assertCursor(cursor, [
A
Alex Dima 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
			new Selection(1, 4, 1, 28),
			new Selection(2, 4, 2, 28),
			new Selection(3, 4, 3, 32),
			new Selection(4, 4, 4, 34),
			new Selection(5, 4, 5, 35),
			new Selection(6, 4, 6, 35),
			new Selection(7, 4, 7, 35),
		]);

		// 3 times => reaching the ending of lines 5 & 6
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
1062
		assertCursor(cursor, [
A
Alex Dima 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
			new Selection(1, 4, 1, 28),
			new Selection(2, 4, 2, 28),
			new Selection(3, 4, 3, 32),
			new Selection(4, 4, 4, 34),
			new Selection(5, 4, 5, 37),
			new Selection(6, 4, 6, 37),
			new Selection(7, 4, 7, 35),
		]);

		// cannot go anywhere anymore
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
1074
		assertCursor(cursor, [
A
Alex Dima 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
			new Selection(1, 4, 1, 28),
			new Selection(2, 4, 2, 28),
			new Selection(3, 4, 3, 32),
			new Selection(4, 4, 4, 34),
			new Selection(5, 4, 5, 37),
			new Selection(6, 4, 6, 37),
			new Selection(7, 4, 7, 35),
		]);

		// cannot go anywhere anymore even if we insist
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
		cursorCommand(cursor, H.CursorColumnSelectRight);
A
Alex Dima 已提交
1089
		assertCursor(cursor, [
A
Alex Dima 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
			new Selection(1, 4, 1, 28),
			new Selection(2, 4, 2, 28),
			new Selection(3, 4, 3, 32),
			new Selection(4, 4, 4, 34),
			new Selection(5, 4, 5, 37),
			new Selection(6, 4, 6, 37),
			new Selection(7, 4, 7, 35),
		]);

		// can easily go back
		cursorCommand(cursor, H.CursorColumnSelectLeft);
A
Alex Dima 已提交
1101
		assertCursor(cursor, [
A
Alex Dima 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
			new Selection(1, 4, 1, 28),
			new Selection(2, 4, 2, 28),
			new Selection(3, 4, 3, 32),
			new Selection(4, 4, 4, 34),
			new Selection(5, 4, 5, 36),
			new Selection(6, 4, 6, 36),
			new Selection(7, 4, 7, 35),
		]);

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

1116
class SurroundingMode extends MockMode {
1117 1118
	constructor() {
		super();
1119
		LanguageConfigurationRegistry.register(this.getId(), {
1120
			autoClosingPairs: [{ open: '(', close: ')' }]
1121 1122 1123 1124
		});
	}
}

1125
class OnEnterMode extends MockMode {
A
Alex Dima 已提交
1126
	constructor(indentAction: IndentAction) {
1127
		super();
1128 1129 1130 1131 1132
		LanguageConfigurationRegistry.register(this.getId(), {
			onEnterRules: [{
				beforeText: /.*/,
				action: {
					indentAction: indentAction
1133
				}
1134 1135
			}]
		});
1136 1137 1138 1139
	}
}

suite('Editor Controller - Regression tests', () => {
E
Erich Gamma 已提交
1140
	test('Bug 9121: Auto indent + undo + redo is funky', () => {
A
Alex Dima 已提交
1141 1142 1143 1144
		usingCursor({
			text: [
				''
			],
1145 1146 1147 1148
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
1149 1150
				tabSize: 4,
				trimAutoWhitespace: false
1151
			}
A
Alex Dima 已提交
1152
		}, (model, cursor) => {
1153
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
1154
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n', 'assert1');
E
Erich Gamma 已提交
1155

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

J
Johannes Rieken 已提交
1159
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
1160
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\n\t', 'assert3');
E
Erich Gamma 已提交
1161

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

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

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

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

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

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

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

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

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

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

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

A
Alex Dima 已提交
1195 1196 1197
			cursorCommand(cursor, H.Redo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), 'x', 'assert15');
		});
E
Erich Gamma 已提交
1198
	});
1199

A
Alex Dima 已提交
1200
	test('issue #183: jump to matching bracket position', () => {
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
		class BracketMode extends MockMode {
			constructor() {
				super();
				LanguageConfigurationRegistry.register(this.getId(), {
					brackets: [
						['{', '}'],
						['[', ']'],
						['(', ')'],
					]
				});
			}
		}

A
Alex Dima 已提交
1214 1215 1216 1217
		usingCursor({
			text: [
				'var x = (3 + (5-7));'
			],
1218
			modeId: new BracketMode().getId()
A
Alex Dima 已提交
1219 1220
		}, (model, cursor) => {
			// ensure is tokenized
A
Alex Dima 已提交
1221
			model.getLineTokens(1, false);
A
Alex Dima 已提交
1222 1223 1224

			moveTo(cursor, 1, 20);

1225
			cursorCommand(cursor, H.JumpToBracket, null, 'keyboard');
A
Alex Dima 已提交
1226
			assertCursor(cursor, new Position(1, 10));
A
Alex Dima 已提交
1227

1228
			cursorCommand(cursor, H.JumpToBracket, null, 'keyboard');
A
Alex Dima 已提交
1229
			assertCursor(cursor, new Position(1, 20));
A
Alex Dima 已提交
1230

1231
			cursorCommand(cursor, H.JumpToBracket, null, 'keyboard');
A
Alex Dima 已提交
1232
			assertCursor(cursor, new Position(1, 10));
A
Alex Dima 已提交
1233
		});
1234 1235
	});

E
Erich Gamma 已提交
1236
	test('bug #16543: Tab should indent to correct indentation spot immediately', () => {
A
Alex Dima 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245
		usingCursor({
			text: [
				'function baz() {',
				'\tfunction hello() { // something here',
				'\t',
				'',
				'\t}',
				'}'
			],
1246 1247 1248 1249
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
1250 1251
				tabSize: 4,
				trimAutoWhitespace: true
1252
			},
1253
			modeId: new OnEnterMode(IndentAction.Indent).getId(),
A
Alex Dima 已提交
1254 1255
		}, (model, cursor) => {
			moveTo(cursor, 4, 1, false);
A
Alex Dima 已提交
1256
			assertCursor(cursor, new Selection(4, 1, 4, 1));
E
Erich Gamma 已提交
1257

1258
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1259 1260
			assert.equal(model.getLineContent(4), '\t\t');
		});
E
Erich Gamma 已提交
1261 1262
	});

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	test('bug #2938 (1): When pressing Tab on white-space only lines, indent straight to the right spot (similar to empty lines)', () => {
		usingCursor({
			text: [
				'\tfunction baz() {',
				'\t\tfunction hello() { // something here',
				'\t\t',
				'\t',
				'\t\t}',
				'\t}'
			],
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
				tabSize: 4,
				trimAutoWhitespace: true
			},
1280
			modeId: new OnEnterMode(IndentAction.Indent).getId(),
1281 1282
		}, (model, cursor) => {
			moveTo(cursor, 4, 2, false);
A
Alex Dima 已提交
1283
			assertCursor(cursor, new Selection(4, 2, 4, 2));
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

			cursorCommand(cursor, H.Tab, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t\t');
		});
	});


	test('bug #2938 (2): When pressing Tab on white-space only lines, indent straight to the right spot (similar to empty lines)', () => {
		usingCursor({
			text: [
				'\tfunction baz() {',
				'\t\tfunction hello() { // something here',
				'\t\t',
				'    ',
				'\t\t}',
				'\t}'
			],
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
				tabSize: 4,
				trimAutoWhitespace: true
			},
1308
			modeId: new OnEnterMode(IndentAction.Indent).getId(),
1309 1310
		}, (model, cursor) => {
			moveTo(cursor, 4, 1, false);
A
Alex Dima 已提交
1311
			assertCursor(cursor, new Selection(4, 1, 4, 1));
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

			cursorCommand(cursor, H.Tab, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t\t');
		});
	});

	test('bug #2938 (3): When pressing Tab on white-space only lines, indent straight to the right spot (similar to empty lines)', () => {
		usingCursor({
			text: [
				'\tfunction baz() {',
				'\t\tfunction hello() { // something here',
				'\t\t',
				'\t\t\t',
				'\t\t}',
				'\t}'
			],
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
				tabSize: 4,
				trimAutoWhitespace: true
			},
1335
			modeId: new OnEnterMode(IndentAction.Indent).getId(),
1336 1337
		}, (model, cursor) => {
			moveTo(cursor, 4, 3, false);
A
Alex Dima 已提交
1338
			assertCursor(cursor, new Selection(4, 3, 4, 3));
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

			cursorCommand(cursor, H.Tab, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t\t\t');
		});
	});

	test('bug #2938 (4): When pressing Tab on white-space only lines, indent straight to the right spot (similar to empty lines)', () => {
		usingCursor({
			text: [
				'\tfunction baz() {',
				'\t\tfunction hello() { // something here',
				'\t\t',
				'\t\t\t\t',
				'\t\t}',
				'\t}'
			],
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
				tabSize: 4,
				trimAutoWhitespace: true
			},
1362
			modeId: new OnEnterMode(IndentAction.Indent).getId(),
1363 1364
		}, (model, cursor) => {
			moveTo(cursor, 4, 4, false);
A
Alex Dima 已提交
1365
			assertCursor(cursor, new Selection(4, 4, 4, 4));
1366 1367 1368 1369 1370 1371

			cursorCommand(cursor, H.Tab, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t\t\t\t');
		});
	});

E
Erich Gamma 已提交
1372
	test('Bug 18276:[editor] Indentation broken when selection is empty', () => {
A
Alex Dima 已提交
1373 1374 1375 1376
		usingCursor({
			text: [
				'function baz() {'
			],
1377 1378 1379 1380
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
1381 1382
				tabSize: 4,
				trimAutoWhitespace: true
1383
			},
A
Alex Dima 已提交
1384 1385
		}, (model, cursor) => {
			moveTo(cursor, 1, 2, false);
A
Alex Dima 已提交
1386
			assertCursor(cursor, new Selection(1, 2, 1, 2));
A
Alex Dima 已提交
1387

1388
			cursorCommand(cursor, H.Indent, null, 'keyboard');
A
Alex Dima 已提交
1389 1390
			assert.equal(model.getLineContent(1), '\tfunction baz() {');

A
Alex Dima 已提交
1391
			assertCursor(cursor, new Selection(1, 3, 1, 3));
1392
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1393 1394
			assert.equal(model.getLineContent(1), '\tf\tunction baz() {');
		});
E
Erich Gamma 已提交
1395 1396 1397
	});

	test('bug #16815:Shift+Tab doesn\'t go back to tabstop', () => {
A
Alex Dima 已提交
1398 1399 1400 1401
		usingCursor({
			text: [
				'     function baz() {'
			],
1402
			modeId: new OnEnterMode(IndentAction.IndentOutdent).getId(),
1403
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1404 1405
		}, (model, cursor) => {
			moveTo(cursor, 1, 6, false);
A
Alex Dima 已提交
1406
			assertCursor(cursor, new Selection(1, 6, 1, 6));
A
Alex Dima 已提交
1407

1408
			cursorCommand(cursor, H.Outdent, null, 'keyboard');
A
Alex Dima 已提交
1409
			assert.equal(model.getLineContent(1), '    function baz() {');
A
Alex Dima 已提交
1410
			assertCursor(cursor, new Selection(1, 5, 1, 5));
A
Alex Dima 已提交
1411
		});
E
Erich Gamma 已提交
1412 1413 1414
	});

	test('Bug #18293:[regression][editor] Can\'t outdent whitespace line', () => {
A
Alex Dima 已提交
1415 1416 1417 1418
		usingCursor({
			text: [
				'      '
			],
1419
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1420 1421
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
1422
			assertCursor(cursor, new Selection(1, 7, 1, 7));
A
Alex Dima 已提交
1423

1424
			cursorCommand(cursor, H.Outdent, null, 'keyboard');
A
Alex Dima 已提交
1425
			assert.equal(model.getLineContent(1), '    ');
A
Alex Dima 已提交
1426
			assertCursor(cursor, new Selection(1, 5, 1, 5));
A
Alex Dima 已提交
1427
		});
E
Erich Gamma 已提交
1428 1429 1430
	});

	test('Bug #16657: [editor] Tab on empty line of zero indentation moves cursor to position (1,1)', () => {
A
Alex Dima 已提交
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
		usingCursor({
			text: [
				'function baz() {',
				'\tfunction hello() { // something here',
				'\t',
				'',
				'\t}',
				'}',
				''
			],
1441 1442 1443 1444
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
1445 1446
				tabSize: 4,
				trimAutoWhitespace: true
1447
			},
A
Alex Dima 已提交
1448 1449
		}, (model, cursor) => {
			moveTo(cursor, 7, 1, false);
A
Alex Dima 已提交
1450
			assertCursor(cursor, new Selection(7, 1, 7, 1));
A
Alex Dima 已提交
1451

1452
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1453
			assert.equal(model.getLineContent(7), '\t');
A
Alex Dima 已提交
1454
			assertCursor(cursor, new Selection(7, 2, 7, 2));
A
Alex Dima 已提交
1455
		});
E
Erich Gamma 已提交
1456 1457 1458 1459
	});

	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 已提交
1460
		let text = [
E
Erich Gamma 已提交
1461 1462 1463
			'asdasd',
			'qwerty'
		];
1464
		let model = Model.createFromString(text.join('\n'));
1465
		let cursor = new Cursor(1, new MockConfiguration(null), model, viewModelHelper(model), true);
E
Erich Gamma 已提交
1466 1467

		moveTo(cursor, 2, 1, false);
A
Alex Dima 已提交
1468
		assertCursor(cursor, new Selection(2, 1, 2, 1));
E
Erich Gamma 已提交
1469

1470
		cursorCommand(cursor, H.Cut, null, 'keyboard');
E
Erich Gamma 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
		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',
			''
		];
1482
		model = Model.createFromString(text.join('\n'));
1483
		cursor = new Cursor(1, new MockConfiguration(null), model, viewModelHelper(model), true);
E
Erich Gamma 已提交
1484 1485

		moveTo(cursor, 2, 1, false);
A
Alex Dima 已提交
1486
		assertCursor(cursor, new Selection(2, 1, 2, 1));
E
Erich Gamma 已提交
1487

1488
		cursorCommand(cursor, H.Cut, null, 'keyboard');
E
Erich Gamma 已提交
1489 1490 1491
		assert.equal(model.getLineCount(), 1);
		assert.equal(model.getLineContent(1), 'asdasd');

1492
		cursorCommand(cursor, H.Cut, null, 'keyboard');
E
Erich Gamma 已提交
1493 1494 1495 1496 1497 1498 1499
		assert.equal(model.getLineCount(), 1);
		assert.equal(model.getLineContent(1), '');

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

1500 1501 1502 1503 1504
	test('Bug #11476: Double bracket surrounding + undo is broken', () => {
		usingCursor({
			text: [
				'hello'
			],
1505
			modeId: new SurroundingMode().getId(),
1506
			modelOpts: { tabSize: 4, insertSpaces: true, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
1507 1508 1509
		}, (model, cursor) => {
			moveTo(cursor, 1, 3, false);
			moveTo(cursor, 1, 5, true);
A
Alex Dima 已提交
1510
			assertCursor(cursor, new Selection(1, 3, 1, 5));
1511

1512
			cursorCommand(cursor, H.Type, { text: '(' }, 'keyboard');
A
Alex Dima 已提交
1513
			assertCursor(cursor, new Selection(1, 4, 1, 6));
1514

1515
			cursorCommand(cursor, H.Type, { text: '(' }, 'keyboard');
A
Alex Dima 已提交
1516
			assertCursor(cursor, new Selection(1, 5, 1, 7));
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
		});
	});

	test('issue #1140: Backspace stops prematurely', () => {
		usingCursor({
			text: [
				'function baz() {',
				'  return 1;',
				'};'
			],
1527
			modeId: new SurroundingMode().getId(),
1528
			modelOpts: { tabSize: 4, insertSpaces: true, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
1529 1530 1531
		}, (model, cursor) => {
			moveTo(cursor, 3, 2, false);
			moveTo(cursor, 1, 14, true);
A
Alex Dima 已提交
1532
			assertCursor(cursor, new Selection(3, 2, 1, 14));
1533 1534

			cursorCommand(cursor, H.DeleteLeft);
A
Alex Dima 已提交
1535
			assertCursor(cursor, new Selection(1, 14, 1, 14));
1536 1537 1538 1539
			assert.equal(model.getLineCount(), 1);
			assert.equal(model.getLineContent(1), 'function baz(;');
		});
	});
A
Alex Dima 已提交
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550

	test('issue #1336: Insert cursor below on last line adds a cursor to the end of the current line', () => {
		usingCursor({
			text: [
				'abc'
			],
		}, (model, cursor) => {
			cursorCommand(cursor, H.AddCursorDown);
			assert.equal(cursor.getSelections().length, 1);
		});
	});
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567

	test('issue #2205: Multi-cursor pastes in reverse order', () => {
		usingCursor({
			text: [
				'abc',
				'def'
			],
		}, (model, cursor) => {
			moveTo(cursor, 2, 1, false);
			cursorCommand(cursor, H.AddCursorUp);
			assert.equal(cursor.getSelections().length, 2);

			cursorCommand(cursor, H.Paste, { text: '1\n2' });
			assert.equal(model.getLineContent(1), '1abc');
			assert.equal(model.getLineContent(2), '2def');
		});
	});
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585

	test('issue #10212: Pasting entire line does not replace selection', () => {
		usingCursor({
			text: [
				'line1',
				'line2'
			],
		}, (model, cursor) => {
			moveTo(cursor, 2, 1, false);
			moveTo(cursor, 2, 6, true);

			cursorCommand(cursor, H.Paste, { text: 'line1\n', pasteOnNewLine: true });

			assert.equal(model.getLineContent(1), 'line1');
			assert.equal(model.getLineContent(2), 'line1');
			assert.equal(model.getLineContent(3), '');
		});
	});
1586

A
Alex Dima 已提交
1587 1588 1589 1590 1591 1592 1593
	test('issue #3071: Investigate why undo stack gets corrupted', () => {
		usingCursor({
			text: [
				'some lines',
				'and more lines',
				'just some text',
			],
1594
			modeId: null,
1595
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1596 1597 1598 1599 1600
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);
			moveTo(cursor, 3, 4, true);

			let isFirst = true;
A
Alex Dima 已提交
1601
			model.onDidChangeContent(() => {
A
Alex Dima 已提交
1602 1603
				if (isFirst) {
					isFirst = false;
1604
					cursorCommand(cursor, H.Type, { text: '\t' }, 'keyboard');
A
Alex Dima 已提交
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
				}
			});

			cursorCommand(cursor, H.Tab);
			assert.equal(model.getValue(), [
				'\t just some text'
			].join('\n'), '001');

			cursorCommand(cursor, H.Undo);
			assert.equal(model.getValue(), [
				'some lines',
				'and more lines',
				'just some text',
			].join('\n'), '002');

			cursorCommand(cursor, H.Undo);
			assert.equal(model.getValue(), [
				'some lines',
				'and more lines',
				'just some text',
			].join('\n'), '003');
		});
	});

1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
	test('issue #12950: Cannot Double Click To Insert Emoji Using OSX Emoji Panel', () => {
		usingCursor({
			text: [
				'some lines',
				'and more lines',
				'just some text',
			],
			modeId: null,
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 3, 1, false);

			cursorCommand(cursor, H.Type, { text: '😍' }, 'keyboard');

			assert.equal(model.getValue(), [
				'some lines',
				'and more lines',
				'😍just some text',
			].join('\n'));
		});
	});

A
Alex Dima 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659
	test('issue #3463: pressing tab adds spaces, but not as many as for a tab', () => {
		usingCursor({
			text: [
				'function a() {',
				'\tvar a = {',
				'\t\tx: 3',
				'\t};',
				'}',
			],
1660
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1661 1662 1663 1664 1665 1666 1667
		}, (model, cursor) => {
			moveTo(cursor, 3, 2, false);
			cursorCommand(cursor, H.Tab);
			assert.equal(model.getLineContent(3), '\t    \tx: 3');
		});
	});

1668 1669 1670
	test('issue #4312: trying to type a tab character over a sequence of spaces results in unexpected behaviour', () => {
		usingCursor({
			text: [
1671
				'var foo = 123;       // this is a comment',
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
				'var bar = 4;       // another comment'
			],
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 1, 15, false);
			moveTo(cursor, 1, 22, true);
			cursorCommand(cursor, H.Tab);
			assert.equal(model.getLineContent(1), 'var foo = 123;\t// this is a comment');
		});
	});

1683 1684 1685 1686 1687 1688 1689
	test('issue #832: deleteWordLeft', () => {
		usingCursor({
			text: [
				'   /* Just some text a+= 3 +5 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 37, false);
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 +5 */', '001');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 +5 ', '002');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 +', '003');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 ', '004');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= ', '005');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a', '006');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text ', '007');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some ', '008');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just ', '009');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   /* ', '010');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '   ', '011');
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), '', '012');
1702 1703 1704
		});
	});

1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
	test('deleteWordStartLeft', () => {
		usingCursor({
			text: [
				'   /* Just some text a+= 3 +5 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 37, false);

			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 +5 ', '001');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 +', '002');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 ', '003');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= ', '004');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a', '005');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text ', '006');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some ', '007');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just ', '008');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   /* ', '009');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '   ', '010');
			deleteWordStartLeft(cursor); assert.equal(model.getLineContent(1), '', '011');
		});
	});

	test('deleteWordEndLeft', () => {
		usingCursor({
			text: [
				'   /* Just some text a+= 3 +5 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 37, false);
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 +5 */', '001');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 +5', '002');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3 +', '003');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+= 3', '004');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a+=', '005');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text a', '006');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some text', '007');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just some', '008');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /* Just', '009');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '   /*', '010');
			deleteWordEndLeft(cursor); assert.equal(model.getLineContent(1), '', '011');
		});
	});

1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
	test('issue #832: deleteWordRight', () => {
		usingCursor({
			text: [
				'   /* Just some text a+= 3 +5-3 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), '/* Just some text a+= 3 +5-3 */  ', '001');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), ' Just some text a+= 3 +5-3 */  ', '002');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), ' some text a+= 3 +5-3 */  ', '003');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), ' text a+= 3 +5-3 */  ', '004');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), ' a+= 3 +5-3 */  ', '005');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), '+= 3 +5-3 */  ', '006');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), ' 3 +5-3 */  ', '007');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), ' +5-3 */  ', '008');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), '5-3 */  ', '009');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), '-3 */  ', '010');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), '3 */  ', '011');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), ' */  ', '012');
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), '  ', '013');
		});
	});
1770

1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
	test('issue #3882: deleteWordRight', () => {
		usingCursor({
			text: [
				'public void Add( int x,',
				'                 int y )'
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 24, false);
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), 'public void Add( int x,int y )', '001');
		});
	});

	test('issue #3882: deleteWordStartRight', () => {
		usingCursor({
			text: [
				'public void Add( int x,',
				'                 int y )'
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 24, false);
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), 'public void Add( int x,int y )', '001');
		});
	});

	test('issue #3882: deleteWordEndRight', () => {
		usingCursor({
			text: [
				'public void Add( int x,',
				'                 int y )'
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 24, false);
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), 'public void Add( int x,int y )', '001');
		});
	});

1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
	test('deleteWordStartRight', () => {
		usingCursor({
			text: [
				'   /* Just some text a+= 3 +5-3 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);

			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '/* Just some text a+= 3 +5-3 */  ', '001');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), 'Just some text a+= 3 +5-3 */  ', '002');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), 'some text a+= 3 +5-3 */  ', '003');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), 'text a+= 3 +5-3 */  ', '004');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), 'a+= 3 +5-3 */  ', '005');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '+= 3 +5-3 */  ', '006');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '3 +5-3 */  ', '007');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '+5-3 */  ', '008');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '5-3 */  ', '009');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '-3 */  ', '010');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '3 */  ', '011');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '*/  ', '012');
			deleteWordStartRight(cursor); assert.equal(model.getLineContent(1), '', '013');
		});
	});

	test('deleteWordEndRight', () => {
		usingCursor({
			text: [
				'   /* Just some text a+= 3 +5-3 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), ' Just some text a+= 3 +5-3 */  ', '001');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), ' some text a+= 3 +5-3 */  ', '002');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), ' text a+= 3 +5-3 */  ', '003');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), ' a+= 3 +5-3 */  ', '004');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), '+= 3 +5-3 */  ', '005');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), ' 3 +5-3 */  ', '006');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), ' +5-3 */  ', '007');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), '5-3 */  ', '008');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), '-3 */  ', '009');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), '3 */  ', '010');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), ' */  ', '011');
			deleteWordEndRight(cursor); assert.equal(model.getLineContent(1), '  ', '012');
		});
	});

1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
	test('issue #832: moveWordLeft', () => {
		usingCursor({
			text: [
				'   /* Just some   more   text a+= 3 +5-3 + 7 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 50, false);

			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 '.length + 1, '001');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + '.length + 1, '002');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 '.length + 1, '003');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-'.length + 1, '004');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5'.length + 1, '005');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +'.length + 1, '006');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 '.length + 1, '007');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= '.length + 1, '008');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a'.length + 1, '009');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text '.length + 1, '010');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   '.length + 1, '011');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   '.length + 1, '012');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just '.length + 1, '013');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   /* '.length + 1, '014');
			moveWordLeft(cursor); assert.equal(cursor.getPosition().column, '   '.length + 1, '015');
		});
	});
1878

1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
	test('moveWordStartLeft', () => {
		usingCursor({
			text: [
				'   /* Just some   more   text a+= 3 +5-3 + 7 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 50, false);

			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 '.length + 1, '001');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + '.length + 1, '002');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 '.length + 1, '003');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-'.length + 1, '004');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5'.length + 1, '005');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +'.length + 1, '006');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 '.length + 1, '007');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= '.length + 1, '008');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a'.length + 1, '009');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text '.length + 1, '010');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   '.length + 1, '011');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   '.length + 1, '012');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just '.length + 1, '013');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   /* '.length + 1, '014');
			moveWordStartLeft(cursor); assert.equal(cursor.getPosition().column, '   '.length + 1, '015');
		});
	});

	test('moveWordEndLeft', () => {
		usingCursor({
			text: [
				'   /* Just some   more   text a+= 3 +5-3 + 7 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 50, false);

			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 */'.length + 1, '001');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7'.length + 1, '002');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 +'.length + 1, '003');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3'.length + 1, '004');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-'.length + 1, '005');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5'.length + 1, '006');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +'.length + 1, '007');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3'.length + 1, '008');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+='.length + 1, '009');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a'.length + 1, '010');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text'.length + 1, '011');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more'.length + 1, '012');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just some'.length + 1, '013');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /* Just'.length + 1, '014');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, '   /*'.length + 1, '015');
			moveWordEndLeft(cursor); assert.equal(cursor.getPosition().column, ''.length + 1, '016');
		});
	});

	test('issue #832: moveWordRight', () => {
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
		usingCursor({
			text: [
				'   /* Just some   more   text a+= 3 +5-3 + 7 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);

			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /*'.length + 1, '001');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just'.length + 1, '003');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some'.length + 1, '004');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more'.length + 1, '005');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text'.length + 1, '006');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a'.length + 1, '007');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+='.length + 1, '008');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3'.length + 1, '009');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +'.length + 1, '010');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5'.length + 1, '011');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-'.length + 1, '012');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3'.length + 1, '013');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 +'.length + 1, '014');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7'.length + 1, '015');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 */'.length + 1, '016');
			moveWordRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 */  '.length + 1, '016');

		});
	});
1959

1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
	test('moveWordEndRight', () => {
		usingCursor({
			text: [
				'   /* Just some   more   text a+= 3 +5-3 + 7 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);

			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /*'.length + 1, '001');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just'.length + 1, '003');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some'.length + 1, '004');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more'.length + 1, '005');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text'.length + 1, '006');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a'.length + 1, '007');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+='.length + 1, '008');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3'.length + 1, '009');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +'.length + 1, '010');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5'.length + 1, '011');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-'.length + 1, '012');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3'.length + 1, '013');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 +'.length + 1, '014');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7'.length + 1, '015');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 */'.length + 1, '016');
			moveWordEndRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 */  '.length + 1, '016');

		});
	});

	test('moveWordStartRight', () => {
		usingCursor({
			text: [
				'   /* Just some   more   text a+= 3 +5-3 + 7 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);

			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   '.length + 1, '001');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* '.length + 1, '002');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just '.length + 1, '003');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   '.length + 1, '004');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   '.length + 1, '005');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text '.length + 1, '006');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a'.length + 1, '007');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= '.length + 1, '008');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 '.length + 1, '009');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +'.length + 1, '010');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5'.length + 1, '011');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-'.length + 1, '012');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 '.length + 1, '013');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + '.length + 1, '014');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 '.length + 1, '015');
			moveWordStartRight(cursor); assert.equal(cursor.getPosition().column, '   /* Just some   more   text a+= 3 +5-3 + 7 */  '.length + 1, '016');
		});
	});

2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
	test('issue #832: word right', () => {

		usingCursor({
			text: [
				'   /* Just some   more   text a+= 3 +5-3 + 7 */  '
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);

			function assertWordRight(col, expectedCol) {
				cursorCommand(cursor, col === 1 ? H.WordSelect : H.WordSelectDrag, {
					position: {
						lineNumber: 1,
						column: col
					},
					preference: 'right'
				});

				assert.equal(cursor.getSelection().startColumn, 1, 'TEST FOR ' + col);
				assert.equal(cursor.getSelection().endColumn, expectedCol, 'TEST FOR ' + col);
			}

J
Johannes Rieken 已提交
2037 2038 2039 2040 2041 2042 2043 2044 2045
			assertWordRight(1, '   '.length + 1);
			assertWordRight(2, '   '.length + 1);
			assertWordRight(3, '   '.length + 1);
			assertWordRight(4, '   '.length + 1);
			assertWordRight(5, '   /'.length + 1);
			assertWordRight(6, '   /*'.length + 1);
			assertWordRight(7, '   /* '.length + 1);
			assertWordRight(8, '   /* Just'.length + 1);
			assertWordRight(9, '   /* Just'.length + 1);
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
			assertWordRight(10, '   /* Just'.length + 1);
			assertWordRight(11, '   /* Just'.length + 1);
			assertWordRight(12, '   /* Just '.length + 1);
			assertWordRight(13, '   /* Just some'.length + 1);
			assertWordRight(14, '   /* Just some'.length + 1);
			assertWordRight(15, '   /* Just some'.length + 1);
			assertWordRight(16, '   /* Just some'.length + 1);
			assertWordRight(17, '   /* Just some '.length + 1);
			assertWordRight(18, '   /* Just some  '.length + 1);
			assertWordRight(19, '   /* Just some   '.length + 1);
			assertWordRight(20, '   /* Just some   more'.length + 1);
			assertWordRight(21, '   /* Just some   more'.length + 1);
			assertWordRight(22, '   /* Just some   more'.length + 1);
			assertWordRight(23, '   /* Just some   more'.length + 1);
			assertWordRight(24, '   /* Just some   more '.length + 1);
			assertWordRight(25, '   /* Just some   more  '.length + 1);
			assertWordRight(26, '   /* Just some   more   '.length + 1);
			assertWordRight(27, '   /* Just some   more   text'.length + 1);
			assertWordRight(28, '   /* Just some   more   text'.length + 1);
			assertWordRight(29, '   /* Just some   more   text'.length + 1);
			assertWordRight(30, '   /* Just some   more   text'.length + 1);
			assertWordRight(31, '   /* Just some   more   text '.length + 1);
			assertWordRight(32, '   /* Just some   more   text a'.length + 1);
			assertWordRight(33, '   /* Just some   more   text a+'.length + 1);
			assertWordRight(34, '   /* Just some   more   text a+='.length + 1);
			assertWordRight(35, '   /* Just some   more   text a+= '.length + 1);
			assertWordRight(36, '   /* Just some   more   text a+= 3'.length + 1);
			assertWordRight(37, '   /* Just some   more   text a+= 3 '.length + 1);
			assertWordRight(38, '   /* Just some   more   text a+= 3 +'.length + 1);
			assertWordRight(39, '   /* Just some   more   text a+= 3 +5'.length + 1);
			assertWordRight(40, '   /* Just some   more   text a+= 3 +5-'.length + 1);
			assertWordRight(41, '   /* Just some   more   text a+= 3 +5-3'.length + 1);
			assertWordRight(42, '   /* Just some   more   text a+= 3 +5-3 '.length + 1);
			assertWordRight(43, '   /* Just some   more   text a+= 3 +5-3 +'.length + 1);
			assertWordRight(44, '   /* Just some   more   text a+= 3 +5-3 + '.length + 1);
			assertWordRight(45, '   /* Just some   more   text a+= 3 +5-3 + 7'.length + 1);
			assertWordRight(46, '   /* Just some   more   text a+= 3 +5-3 + 7 '.length + 1);
			assertWordRight(47, '   /* Just some   more   text a+= 3 +5-3 + 7 *'.length + 1);
			assertWordRight(48, '   /* Just some   more   text a+= 3 +5-3 + 7 */'.length + 1);
			assertWordRight(49, '   /* Just some   more   text a+= 3 +5-3 + 7 */ '.length + 1);
			assertWordRight(50, '   /* Just some   more   text a+= 3 +5-3 + 7 */  '.length + 1);
		});
	});
2089 2090 2091 2092 2093 2094 2095 2096 2097

	test('issue #3882 (1): Ctrl+Delete removing entire line when used at the end of line', () => {
		usingCursor({
			text: [
				'A line with text.',
				'   And another one'
			],
		}, (model, cursor) => {
			moveTo(cursor, 1, 18, false);
2098
			deleteWordRight(cursor); assert.equal(model.getLineContent(1), 'A line with text.And another one', '001');
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
		});
	});

	test('issue #3882 (2): Ctrl+Delete removing entire line when used at the end of line', () => {
		usingCursor({
			text: [
				'A line with text.',
				'   And another one'
			],
		}, (model, cursor) => {
			moveTo(cursor, 2, 1, false);
			deleteWordLeft(cursor); assert.equal(model.getLineContent(1), 'A line with text.   And another one', '001');
		});
	});
A
Alex Dima 已提交
2113

2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
	test('issue Microsoft/monaco-editor#108 part 1/2: Auto indentation on Enter with selection is half broken', () => {
		usingCursor({
			text: [
				'function baz() {',
				'\tvar x = 1;',
				'\t\t\t\t\t\t\treturn x;',
				'}'
			],
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
				tabSize: 4,
				trimAutoWhitespace: true
			},
2129
			modeId: new OnEnterMode(IndentAction.None).getId(),
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
		}, (model, cursor) => {
			moveTo(cursor, 3, 8, false);
			moveTo(cursor, 2, 12, true);
			assertCursor(cursor, new Selection(3, 8, 2, 12));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assert.equal(model.getLineContent(3), '\treturn x;');
			assertCursor(cursor, new Position(3, 2));
		});
	});

	test('issue Microsoft/monaco-editor#108 part 2/2: Auto indentation on Enter with selection is half broken', () => {
		usingCursor({
			text: [
				'function baz() {',
				'\tvar x = 1;',
				'\t\t\t\t\t\t\treturn x;',
				'}'
			],
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
				tabSize: 4,
				trimAutoWhitespace: true
			},
2156
			modeId: new OnEnterMode(IndentAction.None).getId(),
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
		}, (model, cursor) => {
			moveTo(cursor, 2, 12, false);
			moveTo(cursor, 3, 8, true);
			assertCursor(cursor, new Selection(2, 12, 3, 8));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assert.equal(model.getLineContent(3), '\treturn x;');
			assertCursor(cursor, new Position(3, 2));
		});
	});
2167 2168 2169 2170 2171 2172 2173 2174 2175

	test('issue #9675: Undo/Redo adds a stop in between CHN Characters', () => {
		usingCursor({
			text: [
			]
		}, (model, cursor) => {
			assertCursor(cursor, new Position(1, 1));

			// Typing sennsei in Japanese - Hiragana
J
Johannes Rieken 已提交
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
			cursorCommand(cursor, H.Type, { text: '' }, 'keyboard');
			cursorCommand(cursor, H.ReplacePreviousChar, { text: '', replaceCharCnt: 1 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せn', replaceCharCnt: 1 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せん', replaceCharCnt: 2 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せんs', replaceCharCnt: 2 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せんせ', replaceCharCnt: 3 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せんせ', replaceCharCnt: 3 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せんせい', replaceCharCnt: 3 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せんせい', replaceCharCnt: 4 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せんせい', replaceCharCnt: 4 });
			cursorCommand(cursor, H.ReplacePreviousChar, { text: 'せんせい', replaceCharCnt: 4 });
2187 2188 2189 2190 2191 2192 2193 2194 2195

			assert.equal(model.getLineContent(1), 'せんせい');
			assertCursor(cursor, new Position(1, 5));

			cursorCommand(cursor, H.Undo);
			assert.equal(model.getLineContent(1), '');
			assertCursor(cursor, new Position(1, 1));
		});
	});
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
});

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'
			],
2209
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
2210
		}, (model, cursor) => {
2211 2212
			cursorCommand(cursor, H.MoveTo, { position: new Position(1, 21) }, 'keyboard');
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2213 2214 2215 2216 2217
			assert.equal(model.getLineContent(1), '    \tMy First Line\t ');
			assert.equal(model.getLineContent(2), '        ');
		});
	});

E
Erich Gamma 已提交
2218
	test('Cursor honors insertSpaces configuration on tab', () => {
A
Alex Dima 已提交
2219 2220 2221 2222 2223 2224 2225 2226
		usingCursor({
			text: [
				'    \tMy First Line\t ',
				'My Second Line123',
				'    Third Line',
				'',
				'1'
			],
2227
			modelOpts: { insertSpaces: true, tabSize: 13, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
2228 2229
		}, (model, cursor) => {
			// Tab on column 1
2230 2231
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 1) }, 'keyboard');
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
2232
			assert.equal(model.getLineContent(2), '             My Second Line123');
2233
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
2234 2235 2236

			// Tab on column 2
			assert.equal(model.getLineContent(2), 'My Second Line123');
2237 2238
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 2) }, 'keyboard');
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
2239
			assert.equal(model.getLineContent(2), 'M            y Second Line123');
2240
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
2241 2242 2243

			// Tab on column 3
			assert.equal(model.getLineContent(2), 'My Second Line123');
2244 2245
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 3) }, 'keyboard');
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
2246
			assert.equal(model.getLineContent(2), 'My            Second Line123');
2247
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
2248 2249 2250

			// Tab on column 4
			assert.equal(model.getLineContent(2), 'My Second Line123');
2251 2252
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 4) }, 'keyboard');
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
2253
			assert.equal(model.getLineContent(2), 'My           Second Line123');
2254
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
2255 2256 2257

			// Tab on column 5
			assert.equal(model.getLineContent(2), 'My Second Line123');
2258 2259
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 5) }, 'keyboard');
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
2260
			assert.equal(model.getLineContent(2), 'My S         econd Line123');
2261
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
2262 2263 2264

			// Tab on column 5
			assert.equal(model.getLineContent(2), 'My Second Line123');
2265 2266
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 5) }, 'keyboard');
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
2267
			assert.equal(model.getLineContent(2), 'My S         econd Line123');
2268
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
2269 2270 2271

			// Tab on column 13
			assert.equal(model.getLineContent(2), 'My Second Line123');
2272 2273
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 13) }, 'keyboard');
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
2274
			assert.equal(model.getLineContent(2), 'My Second Li ne123');
2275
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
2276 2277 2278

			// Tab on column 14
			assert.equal(model.getLineContent(2), 'My Second Line123');
2279 2280
			cursorCommand(cursor, H.MoveTo, { position: new Position(2, 14) }, 'keyboard');
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
2281 2282
			assert.equal(model.getLineContent(2), 'My Second Lin             e123');
		});
E
Erich Gamma 已提交
2283 2284 2285
	});

	test('Enter auto-indents with insertSpaces setting 1', () => {
A
Alex Dima 已提交
2286 2287 2288 2289
		usingCursor({
			text: [
				'\thello'
			],
2290
			modeId: new OnEnterMode(IndentAction.Indent).getId(),
2291
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
2292 2293
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
2294
			assertCursor(cursor, new Selection(1, 7, 1, 7));
A
Alex Dima 已提交
2295

2296
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
2297 2298
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thello\r\n        ');
		});
E
Erich Gamma 已提交
2299 2300 2301
	});

	test('Enter auto-indents with insertSpaces setting 2', () => {
A
Alex Dima 已提交
2302 2303 2304 2305
		usingCursor({
			text: [
				'\thello'
			],
2306
			modeId: new OnEnterMode(IndentAction.None).getId(),
2307
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
2308 2309
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
2310
			assertCursor(cursor, new Selection(1, 7, 1, 7));
A
Alex Dima 已提交
2311

2312
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
2313 2314
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thello\r\n    ');
		});
E
Erich Gamma 已提交
2315 2316 2317
	});

	test('Enter auto-indents with insertSpaces setting 3', () => {
A
Alex Dima 已提交
2318 2319 2320 2321
		usingCursor({
			text: [
				'\thell()'
			],
2322
			modeId: new OnEnterMode(IndentAction.IndentOutdent).getId(),
2323
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
2324 2325
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
2326
			assertCursor(cursor, new Selection(1, 7, 1, 7));
A
Alex Dima 已提交
2327

2328
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
2329 2330
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thell(\r\n        \r\n    )');
		});
E
Erich Gamma 已提交
2331 2332 2333
	});

	test('Insert line before', () => {
J
Johannes Rieken 已提交
2334
		let testInsertLineBefore = (lineNumber: number, column: number, callback: (model: Model, cursor: Cursor) => void) => {
A
Alex Dima 已提交
2335 2336 2337 2338 2339 2340 2341
			usingCursor({
				text: [
					'First line',
					'Second line',
					'Third line'
				],
			}, (model, cursor) => {
E
Erich Gamma 已提交
2342
				moveTo(cursor, lineNumber, column, false);
A
Alex Dima 已提交
2343
				assertCursor(cursor, new Position(lineNumber, column));
E
Erich Gamma 已提交
2344

2345
				cursorCommand(cursor, H.LineInsertBefore, null, 'keyboard');
E
Erich Gamma 已提交
2346 2347 2348 2349 2350
				callback(model, cursor);
			});
		};

		testInsertLineBefore(1, 3, (model, cursor) => {
A
Alex Dima 已提交
2351
			assertCursor(cursor, new Selection(1, 1, 1, 1));
E
Erich Gamma 已提交
2352 2353 2354 2355 2356 2357 2358
			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) => {
A
Alex Dima 已提交
2359
			assertCursor(cursor, new Selection(2, 1, 2, 1));
E
Erich Gamma 已提交
2360 2361 2362 2363 2364 2365 2366
			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) => {
A
Alex Dima 已提交
2367
			assertCursor(cursor, new Selection(3, 1, 3, 1));
E
Erich Gamma 已提交
2368 2369 2370 2371 2372 2373 2374 2375
			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', () => {
J
Johannes Rieken 已提交
2376
		let testInsertLineAfter = (lineNumber: number, column: number, callback: (model: Model, cursor: Cursor) => void) => {
A
Alex Dima 已提交
2377 2378 2379 2380 2381 2382 2383
			usingCursor({
				text: [
					'First line',
					'Second line',
					'Third line'
				],
			}, (model, cursor) => {
E
Erich Gamma 已提交
2384
				moveTo(cursor, lineNumber, column, false);
A
Alex Dima 已提交
2385
				assertCursor(cursor, new Position(lineNumber, column));
E
Erich Gamma 已提交
2386

2387
				cursorCommand(cursor, H.LineInsertAfter, null, 'keyboard');
E
Erich Gamma 已提交
2388 2389 2390 2391 2392
				callback(model, cursor);
			});
		};

		testInsertLineAfter(1, 3, (model, cursor) => {
A
Alex Dima 已提交
2393
			assertCursor(cursor, new Selection(2, 1, 2, 1));
E
Erich Gamma 已提交
2394 2395 2396 2397 2398 2399 2400
			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) => {
A
Alex Dima 已提交
2401
			assertCursor(cursor, new Selection(3, 1, 3, 1));
E
Erich Gamma 已提交
2402 2403 2404 2405 2406 2407 2408
			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) => {
A
Alex Dima 已提交
2409
			assertCursor(cursor, new Selection(4, 1, 4, 1));
E
Erich Gamma 已提交
2410 2411 2412 2413 2414 2415
			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), '');
		});
	});
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432

	test('removeAutoWhitespace off', () => {
		usingCursor({
			text: [
				'    some  line abc  '
			],
			modelOpts: {
				insertSpaces: true,
				tabSize: 4,
				detectIndentation: false,
				defaultEOL: DefaultEndOfLine.LF,
				trimAutoWhitespace: false
			}
		}, (model, cursor) => {

			// Move cursor to the end, verify that we do not trim whitespaces if line has values
			moveTo(cursor, 1, model.getLineContent(1).length + 1);
2433
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2434 2435 2436 2437
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '    ');

			// Try to enter again, we should trimmed previous line
2438
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '    ');
			assert.equal(model.getLineContent(3), '    ');
		});
	});

	test('removeAutoWhitespace on: removes only whitespace the cursor added 1', () => {
		usingCursor({
			text: [
				'    '
			],
			modelOpts: {
				insertSpaces: true,
				tabSize: 4,
				detectIndentation: false,
				defaultEOL: DefaultEndOfLine.LF,
				trimAutoWhitespace: true
			}
		}, (model, cursor) => {
			moveTo(cursor, 1, model.getLineContent(1).length + 1);
2459
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2460 2461 2462
			assert.equal(model.getLineContent(1), '    ');
			assert.equal(model.getLineContent(2), '    ');

2463
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2464 2465 2466 2467 2468 2469
			assert.equal(model.getLineContent(1), '    ');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), '    ');
		});
	});

2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
	test('issue #6862: Editor removes auto inserted indentation when formatting on type', () => {
		usingCursor({
			text: [
				'function foo (params: string) {}'
			],
			modelOpts: {
				insertSpaces: true,
				tabSize: 4,
				detectIndentation: false,
				defaultEOL: DefaultEndOfLine.LF,
				trimAutoWhitespace: true
			},
2482
			modeId: new OnEnterMode(IndentAction.IndentOutdent).getId(),
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
		}, (model, cursor) => {

			moveTo(cursor, 1, 32);
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assert.equal(model.getLineContent(1), 'function foo (params: string) {');
			assert.equal(model.getLineContent(2), '    ');
			assert.equal(model.getLineContent(3), '}');

			class TestCommand implements ICommand {

				private _selectionId: string = null;

				public getEditOperations(model: ITokenizedModel, builder: IEditOperationBuilder): void {
					builder.addEditOperation(new Range(1, 13, 1, 14), '');
					this._selectionId = builder.trackSelection(cursor.getSelection());
				}

				public computeCursorState(model: ITokenizedModel, helper: ICursorStateComputerData): Selection {
					return helper.getTrackedSelection(this._selectionId);
				}

			}

			cursor.trigger('autoFormat', Handler.ExecuteCommand, new TestCommand());
			assert.equal(model.getLineContent(1), 'function foo(params: string) {');
			assert.equal(model.getLineContent(2), '    ');
			assert.equal(model.getLineContent(3), '}');
		});
	});

2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
	test('removeAutoWhitespace on: removes only whitespace the cursor added 2', () => {
		usingCursor({
			text: [
				'    if (a) {',
				'        ',
				'',
				'',
				'    }'
			],
			modelOpts: {
				insertSpaces: true,
				tabSize: 4,
				detectIndentation: false,
				defaultEOL: DefaultEndOfLine.LF,
				trimAutoWhitespace: true
			}
		}, (model, cursor) => {

			moveTo(cursor, 3, 1);
2532
			cursorCommand(cursor, H.Tab, null, 'keyboard');
2533 2534 2535 2536 2537 2538 2539
			assert.equal(model.getLineContent(1), '    if (a) {');
			assert.equal(model.getLineContent(2), '        ');
			assert.equal(model.getLineContent(3), '    ');
			assert.equal(model.getLineContent(4), '');
			assert.equal(model.getLineContent(5), '    }');

			moveTo(cursor, 4, 1);
2540
			cursorCommand(cursor, H.Tab, null, 'keyboard');
2541 2542 2543 2544 2545 2546 2547
			assert.equal(model.getLineContent(1), '    if (a) {');
			assert.equal(model.getLineContent(2), '        ');
			assert.equal(model.getLineContent(3), '');
			assert.equal(model.getLineContent(4), '    ');
			assert.equal(model.getLineContent(5), '    }');

			moveTo(cursor, 5, model.getLineMaxColumn(5));
2548
			cursorCommand(cursor, H.Type, { text: 'something' }, 'keyboard');
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
			assert.equal(model.getLineContent(1), '    if (a) {');
			assert.equal(model.getLineContent(2), '        ');
			assert.equal(model.getLineContent(3), '');
			assert.equal(model.getLineContent(4), '');
			assert.equal(model.getLineContent(5), '    }something');
		});
	});

	test('removeAutoWhitespace on: test 1', () => {
		usingCursor({
			text: [
				'    some  line abc  '
			],
			modelOpts: {
				insertSpaces: true,
				tabSize: 4,
				detectIndentation: false,
				defaultEOL: DefaultEndOfLine.LF,
				trimAutoWhitespace: true
			}
		}, (model, cursor) => {

			// Move cursor to the end, verify that we do not trim whitespaces if line has values
			moveTo(cursor, 1, model.getLineContent(1).length + 1);
2573
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2574 2575 2576 2577
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '    ');

			// Try to enter again, we should trimmed previous line
2578
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2579 2580 2581 2582 2583
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), '    ');

			// More whitespaces
2584
			cursorCommand(cursor, H.Tab, null, 'keyboard');
2585 2586 2587 2588 2589
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), '        ');

			// Enter and verify that trimmed again
2590
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2591 2592 2593 2594 2595 2596 2597
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), '');
			assert.equal(model.getLineContent(4), '        ');

			// Trimmed if we will keep only text
			moveTo(cursor, 1, 5);
2598
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2599 2600 2601 2602 2603 2604 2605 2606 2607
			assert.equal(model.getLineContent(1), '    ');
			assert.equal(model.getLineContent(2), '    some  line abc  ');
			assert.equal(model.getLineContent(3), '');
			assert.equal(model.getLineContent(4), '');
			assert.equal(model.getLineContent(5), '');

			// Trimmed if we will keep only text by selection
			moveTo(cursor, 2, 5);
			moveTo(cursor, 3, 1, true);
2608
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2609 2610
			assert.equal(model.getLineContent(1), '    ');
			assert.equal(model.getLineContent(2), '    ');
2611
			assert.equal(model.getLineContent(3), '    ');
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
			assert.equal(model.getLineContent(4), '');
			assert.equal(model.getLineContent(5), '');
		});
	});

	test('UseTabStops is off', () => {
		usingCursor({
			text: [
				'    x',
				'        a    ',
				'    '
			],
			modelOpts: {
				insertSpaces: true,
				tabSize: 4,
				detectIndentation: false,
				defaultEOL: DefaultEndOfLine.LF,
				trimAutoWhitespace: true
2630 2631 2632
			},
			editorOpts: {
				useTabStops: false
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654
			}
		}, (model, cursor) => {
			// DeleteLeft removes just one whitespace
			moveTo(cursor, 2, 9);
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(2), '       a    ');
		});
	});

	test('Backspace removes whitespaces with tab size', () => {
		usingCursor({
			text: [
				' \t \t     x',
				'        a    ',
				'    '
			],
			modelOpts: {
				insertSpaces: true,
				tabSize: 4,
				detectIndentation: false,
				defaultEOL: DefaultEndOfLine.LF,
				trimAutoWhitespace: true
2655 2656 2657
			},
			editorOpts: {
				useTabStops: true
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726
			}
		}, (model, cursor) => {
			// DeleteLeft does not remove tab size, because some text exists before
			moveTo(cursor, 2, model.getLineContent(2).length + 1);
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(2), '        a   ');

			// DeleteLeft removes tab size = 4
			moveTo(cursor, 2, 9);
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(2), '    a   ');

			// DeleteLeft removes tab size = 4
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(2), 'a   ');

			// Undo DeleteLeft - get us back to original indentation
			cursorCommand(cursor, H.Undo, {});
			assert.equal(model.getLineContent(2), '        a   ');

			// Nothing is broken when cursor is in (1,1)
			moveTo(cursor, 1, 1);
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(1), ' \t \t     x');

			// DeleteLeft stops at tab stops even in mixed whitespace case
			moveTo(cursor, 1, 10);
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(1), ' \t \t    x');

			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(1), ' \t \tx');

			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(1), ' \tx');

			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(1), 'x');

			// DeleteLeft on last line
			moveTo(cursor, 3, model.getLineContent(3).length + 1);
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(3), '');

			// DeleteLeft with removing new line symbol
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), 'x\n        a   ');

			// In case of selection DeleteLeft only deletes selected text
			moveTo(cursor, 2, 3);
			moveTo(cursor, 2, 4, true);
			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getLineContent(2), '       a   ');
		});
	});

	test('PR #5423: Auto indent + undo + redo is funky', () => {
		usingCursor({
			text: [
				''
			],
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
				tabSize: 4,
				trimAutoWhitespace: true
			}
		}, (model, cursor) => {
2727
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2728 2729 2730 2731 2732
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n', 'assert1');

			cursorCommand(cursor, H.Tab, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t', 'assert2');

J
Johannes Rieken 已提交
2733
			cursorCommand(cursor, H.Type, { text: 'y' }, 'keyboard');
2734 2735
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\ty', 'assert2');

J
Johannes Rieken 已提交
2736
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\ty\n\t', 'assert3');

			cursorCommand(cursor, H.Type, { text: 'x' });
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\ty\n\tx', 'assert4');

			cursorCommand(cursor, H.CursorLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\ty\n\tx', 'assert5');

			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\ty\nx', 'assert6');

			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\tyx', 'assert7');

			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\tx', 'assert8');

			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\nx', 'assert9');

			cursorCommand(cursor, H.DeleteLeft, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), 'x', 'assert10');

			cursorCommand(cursor, H.Undo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\nx', 'assert11');

			cursorCommand(cursor, H.Undo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\ty\nx', 'assert12');

			cursorCommand(cursor, H.Undo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\ty\n\tx', 'assert13');

			cursorCommand(cursor, H.Redo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\ty\nx', 'assert14');

			cursorCommand(cursor, H.Redo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), '\nx', 'assert15');

			cursorCommand(cursor, H.Redo, {});
			assert.equal(model.getValue(EndOfLinePreference.LF), 'x', 'assert16');
		});
	});
E
Erich Gamma 已提交
2779 2780 2781
});

interface ICursorOpts {
A
Alex Dima 已提交
2782
	text: string[];
2783
	modeId?: string;
2784
	modelOpts?: ITextModelCreationOptions;
2785
	editorOpts?: IEditorOptions;
E
Erich Gamma 已提交
2786 2787
}

J
Johannes Rieken 已提交
2788
function usingCursor(opts: ICursorOpts, callback: (model: Model, cursor: Cursor) => void): void {
2789
	let model = Model.createFromString(opts.text.join('\n'), opts.modelOpts, opts.modeId);
2790
	let config = new MockConfiguration(opts.editorOpts);
2791
	let cursor = new Cursor(1, config, model, viewModelHelper(model), false);
E
Erich Gamma 已提交
2792 2793 2794 2795 2796 2797

	callback(model, cursor);

	cursor.dispose();
	config.dispose();
	model.dispose();
2798
}
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956

class ElectricCharMode extends MockMode {
	constructor() {
		super();
		LanguageConfigurationRegistry.register(this.getId(), {
			__electricCharacterSupport: {
				docComment: { open: '/**', close: ' */' }
			},
			brackets: [
				['{', '}'],
				['[', ']'],
				['(', ')']
			]
		});
	}
}

suite('ElectricCharacter', () => {
	test('does nothing if no electric char', () => {
		usingCursor({
			text: [
				'  if (a) {',
				''
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 2, 1);
			cursorCommand(cursor, H.Type, { text: '*' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '*');
		});
	});

	test('indents in order to match bracket', () => {
		usingCursor({
			text: [
				'  if (a) {',
				''
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 2, 1);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  }');
		});
	});

	test('unindents in order to match bracket', () => {
		usingCursor({
			text: [
				'  if (a) {',
				'    '
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 2, 5);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  }');
		});
	});

	test('matches with correct bracket', () => {
		usingCursor({
			text: [
				'  if (a) {',
				'    if (b) {',
				'    }',
				'    '
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 4, 1);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(4), '  }    ');
		});
	});

	test('does nothing if bracket does not match', () => {
		usingCursor({
			text: [
				'  if (a) {',
				'    if (b) {',
				'    }',
				'  }  '
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 4, 6);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(4), '  }  }');
		});
	});

	test('matches bracket even in line with content', () => {
		usingCursor({
			text: [
				'  if (a) {',
				'// hello'
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 2, 1);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  }// hello');
		});
	});

	test('is no-op if bracket is lined up', () => {
		usingCursor({
			text: [
				'  if (a) {',
				'  '
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 2, 3);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  }');
		});
	});

	test('is no-op if there is non-whitespace text before', () => {
		usingCursor({
			text: [
				'  if (a) {',
				'a'
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 2, 2);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  a}');
		});
	});

	test('appends text', () => {
		usingCursor({
			text: [
				'  if (a) {',
				'/*'
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 2, 3);
			cursorCommand(cursor, H.Type, { text: '*' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '/** */');
		});
	});

	test('appends text 2', () => {
		usingCursor({
			text: [
				'  if (a) {',
				'  /*'
			],
			modeId: new ElectricCharMode().getId()
		}, (model, cursor) => {
			moveTo(cursor, 2, 5);
			cursorCommand(cursor, H.Type, { text: '*' }, 'keyboard');
A
Alex Dima 已提交
2957
			assert.deepEqual(model.getLineContent(2), '  /** */');
2958 2959 2960
		});
	});
});