textModel.test.ts 28.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

A
Alex Dima 已提交
7
import * as assert from 'assert';
J
Johannes Rieken 已提交
8 9 10
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { TextModel } from 'vs/editor/common/model/textModel';
A
Alex Dima 已提交
11 12
import { DefaultEndOfLine } from 'vs/editor/common/editorCommon';
import { TextModelData, ITextModelData } from 'vs/editor/common/model/textSource';
E
Erich Gamma 已提交
13

J
Johannes Rieken 已提交
14
function testGuessIndentation(defaultInsertSpaces: boolean, defaultTabSize: number, expectedInsertSpaces: boolean, expectedTabSize: number, text: string[], msg?: string): void {
A
Alex Dima 已提交
15
	var m = new TextModel([], TextModelData.fromString(text.join('\n'), {
16 17
		tabSize: defaultTabSize,
		insertSpaces: defaultInsertSpaces,
18
		detectIndentation: true,
19 20
		defaultEOL: DefaultEndOfLine.LF,
		trimAutoWhitespace: true
21
	}));
22
	var r = m.getOptions();
E
Erich Gamma 已提交
23 24 25
	m.dispose();

	assert.equal(r.insertSpaces, expectedInsertSpaces, msg);
26
	assert.equal(r.tabSize, expectedTabSize, msg);
E
Erich Gamma 已提交
27 28
}

J
Johannes Rieken 已提交
29
function assertGuess(expectedInsertSpaces: boolean, expectedTabSize: number, text: string[], msg?: string): void {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	if (typeof expectedInsertSpaces === 'undefined') {
		// cannot guess insertSpaces
		if (typeof expectedTabSize === 'undefined') {
			// cannot guess tabSize
			testGuessIndentation(true, 13370, true, 13370, text, msg);
			testGuessIndentation(false, 13371, false, 13371, text, msg);
		} else {
			// can guess tabSize
			testGuessIndentation(true, 13370, true, expectedTabSize, text, msg);
			testGuessIndentation(false, 13371, false, expectedTabSize, text, msg);
		}
	} else {
		// can guess insertSpaces
		if (typeof expectedTabSize === 'undefined') {
			// cannot guess tabSize
			testGuessIndentation(true, 13370, expectedInsertSpaces, 13370, text, msg);
			testGuessIndentation(false, 13371, expectedInsertSpaces, 13371, text, msg);
		} else {
			// can guess tabSize
			testGuessIndentation(true, 13370, expectedInsertSpaces, expectedTabSize, text, msg);
			testGuessIndentation(false, 13371, expectedInsertSpaces, expectedTabSize, text, msg);
		}
	}
E
Erich Gamma 已提交
53 54
}

A
Alex Dima 已提交
55 56
suite('TextModel.toRawText', () => {

A
Alex Dima 已提交
57 58
	function testToRawText(text: string, expected: ITextModelData): void {
		let actual = TextModelData.fromString(text, TextModel.DEFAULT_CREATION_OPTIONS);
A
Alex Dima 已提交
59 60 61 62 63
		assert.deepEqual(actual, expected);
	}

	test('one line text', () => {
		testToRawText('Hello world!', {
A
Alex Dima 已提交
64 65 66 67 68 69 70 71 72 73
			text: {
				BOM: '',
				EOL: '\n',
				length: 12,
				'lines': [
					'Hello world!'
				],
				containsRTL: false,
				isBasicASCII: true
			},
A
Alex Dima 已提交
74 75 76 77 78 79 80 81 82 83 84
			options: {
				defaultEOL: DefaultEndOfLine.LF,
				insertSpaces: true,
				tabSize: 4,
				trimAutoWhitespace: true,
			}
		});
	});

	test('multiline text', () => {
		testToRawText('Hello,\r\ndear friend\nHow\rare\r\nyou?', {
A
Alex Dima 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
			text: {
				BOM: '',
				EOL: '\r\n',
				length: 33,
				'lines': [
					'Hello,',
					'dear friend',
					'How',
					'are',
					'you?'
				],
				containsRTL: false,
				isBasicASCII: true
			},
99 100 101 102 103 104 105 106 107 108 109
			options: {
				defaultEOL: DefaultEndOfLine.LF,
				insertSpaces: true,
				tabSize: 4,
				trimAutoWhitespace: true,
			}
		});
	});

	test('Non Basic ASCII 1', () => {
		testToRawText('Hello,\nZürich', {
A
Alex Dima 已提交
110 111 112 113 114 115 116 117 118 119 120
			text: {
				BOM: '',
				EOL: '\n',
				length: 13,
				'lines': [
					'Hello,',
					'Zürich'
				],
				containsRTL: false,
				isBasicASCII: false
			},
A
Alex Dima 已提交
121 122 123 124 125 126 127 128 129 130 131
			options: {
				defaultEOL: DefaultEndOfLine.LF,
				insertSpaces: true,
				tabSize: 4,
				trimAutoWhitespace: true,
			}
		});
	});

	test('containsRTL 1', () => {
		testToRawText('Hello,\nזוהי עובדה מבוססת שדעתו', {
A
Alex Dima 已提交
132 133 134 135 136 137 138 139 140 141 142
			text: {
				BOM: '',
				EOL: '\n',
				length: 30,
				'lines': [
					'Hello,',
					'זוהי עובדה מבוססת שדעתו'
				],
				containsRTL: true,
				isBasicASCII: false
			},
A
Alex Dima 已提交
143 144 145 146 147 148 149 150 151 152 153
			options: {
				defaultEOL: DefaultEndOfLine.LF,
				insertSpaces: true,
				tabSize: 4,
				trimAutoWhitespace: true,
			}
		});
	});

	test('containsRTL 2', () => {
		testToRawText('Hello,\nهناك حقيقة مثبتة منذ زمن طويل', {
A
Alex Dima 已提交
154 155 156 157 158 159 160 161 162 163 164
			text: {
				BOM: '',
				EOL: '\n',
				length: 36,
				'lines': [
					'Hello,',
					'هناك حقيقة مثبتة منذ زمن طويل'
				],
				containsRTL: true,
				isBasicASCII: false
			},
A
Alex Dima 已提交
165 166 167 168 169 170 171 172 173 174 175
			options: {
				defaultEOL: DefaultEndOfLine.LF,
				insertSpaces: true,
				tabSize: 4,
				trimAutoWhitespace: true,
			}
		});
	});

});

