cursor.test.ts 103.8 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';
8
import { Cursor } from 'vs/editor/common/controller/cursor';
J
Johannes Rieken 已提交
9 10 11 12
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 {
14
	EndOfLinePreference, Handler,
15
	DefaultEndOfLine, ITextModelCreationOptions, ICommand,
16
	ITokenizedModel, IEditOperationBuilder, ICursorStateComputerData
17
} from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
18
import { Model } from 'vs/editor/common/model/model';
R
rebornix 已提交
19
import { IndentAction, IndentationRule } from 'vs/editor/common/modes/languageConfiguration';
J
Johannes Rieken 已提交
20
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
21
import { TestConfiguration } from 'vs/editor/test/common/mocks/testConfiguration';
J
Johannes Rieken 已提交
22
import { MockMode } from 'vs/editor/test/common/mocks/mockMode';
A
Alex Dima 已提交
23
import { LanguageIdentifier } from 'vs/editor/common/modes';
J
Johannes Rieken 已提交
24
import { viewModelHelper } from 'vs/editor/test/common/editorTestUtils';
25 26 27
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { ICursorPositionChangedEvent, ICursorSelectionChangedEvent } from 'vs/editor/common/controller/cursorEvents';
import { CoreCommands } from 'vs/editor/common/controller/coreCommands';
E
Erich Gamma 已提交
28

A
Alex Dima 已提交
29
let H = Handler;
E
Erich Gamma 已提交
30 31 32

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

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

A
Alex Dima 已提交
37
function moveTo(cursor: Cursor, lineNumber: number, column: number, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
38
	if (inSelectionMode) {
A
Alex Dima 已提交
39
		CoreCommands.MoveToSelect.runCoreEditorCommand(cursor, {
A
Alex Dima 已提交
40 41 42
			position: new Position(lineNumber, column)
		});
	} else {
A
Alex Dima 已提交
43
		CoreCommands.MoveTo.runCoreEditorCommand(cursor, {
A
Alex Dima 已提交
44 45 46
			position: new Position(lineNumber, column)
		});
	}
E
Erich Gamma 已提交
47 48
}

A
Alex Dima 已提交
49
function moveLeft(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
50 51 52 53 54
	if (inSelectionMode) {
		CoreCommands.CursorLeftSelect.runCoreEditorCommand(cursor, {});
	} else {
		CoreCommands.CursorLeft.runCoreEditorCommand(cursor, {});
	}
E
Erich Gamma 已提交
55 56
}

A
Alex Dima 已提交
57
function moveRight(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
58 59 60 61 62
	if (inSelectionMode) {
		CoreCommands.CursorRightSelect.runCoreEditorCommand(cursor, {});
	} else {
		CoreCommands.CursorRight.runCoreEditorCommand(cursor, {});
	}
E
Erich Gamma 已提交
63 64
}

A
Alex Dima 已提交
65
function moveDown(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
66 67 68 69 70
	if (inSelectionMode) {
		CoreCommands.CursorDownSelect.runCoreEditorCommand(cursor, {});
	} else {
		CoreCommands.CursorDown.runCoreEditorCommand(cursor, {});
	}
E
Erich Gamma 已提交
71 72
}

A
Alex Dima 已提交
73
function moveUp(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
74 75 76 77 78
	if (inSelectionMode) {
		CoreCommands.CursorUpSelect.runCoreEditorCommand(cursor, {});
	} else {
		CoreCommands.CursorUp.runCoreEditorCommand(cursor, {});
	}
E
Erich Gamma 已提交
79 80
}

A
Alex Dima 已提交
81
function moveToBeginningOfLine(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
82 83 84 85 86
	if (inSelectionMode) {
		CoreCommands.CursorHomeSelect.runCoreEditorCommand(cursor, {});
	} else {
		CoreCommands.CursorHome.runCoreEditorCommand(cursor, {});
	}
E
Erich Gamma 已提交
87 88
}

A
Alex Dima 已提交
89
function moveToEndOfLine(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
90 91 92 93 94
	if (inSelectionMode) {
		CoreCommands.CursorEndSelect.runCoreEditorCommand(cursor, {});
	} else {
		CoreCommands.CursorEnd.runCoreEditorCommand(cursor, {});
	}
E
Erich Gamma 已提交
95 96
}

A
Alex Dima 已提交
97
function moveToBeginningOfBuffer(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
98 99 100 101 102
	if (inSelectionMode) {
		CoreCommands.CursorTopSelect.runCoreEditorCommand(cursor, {});
	} else {
		CoreCommands.CursorTop.runCoreEditorCommand(cursor, {});
	}
E
Erich Gamma 已提交
103 104
}

A
Alex Dima 已提交
105
function moveToEndOfBuffer(cursor: Cursor, inSelectionMode: boolean = false) {
A
Alex Dima 已提交
106 107 108 109 110
	if (inSelectionMode) {
		CoreCommands.CursorBottomSelect.runCoreEditorCommand(cursor, {});
	} else {
		CoreCommands.CursorBottom.runCoreEditorCommand(cursor, {});
	}
E
Erich Gamma 已提交
111 112
}

J
Johannes Rieken 已提交
113 114
function assertCursor(cursor: Cursor, what: Position | Selection | Selection[]): void {
	let selections: Selection[];
A
Alex Dima 已提交
115 116 117 118 119 120 121
	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 已提交
122 123 124 125 126
	let actual = cursor.getSelections().map(s => s.toString());
	let expected = selections.map(s => s.toString());

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

suite('Editor Controller - Cursor', () => {
A
Alex Dima 已提交
129 130
	const LINE1 = '    \tMy First Line\t ';
	const LINE2 = '\tMy Second Line';
A
Alex Dima 已提交
131
	const LINE3 = '    Third Line🐶';
A
Alex Dima 已提交
132 133
	const LINE4 = '';
	const LINE5 = '1';
E
Erich Gamma 已提交
134

A
Alex Dima 已提交
135
	let thisModel: Model;
136
	let thisConfiguration: TestConfiguration;
A
Alex Dima 已提交
137
	let thisCursor: Cursor;
E
Erich Gamma 已提交
138 139

	setup(() => {
A
Alex Dima 已提交
140
		let text =
E
Erich Gamma 已提交
141 142 143 144 145 146
			LINE1 + '\r\n' +
			LINE2 + '\n' +
			LINE3 + '\n' +
			LINE4 + '\r\n' +
			LINE5;

147
		thisModel = Model.createFromString(text);
148
		thisConfiguration = new TestConfiguration(null);
149
		thisCursor = new Cursor(thisConfiguration, thisModel, viewModelHelper(thisModel));
E
Erich Gamma 已提交
150 151 152 153 154 155 156 157 158
	});

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

	test('cursor initialized', () => {
A
Alex Dima 已提交
159
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
160 161 162 163 164 165
	});

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

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

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

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

	test('move beyond line end', () => {
		moveTo(thisCursor, 1, 25);
A
Alex Dima 已提交
181
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
182 183 184 185
	});

	test('move empty line', () => {
		moveTo(thisCursor, 4, 20);
A
Alex Dima 已提交
186
		assertCursor(thisCursor, new Position(4, 1));
E
Erich Gamma 已提交
187 188 189 190
	});

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

	test('selection down', () => {
		moveTo(thisCursor, 2, 1, true);
A
Alex Dima 已提交
196
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
E
Erich Gamma 已提交
197 198 199 200
	});

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

		moveTo(thisCursor, 2, 15, true);
A
Alex Dima 已提交
204
		assertCursor(thisCursor, new Selection(2, 3, 2, 15));
E
Erich Gamma 已提交
205 206

		moveTo(thisCursor, 1, 2, true);
A
Alex Dima 已提交
207
		assertCursor(thisCursor, new Selection(2, 3, 1, 2));
E
Erich Gamma 已提交
208 209 210 211 212 213
	});

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

	test('move left on top left position', () => {
		moveLeft(thisCursor);
A
Alex Dima 已提交
214
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
215 216 217 218
	});

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

224 225
	test('move left with surrogate pair', () => {
		moveTo(thisCursor, 3, 17);
A
Alex Dima 已提交
226
		assertCursor(thisCursor, new Position(3, 17));
227
		moveLeft(thisCursor);
A
Alex Dima 已提交
228
		assertCursor(thisCursor, new Position(3, 15));
229 230
	});

E
Erich Gamma 已提交
231 232
	test('move left goes to previous row', () => {
		moveTo(thisCursor, 2, 1);
A
Alex Dima 已提交
233
		assertCursor(thisCursor, new Position(2, 1));
E
Erich Gamma 已提交
234
		moveLeft(thisCursor);
A
Alex Dima 已提交
235
		assertCursor(thisCursor, new Position(1, 21));
E
Erich Gamma 已提交
236 237 238 239
	});

	test('move left selection', () => {
		moveTo(thisCursor, 2, 1);
A
Alex Dima 已提交
240
		assertCursor(thisCursor, new Position(2, 1));
E
Erich Gamma 已提交
241
		moveLeft(thisCursor, true);
A
Alex Dima 已提交
242
		assertCursor(thisCursor, new Selection(2, 1, 1, 21));
E
Erich Gamma 已提交
243 244 245 246 247 248
	});

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

	test('move right on bottom right position', () => {
		moveTo(thisCursor, 5, 2);
A
Alex Dima 已提交
249
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
250
		moveRight(thisCursor);
A
Alex Dima 已提交
251
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
252 253 254 255
	});

	test('move right', () => {
		moveTo(thisCursor, 1, 3);
A
Alex Dima 已提交
256
		assertCursor(thisCursor, new Position(1, 3));
E
Erich Gamma 已提交
257
		moveRight(thisCursor);
A
Alex Dima 已提交
258
		assertCursor(thisCursor, new Position(1, 4));
E
Erich Gamma 已提交
259 260
	});

261 262
	test('move right with surrogate pair', () => {
		moveTo(thisCursor, 3, 15);
A
Alex Dima 已提交
263
		assertCursor(thisCursor, new Position(3, 15));
264
		moveRight(thisCursor);
A
Alex Dima 已提交
265
		assertCursor(thisCursor, new Position(3, 17));
266 267
	});

E
Erich Gamma 已提交
268 269
	test('move right goes to next row', () => {
		moveTo(thisCursor, 1, 21);
A
Alex Dima 已提交
270
		assertCursor(thisCursor, new Position(1, 21));
E
Erich Gamma 已提交
271
		moveRight(thisCursor);
A
Alex Dima 已提交
272
		assertCursor(thisCursor, new Position(2, 1));
E
Erich Gamma 已提交
273 274 275 276
	});

	test('move right selection', () => {
		moveTo(thisCursor, 1, 21);
A
Alex Dima 已提交
277
		assertCursor(thisCursor, new Position(1, 21));
E
Erich Gamma 已提交
278
		moveRight(thisCursor, true);
A
Alex Dima 已提交
279
		assertCursor(thisCursor, new Selection(1, 21, 2, 1));
E
Erich Gamma 已提交
280 281 282 283 284
	});

	// --------- move down

	test('move down', () => {
A
Alex Dima 已提交
285
		moveDown(thisCursor);
A
Alex Dima 已提交
286
		assertCursor(thisCursor, new Position(2, 1));
A
Alex Dima 已提交
287
		moveDown(thisCursor);
A
Alex Dima 已提交
288
		assertCursor(thisCursor, new Position(3, 1));
A
Alex Dima 已提交
289
		moveDown(thisCursor);
A
Alex Dima 已提交
290
		assertCursor(thisCursor, new Position(4, 1));
A
Alex Dima 已提交
291
		moveDown(thisCursor);
A
Alex Dima 已提交
292
		assertCursor(thisCursor, new Position(5, 1));
A
Alex Dima 已提交
293
		moveDown(thisCursor);
A
Alex Dima 已提交
294
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
295 296 297
	});

	test('move down with selection', () => {
A
Alex Dima 已提交
298
		moveDown(thisCursor, true);
A
Alex Dima 已提交
299
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
A
Alex Dima 已提交
300
		moveDown(thisCursor, true);
A
Alex Dima 已提交
301
		assertCursor(thisCursor, new Selection(1, 1, 3, 1));
A
Alex Dima 已提交
302
		moveDown(thisCursor, true);
A
Alex Dima 已提交
303
		assertCursor(thisCursor, new Selection(1, 1, 4, 1));
A
Alex Dima 已提交
304
		moveDown(thisCursor, true);
A
Alex Dima 已提交
305
		assertCursor(thisCursor, new Selection(1, 1, 5, 1));
A
Alex Dima 已提交
306
		moveDown(thisCursor, true);
A
Alex Dima 已提交
307
		assertCursor(thisCursor, new Selection(1, 1, 5, 2));
E
Erich Gamma 已提交
308 309 310 311
	});

	test('move down with tabs', () => {
		moveTo(thisCursor, 1, 5);
A
Alex Dima 已提交
312
		assertCursor(thisCursor, new Position(1, 5));
A
Alex Dima 已提交
313
		moveDown(thisCursor);
A
Alex Dima 已提交
314
		assertCursor(thisCursor, new Position(2, 2));
A
Alex Dima 已提交
315
		moveDown(thisCursor);
A
Alex Dima 已提交
316
		assertCursor(thisCursor, new Position(3, 5));
A
Alex Dima 已提交
317
		moveDown(thisCursor);
A
Alex Dima 已提交
318
		assertCursor(thisCursor, new Position(4, 1));
A
Alex Dima 已提交
319
		moveDown(thisCursor);
A
Alex Dima 已提交
320
		assertCursor(thisCursor, new Position(5, 2));
E
Erich Gamma 已提交
321 322 323 324 325 326
	});

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

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

A
Alex Dima 已提交
329
		moveUp(thisCursor);
A
Alex Dima 已提交
330
		assertCursor(thisCursor, new Position(2, 2));
E
Erich Gamma 已提交
331

A
Alex Dima 已提交
332
		moveUp(thisCursor);
A
Alex Dima 已提交
333
		assertCursor(thisCursor, new Position(1, 5));
E
Erich Gamma 已提交
334 335 336 337
	});

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

