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

A
Alex Dima 已提交
6
import * as assert from 'assert';
A
Alex Dima 已提交
7
import { UTF8_BOM_CHARACTER } from 'vs/base/common/strings';
J
Johannes Rieken 已提交
8 9
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
10
import { TextModel, createTextBuffer } from 'vs/editor/common/model/textModel';
A
Alex Dima 已提交
11
import { createTextModel } from 'vs/editor/test/common/editorTestUtils';
E
Erich Gamma 已提交
12

J
Johannes Rieken 已提交
13
function testGuessIndentation(defaultInsertSpaces: boolean, defaultTabSize: number, expectedInsertSpaces: boolean, expectedTabSize: number, text: string[], msg?: string): void {
A
Alex Dima 已提交
14
	let m = createTextModel(
A
Alex Dima 已提交
15 16 17 18
		text.join('\n'),
		{
			tabSize: defaultTabSize,
			insertSpaces: defaultInsertSpaces,
A
Alex Dima 已提交
19
			detectIndentation: true
A
Alex Dima 已提交
20 21
		}
	);
A
Alex Dima 已提交
22
	let 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
}

29
function assertGuess(expectedInsertSpaces: boolean | undefined, expectedTabSize: number | undefined | [number], text: string[], msg?: string): void {
30 31 32 33 34 35
	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);
36
		} else if (typeof expectedTabSize === 'number') {
37 38 39
			// can guess tabSize
			testGuessIndentation(true, 13370, true, expectedTabSize, text, msg);
			testGuessIndentation(false, 13371, false, expectedTabSize, text, msg);
40 41 42 43
		} else {
			// can only guess tabSize when insertSpaces is true
			testGuessIndentation(true, 13370, true, expectedTabSize[0], text, msg);
			testGuessIndentation(false, 13371, false, 13371, text, msg);
44 45 46 47 48 49 50
		}
	} 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);
51
		} else if (typeof expectedTabSize === 'number') {
52 53 54
			// can guess tabSize
			testGuessIndentation(true, 13370, expectedInsertSpaces, expectedTabSize, text, msg);
			testGuessIndentation(false, 13371, expectedInsertSpaces, expectedTabSize, text, msg);
55 56 57 58 59 60 61 62 63
		} else {
			// can only guess tabSize when insertSpaces is true
			if (expectedInsertSpaces === true) {
				testGuessIndentation(true, 13370, expectedInsertSpaces, expectedTabSize[0], text, msg);
				testGuessIndentation(false, 13371, expectedInsertSpaces, expectedTabSize[0], text, msg);
			} else {
				testGuessIndentation(true, 13370, expectedInsertSpaces, 13370, text, msg);
				testGuessIndentation(false, 13371, expectedInsertSpaces, 13371, text, msg);
			}
64 65
		}
	}
E
Erich Gamma 已提交
66 67
}