E
Erich Gamma 已提交
176 177 178 179
suite('Editor Model - TextModel', () => {

	test('getValueLengthInRange', () => {

A
Alex Dima 已提交
180
		var m = new TextModel([], TextModelData.fromString('My First Line\r\nMy Second Line\r\nMy Third Line', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
181 182 183 184 185 186 187 188 189 190 191
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 1, 1)), ''.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 1, 2)), 'M'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 1, 3)), 'y'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 1, 14)), 'My First Line'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 2, 1)), 'My First Line\r\n'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 2, 1)), 'y First Line\r\n'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 2, 2)), 'y First Line\r\nM'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 2, 1000)), 'y First Line\r\nMy Second Line'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 3, 1)), 'y First Line\r\nMy Second Line\r\n'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 3, 1000)), 'y First Line\r\nMy Second Line\r\nMy Third Line'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 1000, 1000)), 'My First Line\r\nMy Second Line\r\nMy Third Line'.length);
E
Erich Gamma 已提交
192

A
Alex Dima 已提交
193
		m = new TextModel([], TextModelData.fromString('My First Line\nMy Second Line\nMy Third Line', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
194 195 196 197 198 199 200 201 202 203 204
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 1, 1)), ''.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 1, 2)), 'M'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 1, 3)), 'y'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 1, 14)), 'My First Line'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 2, 1)), 'My First Line\n'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 2, 1)), 'y First Line\n'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 2, 2)), 'y First Line\nM'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 2, 1000)), 'y First Line\nMy Second Line'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 3, 1)), 'y First Line\nMy Second Line\n'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 2, 3, 1000)), 'y First Line\nMy Second Line\nMy Third Line'.length);
		assert.equal(m.getValueLengthInRange(new Range(1, 1, 1000, 1000)), 'My First Line\nMy Second Line\nMy Third Line'.length);