A
Alex Dima 已提交
340
		moveUp(thisCursor, true);
A
Alex Dima 已提交
341
		assertCursor(thisCursor, new Selection(3, 5, 2, 2));
E
Erich Gamma 已提交
342

A
Alex Dima 已提交
343
		moveUp(thisCursor, true);
A
Alex Dima 已提交
344
		assertCursor(thisCursor, new Selection(3, 5, 1, 5));
E
Erich Gamma 已提交
345 346 347 348
	});

	test('move up and down with tabs', () => {
		moveTo(thisCursor, 1, 5);
A
Alex Dima 已提交
349
		assertCursor(thisCursor, new Position(1, 5));
A
Alex Dima 已提交
350 351 352 353
		moveDown(thisCursor);
		moveDown(thisCursor);
		moveDown(thisCursor);
		moveDown(thisCursor);
A
Alex Dima 已提交
354
		assertCursor(thisCursor, new Position(5, 2));
A
Alex Dima 已提交
355
		moveUp(thisCursor);
A
Alex Dima 已提交
356
		assertCursor(thisCursor, new Position(4, 1));
A
Alex Dima 已提交
357
		moveUp(thisCursor);
A
Alex Dima 已提交
358
		assertCursor(thisCursor, new Position(3, 5));
A
Alex Dima 已提交
359
		moveUp(thisCursor);
A
Alex Dima 已提交
360
		assertCursor(thisCursor, new Position(2, 2));
A
Alex Dima 已提交
361
		moveUp(thisCursor);
A
Alex Dima 已提交
362
		assertCursor(thisCursor, new Position(1, 5));
E
Erich Gamma 已提交
363 364 365 366
	});

	test('move up and down with end of lines starting from a long one', () => {
		moveToEndOfLine(thisCursor);
367
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
368
		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
369
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
A
Alex Dima 已提交
370
		moveDown(thisCursor);
A
Alex Dima 已提交
371
		assertCursor(thisCursor, new Position(2, LINE2.length + 1));
A
Alex Dima 已提交
372
		moveDown(thisCursor);
A
Alex Dima 已提交
373
		assertCursor(thisCursor, new Position(3, LINE3.length + 1));
A
Alex Dima 已提交
374
		moveDown(thisCursor);
A
Alex Dima 已提交
375
		assertCursor(thisCursor, new Position(4, LINE4.length + 1));
A
Alex Dima 已提交
376
		moveDown(thisCursor);
A
Alex Dima 已提交
377
		assertCursor(thisCursor, new Position(5, LINE5.length + 1));
A
Alex Dima 已提交
378 379 380 381
		moveUp(thisCursor);
		moveUp(thisCursor);
		moveUp(thisCursor);
		moveUp(thisCursor);
A
Alex Dima 已提交
382
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
383 384 385 386 387 388
	});

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

	test('move to beginning of line', () => {
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
389
		assertCursor(thisCursor, new Position(1, 6));
E
Erich Gamma 已提交
390
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
391
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
392 393 394 395 396
	});

	test('move to beginning of line from within line', () => {
		moveTo(thisCursor, 1, 8);
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
397
		assertCursor(thisCursor, new Position(1, 6));
E
Erich Gamma 已提交
398
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
399
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
400 401 402 403 404
	});

	test('move to beginning of line from whitespace at beginning of line', () => {
		moveTo(thisCursor, 1, 2);
		moveToBeginningOfLine(thisCursor);
A
Alex Dima 已提交
405
		assertCursor(thisCursor, new Position(1, 6));
406 407
		moveToBeginningOfLine(thisCursor);
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
408 409 410 411 412
	});

	test('move to beginning of line from within line selection', () => {
		moveTo(thisCursor, 1, 8);
		moveToBeginningOfLine(thisCursor, true);
A
Alex Dima 已提交
413
		assertCursor(thisCursor, new Selection(1, 8, 1, 6));
E
Erich Gamma 已提交
414
		moveToBeginningOfLine(thisCursor, true);
A
Alex Dima 已提交
415
		assertCursor(thisCursor, new Selection(1, 8, 1, 1));
E
Erich Gamma 已提交
416 417
	});

418 419 420 421
	test('move to beginning of line with selection multiline forward', () => {
		moveTo(thisCursor, 1, 8);
		moveTo(thisCursor, 3, 9, true);
		moveToBeginningOfLine(thisCursor, false);
422
		assertCursor(thisCursor, new Selection(3, 5, 3, 5));
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	});

	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));
	});

446
	test('issue #15401: "End" key is behaving weird when text is selected part 1', () => {
447
		moveTo(thisCursor, 1, 8);
448 449
		moveTo(thisCursor, 3, 9, true);
		moveToBeginningOfLine(thisCursor, false);
450 451 452 453 454 455 456 457
		assertCursor(thisCursor, new Selection(3, 5, 3, 5));
	});

	test('issue #17011: Shift+home/end now go to the end of the selection start\'s line, not the selection\'s end', () => {
		moveTo(thisCursor, 1, 8);
		moveTo(thisCursor, 3, 9, true);
		moveToBeginningOfLine(thisCursor, true);
		assertCursor(thisCursor, new Selection(1, 8, 3, 5));
458 459
	});

E
Erich Gamma 已提交
460 461 462 463
	// --------- move to end of line

	test('move to end of line', () => {
		moveToEndOfLine(thisCursor);
464
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
465
		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
466
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
467 468 469 470 471
	});

	test('move to end of line from within line', () => {
		moveTo(thisCursor, 1, 6);
		moveToEndOfLine(thisCursor);
472
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
473
		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
474
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
475 476 477 478 479
	});

	test('move to end of line from whitespace at end of line', () => {
		moveTo(thisCursor, 1, 20);
		moveToEndOfLine(thisCursor);
A
Alex Dima 已提交
480
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
481
		moveToEndOfLine(thisCursor);
482
		assertCursor(thisCursor, new Position(1, LINE1.length + 1));
E
Erich Gamma 已提交
483 484 485 486 487
	});

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

493 494 495 496 497 498 499 500 501 502 503
	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);
504
		assertCursor(thisCursor, new Selection(1, 21, 1, 21));
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	});

	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));
	});

521 522 523 524
	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);
525
		assertCursor(thisCursor, new Selection(3, 17, 3, 17));
526 527
	});

E
Erich Gamma 已提交
528 529 530 531
	// --------- move to beginning of buffer

	test('move to beginning of buffer', () => {
		moveToBeginningOfBuffer(thisCursor);
A
Alex Dima 已提交
532
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
533 534 535 536 537
	});

	test('move to beginning of buffer from within first line', () => {
		moveTo(thisCursor, 1, 3);
		moveToBeginningOfBuffer(thisCursor);
A
Alex Dima 已提交
538
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
539 540 541 542 543
	});

	test('move to beginning of buffer from within another line', () => {
		moveTo(thisCursor, 3, 3);
		moveToBeginningOfBuffer(thisCursor);
A
Alex Dima 已提交
544
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
545 546 547 548 549
	});

	test('move to beginning of buffer from within first line selection', () => {
		moveTo(thisCursor, 1, 3);
		moveToBeginningOfBuffer(thisCursor, true);
A
Alex Dima 已提交
550
		assertCursor(thisCursor, new Selection(1, 3, 1, 1));
E
Erich Gamma 已提交
551 552 553 554 555
	});

	test('move to beginning of buffer from within another line selection', () => {
		moveTo(thisCursor, 3, 3);
		moveToBeginningOfBuffer(thisCursor, true);
A
Alex Dima 已提交
556
		assertCursor(thisCursor, new Selection(3, 3, 1, 1));
E
Erich Gamma 已提交
557 558 559 560 561 562
	});

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

	test('move to end of buffer', () => {
		moveToEndOfBuffer(thisCursor);
A
Alex Dima 已提交
563
		assertCursor(thisCursor, new Position(5, LINE5.length + 1));
E
Erich Gamma 已提交
564 565 566 567 568
	});

	test('move to end of buffer from within last line', () => {
		moveTo(thisCursor, 5, 1);
		moveToEndOfBuffer(thisCursor);
A
Alex Dima 已提交
569
		assertCursor(thisCursor, new Position(5, LINE5.length + 1));
E
Erich Gamma 已提交
570 571 572 573 574
	});

	test('move to end of buffer from within another line', () => {
		moveTo(thisCursor, 3, 3);
		moveToEndOfBuffer(thisCursor);
A
Alex Dima 已提交
575
		assertCursor(thisCursor, new Position(5, LINE5.length + 1));
E
Erich Gamma 已提交
576 577 578 579 580
	});

	test('move to end of buffer from within last line selection', () => {
		moveTo(thisCursor, 5, 1);
		moveToEndOfBuffer(thisCursor, true);
A
Alex Dima 已提交
581
		assertCursor(thisCursor, new Selection(5, 1, 5, LINE5.length + 1));
E
Erich Gamma 已提交
582 583 584 585 586
	});

	test('move to end of buffer from within another line selection', () => {
		moveTo(thisCursor, 3, 3);
		moveToEndOfBuffer(thisCursor, true);
A
Alex Dima 已提交
587
		assertCursor(thisCursor, new Selection(3, 3, 5, LINE5.length + 1));
E
Erich Gamma 已提交
588 589 590 591 592
	});

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

	test('select all', () => {
593
		CoreCommands.SelectAll.runCoreEditorCommand(thisCursor, {});
A
Alex Dima 已提交
594
		assertCursor(thisCursor, new Selection(1, 1, 5, LINE5.length + 1));
E
Erich Gamma 已提交
595 596
	});