A
Alex Dima 已提交
68
suite('TextModelData.fromString', () => {
A
Alex Dima 已提交
69

70 71 72 73 74 75
	interface ITextBufferData {
		EOL: string;
		lines: string[];
		containsRTL: boolean;
		isBasicASCII: boolean;
	}
A
Alex Dima 已提交
76

77 78 79 80 81 82 83 84 85
	function testTextModelDataFromString(text: string, expected: ITextBufferData): void {
		const textBuffer = createTextBuffer(text, TextModel.DEFAULT_CREATION_OPTIONS.defaultEOL);
		let actual: ITextBufferData = {
			EOL: textBuffer.getEOL(),
			lines: textBuffer.getLinesContent(),
			containsRTL: textBuffer.mightContainRTL(),
			isBasicASCII: !textBuffer.mightContainNonBasicASCII()
		};
		assert.deepEqual(actual, expected);
A
Alex Dima 已提交
86 87 88
	}

	test('one line text', () => {
A
Alex Dima 已提交
89 90
		testTextModelDataFromString('Hello world!',
			{
A
Alex Dima 已提交
91
				EOL: '\n',
92
				lines: [
A
Alex Dima 已提交
93 94 95 96
					'Hello world!'
				],
				containsRTL: false,
				isBasicASCII: true
97
			}
A
Alex Dima 已提交
98
		);
A
Alex Dima 已提交
99 100 101
	});

	test('multiline text', () => {
A
Alex Dima 已提交
102 103
		testTextModelDataFromString('Hello,\r\ndear friend\nHow\rare\r\nyou?',
			{
A
Alex Dima 已提交
104
				EOL: '\r\n',
105
				lines: [
A
Alex Dima 已提交
106 107 108 109 110 111 112 113
					'Hello,',
					'dear friend',
					'How',
					'are',
					'you?'
				],
				containsRTL: false,
				isBasicASCII: true
114
			}
A
Alex Dima 已提交
115
		);
116 117 118
	});

	test('Non Basic ASCII 1', () => {
A
Alex Dima 已提交
119 120
		testTextModelDataFromString('Hello,\nZürich',
			{
A
Alex Dima 已提交
121
				EOL: '\n',
122
				lines: [
A
Alex Dima 已提交
123 124 125 126 127
					'Hello,',
					'Zürich'
				],
				containsRTL: false,
				isBasicASCII: false
128
			}
A
Alex Dima 已提交
129
		);
A
Alex Dima 已提交
130 131 132
	});

	test('containsRTL 1', () => {
A
Alex Dima 已提交
133 134
		testTextModelDataFromString('Hello,\nזוהי עובדה מבוססת שדעתו',
			{
A
Alex Dima 已提交
135
				EOL: '\n',
136
				lines: [
A
Alex Dima 已提交
137 138 139 140 141
					'Hello,',
					'זוהי עובדה מבוססת שדעתו'
				],
				containsRTL: true,
				isBasicASCII: false
142
			}
A
Alex Dima 已提交
143
		);
A
Alex Dima 已提交
144 145 146
	});

	test('containsRTL 2', () => {
A
Alex Dima 已提交
147 148
		testTextModelDataFromString('Hello,\nهناك حقيقة مثبتة منذ زمن طويل',
			{
A
Alex Dima 已提交
149
				EOL: '\n',
150
				lines: [
A
Alex Dima 已提交
151 152 153 154 155
					'Hello,',
					'هناك حقيقة مثبتة منذ زمن طويل'
				],
				containsRTL: true,
				isBasicASCII: false
156
			}
A
Alex Dima 已提交
157
		);
A
Alex Dima 已提交
158 159 160 161
	});

});