E
Erich Gamma 已提交
205 206 207 208
	});

	test('guess indentation 1', () => {

209
		assertGuess(undefined, undefined, [
210 211 212 213 214
			'x',
			'x',
			'x',
			'x',
			'x',
E
Erich Gamma 已提交
215 216
			'x',
			'x'
217
		], 'no clues');
E
Erich Gamma 已提交
218

219
		assertGuess(false, undefined, [
E
Erich Gamma 已提交
220
			'\tx',
221 222 223 224 225
			'x',
			'x',
			'x',
			'x',
			'x',
E
Erich Gamma 已提交
226
			'x'
227 228
		], 'no spaces, 1xTAB');

229
		assertGuess(true, 2, [
230 231 232 233 234 235 236 237 238
			'  x',
			'x',
			'x',
			'x',
			'x',
			'x',
			'x'
		], '1x2');

239
		assertGuess(false, undefined, [
240 241 242 243 244 245 246 247 248
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx'
		], '7xTAB');

249
		assertGuess(undefined, 2, [
250 251 252 253 254 255 256 257 258
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
		], '4x2, 4xTAB');
259
		assertGuess(false, undefined, [
260 261 262 263 264 265
			'\tx',
			' x',
			'\tx',
			' x',
			'\tx',
			' x',
E
Erich Gamma 已提交
266 267
			'\tx',
			' x'
268
		], '4x1, 4xTAB');
269
		assertGuess(false, 2, [
270 271 272 273 274 275 276 277 278 279
			'\tx',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
		], '4x2, 5xTAB');
280
		assertGuess(false, 2, [
281 282 283 284 285 286 287 288 289 290
			'\tx',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'  x',
		], '1x2, 5xTAB');
291
		assertGuess(false, 4, [
292 293 294
			'\tx',
			'\tx',
			'x',
E
Erich Gamma 已提交
295
			'\tx',
296 297 298 299 300 301
			'x',
			'\tx',
			'x',
			'\tx',
			'    x',
		], '1x4, 5xTAB');
302
		assertGuess(false, 2, [
303 304 305 306 307 308 309 310 311 312
			'\tx',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'  x',
			'\tx',
			'    x',
		], '1x2, 1x4, 5xTAB');
E
Erich Gamma 已提交
313

314
		assertGuess(undefined, undefined, [
E
Erich Gamma 已提交
315 316 317 318 319 320 321 322 323
			'x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x'
		], '7x1 - 1 space is never guessed as an indentation');
324 325 326 327 328 329 330 331 332 333 334
		assertGuess(true, undefined, [
			'x',
			'          x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x'
		], '1x10, 6x1');
		assertGuess(undefined, undefined, [
E
Erich Gamma 已提交
335 336 337 338
			'',
			'  ',
			'    ',
			'      ',
339 340 341 342
			'        ',
			'          ',
			'            ',
			'              ',
E
Erich Gamma 已提交
343
		], 'whitespace lines don\'t count');
344
		assertGuess(true, 4, [
E
Erich Gamma 已提交
345 346 347
			'x',
			'   x',
			'   x',
348 349 350 351 352 353 354 355 356 357
			'    x',
			'x',
			'   x',
			'   x',
			'    x',
			'x',
			'   x',
			'   x',
			'    x',
		], 'odd number is not allowed: 6x3, 3x4');
358
		assertGuess(true, 4, [
E
Erich Gamma 已提交
359 360 361
			'x',
			'     x',
			'     x',
362 363 364 365 366 367 368 369 370 371
			'    x',
			'x',
			'     x',
			'     x',
			'    x',
			'x',
			'     x',
			'     x',
			'    x',
		], 'odd number is not allowed: 6x5, 3x4');
372
		assertGuess(true, 4, [
E
Erich Gamma 已提交
373 374 375
			'x',
			'       x',
			'       x',
376 377 378 379 380 381 382 383 384 385
			'    x',
			'x',
			'       x',
			'       x',
			'    x',
			'x',
			'       x',
			'       x',
			'    x',
		], 'odd number is not allowed: 6x7, 3x4');
386
		assertGuess(true, 2, [
E
Erich Gamma 已提交
387 388 389 390
			'x',
			'  x',
			'  x',
			'  x',
391 392 393 394 395 396 397
			'  x',
			'x',
			'  x',
			'  x',
			'  x',
			'  x',
		], '8x2');
E
Erich Gamma 已提交
398

399
		assertGuess(true, 2, [
E
Erich Gamma 已提交
400 401 402
			'x',
			'  x',
			'  x',
403 404 405 406 407 408 409 410 411 412
			'x',
			'  x',
			'  x',
			'x',
			'  x',
			'  x',
			'x',
			'  x',
			'  x',
		], '8x2');
413
		assertGuess(true, 2, [
E
Erich Gamma 已提交
414 415 416
			'x',
			'  x',
			'    x',
417 418 419 420 421 422 423 424 425 426
			'x',
			'  x',
			'    x',
			'x',
			'  x',
			'    x',
			'x',
			'  x',
			'    x',
		], '4x2, 4x4');
427
		assertGuess(true, 2, [
E
Erich Gamma 已提交
428 429 430 431
			'x',
			'  x',
			'  x',
			'    x',
432 433 434 435 436 437 438 439 440
			'x',
			'  x',
			'  x',
			'    x',
			'x',
			'  x',
			'  x',
			'    x',
		], '6x2, 3x4');
441
		assertGuess(true, 2, [
E
Erich Gamma 已提交
442 443 444 445 446
			'x',
			'  x',
			'  x',
			'    x',
			'    x',
447 448 449 450 451 452
			'x',
			'  x',
			'  x',
			'    x',
			'    x',
		], '4x2, 4x4');
453
		assertGuess(true, 2, [
E
Erich Gamma 已提交
454 455 456 457
			'x',
			'  x',
			'    x',
			'    x',
458 459 460 461 462
			'x',
			'  x',
			'    x',
			'    x',
		], '2x2, 4x4');
463
		assertGuess(true, 4, [
E
Erich Gamma 已提交
464 465 466
			'x',
			'    x',
			'    x',
467 468 469 470 471 472 473 474 475 476
			'x',
			'    x',
			'    x',
			'x',
			'    x',
			'    x',
			'x',
			'    x',
			'    x',
		], '8x4');
477
		assertGuess(true, 2, [
E
Erich Gamma 已提交
478 479 480 481 482
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
483 484 485 486 487 488
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
		], '2x2, 4x4, 2x6');
489
		assertGuess(true, 2, [
E
Erich Gamma 已提交
490 491 492 493 494 495 496 497
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
			'      x',
			'        x',
		], '1x2, 2x4, 2x6, 1x8');
498
		assertGuess(true, 4, [
E
Erich Gamma 已提交
499 500 501 502 503 504
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
505 506 507 508 509 510 511
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
		], '6x4, 2x5, 2x8');
512
		assertGuess(true, 4, [
E
Erich Gamma 已提交
513 514 515 516 517 518 519 520
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
		], '3x4, 1x5, 2x8');
521
		assertGuess(true, 4, [
E
Erich Gamma 已提交
522 523 524 525 526 527 528
			'x',
			'x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
529 530 531 532 533 534 535 536
			'x',
			'x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
		], '6x4, 2x5, 4x8');
537
		assertGuess(true, 4, [
E
Erich Gamma 已提交
538 539 540 541 542 543 544 545 546 547 548
			'x',
			' x',
			' x',
			' x',
			' x',
			' x',
			'x',
			'   x',
			'    x',
			'    x',
		], '5x1, 2x0, 1x3, 2x4');
549 550 551 552 553 554 555 556 557
		assertGuess(false, undefined, [
			'\t x',
			' \t x',
			'\tx'
		], 'mixed whitespace 1');
		assertGuess(false, 4, [
			'\tx',
			'\t    x'
		], 'mixed whitespace 2');
E
Erich Gamma 已提交
558 559
	});