A
Alex Dima 已提交
597 598 599
	test('expandLineSelection', () => {
		//              0          1         2
		//              01234 56789012345678 0
A
Alex Dima 已提交
600
		// let LINE1 = '    \tMy First Line\t ';
A
Alex Dima 已提交
601
		moveTo(thisCursor, 1, 1);
A
Alex Dima 已提交
602
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
603
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
A
Alex Dima 已提交
604 605

		moveTo(thisCursor, 1, 2);
A
Alex Dima 已提交
606
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
607
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
A
Alex Dima 已提交
608 609

		moveTo(thisCursor, 1, 5);
A
Alex Dima 已提交
610
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
611
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
A
Alex Dima 已提交
612 613

		moveTo(thisCursor, 1, 19);
A
Alex Dima 已提交
614
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
615
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
A
Alex Dima 已提交
616 617

		moveTo(thisCursor, 1, 20);
A
Alex Dima 已提交
618
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
619
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
A
Alex Dima 已提交
620 621

		moveTo(thisCursor, 1, 21);
A
Alex Dima 已提交
622
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
623
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
A
Alex Dima 已提交
624
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
625
		assertCursor(thisCursor, new Selection(1, 1, 3, 1));
A
Alex Dima 已提交
626
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
627
		assertCursor(thisCursor, new Selection(1, 1, 4, 1));
A
Alex Dima 已提交
628
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
629
		assertCursor(thisCursor, new Selection(1, 1, 5, 1));
A
Alex Dima 已提交
630
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
A
Alex Dima 已提交
631
		assertCursor(thisCursor, new Selection(1, 1, 5, LINE5.length + 1));
A
Alex Dima 已提交
632
		CoreCommands.ExpandLineSelection.runCoreEditorCommand(thisCursor, {});
A
Alex Dima 已提交
633
		assertCursor(thisCursor, new Selection(1, 1, 5, LINE5.length + 1));
A
Alex Dima 已提交
634 635
	});

E
Erich Gamma 已提交
636 637 638
	// --------- eventing

	test('no move doesn\'t trigger event', () => {
A
Alex Dima 已提交
639
		thisCursor.onDidChangePosition((e) => {
E
Erich Gamma 已提交
640 641
			assert.ok(false, 'was not expecting event');
		});
A
Alex Dima 已提交
642
		thisCursor.onDidChangeSelection((e) => {
E
Erich Gamma 已提交
643 644 645 646 647 648
			assert.ok(false, 'was not expecting event');
		});
		moveTo(thisCursor, 1, 1);
	});

	test('move eventing', () => {
A
Alex Dima 已提交
649
		let events = 0;
A
Alex Dima 已提交
650
		thisCursor.onDidChangePosition((e: ICursorPositionChangedEvent) => {
E
Erich Gamma 已提交
651
			events++;
A
Alex Dima 已提交
652
			assert.deepEqual(e.position, new Position(1, 2));
E
Erich Gamma 已提交
653
		});
A
Alex Dima 已提交
654
		thisCursor.onDidChangeSelection((e: ICursorSelectionChangedEvent) => {
E
Erich Gamma 已提交
655
			events++;
A
Alex Dima 已提交
656
			assert.deepEqual(e.selection, new Selection(1, 2, 1, 2));
E
Erich Gamma 已提交
657 658 659 660 661 662
		});
		moveTo(thisCursor, 1, 2);
		assert.equal(events, 2, 'receives 2 events');
	});

	test('move in selection mode eventing', () => {
A
Alex Dima 已提交
663
		let events = 0;
A
Alex Dima 已提交
664
		thisCursor.onDidChangePosition((e: ICursorPositionChangedEvent) => {
E
Erich Gamma 已提交
665
			events++;
A
Alex Dima 已提交
666
			assert.deepEqual(e.position, new Position(1, 2));
E
Erich Gamma 已提交
667
		});
A
Alex Dima 已提交
668
		thisCursor.onDidChangeSelection((e: ICursorSelectionChangedEvent) => {
E
Erich Gamma 已提交
669
			events++;
670
			assert.deepEqual(e.selection, new Selection(1, 1, 1, 2));
E
Erich Gamma 已提交
671 672 673 674 675 676 677 678 679
		});
		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 已提交
680
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
E
Erich Gamma 已提交
681

A
Alex Dima 已提交
682
		let savedState = JSON.stringify(thisCursor.saveState());
E
Erich Gamma 已提交
683 684

		moveTo(thisCursor, 1, 1, false);
A
Alex Dima 已提交
685
		assertCursor(thisCursor, new Position(1, 1));
E
Erich Gamma 已提交
686 687

		thisCursor.restoreState(JSON.parse(savedState));
A
Alex Dima 已提交
688
		assertCursor(thisCursor, new Selection(1, 1, 2, 1));
E
Erich Gamma 已提交
689 690 691 692 693 694 695
	});

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

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

A
Alex Dima 已提交
696
		thisModel.applyEdits([EditOperation.delete(new Range(2, 1, 2, 2))]);
A
Alex Dima 已提交
697
		assertCursor(thisCursor, new Selection(1, 1, 2, 15));
E
Erich Gamma 已提交
698
	});
A
Alex Dima 已提交
699 700

	test('column select 1', () => {
701
		let model = Model.createFromString([
A
Alex Dima 已提交
702 703 704 705 706
			'\tprivate compute(a:number): boolean {',
			'\t\tif (a + 3 === 0 || a + 5 === 0) {',
			'\t\t\treturn false;',
			'\t\t}',
			'\t}'
707
		].join('\n'));
708
		let cursor = new Cursor(new TestConfiguration(null), model, viewModelHelper(model));
A
Alex Dima 已提交
709 710

		moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
711
		assertCursor(cursor, new Position(1, 7));
A
Alex Dima 已提交
712

A
Alex Dima 已提交
713
		CoreCommands.ColumnSelect.runCoreEditorCommand(cursor, {
A
Alex Dima 已提交
714 715 716 717 718 719
			position: new Position(4, 4),
			viewPosition: new Position(4, 4),
			mouseColumn: 15
		});

		let expectedSelections = [
A
Alex Dima 已提交
720 721 722
			new Selection(1, 7, 1, 12),
			new Selection(2, 4, 2, 9),
			new Selection(3, 3, 3, 6),
A
Alex Dima 已提交
723 724 725
			new Selection(4, 4, 4, 4),
		];

A
Alex Dima 已提交
726
		assertCursor(cursor, expectedSelections);
A
Alex Dima 已提交
727 728 729 730

		cursor.dispose();
		model.dispose();
	});