E
Erich Gamma 已提交
162 163 164 165
suite('Editor Model - TextModel', () => {

	test('getValueLengthInRange', () => {

A
Alex Dima 已提交
166
		let m = TextModel.createFromString('My First Line\r\nMy Second Line\r\nMy Third Line');
A
Alex Dima 已提交
167 168 169 170 171 172 173 174 175 176 177
		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 已提交
178

A
Alex Dima 已提交
179
		m = TextModel.createFromString('My First Line\nMy Second Line\nMy Third Line');
A
Alex Dima 已提交
180 181 182 183 184 185 186 187 188 189 190
		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 已提交
191 192 193 194
	});

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

195
		assertGuess(undefined, undefined, [
196 197 198 199 200
			'x',
			'x',
			'x',
			'x',
			'x',
E
Erich Gamma 已提交
201 202
			'x',
			'x'
203
		], 'no clues');
E
Erich Gamma 已提交
204

205
		assertGuess(false, undefined, [
E
Erich Gamma 已提交
206
			'\tx',
207 208 209 210 211
			'x',
			'x',
			'x',
			'x',
			'x',
E
Erich Gamma 已提交
212
			'x'
213 214
		], 'no spaces, 1xTAB');

215
		assertGuess(true, 2, [
216 217 218 219 220 221 222 223 224
			'  x',
			'x',
			'x',
			'x',
			'x',
			'x',
			'x'
		], '1x2');

225
		assertGuess(false, undefined, [
226 227 228 229 230 231 232 233 234
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx'
		], '7xTAB');

235
		assertGuess(undefined, [2], [
236 237 238 239 240 241 242 243 244
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
		], '4x2, 4xTAB');
245
		assertGuess(false, undefined, [
246 247 248 249 250 251
			'\tx',
			' x',
			'\tx',
			' x',
			'\tx',
			' x',
E
Erich Gamma 已提交
252 253
			'\tx',
			' x'
254
		], '4x1, 4xTAB');
255
		assertGuess(false, undefined, [
256 257 258 259 260 261 262 263 264 265
			'\tx',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
		], '4x2, 5xTAB');
266
		assertGuess(false, undefined, [
267 268 269 270 271 272 273 274 275 276
			'\tx',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'  x',
		], '1x2, 5xTAB');
277
		assertGuess(false, undefined, [
278 279 280
			'\tx',
			'\tx',
			'x',
E
Erich Gamma 已提交
281
			'\tx',
282 283 284 285 286 287
			'x',
			'\tx',
			'x',
			'\tx',
			'    x',
		], '1x4, 5xTAB');
288
		assertGuess(false, undefined, [
289 290 291 292 293 294 295 296 297 298
			'\tx',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'  x',
			'\tx',
			'    x',
		], '1x2, 1x4, 5xTAB');
E
Erich Gamma 已提交
299

300
		assertGuess(undefined, undefined, [
E
Erich Gamma 已提交
301 302 303 304 305 306 307 308 309
			'x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x'
		], '7x1 - 1 space is never guessed as an indentation');
310 311 312 313 314 315 316 317 318 319 320
		assertGuess(true, undefined, [
			'x',
			'          x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x'
		], '1x10, 6x1');
		assertGuess(undefined, undefined, [
E
Erich Gamma 已提交
321 322 323 324
			'',
			'  ',
			'    ',
			'      ',
325 326 327 328
			'        ',
			'          ',
			'            ',
			'              ',
E
Erich Gamma 已提交
329
		], 'whitespace lines don\'t count');
330
		assertGuess(true, 3, [
E
Erich Gamma 已提交
331 332 333
			'x',
			'   x',
			'   x',
334 335 336 337 338 339 340 341 342
			'    x',
			'x',
			'   x',
			'   x',
			'    x',
			'x',
			'   x',
			'   x',
			'    x',
343 344
		], '6x3, 3x4');
		assertGuess(true, 5, [
E
Erich Gamma 已提交
345 346 347
			'x',
			'     x',
			'     x',
348 349 350 351 352 353 354 355 356
			'    x',
			'x',
			'     x',
			'     x',
			'    x',
			'x',
			'     x',
			'     x',
			'    x',
357 358
		], '6x5, 3x4');
		assertGuess(true, 7, [
E
Erich Gamma 已提交
359 360 361
			'x',
			'       x',
			'       x',
362
			'     x',
363 364 365 366 367 368 369 370
			'x',
			'       x',
			'       x',
			'    x',
			'x',
			'       x',
			'       x',
			'    x',
371
		], '6x7, 1x5, 2x4');
372
		assertGuess(true, 2, [
E
Erich Gamma 已提交
373 374 375 376
			'x',
			'  x',
			'  x',
			'  x',
377 378 379 380 381 382 383
			'  x',
			'x',
			'  x',
			'  x',
			'  x',
			'  x',
		], '8x2');
E
Erich Gamma 已提交
384

385
		assertGuess(true, 2, [
E
Erich Gamma 已提交
386 387 388
			'x',
			'  x',
			'  x',
389 390 391 392 393 394 395 396 397 398
			'x',
			'  x',
			'  x',
			'x',
			'  x',
			'  x',
			'x',
			'  x',
			'  x',
		], '8x2');
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',
		], '4x2, 4x4');
