textModelTokens.ts 16.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  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 已提交
6
import * as arrays from 'vs/base/common/arrays';
A
Alex Dima 已提交
7 8
import { onUnexpectedError } from 'vs/base/common/errors';
import { LineTokens } from 'vs/editor/common/core/lineTokens';
9 10
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
11
import { TokenizationResult2 } from 'vs/editor/common/core/token';
A
Alex Dima 已提交
12
import { ITextBuffer } from 'vs/editor/common/model';
A
Alex Dima 已提交
13
import { IModelTokensChangedEvent } from 'vs/editor/common/model/textModelEvents';
14
import { ColorId, FontStyle, IState, ITokenizationSupport, LanguageId, LanguageIdentifier, MetadataConsts, StandardTokenType, TokenMetadata } from 'vs/editor/common/modes';
A
Alex Dima 已提交
15
import { nullTokenize2 } from 'vs/editor/common/modes/nullMode';
A
Alex Dima 已提交
16

A
Alex Dima 已提交
17 18 19 20 21 22 23 24 25
function getDefaultMetadata(topLevelLanguageId: LanguageId): number {
	return (
		(topLevelLanguageId << MetadataConsts.LANGUAGEID_OFFSET)
		| (StandardTokenType.Other << MetadataConsts.TOKEN_TYPE_OFFSET)
		| (FontStyle.None << MetadataConsts.FONT_STYLE_OFFSET)
		| (ColorId.DefaultForeground << MetadataConsts.FOREGROUND_OFFSET)
		| (ColorId.DefaultBackground << MetadataConsts.BACKGROUND_OFFSET)
	) >>> 0;
}
A
Alex Dima 已提交
26

A
Alex Dima 已提交
27
const EMPTY_LINE_TOKENS = (new Uint32Array(0)).buffer;
A
Alex Dima 已提交
28

A
Alex Dima 已提交
29
class ModelLineTokens {
A
Alex Dima 已提交
30 31
	_state: IState | null;
	_lineTokens: ArrayBuffer | null;
A
Alex Dima 已提交
32
	_invalid: boolean;
A
Alex Dima 已提交
33

A
Alex Dima 已提交
34
	constructor(state: IState | null) {
A
Alex Dima 已提交
35 36 37 38 39 40 41
		this._state = state;
		this._lineTokens = null;
		this._invalid = true;
	}