A
Alex Dima 已提交
731 732

	test('issue #4905 - column select is biased to the right', () => {
733
		let model = Model.createFromString([
A
Alex Dima 已提交
734 735 736 737 738 739 740
			'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");',
741
		].join('\n'));
742
		let cursor = new Cursor(new TestConfiguration(null), model, viewModelHelper(model));
A
Alex Dima 已提交
743 744

		moveTo(cursor, 1, 4, false);
A
Alex Dima 已提交
745
		assertCursor(cursor, new Position(1, 4));
A
Alex Dima 已提交
746

A
Alex Dima 已提交
747
		CoreCommands.ColumnSelect.runCoreEditorCommand(cursor, {
A
Alex Dima 已提交
748 749 750 751 752
			position: new Position(4, 1),
			viewPosition: new Position(4, 1),
			mouseColumn: 1
		});

A
Alex Dima 已提交
753
		assertCursor(cursor, [
A
Alex Dima 已提交
754 755 756 757 758 759 760 761 762 763
			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();
	});

A
Alex Dima 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776
	test('issue #20087: column select with mouse', () => {
		let model = Model.createFromString([
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" Key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SoMEKEy" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" valuE="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="00X"/>',
		].join('\n'));
777
		let cursor = new Cursor(new TestConfiguration(null), model, viewModelHelper(model));
A
Alex Dima 已提交
778 779 780 781

		moveTo(cursor, 10, 10, false);
		assertCursor(cursor, new Position(10, 10));

A
Alex Dima 已提交
782
		CoreCommands.ColumnSelect.runCoreEditorCommand(cursor, {
A
Alex Dima 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
			position: new Position(1, 1),
			viewPosition: new Position(1, 1),
			mouseColumn: 1
		});
		assertCursor(cursor, [
			new Selection(10, 10, 10, 1),
			new Selection(9, 10, 9, 1),
			new Selection(8, 10, 8, 1),
			new Selection(7, 10, 7, 1),
			new Selection(6, 10, 6, 1),
			new Selection(5, 10, 5, 1),
			new Selection(4, 10, 4, 1),
			new Selection(3, 10, 3, 1),
			new Selection(2, 10, 2, 1),
			new Selection(1, 10, 1, 1),
		]);

A
Alex Dima 已提交
800
		CoreCommands.ColumnSelect.runCoreEditorCommand(cursor, {
A
Alex Dima 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
			position: new Position(1, 1),
			viewPosition: new Position(1, 1),
			mouseColumn: 1
		});
		assertCursor(cursor, [
			new Selection(10, 10, 10, 1),
			new Selection(9, 10, 9, 1),
			new Selection(8, 10, 8, 1),
			new Selection(7, 10, 7, 1),
			new Selection(6, 10, 6, 1),
			new Selection(5, 10, 5, 1),
			new Selection(4, 10, 4, 1),
			new Selection(3, 10, 3, 1),
			new Selection(2, 10, 2, 1),
			new Selection(1, 10, 1, 1),
		]);

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

	test('issue #20087: column select with keyboard', () => {
		let model = Model.createFromString([
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" Key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SoMEKEy" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" valuE="000"/>',
			'<property id="SomeThing" key="SomeKey" value="000"/>',
			'<property id="SomeThing" key="SomeKey" value="00X"/>',
		].join('\n'));
835
		let cursor = new Cursor(new TestConfiguration(null), model, viewModelHelper(model));
A
Alex Dima 已提交
836 837 838 839

		moveTo(cursor, 10, 10, false);
		assertCursor(cursor, new Position(10, 10));

A
Alex Dima 已提交
840
		CoreCommands.CursorColumnSelectLeft.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
841 842 843 844
		assertCursor(cursor, [
			new Selection(10, 10, 10, 9)
		]);

A
Alex Dima 已提交
845
		CoreCommands.CursorColumnSelectLeft.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
846 847 848 849
		assertCursor(cursor, [
			new Selection(10, 10, 10, 8)
		]);

A
Alex Dima 已提交
850
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
851 852 853 854
		assertCursor(cursor, [
			new Selection(10, 10, 10, 9)
		]);

A
Alex Dima 已提交
855
		CoreCommands.CursorColumnSelectUp.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
856 857 858 859 860
		assertCursor(cursor, [
			new Selection(10, 10, 10, 9),
			new Selection(9, 10, 9, 9),
		]);

A
Alex Dima 已提交
861
		CoreCommands.CursorColumnSelectDown.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
862 863 864 865 866 867 868 869
		assertCursor(cursor, [
			new Selection(10, 10, 10, 9)
		]);

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

A
Alex Dima 已提交
870
	test('column select with keyboard', () => {
871
		let model = Model.createFromString([
A
Alex Dima 已提交
872 873 874 875 876 877 878
			'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");',
879
		].join('\n'));
880
		let cursor = new Cursor(new TestConfiguration(null), model, viewModelHelper(model));
A
Alex Dima 已提交
881 882

		moveTo(cursor, 1, 4, false);
A
Alex Dima 已提交
883
		assertCursor(cursor, new Position(1, 4));
A
Alex Dima 已提交
884

A
Alex Dima 已提交
885
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
886
		assertCursor(cursor, [
A
Alex Dima 已提交
887 888 889
			new Selection(1, 4, 1, 5)
		]);

A
Alex Dima 已提交
890
		CoreCommands.CursorColumnSelectDown.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
891
		assertCursor(cursor, [
A
Alex Dima 已提交
892 893 894 895
			new Selection(1, 4, 1, 5),
			new Selection(2, 4, 2, 5)
		]);

A
Alex Dima 已提交
896
		CoreCommands.CursorColumnSelectDown.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
897
		assertCursor(cursor, [
A
Alex Dima 已提交
898 899 900 901 902
			new Selection(1, 4, 1, 5),
			new Selection(2, 4, 2, 5),
			new Selection(3, 4, 3, 5),
		]);

A
Alex Dima 已提交
903 904 905 906
		CoreCommands.CursorColumnSelectDown.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectDown.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectDown.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectDown.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
907
		assertCursor(cursor, [
A
Alex Dima 已提交
908 909 910 911 912 913 914 915 916
			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),
		]);

A
Alex Dima 已提交
917
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
918
		assertCursor(cursor, [
A
Alex Dima 已提交
919 920 921 922 923 924 925 926 927 928
			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
A
Alex Dima 已提交
929 930 931 932 933 934 935 936 937 938
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
939
		assertCursor(cursor, [
A
Alex Dima 已提交
940 941 942 943 944 945 946 947 948 949
			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
A
Alex Dima 已提交
950 951 952 953 954 955 956 957 958 959
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
960
		assertCursor(cursor, [
A
Alex Dima 已提交
961 962 963 964 965 966 967 968 969 970
			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
A
Alex Dima 已提交
971 972
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
973
		assertCursor(cursor, [
A
Alex Dima 已提交
974 975 976 977 978 979 980 981 982 983
			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
A
Alex Dima 已提交
984 985 986 987
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
988
		assertCursor(cursor, [
A
Alex Dima 已提交
989 990 991 992 993 994 995 996 997 998
			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
A
Alex Dima 已提交
999 1000
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
1001
		assertCursor(cursor, [
A
Alex Dima 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
			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
A
Alex Dima 已提交
1012
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
1013
		assertCursor(cursor, [
A
Alex Dima 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
			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
A
Alex Dima 已提交
1024 1025 1026
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
1027
		assertCursor(cursor, [
A
Alex Dima 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
			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
A
Alex Dima 已提交
1038
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
1039
		assertCursor(cursor, [
A
Alex Dima 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
			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
A
Alex Dima 已提交
1050 1051 1052 1053
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
		CoreCommands.CursorColumnSelectRight.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
1054
		assertCursor(cursor, [
A
Alex Dima 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
			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
A
Alex Dima 已提交
1065
		CoreCommands.CursorColumnSelectLeft.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
1066
		assertCursor(cursor, [
A
Alex Dima 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
			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();
	});
1079 1080
});

1081
class SurroundingMode extends MockMode {
A
Alex Dima 已提交
1082 1083 1084

	private static _id = new LanguageIdentifier('surroundingMode', 3);

1085
	constructor() {
A
Alex Dima 已提交
1086 1087
		super(SurroundingMode._id);
		this._register(LanguageConfigurationRegistry.register(this.getLanguageIdentifier(), {
1088
			autoClosingPairs: [{ open: '(', close: ')' }]
A
Alex Dima 已提交
1089
		}));
1090 1091 1092
	}
}

1093
class OnEnterMode extends MockMode {
A
Alex Dima 已提交
1094 1095
	private static _id = new LanguageIdentifier('onEnterMode', 3);

1096
	constructor(indentAction: IndentAction, outdentCurrentLine?: boolean) {
A
Alex Dima 已提交
1097 1098
		super(OnEnterMode._id);
		this._register(LanguageConfigurationRegistry.register(this.getLanguageIdentifier(), {
1099 1100 1101
			onEnterRules: [{
				beforeText: /.*/,
				action: {
1102 1103
					indentAction: indentAction,
					outdentCurrentLine: outdentCurrentLine
1104
				}
1105
			}]
A
Alex Dima 已提交
1106
		}));
1107 1108 1109
	}
}

1110 1111
class IndentRulesMode extends MockMode {
	private static _id = new LanguageIdentifier('indentRulesMode', 4);
R
rebornix 已提交
1112
	constructor(indentationRules: IndentationRule) {
1113
		super(IndentRulesMode._id);
R
rebornix 已提交
1114 1115 1116 1117 1118 1119
		this._register(LanguageConfigurationRegistry.register(this.getLanguageIdentifier(), {
			indentationRules: indentationRules
		}));
	}
}

1120
suite('Editor Controller - Regression tests', () => {
E
Erich Gamma 已提交
1121
	test('Bug 9121: Auto indent + undo + redo is funky', () => {
A
Alex Dima 已提交
1122 1123 1124 1125
		usingCursor({
			text: [
				''
			],
1126 1127 1128 1129
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
1130 1131
				tabSize: 4,
				trimAutoWhitespace: false
1132
			}
A
Alex Dima 已提交
1133
		}, (model, cursor) => {
1134
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
1135
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n', 'assert1');
E
Erich Gamma 已提交
1136

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

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

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

A
Alex Dima 已提交
1146
			CoreCommands.CursorLeft.runCoreEditorCommand(cursor, {});
A
Alex Dima 已提交
1147
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n\t\n\tx', 'assert5');
E
Erich Gamma 已提交
1148

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

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

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

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

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

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

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

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

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

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

A
Alex Dima 已提交
1181

1182

E
Erich Gamma 已提交
1183
	test('bug #16543: Tab should indent to correct indentation spot immediately', () => {
A
Alex Dima 已提交
1184
		let mode = new OnEnterMode(IndentAction.Indent);
A
Alex Dima 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193
		usingCursor({
			text: [
				'function baz() {',
				'\tfunction hello() { // something here',
				'\t',
				'',
				'\t}',
				'}'
			],
1194 1195 1196 1197
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
1198 1199
				tabSize: 4,
				trimAutoWhitespace: true
1200
			},
A
Alex Dima 已提交
1201
			languageIdentifier: mode.getLanguageIdentifier(),
A
Alex Dima 已提交
1202 1203
		}, (model, cursor) => {
			moveTo(cursor, 4, 1, false);
A
Alex Dima 已提交
1204
			assertCursor(cursor, new Selection(4, 1, 4, 1));
E
Erich Gamma 已提交
1205

1206
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1207 1208
			assert.equal(model.getLineContent(4), '\t\t');
		});
A
Alex Dima 已提交
1209
		mode.dispose();
E
Erich Gamma 已提交
1210 1211
	});

1212
	test('bug #2938 (1): When pressing Tab on white-space only lines, indent straight to the right spot (similar to empty lines)', () => {
A
Alex Dima 已提交
1213
		let mode = new OnEnterMode(IndentAction.Indent);
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
		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
			},
A
Alex Dima 已提交
1230
			languageIdentifier: mode.getLanguageIdentifier(),
1231 1232
		}, (model, cursor) => {
			moveTo(cursor, 4, 2, false);
A
Alex Dima 已提交
1233
			assertCursor(cursor, new Selection(4, 2, 4, 2));
1234 1235 1236 1237

			cursorCommand(cursor, H.Tab, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t\t');
		});
A
Alex Dima 已提交
1238
		mode.dispose();
1239 1240 1241 1242
	});


	test('bug #2938 (2): When pressing Tab on white-space only lines, indent straight to the right spot (similar to empty lines)', () => {
A
Alex Dima 已提交
1243
		let mode = new OnEnterMode(IndentAction.Indent);
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
		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
			},
A
Alex Dima 已提交
1260
			languageIdentifier: mode.getLanguageIdentifier(),
1261 1262
		}, (model, cursor) => {
			moveTo(cursor, 4, 1, false);
A
Alex Dima 已提交
1263
			assertCursor(cursor, new Selection(4, 1, 4, 1));
1264 1265 1266 1267

			cursorCommand(cursor, H.Tab, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t\t');
		});
A
Alex Dima 已提交
1268
		mode.dispose();
1269 1270 1271
	});

	test('bug #2938 (3): When pressing Tab on white-space only lines, indent straight to the right spot (similar to empty lines)', () => {
A
Alex Dima 已提交
1272
		let mode = new OnEnterMode(IndentAction.Indent);
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
		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
			},
A
Alex Dima 已提交
1289
			languageIdentifier: mode.getLanguageIdentifier(),
1290 1291
		}, (model, cursor) => {
			moveTo(cursor, 4, 3, false);
A
Alex Dima 已提交
1292
			assertCursor(cursor, new Selection(4, 3, 4, 3));
1293 1294 1295 1296

			cursorCommand(cursor, H.Tab, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t\t\t');
		});
A
Alex Dima 已提交
1297
		mode.dispose();
1298 1299 1300
	});

	test('bug #2938 (4): When pressing Tab on white-space only lines, indent straight to the right spot (similar to empty lines)', () => {
A
Alex Dima 已提交
1301
		let mode = new OnEnterMode(IndentAction.Indent);
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
		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
			},
A
Alex Dima 已提交
1318
			languageIdentifier: mode.getLanguageIdentifier(),
1319 1320
		}, (model, cursor) => {
			moveTo(cursor, 4, 4, false);
A
Alex Dima 已提交
1321
			assertCursor(cursor, new Selection(4, 4, 4, 4));
1322 1323 1324 1325

			cursorCommand(cursor, H.Tab, null, 'keyboard');
			assert.equal(model.getLineContent(4), '\t\t\t\t\t');
		});
A
Alex Dima 已提交
1326
		mode.dispose();
1327 1328
	});

E
Erich Gamma 已提交
1329
	test('Bug 18276:[editor] Indentation broken when selection is empty', () => {
A
Alex Dima 已提交
1330 1331 1332 1333
		usingCursor({
			text: [
				'function baz() {'
			],
1334 1335 1336 1337
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
1338 1339
				tabSize: 4,
				trimAutoWhitespace: true
1340
			},
A
Alex Dima 已提交
1341 1342
		}, (model, cursor) => {
			moveTo(cursor, 1, 2, false);
A
Alex Dima 已提交
1343
			assertCursor(cursor, new Selection(1, 2, 1, 2));
A
Alex Dima 已提交
1344

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

A
Alex Dima 已提交
1348
			assertCursor(cursor, new Selection(1, 3, 1, 3));
1349
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1350 1351
			assert.equal(model.getLineContent(1), '\tf\tunction baz() {');
		});
E
Erich Gamma 已提交
1352 1353 1354
	});

	test('bug #16815:Shift+Tab doesn\'t go back to tabstop', () => {
A
Alex Dima 已提交
1355
		let mode = new OnEnterMode(IndentAction.IndentOutdent);
A
Alex Dima 已提交
1356 1357 1358 1359
		usingCursor({
			text: [
				'     function baz() {'
			],
A
Alex Dima 已提交
1360
			languageIdentifier: mode.getLanguageIdentifier(),
1361
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1362 1363
		}, (model, cursor) => {
			moveTo(cursor, 1, 6, false);
A
Alex Dima 已提交
1364
			assertCursor(cursor, new Selection(1, 6, 1, 6));
A
Alex Dima 已提交
1365

1366
			cursorCommand(cursor, H.Outdent, null, 'keyboard');
A
Alex Dima 已提交
1367
			assert.equal(model.getLineContent(1), '    function baz() {');
A
Alex Dima 已提交
1368
			assertCursor(cursor, new Selection(1, 5, 1, 5));
A
Alex Dima 已提交
1369
		});
A
Alex Dima 已提交
1370
		mode.dispose();
E
Erich Gamma 已提交
1371 1372 1373
	});

	test('Bug #18293:[regression][editor] Can\'t outdent whitespace line', () => {
A
Alex Dima 已提交
1374 1375 1376 1377
		usingCursor({
			text: [
				'      '
			],
1378
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1379 1380
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
1381
			assertCursor(cursor, new Selection(1, 7, 1, 7));
A
Alex Dima 已提交
1382

1383
			cursorCommand(cursor, H.Outdent, null, 'keyboard');
A
Alex Dima 已提交
1384
			assert.equal(model.getLineContent(1), '    ');
A
Alex Dima 已提交
1385
			assertCursor(cursor, new Selection(1, 5, 1, 5));
A
Alex Dima 已提交
1386
		});
E
Erich Gamma 已提交
1387 1388 1389
	});

	test('Bug #16657: [editor] Tab on empty line of zero indentation moves cursor to position (1,1)', () => {
A
Alex Dima 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
		usingCursor({
			text: [
				'function baz() {',
				'\tfunction hello() { // something here',
				'\t',
				'',
				'\t}',
				'}',
				''
			],
1400 1401 1402 1403
			modelOpts: {
				defaultEOL: DefaultEndOfLine.LF,
				detectIndentation: false,
				insertSpaces: false,
1404 1405
				tabSize: 4,
				trimAutoWhitespace: true
1406
			},
A
Alex Dima 已提交
1407 1408
		}, (model, cursor) => {
			moveTo(cursor, 7, 1, false);
A
Alex Dima 已提交
1409
			assertCursor(cursor, new Selection(7, 1, 7, 1));
A
Alex Dima 已提交
1410

1411
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1412
			assert.equal(model.getLineContent(7), '\t');
A
Alex Dima 已提交
1413
			assertCursor(cursor, new Selection(7, 2, 7, 2));
A
Alex Dima 已提交
1414
		});
E
Erich Gamma 已提交
1415 1416 1417 1418
	});

	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 已提交
1419
		let text = [
E
Erich Gamma 已提交
1420 1421 1422
			'asdasd',
			'qwerty'
		];
1423
		let model = Model.createFromString(text.join('\n'));
1424
		let cursor = new Cursor(new TestConfiguration(null), model, viewModelHelper(model));
E
Erich Gamma 已提交
1425 1426

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

1429
		cursorCommand(cursor, H.Cut, null, 'keyboard');
E
Erich Gamma 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
		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',
			''
		];
1441
		model = Model.createFromString(text.join('\n'));
1442
		cursor = new Cursor(new TestConfiguration(null), model, viewModelHelper(model));
E
Erich Gamma 已提交
1443 1444

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

1447
		cursorCommand(cursor, H.Cut, null, 'keyboard');
E
Erich Gamma 已提交
1448 1449 1450
		assert.equal(model.getLineCount(), 1);
		assert.equal(model.getLineContent(1), 'asdasd');

1451
		cursorCommand(cursor, H.Cut, null, 'keyboard');
E
Erich Gamma 已提交
1452 1453 1454 1455 1456 1457 1458
		assert.equal(model.getLineCount(), 1);
		assert.equal(model.getLineContent(1), '');

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

1459
	test('Bug #11476: Double bracket surrounding + undo is broken', () => {
A
Alex Dima 已提交
1460
		let mode = new SurroundingMode();
1461 1462 1463 1464
		usingCursor({
			text: [
				'hello'
			],
A
Alex Dima 已提交
1465
			languageIdentifier: mode.getLanguageIdentifier(),
1466
			modelOpts: { tabSize: 4, insertSpaces: true, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
1467 1468 1469
		}, (model, cursor) => {
			moveTo(cursor, 1, 3, false);
			moveTo(cursor, 1, 5, true);
A
Alex Dima 已提交
1470
			assertCursor(cursor, new Selection(1, 3, 1, 5));
1471

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

1475
			cursorCommand(cursor, H.Type, { text: '(' }, 'keyboard');
A
Alex Dima 已提交
1476
			assertCursor(cursor, new Selection(1, 5, 1, 7));
1477
		});
A
Alex Dima 已提交
1478
		mode.dispose();
1479 1480 1481
	});

	test('issue #1140: Backspace stops prematurely', () => {
A
Alex Dima 已提交
1482
		let mode = new SurroundingMode();
1483 1484 1485 1486 1487 1488
		usingCursor({
			text: [
				'function baz() {',
				'  return 1;',
				'};'
			],
A
Alex Dima 已提交
1489
			languageIdentifier: mode.getLanguageIdentifier(),
1490
			modelOpts: { tabSize: 4, insertSpaces: true, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
1491 1492 1493
		}, (model, cursor) => {
			moveTo(cursor, 3, 2, false);
			moveTo(cursor, 1, 14, true);
A
Alex Dima 已提交
1494
			assertCursor(cursor, new Selection(3, 2, 1, 14));
1495 1496

			cursorCommand(cursor, H.DeleteLeft);
A
Alex Dima 已提交
1497
			assertCursor(cursor, new Selection(1, 14, 1, 14));
1498 1499 1500
			assert.equal(model.getLineCount(), 1);
			assert.equal(model.getLineContent(1), 'function baz(;');
		});
A
Alex Dima 已提交
1501
		mode.dispose();
1502
	});
A
Alex Dima 已提交
1503

1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
	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), '');
		});
	});
1521

A
Alex Dima 已提交
1522 1523 1524 1525 1526 1527 1528
	test('issue #3071: Investigate why undo stack gets corrupted', () => {
		usingCursor({
			text: [
				'some lines',
				'and more lines',
				'just some text',
			],
A
Alex Dima 已提交
1529
			languageIdentifier: null,
1530
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1531 1532 1533 1534 1535
		}, (model, cursor) => {
			moveTo(cursor, 1, 1, false);
			moveTo(cursor, 3, 4, true);

			let isFirst = true;
A
Alex Dima 已提交
1536
			model.onDidChangeContent(() => {
A
Alex Dima 已提交
1537 1538
				if (isFirst) {
					isFirst = false;
1539
					cursorCommand(cursor, H.Type, { text: '\t' }, 'keyboard');
A
Alex Dima 已提交
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
				}
			});

			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');
		});
	});

1564 1565 1566 1567 1568 1569 1570
	test('issue #12950: Cannot Double Click To Insert Emoji Using OSX Emoji Panel', () => {
		usingCursor({
			text: [
				'some lines',
				'and more lines',
				'just some text',
			],
A
Alex Dima 已提交
1571
			languageIdentifier: null,
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
			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 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594
	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};',
				'}',
			],
1595
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1596 1597 1598 1599 1600 1601 1602
		}, (model, cursor) => {
			moveTo(cursor, 3, 2, false);
			cursorCommand(cursor, H.Tab);
			assert.equal(model.getLineContent(3), '\t    \tx: 3');
		});
	});

1603 1604 1605
	test('issue #4312: trying to type a tab character over a sequence of spaces results in unexpected behaviour', () => {
		usingCursor({
			text: [
1606
				'var foo = 123;       // this is a comment',
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
				'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');
		});
	});

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
	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) {
A
Alex Dima 已提交
1628
				let args = {
1629 1630 1631
					position: {
						lineNumber: 1,
						column: col
A
Alex Dima 已提交
1632 1633 1634 1635 1636 1637 1638
					}
				};
				if (col === 1) {
					CoreCommands.WordSelect.runCoreEditorCommand(cursor, args);
				} else {
					CoreCommands.WordSelectDrag.runCoreEditorCommand(cursor, args);
				}
1639 1640 1641 1642 1643

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

J
Johannes Rieken 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652
			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);
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
			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);
		});
	});
1696

1697 1698 1699 1700 1701 1702 1703 1704
	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 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
			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 });
1716 1717 1718 1719 1720 1721 1722 1723 1724

			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));
		});
	});