413
		assertGuess(true, 2, [
E
Erich Gamma 已提交
414 415 416 417
			'x',
			'  x',
			'  x',
			'    x',
418 419 420 421 422 423 424 425 426
			'x',
			'  x',
			'  x',
			'    x',
			'x',
			'  x',
			'  x',
			'    x',
		], '6x2, 3x4');
427
		assertGuess(true, 2, [
E
Erich Gamma 已提交
428 429 430 431 432
			'x',
			'  x',
			'  x',
			'    x',
			'    x',
433 434 435 436 437 438
			'x',
			'  x',
			'  x',
			'    x',
			'    x',
		], '4x2, 4x4');
439
		assertGuess(true, 2, [
E
Erich Gamma 已提交
440 441 442 443
			'x',
			'  x',
			'    x',
			'    x',
444 445 446 447 448
			'x',
			'  x',
			'    x',
			'    x',
		], '2x2, 4x4');
449
		assertGuess(true, 4, [
E
Erich Gamma 已提交
450 451 452
			'x',
			'    x',
			'    x',
453 454 455 456 457 458 459 460 461 462
			'x',
			'    x',
			'    x',
			'x',
			'    x',
			'    x',
			'x',
			'    x',
			'    x',
		], '8x4');
463
		assertGuess(true, 2, [
E
Erich Gamma 已提交
464 465 466 467 468
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
469 470 471 472 473 474
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
		], '2x2, 4x4, 2x6');
475
		assertGuess(true, 2, [
E
Erich Gamma 已提交
476 477 478 479 480 481 482 483
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
			'      x',
			'        x',
		], '1x2, 2x4, 2x6, 1x8');
484
		assertGuess(true, 4, [
E
Erich Gamma 已提交
485 486 487 488 489 490
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
491 492 493 494 495 496 497
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
		], '6x4, 2x5, 2x8');
498
		assertGuess(true, 4, [
E
Erich Gamma 已提交
499 500 501 502 503 504 505 506
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
		], '3x4, 1x5, 2x8');
507
		assertGuess(true, 4, [
E
Erich Gamma 已提交
508 509 510 511 512 513 514
			'x',
			'x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
515 516 517 518 519 520 521 522
			'x',
			'x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
		], '6x4, 2x5, 4x8');
523
		assertGuess(true, 3, [
E
Erich Gamma 已提交
524 525 526 527 528 529 530 531 532 533 534
			'x',
			' x',
			' x',
			' x',
			' x',
			' x',
			'x',
			'   x',
			'    x',
			'    x',
		], '5x1, 2x0, 1x3, 2x4');
535 536 537 538 539
		assertGuess(false, undefined, [
			'\t x',
			' \t x',
			'\tx'
		], 'mixed whitespace 1');
540
		assertGuess(false, undefined, [
541 542 543
			'\tx',
			'\t    x'
		], 'mixed whitespace 2');
E
Erich Gamma 已提交
544 545
	});