	public deleteBeginning(toChIndex: number): void {
		if (this._lineTokens === null || this._lineTokens === EMPTY_LINE_TOKENS) {
A
Alex Dima 已提交
42 43
			return;
		}
A
Alex Dima 已提交
44
		this.delete(0, toChIndex);
A
Alex Dima 已提交
45 46
	}

A
Alex Dima 已提交
47 48 49
	public deleteEnding(fromChIndex: number): void {
		if (this._lineTokens === null || this._lineTokens === EMPTY_LINE_TOKENS) {
			return;
A
Alex Dima 已提交
50 51
		}

A
Alex Dima 已提交
52 53 54
		const tokens = new Uint32Array(this._lineTokens);
		const lineTextLength = tokens[tokens.length - 2];
		this.delete(fromChIndex, lineTextLength);
A
Alex Dima 已提交
55 56
	}

A
Alex Dima 已提交
57 58 59 60
	public delete(fromChIndex: number, toChIndex: number): void {
		if (this._lineTokens === null || this._lineTokens === EMPTY_LINE_TOKENS || fromChIndex === toChIndex) {
			return;
		}
A
Alex Dima 已提交
61

A
Alex Dima 已提交
62 63
		const tokens = new Uint32Array(this._lineTokens);
		const tokensCount = (tokens.length >>> 1);
A
Alex Dima 已提交
64

A
Alex Dima 已提交
65 66 67 68 69
		// special case: deleting everything
		if (fromChIndex === 0 && tokens[tokens.length - 2] === toChIndex) {
			this._lineTokens = EMPTY_LINE_TOKENS;
			return;
		}
A
Alex Dima 已提交
70

A
Alex Dima 已提交
71
		const fromTokenIndex = LineTokens.findIndexInTokensArray(tokens, fromChIndex);
A
Alex Dima 已提交
72 73
		const fromTokenStartOffset = (fromTokenIndex > 0 ? tokens[(fromTokenIndex - 1) << 1] : 0);
		const fromTokenEndOffset = tokens[fromTokenIndex << 1];
A
Alex Dima 已提交
74

A
Alex Dima 已提交
75 76 77 78 79 80 81
		if (toChIndex < fromTokenEndOffset) {
			// the delete range is inside a single token
			const delta = (toChIndex - fromChIndex);
			for (let i = fromTokenIndex; i < tokensCount; i++) {
				tokens[i << 1] -= delta;
			}
			return;
A
Alex Dima 已提交
82 83
		}

A
Alex Dima 已提交
84 85 86 87 88 89 90 91 92
		let dest: number;
		let lastEnd: number;
		if (fromTokenStartOffset !== fromChIndex) {
			tokens[fromTokenIndex << 1] = fromChIndex;
			dest = ((fromTokenIndex + 1) << 1);
			lastEnd = fromChIndex;
		} else {
			dest = (fromTokenIndex << 1);
			lastEnd = fromTokenStartOffset;
A
Alex Dima 已提交
93 94
		}

A
Alex Dima 已提交
95 96 97 98 99 100 101
		const delta = (toChIndex - fromChIndex);
		for (let tokenIndex = fromTokenIndex + 1; tokenIndex < tokensCount; tokenIndex++) {
			const tokenEndOffset = tokens[tokenIndex << 1] - delta;
			if (tokenEndOffset > lastEnd) {
				tokens[dest++] = tokenEndOffset;
				tokens[dest++] = tokens[(tokenIndex << 1) + 1];
				lastEnd = tokenEndOffset;
A
Alex Dima 已提交
102
			}
A
Alex Dima 已提交
103
		}
A
Alex Dima 已提交
104

A
Alex Dima 已提交
105 106 107
		if (dest === tokens.length) {
			// nothing to trim
			return;
A
Alex Dima 已提交
108 109
		}

A
Alex Dima 已提交
110 111 112
		let tmp = new Uint32Array(dest);
		tmp.set(tokens.subarray(0, dest), 0);
		this._lineTokens = tmp.buffer;
A
Alex Dima 已提交
113 114
	}

A
Alex Dima 已提交
115
	public append(_otherTokens: ArrayBuffer | null): void {
A
Alex Dima 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128
		if (_otherTokens === EMPTY_LINE_TOKENS) {
			return;
		}
		if (this._lineTokens === EMPTY_LINE_TOKENS) {
			this._lineTokens = _otherTokens;
			return;
		}
		if (this._lineTokens === null) {
			return;
		}
		if (_otherTokens === null) {
			// cannot determine combined line length...
			this._lineTokens = null;
A
Alex Dima 已提交
129 130
			return;
		}
A
Alex Dima 已提交
131 132 133
		const myTokens = new Uint32Array(this._lineTokens);
		const otherTokens = new Uint32Array(_otherTokens);
		const otherTokensCount = (otherTokens.length >>> 1);
A
Alex Dima 已提交
134

A
Alex Dima 已提交
135 136 137 138 139 140 141
		let result = new Uint32Array(myTokens.length + otherTokens.length);
		result.set(myTokens, 0);
		let dest = myTokens.length;
		const delta = myTokens[myTokens.length - 2];
		for (let i = 0; i < otherTokensCount; i++) {
			result[dest++] = otherTokens[(i << 1)] + delta;
			result[dest++] = otherTokens[(i << 1) + 1];
A
Alex Dima 已提交
142
		}
A
Alex Dima 已提交
143
		this._lineTokens = result.buffer;
A
Alex Dima 已提交
144 145
	}

A
Alex Dima 已提交
146 147 148 149
	public insert(chIndex: number, textLength: number): void {
		if (!this._lineTokens) {
			// nothing to do
			return;
A
Alex Dima 已提交
150 151
		}

A
Alex Dima 已提交
152 153 154
		const tokens = new Uint32Array(this._lineTokens);
		const tokensCount = (tokens.length >>> 1);

A
Alex Dima 已提交
155
		let fromTokenIndex = LineTokens.findIndexInTokensArray(tokens, chIndex);
A
Alex Dima 已提交
156
		if (fromTokenIndex > 0) {
A
Alex Dima 已提交
157
			const fromTokenStartOffset = tokens[(fromTokenIndex - 1) << 1];
A
Alex Dima 已提交
158 159 160 161 162 163 164 165
			if (fromTokenStartOffset === chIndex) {
				fromTokenIndex--;
			}
		}
		for (let tokenIndex = fromTokenIndex; tokenIndex < tokensCount; tokenIndex++) {
			tokens[tokenIndex << 1] += textLength;
		}
	}
A
Alex Dima 已提交
166
}
167

