textModel.test.ts 12.9 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 8 9 10
import * as assert from 'assert';
import {Position} from 'vs/editor/common/core/position';
import {Range} from 'vs/editor/common/core/range';
import {TextModel} from 'vs/editor/common/model/textModel';
11
import {DefaultEndOfLine} from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
12

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

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

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
function assertGuess(expectedInsertSpaces:boolean, expectedTabSize:number, text:string[], msg?:string): void {
	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 已提交
51 52 53 54 55 56
}

suite('Editor Model - TextModel', () => {

	test('getValueLengthInRange', () => {

57
		var m = new TextModel([], TextModel.toRawText('My First Line\r\nMy Second Line\r\nMy Third Line', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
58 59 60 61 62 63 64 65 66 67 68
		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 已提交
69

70
		m = new TextModel([], TextModel.toRawText('My First Line\nMy Second Line\nMy Third Line', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
71 72 73 74 75 76 77 78 79 80 81
		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 已提交
82 83 84 85
	});

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

86
		assertGuess(undefined, undefined, [
87 88 89 90 91
			'x',
			'x',
			'x',
			'x',
			'x',
E
Erich Gamma 已提交
92 93
			'x',
			'x'
94
		], 'no clues');
E
Erich Gamma 已提交
95

96
		assertGuess(false, undefined, [
E
Erich Gamma 已提交
97
			'\tx',
98 99 100 101 102
			'x',
			'x',
			'x',
			'x',
			'x',
E
Erich Gamma 已提交
103
			'x'
104 105
		], 'no spaces, 1xTAB');

106
		assertGuess(true, 2, [
107 108 109 110 111 112 113 114 115
			'  x',
			'x',
			'x',
			'x',
			'x',
			'x',
			'x'
		], '1x2');

116
		assertGuess(false, undefined, [
117 118 119 120 121 122 123 124 125
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx',
			'\tx'
		], '7xTAB');

126
		assertGuess(undefined, 2, [
127 128 129 130 131 132 133 134 135
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
		], '4x2, 4xTAB');
136
		assertGuess(false, undefined, [
137 138 139 140 141 142
			'\tx',
			' x',
			'\tx',
			' x',
			'\tx',
			' x',
E
Erich Gamma 已提交
143 144
			'\tx',
			' x'
145
		], '4x1, 4xTAB');
146
		assertGuess(false, 2, [
147 148 149 150 151 152 153 154 155 156
			'\tx',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
			'\tx',
			'  x',
		], '4x2, 5xTAB');
157
		assertGuess(false, 2, [
158 159 160 161 162 163 164 165 166 167
			'\tx',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'  x',
		], '1x2, 5xTAB');
168
		assertGuess(false, 4, [
169 170 171
			'\tx',
			'\tx',
			'x',
E
Erich Gamma 已提交
172
			'\tx',
173 174 175 176 177 178
			'x',
			'\tx',
			'x',
			'\tx',
			'    x',
		], '1x4, 5xTAB');
179
		assertGuess(false, 2, [
180 181 182 183 184 185 186 187 188 189
			'\tx',
			'\tx',
			'x',
			'\tx',
			'x',
			'\tx',
			'  x',
			'\tx',
			'    x',
		], '1x2, 1x4, 5xTAB');
E
Erich Gamma 已提交
190

191
		assertGuess(undefined, undefined, [
E
Erich Gamma 已提交
192 193 194 195 196 197 198 199 200
			'x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x'
		], '7x1 - 1 space is never guessed as an indentation');
201 202 203 204 205 206 207 208 209 210 211
		assertGuess(true, undefined, [
			'x',
			'          x',
			' x',
			' x',
			' x',
			' x',
			' x',
			' x'
		], '1x10, 6x1');
		assertGuess(undefined, undefined, [
E
Erich Gamma 已提交
212 213 214 215
			'',
			'  ',
			'    ',
			'      ',
216 217 218 219
			'        ',
			'          ',
			'            ',
			'              ',
E
Erich Gamma 已提交
220
		], 'whitespace lines don\'t count');
221
		assertGuess(true, 4, [
E
Erich Gamma 已提交
222 223 224
			'x',
			'   x',
			'   x',
225 226 227 228 229 230 231 232 233 234
			'    x',
			'x',
			'   x',
			'   x',
			'    x',
			'x',
			'   x',
			'   x',
			'    x',
		], 'odd number is not allowed: 6x3, 3x4');
235
		assertGuess(true, 4, [
E
Erich Gamma 已提交
236 237 238
			'x',
			'     x',
			'     x',
239 240 241 242 243 244 245 246 247 248
			'    x',
			'x',
			'     x',
			'     x',
			'    x',
			'x',
			'     x',
			'     x',
			'    x',
		], 'odd number is not allowed: 6x5, 3x4');
249
		assertGuess(true, 4, [
E
Erich Gamma 已提交
250 251 252
			'x',
			'       x',
			'       x',
253 254 255 256 257 258 259 260 261 262
			'    x',
			'x',
			'       x',
			'       x',
			'    x',
			'x',
			'       x',
			'       x',
			'    x',
		], 'odd number is not allowed: 6x7, 3x4');
263
		assertGuess(true, 2, [
E
Erich Gamma 已提交
264 265 266 267
			'x',
			'  x',
			'  x',
			'  x',
268 269 270 271 272 273 274
			'  x',
			'x',
			'  x',
			'  x',
			'  x',
			'  x',
		], '8x2');
E
Erich Gamma 已提交
275

276
		assertGuess(true, 2, [
E
Erich Gamma 已提交
277 278 279
			'x',
			'  x',
			'  x',
280 281 282 283 284 285 286 287 288 289
			'x',
			'  x',
			'  x',
			'x',
			'  x',
			'  x',
			'x',
			'  x',
			'  x',
		], '8x2');
290
		assertGuess(true, 2, [
E
Erich Gamma 已提交
291 292 293
			'x',
			'  x',
			'    x',
294 295 296 297 298 299 300 301 302 303
			'x',
			'  x',
			'    x',
			'x',
			'  x',
			'    x',
			'x',
			'  x',
			'    x',
		], '4x2, 4x4');
304
		assertGuess(true, 2, [
E
Erich Gamma 已提交
305 306 307 308
			'x',
			'  x',
			'  x',
			'    x',
309 310 311 312 313 314 315 316 317
			'x',
			'  x',
			'  x',
			'    x',
			'x',
			'  x',
			'  x',
			'    x',
		], '6x2, 3x4');
318
		assertGuess(true, 2, [
E
Erich Gamma 已提交
319 320 321 322 323
			'x',
			'  x',
			'  x',
			'    x',
			'    x',
324 325 326 327 328 329
			'x',
			'  x',
			'  x',
			'    x',
			'    x',
		], '4x2, 4x4');
330
		assertGuess(true, 2, [
E
Erich Gamma 已提交
331 332 333 334
			'x',
			'  x',
			'    x',
			'    x',
335 336 337 338 339
			'x',
			'  x',
			'    x',
			'    x',
		], '2x2, 4x4');
340
		assertGuess(true, 4, [
E
Erich Gamma 已提交
341 342 343
			'x',
			'    x',
			'    x',
344 345 346 347 348 349 350 351 352 353
			'x',
			'    x',
			'    x',
			'x',
			'    x',
			'    x',
			'x',
			'    x',
			'    x',
		], '8x4');
354
		assertGuess(true, 2, [
E
Erich Gamma 已提交
355 356 357 358 359
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
360 361 362 363 364 365
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
		], '2x2, 4x4, 2x6');
366
		assertGuess(true, 2, [
E
Erich Gamma 已提交
367 368 369 370 371 372 373 374
			'x',
			'  x',
			'    x',
			'    x',
			'      x',
			'      x',
			'        x',
		], '1x2, 2x4, 2x6, 1x8');
375
		assertGuess(true, 4, [
E
Erich Gamma 已提交
376 377 378 379 380 381
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
382 383 384 385 386 387 388
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
		], '6x4, 2x5, 2x8');
389
		assertGuess(true, 4, [
E
Erich Gamma 已提交
390 391 392 393 394 395 396 397
			'x',
			'    x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
		], '3x4, 1x5, 2x8');
398
		assertGuess(true, 4, [
E
Erich Gamma 已提交
399 400 401 402 403 404 405
			'x',
			'x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
406 407 408 409 410 411 412 413
			'x',
			'x',
			'    x',
			'    x',
			'     x',
			'        x',
			'        x',
		], '6x4, 2x5, 4x8');
414
		assertGuess(true, 4, [
E
Erich Gamma 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
			'x',
			' x',
			' x',
			' x',
			' x',
			' x',
			'x',
			'   x',
			'    x',
			'    x',
		], '5x1, 2x0, 1x3, 2x4');
	});

	test('modifyPosition', () => {

430
		var m = new TextModel([], TextModel.toRawText('line one\nline two', TextModel.DEFAULT_CREATION_OPTIONS));
A
Alex Dima 已提交
431 432 433
		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));
		assert.deepEqual(m.modifyPosition(new Position(30, 1), 0), new Position(2, 1));
E
Erich Gamma 已提交
434

A
Alex Dima 已提交
435 436 437 438 439 440
		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));
		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 已提交
441

A
Alex Dima 已提交
442 443 444 445 446 447
		assert.deepEqual(m.modifyPosition(new Position(2, 9), -17), new Position(1, 1));
		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));
		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 已提交
448

A
Alex Dima 已提交
449 450
		assert.throws(() => m.modifyPosition(new Position(1, 2), 17));
		assert.throws(() => m.modifyPosition(new Position(1, 2), 100));
E
Erich Gamma 已提交
451

A
Alex Dima 已提交
452 453 454 455
		assert.throws(() => m.modifyPosition(new Position(1, 2), -2));
		assert.throws(() => m.modifyPosition(new Position(1, 2), -100));
		assert.throws(() => m.modifyPosition(new Position(2, 2), -100));
		assert.throws(() => m.modifyPosition(new Position(2, 9), -18));
E
Erich Gamma 已提交
456
	});
A
Alex Dima 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

	test('normalizeIndentation 1', () => {
		let model = new TextModel([], {
			length: 0,
			lines: [],
			BOM: '',
			EOL: '\n',
			options: {
				tabSize: 4,
				insertSpaces: false,
				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([], {
			length: 0,
			lines: [],
			BOM: '',
			EOL: '\n',
			options: {
				tabSize: 4,
				insertSpaces: true,
				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 已提交
522
});