A
Alex Dima 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	test('issue #44991: Wrong indentation size auto-detection', () => {
		assertGuess(true, 4, [
			'a = 10             # 0 space indent',
			'b = 5              # 0 space indent',
			'if a > 10:         # 0 space indent',
			'    a += 1         # 4 space indent      delta 4 spaces',
			'    if b > 5:      # 4 space indent',
			'        b += 1     # 8 space indent      delta 4 spaces',
			'        b += 1     # 8 space indent',
			'        b += 1     # 8 space indent',
			'# comment line 1   # 0 space indent      delta 8 spaces',
			'# comment line 2   # 0 space indent',
			'# comment line 3   # 0 space indent',
			'        b += 1     # 8 space indent      delta 8 spaces',
			'        b += 1     # 8 space indent',
			'        b += 1     # 8 space indent',
		]);
	});

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
	test('issue #55818: Broken indentation detection', () => {
		assertGuess(true, 2, [
			'',
			'/* REQUIRE */',
			'',
			'const foo = require ( \'foo\' ),',
			'      bar = require ( \'bar\' );',
			'',
			'/* MY FN */',
			'',
			'function myFn () {',
			'',
			'  const asd = 1,',
			'        dsa = 2;',
			'',
			'  return bar ( foo ( asd ) );',
			'',
			'}',
			'',
			'/* EXPORT */',
			'',
			'module.exports = myFn;',
			'',
		]);
	});

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
	test('issue #70832: Broken indentation detection', () => {
		assertGuess(false, undefined, [
			'x',
			'x',
			'x',
			'x',
			'	x',
			'		x',
			'    x',
			'		x',
			'	x',
			'		x',
			'	x',
			'	x',
			'	x',
			'	x',
			'x',
		]);
	});

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	test('issue #62143: Broken indentation detection', () => {
		// works before the fix
		assertGuess(true, 2, [
			'x',
			'x',
			'  x',
			'  x'
		]);

		// works before the fix
		assertGuess(true, 2, [
			'x',
			'  - item2',
			'  - item3'
		]);

		// works before the fix
		testGuessIndentation(true, 2, true, 2, [
			'x x',
			'  x',
			'  x',
		]);

		// fails before the fix
		// empty space inline breaks the indentation guess
		testGuessIndentation(true, 2, true, 2, [
			'x x',
			'  x',
			'  x',
			'    x'
		]);

		testGuessIndentation(true, 2, true, 2, [
			'<!--test1.md -->',
			'- item1',
			'  - item2',
			'    - item3'
		]);
	});

A
aioute Gao 已提交
651 652
	test('validatePosition', () => {

A
Alex Dima 已提交
653
		let m = TextModel.createFromString('line one\nline two');
A
aioute Gao 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672

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

673 674 675 676 677 678 679 680 681
		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 已提交
682
		let m = TextModel.createFromString('a📚b');
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

		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 已提交
709
		let m = TextModel.createFromString('a📚📚b');
710 711 712 713 714 715 716 717 718 719 720

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

	});

721 722 723 724 725 726 727 728 729 730 731 732
	test('validatePosition handle NaN.', () => {

		let m = TextModel.createFromString('line one\nline two');

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

		assert.deepEqual(m.validatePosition(new Position(NaN, NaN)), new Position(1, 1));
		assert.deepEqual(m.validatePosition(new Position(2, NaN)), new Position(2, 1));
		assert.deepEqual(m.validatePosition(new Position(NaN, 3)), new Position(1, 3));
	});

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	test('issue #71480: validatePosition handle floats', () => {
		let m = TextModel.createFromString('line one\nline two');

		assert.deepEqual(m.validatePosition(new Position(0.2, 1)), new Position(1, 1), 'a');
		assert.deepEqual(m.validatePosition(new Position(1.2, 1)), new Position(1, 1), 'b');
		assert.deepEqual(m.validatePosition(new Position(1.5, 2)), new Position(1, 2), 'c');
		assert.deepEqual(m.validatePosition(new Position(1.8, 3)), new Position(1, 3), 'd');
		assert.deepEqual(m.validatePosition(new Position(1, 0.3)), new Position(1, 1), 'e');
		assert.deepEqual(m.validatePosition(new Position(2, 0.8)), new Position(2, 1), 'f');
		assert.deepEqual(m.validatePosition(new Position(1, 1.2)), new Position(1, 1), 'g');
		assert.deepEqual(m.validatePosition(new Position(2, 1.5)), new Position(2, 1), 'h');
	});

	test('issue #71480: validateRange handle floats', () => {
		let m = TextModel.createFromString('line one\nline two');

		assert.deepEqual(m.validateRange(new Range(0.2, 1.5, 0.8, 2.5)), new Range(1, 1, 1, 1));
		assert.deepEqual(m.validateRange(new Range(1.2, 1.7, 1.8, 2.2)), new Range(1, 1, 1, 2));
	});

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

A
Alex Dima 已提交
755
		let m = TextModel.createFromString('a📚b');
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

		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 已提交
783
		let m = TextModel.createFromString('a📚📚b');
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

		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 已提交
822 823
	});