A
Alex Dima 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753

	test('issue #23913: Greater than 1000+ multi cursor typing replacement text appears inverted, lines begin to drop off selection', () => {
		const LINE_CNT = 2000;

		let text = [];
		for (let i = 0; i < LINE_CNT; i++) {
			text[i] = 'asd';
		}
		usingCursor({
			text: text
		}, (model, cursor) => {

			let selections: Selection[] = [];
			for (let i = 0; i < LINE_CNT; i++) {
				selections[i] = new Selection(i + 1, 1, i + 1, 1);
			}
			cursor.setSelections('test', selections);

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

			for (let i = 0; i < LINE_CNT; i++) {
				assert.equal(model.getLineContent(i + 1), 'nnasd', 'line #' + (i + 1));
			}

			assert.equal(cursor.getSelections().length, LINE_CNT);
			assert.equal(cursor.getSelections()[LINE_CNT - 1].startLineNumber, LINE_CNT);
		});
	});
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
});

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'
			],
1767
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
1768
		}, (model, cursor) => {
A
Alex Dima 已提交
1769
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(1, 21) }, 'keyboard');
1770
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
1771 1772 1773 1774 1775
			assert.equal(model.getLineContent(1), '    \tMy First Line\t ');
			assert.equal(model.getLineContent(2), '        ');
		});
	});

E
Erich Gamma 已提交
1776
	test('Cursor honors insertSpaces configuration on tab', () => {
A
Alex Dima 已提交
1777 1778 1779 1780 1781 1782 1783 1784
		usingCursor({
			text: [
				'    \tMy First Line\t ',
				'My Second Line123',
				'    Third Line',
				'',
				'1'
			],
1785
			modelOpts: { insertSpaces: true, tabSize: 13, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1786 1787
		}, (model, cursor) => {
			// Tab on column 1
A
Alex Dima 已提交
1788
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(2, 1) }, 'keyboard');
1789
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1790
			assert.equal(model.getLineContent(2), '             My Second Line123');
1791
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
1792 1793 1794

			// Tab on column 2
			assert.equal(model.getLineContent(2), 'My Second Line123');
A
Alex Dima 已提交
1795
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(2, 2) }, 'keyboard');
1796
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1797
			assert.equal(model.getLineContent(2), 'M            y Second Line123');
1798
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
1799 1800 1801

			// Tab on column 3
			assert.equal(model.getLineContent(2), 'My Second Line123');
A
Alex Dima 已提交
1802
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(2, 3) }, 'keyboard');
1803
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1804
			assert.equal(model.getLineContent(2), 'My            Second Line123');
1805
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
1806 1807 1808

			// Tab on column 4
			assert.equal(model.getLineContent(2), 'My Second Line123');
A
Alex Dima 已提交
1809
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(2, 4) }, 'keyboard');
1810
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1811
			assert.equal(model.getLineContent(2), 'My           Second Line123');
1812
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
1813 1814 1815

			// Tab on column 5
			assert.equal(model.getLineContent(2), 'My Second Line123');
A
Alex Dima 已提交
1816
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(2, 5) }, 'keyboard');
1817
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1818
			assert.equal(model.getLineContent(2), 'My S         econd Line123');
1819
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
1820 1821 1822

			// Tab on column 5
			assert.equal(model.getLineContent(2), 'My Second Line123');
A
Alex Dima 已提交
1823
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(2, 5) }, 'keyboard');
1824
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1825
			assert.equal(model.getLineContent(2), 'My S         econd Line123');
1826
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
1827 1828 1829

			// Tab on column 13
			assert.equal(model.getLineContent(2), 'My Second Line123');
A
Alex Dima 已提交
1830
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(2, 13) }, 'keyboard');
1831
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1832
			assert.equal(model.getLineContent(2), 'My Second Li ne123');
1833
			cursorCommand(cursor, H.Undo, null, 'keyboard');
A
Alex Dima 已提交
1834 1835 1836

			// Tab on column 14
			assert.equal(model.getLineContent(2), 'My Second Line123');
A
Alex Dima 已提交
1837
			cursorCommand(cursor, CoreCommands.MoveTo.id, { position: new Position(2, 14) }, 'keyboard');
1838
			cursorCommand(cursor, H.Tab, null, 'keyboard');
A
Alex Dima 已提交
1839 1840
			assert.equal(model.getLineContent(2), 'My Second Lin             e123');
		});