A
Alex Dima 已提交
168
class TokensStore {
A
Alex Dima 已提交
169
	private _tokens: ModelLineTokens[];
A
Alex Dima 已提交
170 171
	_invalidLineStartIndex: number;
	_lastState: IState | null;
172

A
Alex Dima 已提交
173
	constructor(initialState: IState | null) {
A
Alex Dima 已提交
174
		this._tokens = [];
175 176
		this._invalidLineStartIndex = 0;
		this._lastState = null;
A
Alex Dima 已提交
177 178 179 180

		if (initialState) {
			this._tokens[0] = new ModelLineTokens(initialState);
		}
181 182
	}

A
Alex Dima 已提交
183
	public get invalidLineStartIndex() {
184 185 186
		return this._invalidLineStartIndex;
	}

A
Alex Dima 已提交
187
	public getTokens(topLevelLanguageId: LanguageId, lineIndex: number, lineText: string): LineTokens {
A
Alex Dima 已提交
188
		let rawLineTokens: ArrayBuffer | null = null;
P
Peng Lyu 已提交
189
		if (lineIndex < this._tokens.length && this._tokens[lineIndex]) {
A
Alex Dima 已提交
190 191
			rawLineTokens = this._tokens[lineIndex]._lineTokens;
		}
192

A
Alex Dima 已提交
193 194 195
		if (rawLineTokens !== null && rawLineTokens !== EMPTY_LINE_TOKENS) {
			return new LineTokens(new Uint32Array(rawLineTokens), lineText);
		}
196

A
Alex Dima 已提交
197 198 199 200
		let lineTokens = new Uint32Array(2);
		lineTokens[0] = lineText.length;
		lineTokens[1] = getDefaultMetadata(topLevelLanguageId);
		return new LineTokens(lineTokens, lineText);
201 202
	}

A
Alex Dima 已提交
203
	public invalidateLine(lineIndex: number): void {
204 205
		this._setIsInvalid(lineIndex, true);
		if (lineIndex < this._invalidLineStartIndex) {
A
Alex Dima 已提交
206
			this._setIsInvalid(this._invalidLineStartIndex, true);
207 208 209 210
			this._invalidLineStartIndex = lineIndex;
		}
	}

A
Alex Dima 已提交
211
	private _setIsInvalid(lineIndex: number, invalid: boolean): void {
P
Peng Lyu 已提交
212
		if (lineIndex < this._tokens.length && this._tokens[lineIndex]) {
A
Alex Dima 已提交
213 214
			this._tokens[lineIndex]._invalid = invalid;
		}
215 216
	}

A
Alex Dima 已提交
217
	public isInvalid(lineIndex: number): boolean {
P
Peng Lyu 已提交
218
		if (lineIndex < this._tokens.length && this._tokens[lineIndex]) {
A
Alex Dima 已提交
219 220 221
			return this._tokens[lineIndex]._invalid;
		}
		return true;
222 223
	}

A
Alex Dima 已提交
224
	public getState(lineIndex: number): IState | null {
P
Peng Lyu 已提交
225
		if (lineIndex < this._tokens.length && this._tokens[lineIndex]) {
A
Alex Dima 已提交
226 227 228
			return this._tokens[lineIndex]._state;
		}
		return null;
229 230
	}

A
Alex Dima 已提交
231
	public setTokens(topLevelLanguageId: LanguageId, lineIndex: number, lineTextLength: number, tokens: Uint32Array): void {
A
Alex Dima 已提交
232
		let target: ModelLineTokens;
P
Peng Lyu 已提交
233
		if (lineIndex < this._tokens.length && this._tokens[lineIndex]) {
A
Alex Dima 已提交
234 235 236 237 238 239 240
			target = this._tokens[lineIndex];
		} else {
			target = new ModelLineTokens(null);
			this._tokens[lineIndex] = target;
		}

		if (lineTextLength === 0) {
241 242 243 244 245 246 247 248 249
			let hasDifferentLanguageId = false;
			if (tokens && tokens.length > 1) {
				hasDifferentLanguageId = (TokenMetadata.getLanguageId(tokens[1]) !== topLevelLanguageId);
			}

			if (!hasDifferentLanguageId) {
				target._lineTokens = EMPTY_LINE_TOKENS;
				return;
			}
A
Alex Dima 已提交
250 251 252 253 254 255 256 257 258
		}

		if (!tokens || tokens.length === 0) {
			tokens = new Uint32Array(2);
			tokens[0] = 0;
			tokens[1] = getDefaultMetadata(topLevelLanguageId);
		}

		LineTokens.convertToEndOffset(tokens, lineTextLength);
259

A
Alex Dima 已提交
260
		target._lineTokens = tokens.buffer;
261 262
	}

A
Alex Dima 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	public setGoodTokens(topLevelLanguageId: LanguageId, linesLength: number, lineIndex: number, text: string, r: TokenizationResult2): void {
		const endStateIndex = lineIndex + 1;
		this.setTokens(topLevelLanguageId, lineIndex, text.length, r.tokens);
		this._setIsInvalid(lineIndex, false);

		if (endStateIndex < linesLength) {
			const previousEndState = this.getState(endStateIndex);
			if (previousEndState !== null && r.endState.equals(previousEndState)) {
				// The end state of this line remains the same
				let nextInvalidLineIndex = lineIndex + 1;
				while (nextInvalidLineIndex < linesLength) {
					if (this.isInvalid(nextInvalidLineIndex)) {
						break;
					}
					if (nextInvalidLineIndex + 1 < linesLength) {
						if (this.getState(nextInvalidLineIndex + 1) === null) {
							break;
						}
					} else {
						if (this._lastState === null) {
							break;
						}
					}
					nextInvalidLineIndex++;
				}
				this._invalidLineStartIndex = nextInvalidLineIndex;
			} else {
				this._invalidLineStartIndex = lineIndex + 1;
				this.setState(endStateIndex, r.endState);
			}
		} else {
			this._lastState = r.endState;
			this._invalidLineStartIndex = linesLength;
		}
	}