E
Erich Gamma 已提交
824 825
	test('modifyPosition', () => {

A
Alex Dima 已提交
826
		let m = TextModel.createFromString('line one\nline two');
J
Johannes Rieken 已提交
827 828
		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 已提交
829
		assert.deepEqual(m.modifyPosition(new Position(30, 1), 0), new Position(2, 9));
E
Erich Gamma 已提交
830

J
Johannes Rieken 已提交
831 832 833
		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 已提交
834 835 836
		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 已提交
837

A
Alex Dima 已提交
838
		assert.deepEqual(m.modifyPosition(new Position(2, 9), -17), new Position(1, 1));
J
Johannes Rieken 已提交
839 840
		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 已提交
841 842 843
		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 已提交
844

845 846
		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 已提交
847

848 849 850 851
		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 已提交
852
	});
A
Alex Dima 已提交
853 854

	test('normalizeIndentation 1', () => {
A
Alex Dima 已提交
855
		let model = createTextModel('',
A
Alex Dima 已提交
856
			{
A
Alex Dima 已提交
857
				insertSpaces: false
A
Alex Dima 已提交
858
			}
A
Alex Dima 已提交
859
		);
A
Alex Dima 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886

		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', () => {
A
Alex Dima 已提交
887
		let model = createTextModel('');
A
Alex Dima 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901

		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();
	});
A
Alex Dima 已提交
902 903 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 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

	test('getLineFirstNonWhitespaceColumn', () => {
		let model = TextModel.createFromString([
			'asd',
			' asd',
			'\tasd',
			'  asd',
			'\t\tasd',
			' ',
			'  ',
			'\t',
			'\t\t',
			'  \tasd',
			'',
			''
		].join('\n'));

		assert.equal(model.getLineFirstNonWhitespaceColumn(1), 1, '1');
		assert.equal(model.getLineFirstNonWhitespaceColumn(2), 2, '2');
		assert.equal(model.getLineFirstNonWhitespaceColumn(3), 2, '3');
		assert.equal(model.getLineFirstNonWhitespaceColumn(4), 3, '4');
		assert.equal(model.getLineFirstNonWhitespaceColumn(5), 3, '5');
		assert.equal(model.getLineFirstNonWhitespaceColumn(6), 0, '6');
		assert.equal(model.getLineFirstNonWhitespaceColumn(7), 0, '7');
		assert.equal(model.getLineFirstNonWhitespaceColumn(8), 0, '8');
		assert.equal(model.getLineFirstNonWhitespaceColumn(9), 0, '9');
		assert.equal(model.getLineFirstNonWhitespaceColumn(10), 4, '10');
		assert.equal(model.getLineFirstNonWhitespaceColumn(11), 0, '11');
		assert.equal(model.getLineFirstNonWhitespaceColumn(12), 0, '12');
	});

	test('getLineLastNonWhitespaceColumn', () => {
		let model = TextModel.createFromString([
			'asd',
			'asd ',
			'asd\t',
			'asd  ',
			'asd\t\t',
			' ',
			'  ',
			'\t',
			'\t\t',
			'asd  \t',
			'',
			''
		].join('\n'));

		assert.equal(model.getLineLastNonWhitespaceColumn(1), 4, '1');
		assert.equal(model.getLineLastNonWhitespaceColumn(2), 4, '2');
		assert.equal(model.getLineLastNonWhitespaceColumn(3), 4, '3');
		assert.equal(model.getLineLastNonWhitespaceColumn(4), 4, '4');
		assert.equal(model.getLineLastNonWhitespaceColumn(5), 4, '5');
		assert.equal(model.getLineLastNonWhitespaceColumn(6), 0, '6');
		assert.equal(model.getLineLastNonWhitespaceColumn(7), 0, '7');
		assert.equal(model.getLineLastNonWhitespaceColumn(8), 0, '8');
		assert.equal(model.getLineLastNonWhitespaceColumn(9), 0, '9');
		assert.equal(model.getLineLastNonWhitespaceColumn(10), 4, '10');
		assert.equal(model.getLineLastNonWhitespaceColumn(11), 0, '11');
		assert.equal(model.getLineLastNonWhitespaceColumn(12), 0, '12');
	});