E
Erich Gamma 已提交
1841 1842 1843
	});

	test('Enter auto-indents with insertSpaces setting 1', () => {
A
Alex Dima 已提交
1844
		let mode = new OnEnterMode(IndentAction.Indent);
A
Alex Dima 已提交
1845 1846 1847 1848
		usingCursor({
			text: [
				'\thello'
			],
A
Alex Dima 已提交
1849
			languageIdentifier: mode.getLanguageIdentifier(),
1850
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1851 1852
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
1853
			assertCursor(cursor, new Selection(1, 7, 1, 7));
A
Alex Dima 已提交
1854

1855
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
1856 1857
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thello\r\n        ');
		});
A
Alex Dima 已提交
1858
		mode.dispose();
E
Erich Gamma 已提交
1859 1860 1861
	});

	test('Enter auto-indents with insertSpaces setting 2', () => {
A
Alex Dima 已提交
1862
		let mode = new OnEnterMode(IndentAction.None);
A
Alex Dima 已提交
1863 1864 1865 1866
		usingCursor({
			text: [
				'\thello'
			],
A
Alex Dima 已提交
1867
			languageIdentifier: mode.getLanguageIdentifier(),
1868
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1869 1870
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
1871
			assertCursor(cursor, new Selection(1, 7, 1, 7));
A
Alex Dima 已提交
1872

1873
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
1874 1875
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thello\r\n    ');
		});
A
Alex Dima 已提交
1876
		mode.dispose();
E
Erich Gamma 已提交
1877 1878 1879
	});

	test('Enter auto-indents with insertSpaces setting 3', () => {
A
Alex Dima 已提交
1880
		let mode = new OnEnterMode(IndentAction.IndentOutdent);
A
Alex Dima 已提交
1881 1882 1883 1884
		usingCursor({
			text: [
				'\thell()'
			],
A
Alex Dima 已提交
1885
			languageIdentifier: mode.getLanguageIdentifier(),
1886
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
A
Alex Dima 已提交
1887 1888
		}, (model, cursor) => {
			moveTo(cursor, 1, 7, false);
A
Alex Dima 已提交
1889
			assertCursor(cursor, new Selection(1, 7, 1, 7));
A
Alex Dima 已提交
1890

1891
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
1892 1893
			assert.equal(model.getValue(EndOfLinePreference.CRLF), '\thell(\r\n        \r\n    )');
		});
A
Alex Dima 已提交
1894
		mode.dispose();
E
Erich Gamma 已提交
1895 1896 1897
	});

	test('Insert line before', () => {
J
Johannes Rieken 已提交
1898
		let testInsertLineBefore = (lineNumber: number, column: number, callback: (model: Model, cursor: Cursor) => void) => {
A
Alex Dima 已提交
1899 1900 1901 1902 1903 1904 1905
			usingCursor({
				text: [
					'First line',
					'Second line',
					'Third line'
				],
			}, (model, cursor) => {
E
Erich Gamma 已提交
1906
				moveTo(cursor, lineNumber, column, false);
A
Alex Dima 已提交
1907
				assertCursor(cursor, new Position(lineNumber, column));
E
Erich Gamma 已提交
1908

1909
				cursorCommand(cursor, H.LineInsertBefore, null, 'keyboard');
E
Erich Gamma 已提交
1910 1911 1912 1913 1914
				callback(model, cursor);
			});
		};

		testInsertLineBefore(1, 3, (model, cursor) => {
A
Alex Dima 已提交
1915
			assertCursor(cursor, new Selection(1, 1, 1, 1));
E
Erich Gamma 已提交
1916 1917 1918 1919 1920 1921 1922
			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 已提交
1923
			assertCursor(cursor, new Selection(2, 1, 2, 1));
E
Erich Gamma 已提交
1924 1925 1926 1927 1928 1929 1930
			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 已提交
1931
			assertCursor(cursor, new Selection(3, 1, 3, 1));
E
Erich Gamma 已提交
1932 1933 1934 1935 1936 1937 1938 1939
			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 已提交
1940
		let testInsertLineAfter = (lineNumber: number, column: number, callback: (model: Model, cursor: Cursor) => void) => {
A
Alex Dima 已提交
1941 1942 1943 1944 1945 1946 1947
			usingCursor({
				text: [
					'First line',
					'Second line',
					'Third line'
				],
			}, (model, cursor) => {
E
Erich Gamma 已提交
1948
				moveTo(cursor, lineNumber, column, false);
A
Alex Dima 已提交
1949
				assertCursor(cursor, new Position(lineNumber, column));
E
Erich Gamma 已提交
1950

1951
				cursorCommand(cursor, H.LineInsertAfter, null, 'keyboard');
E
Erich Gamma 已提交
1952 1953 1954 1955 1956
				callback(model, cursor);
			});
		};

		testInsertLineAfter(1, 3, (model, cursor) => {
A
Alex Dima 已提交
1957
			assertCursor(cursor, new Selection(2, 1, 2, 1));
E
Erich Gamma 已提交
1958 1959 1960 1961 1962 1963 1964
			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 已提交
1965
			assertCursor(cursor, new Selection(3, 1, 3, 1));
E
Erich Gamma 已提交
1966 1967 1968 1969 1970 1971 1972
			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 已提交
1973
			assertCursor(cursor, new Selection(4, 1, 4, 1));
E
Erich Gamma 已提交
1974 1975 1976 1977 1978 1979
			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), '');
		});
	});
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996

	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);
1997
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
1998 1999 2000 2001
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '    ');

			// Try to enter again, we should trimmed previous line
2002
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
			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);
2023
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2024 2025 2026
			assert.equal(model.getLineContent(1), '    ');
			assert.equal(model.getLineContent(2), '    ');

2027
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2028 2029 2030 2031 2032 2033
			assert.equal(model.getLineContent(1), '    ');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), '    ');
		});
	});

2034
	test('issue #6862: Editor removes auto inserted indentation when formatting on type', () => {
A
Alex Dima 已提交
2035
		let mode = new OnEnterMode(IndentAction.IndentOutdent);
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
		usingCursor({
			text: [
				'function foo (params: string) {}'
			],
			modelOpts: {
				insertSpaces: true,
				tabSize: 4,
				detectIndentation: false,
				defaultEOL: DefaultEndOfLine.LF,
				trimAutoWhitespace: true
			},
A
Alex Dima 已提交
2047
			languageIdentifier: mode.getLanguageIdentifier(),
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
		}, (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), '}');
		});
A
Alex Dima 已提交
2076
		mode.dispose();
2077 2078
	});

2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
	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);
2098
			cursorCommand(cursor, H.Tab, null, 'keyboard');
2099 2100 2101 2102 2103 2104 2105
			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);
2106
			cursorCommand(cursor, H.Tab, null, 'keyboard');
2107 2108 2109 2110 2111 2112 2113
			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));
2114
			cursorCommand(cursor, H.Type, { text: 'something' }, 'keyboard');
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
			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);
2139
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2140 2141 2142 2143
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '    ');

			// Try to enter again, we should trimmed previous line
2144
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2145 2146 2147 2148 2149
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), '    ');

			// More whitespaces
2150
			cursorCommand(cursor, H.Tab, null, 'keyboard');
2151 2152 2153 2154 2155
			assert.equal(model.getLineContent(1), '    some  line abc  ');
			assert.equal(model.getLineContent(2), '');
			assert.equal(model.getLineContent(3), '        ');

			// Enter and verify that trimmed again
2156
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2157 2158 2159 2160 2161 2162 2163
			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);
2164
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2165 2166 2167 2168 2169 2170 2171 2172 2173
			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);
2174
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2175 2176
			assert.equal(model.getLineContent(1), '    ');
			assert.equal(model.getLineContent(2), '    ');
2177
			assert.equal(model.getLineContent(3), '    ');
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
			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
2196 2197 2198
			},
			editorOpts: {
				useTabStops: false
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
			}
		}, (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
2221 2222 2223
			},
			editorOpts: {
				useTabStops: true
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
			}
		}, (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) => {
2293
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2294 2295 2296 2297 2298
			assert.equal(model.getValue(EndOfLinePreference.LF), '\n', 'assert1');

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

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

J
Johannes Rieken 已提交
2302
			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2303 2304 2305 2306 2307
			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');

A
Alex Dima 已提交
2308
			CoreCommands.CursorLeft.runCoreEditorCommand(cursor, {});
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
			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 已提交
2345 2346
});

2347 2348
suite('Editor Controller - Indentation Rules', () => {
	let mode = new IndentRulesMode({
2349
		decreaseIndentPattern: /^\s*((?!\S.*\/[*]).*[*]\/\s*)?[})\]]|^\s*(case\b.*|default):\s*(\/\/.*|\/[*].*[*]\/\s*)?$/,
A
Alex Dima 已提交
2350
		increaseIndentPattern: /(\{[^}"'`]*|\([^)"']*|\[[^\]"']*|^\s*(\{\}|\(\)|\[\]|(case\b.*|default):))\s*(\/\/.*|\/[*].*[*]\/\s*)?$/,
2351 2352
		indentNextLinePattern: /^\s*(for|while|if|else)\b(?!.*[;{}]\s*(\/\/.*|\/[*].*[*]\/\s*)?$)/,
		unIndentedLinePattern: /^(?!.*([;{}]|\S:)\s*(\/\/.*|\/[*].*[*]\/\s*)?$)(?!.*(\{[^}"']*|\([^)"']*|\[[^\]"']*|^\s*(\{\}|\(\)|\[\]|(case\b.*|default):))\s*(\/\/.*|\/[*].*[*]\/\s*)?$)(?!^\s*((?!\S.*\/[*]).*[*]\/\s*)?[})\]]|^\s*(case\b.*|default):\s*(\/\/.*|\/[*].*[*]\/\s*)?$)(?!^\s*(for|while|if|else)\b(?!.*[;{}]\s*(\/\/.*|\/[*].*[*]\/\s*)?$))/
2353 2354
	});

2355 2356
	let emptyRulesMode = new OnEnterMode(IndentAction.None);

2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
	test('Enter honors increaseIndentPattern', () => {
		usingCursor({
			text: [
				'if (true) {',
				'\tif (true) {'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 1, 12, false);
			assertCursor(cursor, new Selection(1, 12, 1, 12));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(2, 2, 2, 2));

			moveTo(cursor, 3, 13, false);
			assertCursor(cursor, new Selection(3, 13, 3, 13));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(4, 3, 4, 3));
		});
	});

	test('Enter honors decreaseIndentPattern', () => {
		usingCursor({
			text: [
				'if (true) {',
2384
				'\t}'
2385 2386 2387 2388
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
2389 2390
			moveTo(cursor, 2, 3, false);
			assertCursor(cursor, new Selection(2, 3, 2, 3));
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475

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

	test('Enter honors unIndentedLinePattern', () => {
		usingCursor({
			text: [
				'if (true) {',
				'\t\t\treturn true'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 2, 15, false);
			assertCursor(cursor, new Selection(2, 15, 2, 15));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(3, 2, 3, 2));
		});
	});

	test('Enter honors indentNextLinePattern', () => {
		usingCursor({
			text: [
				'if (true)',
				'\treturn true;',
				'if (true)',
				'\t\t\t\treturn true'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 2, 14, false);
			assertCursor(cursor, new Selection(2, 14, 2, 14));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(3, 1, 3, 1));

			moveTo(cursor, 5, 16, false);
			assertCursor(cursor, new Selection(5, 16, 5, 16));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(6, 2, 6, 2));
		});
	});

	test('Enter adjusts indentation of current line 1', () => {
		usingCursor({
			text: [
				'if (true) {',
				'\tif (true) {',
				'\t\treturn true;',
				'\t\t}}'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 4, 4, false);
			assertCursor(cursor, new Selection(4, 4, 4, 4));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(5, 1, 5, 1));
			assert.equal(model.getLineContent(4), '\t}', '001');
		});
	});

	test('Enter adjusts indentation of current line 2', () => {
		usingCursor({
			text: [
				'if (true) {',
				'\tif (true) {',
				'\t\treturn true;',
				'}}'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 4, 2, false);
			assertCursor(cursor, new Selection(4, 2, 4, 2));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(5, 1, 5, 1));
2476
			assert.equal(model.getLineContent(4), '}', '001');
2477
		});
R
rebornix 已提交
2478 2479 2480 2481 2482 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 2513 2514 2515 2516 2517 2518 2519 2520
	});

	test('Enter supports selection 1', () => {
		usingCursor({
			text: [
				'if (true) {',
				'\tif (true) {',
				'\t\treturn true;',
				'\t\t}a}'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 4, 4, false);
			moveTo(cursor, 4, 5, true);
			assertCursor(cursor, new Selection(4, 4, 4, 5));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(5, 1, 5, 1));
			assert.equal(model.getLineContent(4), '\t}', '001');
		});
	});

	test('Enter supports selection 2', () => {
		usingCursor({
			text: [
				'if (true) {',
				'\tif (true) {'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 2, 12, false);
			moveTo(cursor, 2, 13, true);
			assertCursor(cursor, new Selection(2, 12, 2, 13));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(3, 3, 3, 3));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(4, 3, 4, 3));
		});
	});