A
aioute Gao 已提交
560 561
	test('validatePosition', () => {

A
Alex Dima 已提交
562
		let m = new TextModel([], TextModelData.fromString('line one\nline two', TextModel.DEFAULT_CREATION_OPTIONS));
A
aioute Gao 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

		assert.deepEqual(m.validatePosition(new Position(0, 0)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(0, 1)), new Position(1, 1));

		assert.deepEqual(m.validatePosition(new Position(1, 1)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(1, 2)), new Position(1, 2));
		assert.deepEqual(m.validatePosition(new Position(1, 30)), new Position(1, 9));

		assert.deepEqual(m.validatePosition(new Position(2, 0)), new Position(2, 1));
		assert.deepEqual(m.validatePosition(new Position(2, 1)), new Position(2, 1));
		assert.deepEqual(m.validatePosition(new Position(2, 2)), new Position(2, 2));
		assert.deepEqual(m.validatePosition(new Position(2, 30)), new Position(2, 9));

		assert.deepEqual(m.validatePosition(new Position(3, 0)), new Position(2, 9));
		assert.deepEqual(m.validatePosition(new Position(3, 1)), new Position(2, 9));
		assert.deepEqual(m.validatePosition(new Position(3, 30)), new Position(2, 9));

		assert.deepEqual(m.validatePosition(new Position(30, 30)), new Position(2, 9));

582 583 584 585 586 587 588 589 590
		assert.deepEqual(m.validatePosition(new Position(-123.123, -0.5)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(Number.MIN_VALUE, Number.MIN_VALUE)), new Position(1, 1));

		assert.deepEqual(m.validatePosition(new Position(Number.MAX_VALUE, Number.MAX_VALUE)), new Position(2, 9));
		assert.deepEqual(m.validatePosition(new Position(123.23, 47.5)), new Position(2, 9));
	});

	test('validatePosition around high-low surrogate pairs 1', () => {

A
Alex Dima 已提交
591
		let m = new TextModel([], TextModelData.fromString('a📚b', TextModel.DEFAULT_CREATION_OPTIONS));
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

		assert.deepEqual(m.validatePosition(new Position(0, 0)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(0, 1)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(0, 7)), new Position(1, 1));

		assert.deepEqual(m.validatePosition(new Position(1, 1)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(1, 2)), new Position(1, 2));
		assert.deepEqual(m.validatePosition(new Position(1, 3)), new Position(1, 2));
		assert.deepEqual(m.validatePosition(new Position(1, 4)), new Position(1, 4));
		assert.deepEqual(m.validatePosition(new Position(1, 5)), new Position(1, 5));
		assert.deepEqual(m.validatePosition(new Position(1, 30)), new Position(1, 5));

		assert.deepEqual(m.validatePosition(new Position(2, 0)), new Position(1, 5));
		assert.deepEqual(m.validatePosition(new Position(2, 1)), new Position(1, 5));
		assert.deepEqual(m.validatePosition(new Position(2, 2)), new Position(1, 5));
		assert.deepEqual(m.validatePosition(new Position(2, 30)), new Position(1, 5));

		assert.deepEqual(m.validatePosition(new Position(-123.123, -0.5)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(Number.MIN_VALUE, Number.MIN_VALUE)), new Position(1, 1));

		assert.deepEqual(m.validatePosition(new Position(Number.MAX_VALUE, Number.MAX_VALUE)), new Position(1, 5));
		assert.deepEqual(m.validatePosition(new Position(123.23, 47.5)), new Position(1, 5));
	});

	test('validatePosition around high-low surrogate pairs 2', () => {

A
Alex Dima 已提交
618
		let m = new TextModel([], TextModelData.fromString('a📚📚b', TextModel.DEFAULT_CREATION_OPTIONS));
619 620 621 622 623 624 625 626 627 628 629 630 631

		assert.deepEqual(m.validatePosition(new Position(1, 1)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(1, 2)), new Position(1, 2));
		assert.deepEqual(m.validatePosition(new Position(1, 3)), new Position(1, 2));
		assert.deepEqual(m.validatePosition(new Position(1, 4)), new Position(1, 4));
		assert.deepEqual(m.validatePosition(new Position(1, 5)), new Position(1, 4));
		assert.deepEqual(m.validatePosition(new Position(1, 6)), new Position(1, 6));
		assert.deepEqual(m.validatePosition(new Position(1, 7)), new Position(1, 7));

	});

	test('validateRange around high-low surrogate pairs 1', () => {

A
Alex Dima 已提交
632
		let m = new TextModel([], TextModelData.fromString('a📚b', TextModel.DEFAULT_CREATION_OPTIONS));
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

		assert.deepEqual(m.validateRange(new Range(0, 0, 0, 1)), new Range(1, 1, 1, 1));
		assert.deepEqual(m.validateRange(new Range(0, 0, 0, 7)), new Range(1, 1, 1, 1));

		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 1)), new Range(1, 1, 1, 1));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 2)), new Range(1, 1, 1, 2));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 3)), new Range(1, 1, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 4)), new Range(1, 1, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 5)), new Range(1, 1, 1, 5));

		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 2)), new Range(1, 2, 1, 2));
		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 3)), new Range(1, 2, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 4)), new Range(1, 2, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 5)), new Range(1, 2, 1, 5));

		assert.deepEqual(m.validateRange(new Range(1, 3, 1, 3)), new Range(1, 2, 1, 2));
		assert.deepEqual(m.validateRange(new Range(1, 3, 1, 4)), new Range(1, 2, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 3, 1, 5)), new Range(1, 2, 1, 5));

		assert.deepEqual(m.validateRange(new Range(1, 4, 1, 4)), new Range(1, 4, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 4, 1, 5)), new Range(1, 4, 1, 5));

		assert.deepEqual(m.validateRange(new Range(1, 5, 1, 5)), new Range(1, 5, 1, 5));
	});

	test('validateRange around high-low surrogate pairs 2', () => {

A
Alex Dima 已提交
660
		let m = new TextModel([], TextModelData.fromString('a📚📚b', TextModel.DEFAULT_CREATION_OPTIONS));
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

		assert.deepEqual(m.validateRange(new Range(0, 0, 0, 1)), new Range(1, 1, 1, 1));
		assert.deepEqual(m.validateRange(new Range(0, 0, 0, 7)), new Range(1, 1, 1, 1));

		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 1)), new Range(1, 1, 1, 1));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 2)), new Range(1, 1, 1, 2));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 3)), new Range(1, 1, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 4)), new Range(1, 1, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 5)), new Range(1, 1, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 6)), new Range(1, 1, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 1, 1, 7)), new Range(1, 1, 1, 7));

		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 2)), new Range(1, 2, 1, 2));
		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 3)), new Range(1, 2, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 4)), new Range(1, 2, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 5)), new Range(1, 2, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 6)), new Range(1, 2, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 2, 1, 7)), new Range(1, 2, 1, 7));

		assert.deepEqual(m.validateRange(new Range(1, 3, 1, 3)), new Range(1, 2, 1, 2));
		assert.deepEqual(m.validateRange(new Range(1, 3, 1, 4)), new Range(1, 2, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 3, 1, 5)), new Range(1, 2, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 3, 1, 6)), new Range(1, 2, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 3, 1, 7)), new Range(1, 2, 1, 7));

		assert.deepEqual(m.validateRange(new Range(1, 4, 1, 4)), new Range(1, 4, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 4, 1, 5)), new Range(1, 4, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 4, 1, 6)), new Range(1, 4, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 4, 1, 7)), new Range(1, 4, 1, 7));

		assert.deepEqual(m.validateRange(new Range(1, 5, 1, 5)), new Range(1, 4, 1, 4));
		assert.deepEqual(m.validateRange(new Range(1, 5, 1, 6)), new Range(1, 4, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 5, 1, 7)), new Range(1, 4, 1, 7));

		assert.deepEqual(m.validateRange(new Range(1, 6, 1, 6)), new Range(1, 6, 1, 6));
		assert.deepEqual(m.validateRange(new Range(1, 6, 1, 7)), new Range(1, 6, 1, 7));

		assert.deepEqual(m.validateRange(new Range(1, 7, 1, 7)), new Range(1, 7, 1, 7));