	public setState(lineIndex: number, state: IState): void {
P
Peng Lyu 已提交
300
		if (lineIndex < this._tokens.length && this._tokens[lineIndex]) {
A
Alex Dima 已提交
301 302 303 304 305
			this._tokens[lineIndex]._state = state;
		} else {
			const tmp = new ModelLineTokens(state);
			this._tokens[lineIndex] = tmp;
		}
306 307
	}

308
	//#region Editing
A
Alex Dima 已提交
309

310
	public applyEdits(range: Range, eolCount: number, firstLineLength: number): void {
A
Alex Dima 已提交
311 312

		const deletingLinesCnt = range.endLineNumber - range.startLineNumber;
313
		const insertingLinesCnt = eolCount;
A
Alex Dima 已提交
314 315 316
		const editingLinesCnt = Math.min(deletingLinesCnt, insertingLinesCnt);

		for (let j = editingLinesCnt; j >= 0; j--) {
317
			this.invalidateLine(range.startLineNumber + j - 1);
A
Alex Dima 已提交
318 319
		}

A
Alex Dima 已提交
320
		this._acceptDeleteRange(range);
321
		this._acceptInsertText(new Position(range.startLineNumber, range.startColumn), eolCount, firstLineLength);
322 323
	}

A
Alex Dima 已提交
324
	private _acceptDeleteRange(range: Range): void {
325

A
Alex Dima 已提交
326 327 328 329
		const firstLineIndex = range.startLineNumber - 1;
		if (firstLineIndex >= this._tokens.length) {
			return;
		}
330

A
Alex Dima 已提交
331 332 333 334 335
		if (range.startLineNumber === range.endLineNumber) {
			if (range.startColumn === range.endColumn) {
				// Nothing to delete
				return;
			}
336

A
Alex Dima 已提交
337 338 339
			this._tokens[firstLineIndex].delete(range.startColumn - 1, range.endColumn - 1);
			return;
		}
340

A
Alex Dima 已提交
341 342
		const firstLine = this._tokens[firstLineIndex];
		firstLine.deleteEnding(range.startColumn - 1);
343

A
Alex Dima 已提交
344
		const lastLineIndex = range.endLineNumber - 1;
A
Alex Dima 已提交
345
		let lastLineTokens: ArrayBuffer | null = null;
A
Alex Dima 已提交
346 347 348 349 350
		if (lastLineIndex < this._tokens.length) {
			const lastLine = this._tokens[lastLineIndex];
			lastLine.deleteBeginning(range.endColumn - 1);
			lastLineTokens = lastLine._lineTokens;
		}
351

A
Alex Dima 已提交
352 353 354 355 356
		// Take remaining text on last line and append it to remaining text on first line
		firstLine.append(lastLineTokens);

		// Delete middle lines
		this._tokens.splice(range.startLineNumber, range.endLineNumber - range.startLineNumber);
357 358
	}

359
	private _acceptInsertText(position: Position, eolCount: number, firstLineLength: number): void {
A
Alex Dima 已提交
360

361
		if (eolCount === 0 && firstLineLength === 0) {
A
Alex Dima 已提交
362 363 364 365 366 367 368 369 370
			// Nothing to insert
			return;
		}

		const lineIndex = position.lineNumber - 1;
		if (lineIndex >= this._tokens.length) {
			return;
		}

371
		if (eolCount === 0) {
A
Alex Dima 已提交
372
			// Inserting text on one line
373
			this._tokens[lineIndex].insert(position.column - 1, firstLineLength);
A
Alex Dima 已提交
374 375 376 377 378
			return;
		}

		const line = this._tokens[lineIndex];
		line.deleteEnding(position.column - 1);
379
		line.insert(position.column - 1, firstLineLength);
A
Alex Dima 已提交
380

381 382
		let insert: ModelLineTokens[] = new Array<ModelLineTokens>(eolCount);
		for (let i = eolCount - 1; i >= 0; i--) {
A
Alex Dima 已提交
383 384 385 386
			insert[i] = new ModelLineTokens(null);
		}
		this._tokens = arrays.arrayInsert(this._tokens, position.lineNumber, insert);
	}
387 388