R
rebornix 已提交
2521

2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
	test('Enter honors tabSize and insertSpaces 1', () => {
		usingCursor({
			text: [
				'if (true) {',
				'\tif (true) {'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 1, 12, false);
			assertCursor(cursor, new Selection(1, 12, 1, 12));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(2, 5, 2, 5));

			moveTo(cursor, 3, 13, false);
			assertCursor(cursor, new Selection(3, 13, 3, 13));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(4, 9, 4, 9));
		});
	});

	test('Enter honors tabSize and insertSpaces 2', () => {
		usingCursor({
			text: [
				'if (true) {',
				'    if (true) {'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: true, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 1, 12, false);
			assertCursor(cursor, new Selection(1, 12, 1, 12));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(2, 5, 2, 5));

			moveTo(cursor, 3, 16, false);
			assertCursor(cursor, new Selection(3, 16, 3, 16));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
2564
			assert.equal(model.getLineContent(3), '    if (true) {');
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
			assertCursor(cursor, new Selection(4, 9, 4, 9));
		});
	});

	test('Enter honors tabSize and insertSpaces 3', () => {
		usingCursor({
			text: [
				'if (true) {',
				'    if (true) {'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 1, 12, false);
			assertCursor(cursor, new Selection(1, 12, 1, 12));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(2, 2, 2, 2));

			moveTo(cursor, 3, 16, false);
			assertCursor(cursor, new Selection(3, 16, 3, 16));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
A
Alex Dima 已提交
2588
			assert.equal(model.getLineContent(3), '    if (true) {');
2589 2590 2591 2592
			assertCursor(cursor, new Selection(4, 3, 4, 3));
		});
	});

2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
	test('Enter supports intentional indentation', () => {
		usingCursor({
			text: [
				'\tif (true) {',
				'\t\tswitch(true) {',
				'\t\t\tcase true:',
				'\t\t\t\tbreak;',
				'\t\t}',
				'\t}'
			],
			languageIdentifier: mode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 5, 4, false);
			assertCursor(cursor, new Selection(5, 4, 5, 4));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assert.equal(model.getLineContent(5), '\t\t}');
			assertCursor(cursor, new Selection(6, 3, 6, 3));
		});
	});

R
rebornix 已提交
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
	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
			},
			languageIdentifier: mode.getLanguageIdentifier(),
		}, (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
			},
			languageIdentifier: mode.getLanguageIdentifier(),
		}, (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));
		});
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
	});

	test('onEnter works if there are no indentation rules', () => {
		usingCursor({
			text: [
				'<?',
				'\tif (true) {',
				'\t\techo $hi;',
				'\t\techo $bye;',
				'\t}',
				'?>'
			],
			languageIdentifier: emptyRulesMode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 5, 3, false);
			assertCursor(cursor, new Selection(5, 3, 5, 3));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assert.equal(model.getLineContent(6), '\t');
			assertCursor(cursor, new Selection(6, 2, 6, 2));
			assert.equal(model.getLineContent(5), '\t}');
		});
	});

	test('onEnter works if there are no indentation rules 2', () => {
		usingCursor({
			text: [
				'	if (5)',
				'		return 5;',
				'	'
			],
			languageIdentifier: emptyRulesMode.getLanguageIdentifier(),
			modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
		}, (model, cursor) => {
			moveTo(cursor, 3, 2, false);
			assertCursor(cursor, new Selection(3, 2, 3, 2));

			cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
			assertCursor(cursor, new Selection(4, 2, 4, 2));
			assert.equal(model.getLineContent(4), '\t');
		});
R
rebornix 已提交
2709
	});
2710
});
2711

E
Erich Gamma 已提交
2712
interface ICursorOpts {
A
Alex Dima 已提交
2713
	text: string[];
A
Alex Dima 已提交
2714
	languageIdentifier?: LanguageIdentifier;
2715
	modelOpts?: ITextModelCreationOptions;
2716
	editorOpts?: IEditorOptions;
E
Erich Gamma 已提交
2717 2718
}

J
Johannes Rieken 已提交
2719
function usingCursor(opts: ICursorOpts, callback: (model: Model, cursor: Cursor) => void): void {
A
Alex Dima 已提交
2720
	let model = Model.createFromString(opts.text.join('\n'), opts.modelOpts, opts.languageIdentifier);
2721
	let config = new TestConfiguration(opts.editorOpts);
2722
	let cursor = new Cursor(config, model, viewModelHelper(model));
E
Erich Gamma 已提交
2723 2724 2725 2726 2727 2728

	callback(model, cursor);

	cursor.dispose();
	config.dispose();
	model.dispose();
2729
}
2730 2731

class ElectricCharMode extends MockMode {
A
Alex Dima 已提交
2732 2733 2734

	private static _id = new LanguageIdentifier('electricCharMode', 3);

2735
	constructor() {
A
Alex Dima 已提交
2736 2737
		super(ElectricCharMode._id);
		this._register(LanguageConfigurationRegistry.register(this.getLanguageIdentifier(), {
2738 2739 2740 2741 2742 2743 2744 2745
			__electricCharacterSupport: {
				docComment: { open: '/**', close: ' */' }
			},
			brackets: [
				['{', '}'],
				['[', ']'],
				['(', ')']
			]
A
Alex Dima 已提交
2746
		}));
2747 2748 2749 2750 2751
	}
}

suite('ElectricCharacter', () => {
	test('does nothing if no electric char', () => {
A
Alex Dima 已提交
2752
		let mode = new ElectricCharMode();
2753 2754 2755 2756 2757
		usingCursor({
			text: [
				'  if (a) {',
				''
			],
A
Alex Dima 已提交
2758
			languageIdentifier: mode.getLanguageIdentifier()
2759 2760 2761 2762 2763
		}, (model, cursor) => {
			moveTo(cursor, 2, 1);
			cursorCommand(cursor, H.Type, { text: '*' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '*');
		});
A
Alex Dima 已提交
2764
		mode.dispose();
2765 2766 2767
	});

	test('indents in order to match bracket', () => {
A
Alex Dima 已提交
2768
		let mode = new ElectricCharMode();
2769 2770 2771 2772 2773
		usingCursor({
			text: [
				'  if (a) {',
				''
			],
A
Alex Dima 已提交
2774
			languageIdentifier: mode.getLanguageIdentifier()
2775 2776 2777 2778 2779
		}, (model, cursor) => {
			moveTo(cursor, 2, 1);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  }');
		});
A
Alex Dima 已提交
2780
		mode.dispose();
2781 2782 2783
	});

	test('unindents in order to match bracket', () => {
A
Alex Dima 已提交
2784
		let mode = new ElectricCharMode();
2785 2786 2787 2788 2789
		usingCursor({
			text: [
				'  if (a) {',
				'    '
			],
A
Alex Dima 已提交
2790
			languageIdentifier: mode.getLanguageIdentifier()
2791 2792 2793 2794 2795
		}, (model, cursor) => {
			moveTo(cursor, 2, 5);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  }');
		});
A
Alex Dima 已提交
2796
		mode.dispose();
2797 2798 2799
	});

	test('matches with correct bracket', () => {
A
Alex Dima 已提交
2800
		let mode = new ElectricCharMode();
2801 2802 2803 2804 2805 2806 2807
		usingCursor({
			text: [
				'  if (a) {',
				'    if (b) {',
				'    }',
				'    '
			],
A
Alex Dima 已提交
2808
			languageIdentifier: mode.getLanguageIdentifier()
2809 2810 2811 2812 2813
		}, (model, cursor) => {
			moveTo(cursor, 4, 1);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(4), '  }    ');
		});
A
Alex Dima 已提交
2814
		mode.dispose();
2815 2816 2817
	});

	test('does nothing if bracket does not match', () => {
A
Alex Dima 已提交
2818
		let mode = new ElectricCharMode();
2819 2820 2821 2822 2823 2824 2825
		usingCursor({
			text: [
				'  if (a) {',
				'    if (b) {',
				'    }',
				'  }  '
			],
A
Alex Dima 已提交
2826
			languageIdentifier: mode.getLanguageIdentifier()
2827 2828 2829 2830 2831
		}, (model, cursor) => {
			moveTo(cursor, 4, 6);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(4), '  }  }');
		});
A
Alex Dima 已提交
2832
		mode.dispose();
2833 2834 2835
	});

	test('matches bracket even in line with content', () => {
A
Alex Dima 已提交
2836
		let mode = new ElectricCharMode();
2837 2838 2839 2840 2841
		usingCursor({
			text: [
				'  if (a) {',
				'// hello'
			],
A
Alex Dima 已提交
2842
			languageIdentifier: mode.getLanguageIdentifier()
2843 2844 2845 2846 2847
		}, (model, cursor) => {
			moveTo(cursor, 2, 1);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  }// hello');
		});
A
Alex Dima 已提交
2848
		mode.dispose();
2849 2850 2851
	});

	test('is no-op if bracket is lined up', () => {
A
Alex Dima 已提交
2852
		let mode = new ElectricCharMode();
2853 2854 2855 2856 2857
		usingCursor({
			text: [
				'  if (a) {',
				'  '
			],
A
Alex Dima 已提交
2858
			languageIdentifier: mode.getLanguageIdentifier()
2859 2860 2861 2862 2863
		}, (model, cursor) => {
			moveTo(cursor, 2, 3);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  }');
		});
A
Alex Dima 已提交
2864
		mode.dispose();
2865 2866 2867
	});

	test('is no-op if there is non-whitespace text before', () => {
A
Alex Dima 已提交
2868
		let mode = new ElectricCharMode();
2869 2870 2871 2872 2873
		usingCursor({
			text: [
				'  if (a) {',
				'a'
			],
A
Alex Dima 已提交
2874
			languageIdentifier: mode.getLanguageIdentifier()
2875 2876 2877
		}, (model, cursor) => {
			moveTo(cursor, 2, 2);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
2878
			assert.deepEqual(model.getLineContent(2), 'a}');
2879
		});
A
Alex Dima 已提交
2880
		mode.dispose();
2881 2882
	});