962 963 964 965 966 967

	test('#50471. getValueInRange with invalid range', () => {
		let m = TextModel.createFromString('My First Line\r\nMy Second Line\r\nMy Third Line');
		assert.equal(m.getValueInRange(new Range(1, NaN, 1, 3)), 'My');
		assert.equal(m.getValueInRange(new Range(NaN, NaN, NaN, NaN)), '');
	});
E
Erich Gamma 已提交
968
});
A
Alex Dima 已提交
969

A
Alex Dima 已提交
970 971 972
suite('TextModel.mightContainRTL', () => {

	test('nope', () => {
A
Alex Dima 已提交
973
		let model = TextModel.createFromString('hello world!');
A
Alex Dima 已提交
974 975 976 977
		assert.equal(model.mightContainRTL(), false);
	});

	test('yes', () => {
A
Alex Dima 已提交
978
		let model = TextModel.createFromString('Hello,\nזוהי עובדה מבוססת שדעתו');
A
Alex Dima 已提交
979 980 981 982
		assert.equal(model.mightContainRTL(), true);
	});

	test('setValue resets 1', () => {
A
Alex Dima 已提交
983
		let model = TextModel.createFromString('hello world!');
A
Alex Dima 已提交
984 985 986 987 988 989
		assert.equal(model.mightContainRTL(), false);
		model.setValue('Hello,\nזוהי עובדה מבוססת שדעתו');
		assert.equal(model.mightContainRTL(), true);
	});

	test('setValue resets 2', () => {
A
Alex Dima 已提交
990
		let model = TextModel.createFromString('Hello,\nهناك حقيقة مثبتة منذ زمن طويل');
A
Alex Dima 已提交
991 992 993 994 995
		assert.equal(model.mightContainRTL(), true);
		model.setValue('hello world!');
		assert.equal(model.mightContainRTL(), false);
	});

A
Alex Dima 已提交
996
});
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

suite('TextModel.createSnapshot', () => {

	test('empty file', () => {
		let model = TextModel.createFromString('');
		let snapshot = model.createSnapshot();
		assert.equal(snapshot.read(), null);
		model.dispose();
	});

	test('file with BOM', () => {
		let model = TextModel.createFromString(UTF8_BOM_CHARACTER + 'Hello');
		assert.equal(model.getLineContent(1), 'Hello');
		let snapshot = model.createSnapshot(true);
		assert.equal(snapshot.read(), UTF8_BOM_CHARACTER + 'Hello');
		assert.equal(snapshot.read(), null);
		model.dispose();
	});

	test('regular file', () => {
		let model = TextModel.createFromString('My First Line\n\t\tMy Second Line\n    Third Line\n\n1');
		let snapshot = model.createSnapshot();
		assert.equal(snapshot.read(), 'My First Line\n\t\tMy Second Line\n    Third Line\n\n1');
		assert.equal(snapshot.read(), null);
		model.dispose();
	});

	test('large file', () => {
		let lines: string[] = [];
		for (let i = 0; i < 1000; i++) {
			lines[i] = 'Just some text that is a bit long such that it can consume some memory';
		}
		const text = lines.join('\n');

		let model = TextModel.createFromString(text);
		let snapshot = model.createSnapshot();
		let actual = '';

1035
		// 70999 length => at most 2 read calls are necessary
1036 1037 1038 1039 1040
		let tmp1 = snapshot.read();
		assert.ok(tmp1);
		actual += tmp1;

		let tmp2 = snapshot.read();
1041 1042 1043 1044 1045 1046
		if (tmp2 === null) {
			// all good
		} else {
			actual += tmp2;
			assert.equal(snapshot.read(), null);
		}
1047 1048 1049 1050 1051 1052 1053

		assert.equal(actual, text);

		model.dispose();
	});

});