	//#endregion
A
Alex Dima 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
}

export class ModelLinesTokens {

	public readonly languageIdentifier: LanguageIdentifier;
	public readonly tokenizationSupport: ITokenizationSupport | null;
	public readonly store: TokensStore;

	constructor(languageIdentifier: LanguageIdentifier, tokenizationSupport: ITokenizationSupport | null) {
		this.languageIdentifier = languageIdentifier;
		this.tokenizationSupport = tokenizationSupport;

		let initialState: IState | null = null;
		if (this.tokenizationSupport) {
			try {
				initialState = this.tokenizationSupport.getInitialState();
			} catch (e) {
				onUnexpectedError(e);
				this.tokenizationSupport = null;
			}
		}
		this.store = new TokensStore(initialState);
	}

	public get invalidLineStartIndex() {
		return this.store.invalidLineStartIndex;
	}

	public getTokens(topLevelLanguageId: LanguageId, lineIndex: number, lineText: string): LineTokens {
		return this.store.getTokens(topLevelLanguageId, lineIndex, lineText);
	}

	public isCheapToTokenize(lineNumber: number): boolean {
		const firstInvalidLineNumber = this.store.invalidLineStartIndex + 1;
		return (firstInvalidLineNumber >= lineNumber);
	}

	public hasLinesToTokenize(buffer: ITextBuffer): boolean {
		return (this.store.invalidLineStartIndex < buffer.getLineCount());
	}

	_isInvalid(lineIndex: number): boolean {
		return this.store.isInvalid(lineIndex);
	}

	_getState(lineIndex: number): IState | null {
		return this.store.getState(lineIndex);
	}

	_setTokens(topLevelLanguageId: LanguageId, lineIndex: number, lineTextLength: number, tokens: Uint32Array): void {
		this.store.setTokens(topLevelLanguageId, lineIndex, lineTextLength, tokens);
	}

	_setState(lineIndex: number, state: IState): void {
		this.store.setState(lineIndex, state);
	}

	//#region Editing

	public applyEdits(range: Range, eolCount: number, firstLineLength: number): void {
		this.store.applyEdits(range, eolCount, firstLineLength);
	}