A
aioute Gao 已提交
699 700
	});

E
Erich Gamma 已提交
701 702
	test('modifyPosition', () => {

A
Alex Dima 已提交
703
		var m = new TextModel([], TextModelData.fromString('line one\nline two', TextModel.DEFAULT_CREATION_OPTIONS));
J
Johannes Rieken 已提交
704 705
		assert.deepEqual(m.modifyPosition(new Position(1, 1), 0), new Position(1, 1));
		assert.deepEqual(m.modifyPosition(new Position(0, 0), 0), new Position(1, 1));
A
aioute Gao 已提交
706
		assert.deepEqual(m.modifyPosition(new Position(30, 1), 0), new Position(2, 9));
E
Erich Gamma 已提交
707

J
Johannes Rieken 已提交
708 709 710
		assert.deepEqual(m.modifyPosition(new Position(1, 1), 17), new Position(2, 9));
		assert.deepEqual(m.modifyPosition(new Position(1, 1), 1), new Position(1, 2));
		assert.deepEqual(m.modifyPosition(new Position(1, 1), 3), new Position(1, 4));
A
Alex Dima 已提交
711 712 713
		assert.deepEqual(m.modifyPosition(new Position(1, 2), 10), new Position(2, 3));
		assert.deepEqual(m.modifyPosition(new Position(1, 5), 13), new Position(2, 9));
		assert.deepEqual(m.modifyPosition(new Position(1, 2), 16), new Position(2, 9));
E
Erich Gamma 已提交
714

A
Alex Dima 已提交
715
		assert.deepEqual(m.modifyPosition(new Position(2, 9), -17), new Position(1, 1));
J
Johannes Rieken 已提交
716 717
		assert.deepEqual(m.modifyPosition(new Position(1, 2), -1), new Position(1, 1));
		assert.deepEqual(m.modifyPosition(new Position(1, 4), -3), new Position(1, 1));
A
Alex Dima 已提交
718 719 720
		assert.deepEqual(m.modifyPosition(new Position(2, 3), -10), new Position(1, 2));
		assert.deepEqual(m.modifyPosition(new Position(2, 9), -13), new Position(1, 5));
		assert.deepEqual(m.modifyPosition(new Position(2, 9), -16), new Position(1, 2));
E
Erich Gamma 已提交
721

722 723
		assert.deepEqual(m.modifyPosition(new Position(1, 2), 17), new Position(2, 9));
		assert.deepEqual(m.modifyPosition(new Position(1, 2), 100), new Position(2, 9));
E
Erich Gamma 已提交
724

725 726 727 728
		assert.deepEqual(m.modifyPosition(new Position(1, 2), -2), new Position(1, 1));
		assert.deepEqual(m.modifyPosition(new Position(1, 2), -100), new Position(1, 1));
		assert.deepEqual(m.modifyPosition(new Position(2, 2), -100), new Position(1, 1));
		assert.deepEqual(m.modifyPosition(new Position(2, 9), -18), new Position(1, 1));
E
Erich Gamma 已提交
729
	});