2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
	test('is no-op if pairs are all matched before', () => {
		let mode = new ElectricCharMode();
		usingCursor({
			text: [
				'foo(() => {',
				'  ( 1 + 2 ) ',
				'})'
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {
			moveTo(cursor, 2, 13);
			cursorCommand(cursor, H.Type, { text: '*' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '  ( 1 + 2 ) *');
		});
		mode.dispose();
	});

2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
	test('is no-op if matching bracket is on the same line', () => {
		let mode = new ElectricCharMode();
		usingCursor({
			text: [
				'(div',
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {
			moveTo(cursor, 1, 5);
			let changeText: string = null;
			model.onDidChangeContent(e => {
2911
				changeText = e.changes[0].text;
2912 2913 2914 2915 2916 2917 2918 2919
			});
			cursorCommand(cursor, H.Type, { text: ')' }, 'keyboard');
			assert.deepEqual(model.getLineContent(1), '(div)');
			assert.deepEqual(changeText, ')');
		});
		mode.dispose();
	});

2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
	test('is no-op if the line has other content', () => {
		let mode = new ElectricCharMode();
		usingCursor({
			text: [
				'Math.max(',
				'\t2',
				'\t3'
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {
			moveTo(cursor, 3, 3);
			cursorCommand(cursor, H.Type, { text: ')' }, 'keyboard');
			assert.deepEqual(model.getLineContent(3), '\t3)');
		});
		mode.dispose();
	});

2937
	test('appends text', () => {
A
Alex Dima 已提交
2938
		let mode = new ElectricCharMode();
2939 2940 2941 2942 2943
		usingCursor({
			text: [
				'  if (a) {',
				'/*'
			],
A
Alex Dima 已提交
2944
			languageIdentifier: mode.getLanguageIdentifier()
2945 2946 2947 2948 2949
		}, (model, cursor) => {
			moveTo(cursor, 2, 3);
			cursorCommand(cursor, H.Type, { text: '*' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '/** */');
		});
A
Alex Dima 已提交
2950
		mode.dispose();
2951 2952 2953
	});

	test('appends text 2', () => {
A
Alex Dima 已提交
2954
		let mode = new ElectricCharMode();
2955 2956 2957 2958 2959
		usingCursor({
			text: [
				'  if (a) {',
				'  /*'
			],
A
Alex Dima 已提交
2960
			languageIdentifier: mode.getLanguageIdentifier()
2961 2962 2963
		}, (model, cursor) => {
			moveTo(cursor, 2, 5);
			cursorCommand(cursor, H.Type, { text: '*' }, 'keyboard');
A
Alex Dima 已提交
2964
			assert.deepEqual(model.getLineContent(2), '  /** */');
2965
		});
A
Alex Dima 已提交
2966
		mode.dispose();
A
Alex Dima 已提交
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
	});

	test('issue #23711: Replacing selected text with )]} fails to delete old text with backwards-dragged selection', () => {
		let mode = new ElectricCharMode();
		usingCursor({
			text: [
				'{',
				'word'
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {
			moveTo(cursor, 2, 5);
			moveTo(cursor, 2, 1, true);
			cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
			assert.deepEqual(model.getLineContent(2), '}');
		});
		mode.dispose();
2984 2985
	});
});
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100

suite('autoClosingPairs', () => {

	class AutoClosingMode extends MockMode {

		private static _id = new LanguageIdentifier('autoClosingMode', 3);

		constructor() {
			super(AutoClosingMode._id);
			this._register(LanguageConfigurationRegistry.register(this.getLanguageIdentifier(), {
				autoClosingPairs: [
					{ open: '{', close: '}' },
					{ open: '[', close: ']' },
					{ open: '(', close: ')' },
					{ open: '\'', close: '\'', notIn: ['string', 'comment'] },
					{ open: '\"', close: '\"', notIn: ['string'] },
					{ open: '`', close: '`', notIn: ['string', 'comment'] },
					{ open: '/**', close: ' */', notIn: ['string'] }
				],
			}));
		}
	}

	const enum ColumnType {
		Normal = 0,
		Special1 = 1,
		Special2 = 2
	}

	function extractSpecialColumns(maxColumn: number, annotatedLine: string): ColumnType[] {
		let result: ColumnType[] = [];
		for (let j = 1; j <= maxColumn; j++) {
			result[j] = ColumnType.Normal;
		}
		let column = 1;
		for (let j = 0; j < annotatedLine.length; j++) {
			if (annotatedLine.charAt(j) === '|') {
				result[column] = ColumnType.Special1;
			} else if (annotatedLine.charAt(j) === '!') {
				result[column] = ColumnType.Special2;
			} else {
				column++;
			}
		}
		return result;
	}

	function assertType(model: Model, cursor: Cursor, lineNumber: number, column: number, chr: string, expectedInsert: string, message: string): void {
		let lineContent = model.getLineContent(lineNumber);
		let expected = lineContent.substr(0, column - 1) + expectedInsert + lineContent.substr(column - 1);
		moveTo(cursor, lineNumber, column);
		cursorCommand(cursor, H.Type, { text: chr }, 'keyboard');
		assert.deepEqual(model.getLineContent(lineNumber), expected, message);
		cursorCommand(cursor, H.Undo);
	}

	test('open parens', () => {
		let mode = new AutoClosingMode();
		usingCursor({
			text: [
				'var a = [];',
				'var b = `asd`;',
				'var c = \'asd\';',
				'var d = "asd";',
				'var e = /*3*/	3;',
				'var f = /** 3 */3;',
				'var g = (3+5);',
				'var h = { a: \'value\' };',
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {

			let autoClosePositions = [
				'var| a| =| [|];|',
				'var| b| =| `asd`;|',
				'var| c| =| \'asd\';|',
				'var| d| =| "asd";|',
				'var| e| =| /*3*/|	3;|',
				'var| f| =| /**| 3| */3;|',
				'var| g| =| (3+5|);|',
				'var| h| =| {| a:| \'value\'| |};|',
			];
			for (let i = 0, len = autoClosePositions.length; i < len; i++) {
				const lineNumber = i + 1;
				const autoCloseColumns = extractSpecialColumns(model.getLineMaxColumn(lineNumber), autoClosePositions[i]);

				for (let column = 1; column < autoCloseColumns.length; column++) {
					if (autoCloseColumns[column] === ColumnType.Special1) {
						assertType(model, cursor, lineNumber, column, '(', '()', `auto closes @ (${lineNumber}, ${column})`);
					} else {
						assertType(model, cursor, lineNumber, column, '(', '(', `does not auto close @ (${lineNumber}, ${column})`);
					}
				}
			}
		});
		mode.dispose();
	});

	test('quote', () => {
		let mode = new AutoClosingMode();
		usingCursor({
			text: [
				'var a = [];',
				'var b = `asd`;',
				'var c = \'asd\';',
				'var d = "asd";',
				'var e = /*3*/	3;',
				'var f = /** 3 */3;',
				'var g = (3+5);',
				'var h = { a: \'value\' };',
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {

			let autoClosePositions = [
3101 3102 3103 3104 3105 3106 3107 3108
				'var a =| [|];|',
				'var b =| |`asd`;|',
				'var c =| !\'asd!\';|',
				'var d =| |"asd";|',
				'var e =| /*3*/|	3;|',
				'var f =| /**| 3 */3;|',
				'var g =| (3+5);|',
				'var h =| {| a:| !\'value!\'| |};|',
3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
			];
			for (let i = 0, len = autoClosePositions.length; i < len; i++) {
				const lineNumber = i + 1;
				const autoCloseColumns = extractSpecialColumns(model.getLineMaxColumn(lineNumber), autoClosePositions[i]);

				for (let column = 1; column < autoCloseColumns.length; column++) {
					if (autoCloseColumns[column] === ColumnType.Special1) {
						assertType(model, cursor, lineNumber, column, '\'', '\'\'', `auto closes @ (${lineNumber}, ${column})`);
					} else if (autoCloseColumns[column] === ColumnType.Special2) {
						assertType(model, cursor, lineNumber, column, '\'', '', `over types @ (${lineNumber}, ${column})`);
					} else {
						assertType(model, cursor, lineNumber, column, '\'', '\'', `does not auto close @ (${lineNumber}, ${column})`);
					}
				}
			}
		});
		mode.dispose();
	});
3127

3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
	test('issue #25658 - Do not auto-close single/double quotes after word characters', () => {
		let mode = new AutoClosingMode();
		usingCursor({
			text: [
				'',
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {

			function typeCharacters(cursor: Cursor, chars: string): void {
				for (let i = 0, len = chars.length; i < len; i++) {
					cursorCommand(cursor, H.Type, { text: chars[i] }, 'keyboard');
				}
			}

			// First gif
			typeCharacters(cursor, 'teste1 = teste\' ok');
			assert.equal(model.getLineContent(1), 'teste1 = teste\' ok');

			cursor.setSelections('test', [new Selection(1, 1000, 1, 1000)]);
			typeCharacters(cursor, '\n');
			typeCharacters(cursor, 'teste2 = teste \'ok');
			assert.equal(model.getLineContent(2), 'teste2 = teste \'ok\'');

			cursor.setSelections('test', [new Selection(2, 1000, 2, 1000)]);
			typeCharacters(cursor, '\n');
			typeCharacters(cursor, 'teste3 = teste" ok');
			assert.equal(model.getLineContent(3), 'teste3 = teste" ok');

			cursor.setSelections('test', [new Selection(3, 1000, 3, 1000)]);
			typeCharacters(cursor, '\n');
			typeCharacters(cursor, 'teste4 = teste "ok');
			assert.equal(model.getLineContent(4), 'teste4 = teste "ok"');

			// Second gif
			cursor.setSelections('test', [new Selection(4, 1000, 4, 1000)]);
			typeCharacters(cursor, '\n');
			typeCharacters(cursor, 'teste \'');
			assert.equal(model.getLineContent(5), 'teste \'\'');

			cursor.setSelections('test', [new Selection(5, 1000, 5, 1000)]);
			typeCharacters(cursor, '\n');
			typeCharacters(cursor, 'teste "');
			assert.equal(model.getLineContent(6), 'teste ""');

			cursor.setSelections('test', [new Selection(6, 1000, 6, 1000)]);
			typeCharacters(cursor, '\n');
			typeCharacters(cursor, 'teste\'');
			assert.equal(model.getLineContent(7), 'teste\'');

			cursor.setSelections('test', [new Selection(7, 1000, 7, 1000)]);
			typeCharacters(cursor, '\n');
			typeCharacters(cursor, 'teste"');
			assert.equal(model.getLineContent(8), 'teste"');
		});
		mode.dispose();
	});

3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
	test('issue #15825: accents on mac US intl keyboard', () => {
		let mode = new AutoClosingMode();
		usingCursor({
			text: [
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {
			assertCursor(cursor, new Position(1, 1));

			// Typing ` + e on the mac US intl kb layout
			cursorCommand(cursor, H.CompositionStart, null, 'keyboard');
			cursorCommand(cursor, H.Type, { text: '`' }, 'keyboard');
			cursorCommand(cursor, H.ReplacePreviousChar, { replaceCharCnt: 1, text: 'è' }, 'keyboard');
			cursorCommand(cursor, H.CompositionEnd, null, 'keyboard');

			assert.equal(model.getValue(), 'è');
		});
		mode.dispose();
3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229
	});

	test('issue #2773: Accents (´`¨^, others?) are inserted in the wrong position (Mac)', () => {
		let mode = new AutoClosingMode();
		usingCursor({
			text: [
				'hello',
				'world'
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {
			assertCursor(cursor, new Position(1, 1));

			// Typing ` and pressing shift+down on the mac US intl kb layout
			// Here we're just replaying what the cursor gets
			cursorCommand(cursor, H.CompositionStart, null, 'keyboard');
			cursorCommand(cursor, H.Type, { text: '`' }, 'keyboard');
			moveDown(cursor, true);
			cursorCommand(cursor, H.ReplacePreviousChar, { replaceCharCnt: 1, text: '`' }, 'keyboard');
			cursorCommand(cursor, H.ReplacePreviousChar, { replaceCharCnt: 1, text: '`' }, 'keyboard');
			cursorCommand(cursor, H.CompositionEnd, null, 'keyboard');

			assert.equal(model.getValue(), '`hello\nworld');
			assertCursor(cursor, new Selection(1, 2, 2, 2));
		});
		mode.dispose();
3230
	});
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252

	test('issue #20891: All cursors should do the same thing', () => {
		let mode = new AutoClosingMode();
		usingCursor({
			text: [
				'var a = asd'
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {

			cursor.setSelections('test', [
				new Selection(1, 9, 1, 9),
				new Selection(1, 12, 1, 12),
			]);

			// type a `
			cursorCommand(cursor, H.Type, { text: '`' }, 'keyboard');

			assert.equal(model.getValue(), 'var a = `asd`');
		});
		mode.dispose();
	});
3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274

	test('All cursors should do the same thing when deleting left', () => {
		let mode = new AutoClosingMode();
		usingCursor({
			text: [
				'var a = ()'
			],
			languageIdentifier: mode.getLanguageIdentifier()
		}, (model, cursor) => {

			cursor.setSelections('test', [
				new Selection(1, 4, 1, 4),
				new Selection(1, 10, 1, 10),
			]);

			// delete left
			cursorCommand(cursor, H.DeleteLeft, null, 'keyboard');

			assert.equal(model.getValue(), 'va a = )');
		});
		mode.dispose();
	});
3275
});