	_invalidateLine(lineIndex: number): void {
		this.store.invalidateLine(lineIndex);
	}

	//#endregion
457 458 459

	//#region Tokenization

A
Alex Dima 已提交
460
	public _tokenizeOneLine(buffer: ITextBuffer, eventBuilder: ModelTokensChangedEventBuilder): number {
461 462 463
		if (!this.hasLinesToTokenize(buffer)) {
			return buffer.getLineCount() + 1;
		}
A
Alex Dima 已提交
464
		const lineNumber = this.store.invalidLineStartIndex + 1;
465 466 467 468
		this._updateTokensUntilLine(buffer, eventBuilder, lineNumber);
		return lineNumber;
	}

469
	public _tokenizeText(buffer: ITextBuffer, text: string, state: IState): TokenizationResult2 {
A
Alex Dima 已提交
470
		let r: TokenizationResult2 | null = null;
P
Peng Lyu 已提交
471

A
Alex Dima 已提交
472 473 474 475 476 477
		if (this.tokenizationSupport) {
			try {
				r = this.tokenizationSupport.tokenize2(text, state, 0);
			} catch (e) {
				onUnexpectedError(e);
			}
P
Peng Lyu 已提交
478 479 480 481 482 483 484 485
		}

		if (!r) {
			r = nullTokenize2(this.languageIdentifier.id, text, state, 0);
		}
		return r;
	}

A
Alex Dima 已提交
486
	public _updateTokensUntilLine(buffer: ITextBuffer, eventBuilder: ModelTokensChangedEventBuilder, lineNumber: number): void {
487
		if (!this.tokenizationSupport) {
A
Alex Dima 已提交
488
			this.store._invalidLineStartIndex = buffer.getLineCount();
489 490 491 492 493 494 495
			return;
		}

		const linesLength = buffer.getLineCount();
		const endLineIndex = lineNumber - 1;

		// Validate all states up to and including endLineIndex
A
Alex Dima 已提交
496
		for (let lineIndex = this.store.invalidLineStartIndex; lineIndex <= endLineIndex; lineIndex++) {
497
			const text = buffer.getLineContent(lineIndex + 1);
A
Alex Dima 已提交
498 499 500
			const lineStartState = this._getState(lineIndex);

			let r: TokenizationResult2 | null = null;
501 502 503

			try {
				// Tokenize only the first X characters
A
Alex Dima 已提交
504
				let freshState = lineStartState!.clone();
505 506 507 508 509 510
				r = this.tokenizationSupport.tokenize2(text, freshState, 0);
			} catch (e) {
				onUnexpectedError(e);
			}

			if (!r) {
A
Alex Dima 已提交
511
				r = nullTokenize2(this.languageIdentifier.id, text, lineStartState, 0);
512
			}
A
Alex Dima 已提交
513
			this.store.setGoodTokens(this.languageIdentifier.id, linesLength, lineIndex, text, r);
514
			eventBuilder.registerChangedTokens(lineIndex + 1);
A
Alex Dima 已提交
515
			lineIndex = this.store.invalidLineStartIndex - 1; // -1 because the outer loop increments it
516 517 518 519 520 521 522 523
		}
	}

	// #endregion
}

export class ModelTokensChangedEventBuilder {

524
	private readonly _ranges: { fromLineNumber: number; toLineNumber: number; }[];
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

	constructor() {
		this._ranges = [];
	}

	public registerChangedTokens(lineNumber: number): void {
		const ranges = this._ranges;
		const rangesLength = ranges.length;
		const previousRange = rangesLength > 0 ? ranges[rangesLength - 1] : null;

		if (previousRange && previousRange.toLineNumber === lineNumber - 1) {
			// extend previous range
			previousRange.toLineNumber++;
		} else {
			// insert new range
			ranges[rangesLength] = {
				fromLineNumber: lineNumber,
				toLineNumber: lineNumber
			};
		}
	}

A
Alex Dima 已提交
547
	public build(): IModelTokensChangedEvent | null {
548 549 550 551
		if (this._ranges.length === 0) {
			return null;
		}
		return {
552
			tokenizationSupportChanged: false,
553 554 555
			ranges: this._ranges
		};
	}
556
}