A
Alex Dima 已提交
730 731 732

	test('normalizeIndentation 1', () => {
		let model = new TextModel([], {
A
Alex Dima 已提交
733 734 735 736 737 738 739 740
			text: {
				length: 0,
				lines: [],
				BOM: '',
				EOL: '\n',
				containsRTL: false,
				isBasicASCII: true
			},
A
Alex Dima 已提交
741 742 743
			options: {
				tabSize: 4,
				insertSpaces: false,
744
				trimAutoWhitespace: true,
A
Alex Dima 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
				defaultEOL: DefaultEndOfLine.LF
			}
		});

		assert.equal(model.normalizeIndentation('\t'), '\t');
		assert.equal(model.normalizeIndentation('    '), '\t');
		assert.equal(model.normalizeIndentation('   '), '   ');
		assert.equal(model.normalizeIndentation('  '), '  ');
		assert.equal(model.normalizeIndentation(' '), ' ');
		assert.equal(model.normalizeIndentation(''), '');
		assert.equal(model.normalizeIndentation(' \t   '), '\t\t');
		assert.equal(model.normalizeIndentation(' \t  '), '\t   ');
		assert.equal(model.normalizeIndentation(' \t '), '\t  ');
		assert.equal(model.normalizeIndentation(' \t'), '\t ');

		assert.equal(model.normalizeIndentation('\ta'), '\ta');
		assert.equal(model.normalizeIndentation('    a'), '\ta');
		assert.equal(model.normalizeIndentation('   a'), '   a');
		assert.equal(model.normalizeIndentation('  a'), '  a');
		assert.equal(model.normalizeIndentation(' a'), ' a');
		assert.equal(model.normalizeIndentation('a'), 'a');
		assert.equal(model.normalizeIndentation(' \t   a'), '\t\ta');
		assert.equal(model.normalizeIndentation(' \t  a'), '\t   a');
		assert.equal(model.normalizeIndentation(' \t a'), '\t  a');
		assert.equal(model.normalizeIndentation(' \ta'), '\t a');

		model.dispose();
	});

	test('normalizeIndentation 2', () => {
		let model = new TextModel([], {
A
Alex Dima 已提交
776 777 778 779 780 781 782 783
			text: {
				length: 0,
				lines: [],
				BOM: '',
				EOL: '\n',
				containsRTL: false,
				isBasicASCII: true
			},
A
Alex Dima 已提交
784 785 786
			options: {
				tabSize: 4,
				insertSpaces: true,
787
				trimAutoWhitespace: true,
A
Alex Dima 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
				defaultEOL: DefaultEndOfLine.LF
			}
		});

		assert.equal(model.normalizeIndentation('\ta'), '    a');
		assert.equal(model.normalizeIndentation('    a'), '    a');
		assert.equal(model.normalizeIndentation('   a'), '   a');
		assert.equal(model.normalizeIndentation('  a'), '  a');
		assert.equal(model.normalizeIndentation(' a'), ' a');
		assert.equal(model.normalizeIndentation('a'), 'a');
		assert.equal(model.normalizeIndentation(' \t   a'), '        a');
		assert.equal(model.normalizeIndentation(' \t  a'), '       a');
		assert.equal(model.normalizeIndentation(' \t a'), '      a');
		assert.equal(model.normalizeIndentation(' \ta'), '     a');

		model.dispose();
	});
E
Erich Gamma 已提交
805
});
A
Alex Dima 已提交
806

A
Alex Dima 已提交
807 808 809
suite('TextModel.mightContainRTL', () => {

	test('nope', () => {
A
Alex Dima 已提交
810
		let model = new TextModel([], TextModelData.fromString('hello world!', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
811 812 813 814
		assert.equal(model.mightContainRTL(), false);
	});

	test('yes', () => {
A
Alex Dima 已提交
815
		let model = new TextModel([], TextModelData.fromString('Hello,\nזוהי עובדה מבוססת שדעתו', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
816 817 818 819
		assert.equal(model.mightContainRTL(), true);
	});

	test('setValue resets 1', () => {
A
Alex Dima 已提交
820
		let model = new TextModel([], TextModelData.fromString('hello world!', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
821 822 823 824 825 826
		assert.equal(model.mightContainRTL(), false);
		model.setValue('Hello,\nזוהי עובדה מבוססת שדעתו');
		assert.equal(model.mightContainRTL(), true);
	});

	test('setValue resets 2', () => {
A
Alex Dima 已提交
827
		let model = new TextModel([], TextModelData.fromString('Hello,\nهناك حقيقة مثبتة منذ زمن طويل', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
828 829 830 831 832 833 834
		assert.equal(model.mightContainRTL(), true);
		model.setValue('hello world!');
		assert.equal(model.mightContainRTL(), false);
	});

});

A
Alex Dima 已提交
835
suite('TextModel.getLineIndentGuide', () => {
J
Johannes Rieken 已提交
836
	function assertIndentGuides(lines: [number, string][]): void {
A
Alex Dima 已提交
837
		let text = lines.map(l => l[1]).join('\n');
A
Alex Dima 已提交
838
		let model = new TextModel([], TextModelData.fromString(text, TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
839

J
Johannes Rieken 已提交
840
		let actual: [number, string][] = [];
A
Alex Dima 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
		for (let line = 1; line <= model.getLineCount(); line++) {
			actual[line - 1] = [model.getLineIndentGuide(line), model.getLineContent(line)];
		}

		// let expected = lines.map(l => l[0]);

		assert.deepEqual(actual, lines);

		model.dispose();
	}

	test('getLineIndentGuide one level', () => {
		assertIndentGuides([
			[0, 'A'],
			[1, '  A'],
			[1, '  A'],
			[1, '  A'],
		]);
	});

	test('getLineIndentGuide two levels', () => {
		assertIndentGuides([
			[0, 'A'],
			[1, '  A'],
			[1, '  A'],
			[1, '    A'],
			[1, '    A'],
		]);
	});

	test('getLineIndentGuide three levels', () => {
		assertIndentGuides([
			[0, 'A'],
			[1, '  A'],
			[1, '    A'],
			[2, '      A'],
877
			[0, 'A'],
A
Alex Dima 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890
		]);
	});

	test('getLineIndentGuide decreasing indent', () => {
		assertIndentGuides([
			[0, '    A'],
			[0, '  A'],
			[0, 'A'],
		]);
	});

	test('getLineIndentGuide Java', () => {
		assertIndentGuides([
891 892 893 894 895 896 897 898 899 900 901 902 903
			/* 1*/[0, 'class A {'],
			/* 2*/[1, '  void foo() {'],
			/* 3*/[1, '    console.log(1);'],
			/* 4*/[1, '    console.log(2);'],
			/* 5*/[1, '  }'],
			/* 6*/[1, ''],
			/* 7*/[1, '  void bar() {'],
			/* 8*/[1, '    console.log(3);'],
			/* 9*/[1, '  }'],
			/*10*/[0, '}'],
			/*11*/[0, 'interface B {'],
			/*12*/[1, '  void bar();'],
			/*13*/[0, '}'],
A
Alex Dima 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
		]);
	});

	test('getLineIndentGuide Javadoc', () => {
		assertIndentGuides([
			[0, '/**'],
			[1, ' * Comment'],
			[1, ' */'],
			[0, 'class A {'],
			[1, '  void foo() {'],
			[1, '  }'],
			[0, '}'],
		]);
	});

	test('getLineIndentGuide Whitespace', () => {
		assertIndentGuides([
			[0, 'class A {'],
			[1, ''],
			[1, '  void foo() {'],
			[1, '     '],
			[1, '     return 1;'],
			[1, '  }'],
			[1, '      '],
			[0, '}'],
		]);
	});

	test('getLineIndentGuide Tabs', () => {
		assertIndentGuides([
			[0, 'class A {'],
			[1, '\t\t'],
			[1, '\tvoid foo() {'],
			[2, '\t \t//hello'],
			[2, '\t    return 2;'],
			[1, '  \t}'],
			[1, '      '],
			[0, '}'],
		]);
	});

	test('getLineIndentGuide checker.ts', () => {
		assertIndentGuides([
J
Johannes Rieken 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
			/* 1*/[0, '/// <reference path="binder.ts"/>'],
			/* 2*/[0, ''],
			/* 3*/[0, '/* @internal */'],
			/* 4*/[0, 'namespace ts {'],
			/* 5*/[1, '    let nextSymbolId = 1;'],
			/* 6*/[1, '    let nextNodeId = 1;'],
			/* 7*/[1, '    let nextMergeId = 1;'],
			/* 8*/[1, '    let nextFlowId = 1;'],
			/* 9*/[1, ''],
			/*10*/[1, '    export function getNodeId(node: Node): number {'],
			/*11*/[2, '        if (!node.id) {'],
			/*12*/[3, '            node.id = nextNodeId;'],
			/*13*/[3, '            nextNodeId++;'],
			/*14*/[2, '        }'],
			/*15*/[2, '        return node.id;'],
			/*16*/[1, '    }'],
			/*17*/[0, '}'],
A
Alex Dima 已提交
964 965
		]);
	});
966 967 968 969 970 971 972 973 974

	test('issue #8425 - Missing indentation lines for first level indentation', () => {
		assertIndentGuides([
			[1, '\tindent1'],
			[2, '\t\tindent2'],
			[2, '\t\tindent2'],
			[1, '\tindent1'],
		]);
	});
975 976 977 978 979 980 981 982 983 984 985

	test('issue #8952 - Indentation guide lines going through text on .yml file', () => {
		assertIndentGuides([
			[0, 'properties:'],
			[1, '    emailAddress:'],
			[2, '        - bla'],
			[2, '        - length:'],
			[3, '            max: 255'],
			[0, 'getters:'],
		]);
	});
986 987 988 989 990 991 992 993 994 995 996 997 998

	test('issue #11892 - Indent guides look funny', () => {
		assertIndentGuides([
			[0, 'function test(base) {'],
			[1, '\tswitch (base) {'],
			[2, '\t\tcase 1:'],
			[3, '\t\t\treturn 1;'],
			[2, '\t\tcase 2:'],
			[3, '\t\t\treturn 2;'],
			[1, '\t}'],
			[0, '}'],
		]);
	});
999 1000 1001 1002 1003 1004 1005 1006

	test('issue #12398 - Problem in indent guidelines', () => {
		assertIndentGuides([
			[2, '\t\t.bla'],
			[3, '\t\t\tlabel(for)'],
			[0, 'include script'],
		]);
	});
A
Alex Dima 已提交
1007
});