src/vs/editor/common/tokens/sparseTokensStore.ts
260 LOC · 238 covered · 22 uncovered · 69 ranges · 1708 concepts · 34 introducers · 861 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
textModel.ts ×190
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as arrays from '../../../base/common/arrays.js';
import { IRange, Range } from '../core/range.js';
import { LineTokens } from './lineTokens.js';
import { SparseMultilineTokens } from './sparseMultilineTokens.js';
import { ILanguageIdCodec } from '../languages.js';
import { MetadataConsts } from '../encodedTokenAttributes.js';
import { ITextModel } from '../model.js';
/**
* Represents sparse tokens in a text model.
*/
export class SparseTokensStore {
private _pieces: SparseMultilineTokens[];
private _isComplete: boolean;
private readonly _languageIdCodec: ILanguageIdCodec;
constructor(languageIdCodec: ILanguageIdCodec) {
this._isComplete = false;
this._languageIdCodec = languageIdCodec;
}
public flush(): void {
this._isComplete = false;
}
public isEmpty(): boolean {
}
public set(pieces: SparseMultilineTokens[] | null, isComplete: boolean, textModel: ITextModel | undefined = undefined): void {
this._isComplete = isComplete;
if (textModel) {
p.reportIfInvalid(textModel);
}
}
public setPartial(_range: Range, pieces: SparseMultilineTokens[]): Range {
// console.log(`setPartial ${_range} ${pieces.map(p => p.toString()).join(', ')}`);
sparseTokensStore.ts ×6
let range = _range;
if (pieces.length > 0) {
const _lastRange = pieces[pieces.length - 1].getRange();
if (!_firstRange || !_lastRange) {
return _range;
}
}
let insertPosition: { index: number } | null = null;
for (let i = 0, len = this._pieces.length; i < len; i++) {
const piece = this._pieces[i];
if (piece.endLineNumber < range.startLineNumber) {
continue;
}
if (piece.startLineNumber > range.endLineNumber) {
// this piece is after the range, so mark the spot before this piece
// as a good insertion position and stop looping
insertPosition = insertPosition || { index: i };
break;
}
// this piece might intersect with the range
piece.removeTokens(range);
if (piece.isEmpty()) {
this._pieces.splice(i, 1);
i--;
len--;
continue;
}
if (piece.endLineNumber < range.startLineNumber) {
continue;
}
if (piece.startLineNumber > range.endLineNumber) {
insertPosition = insertPosition || { index: i };
continue;
}
// after removal, this piece contains the range
const [a, b] = piece.split(range);
if (a.isEmpty()) {
insertPosition = insertPosition || { index: i };
continue;
}
// this piece is actually before the range
continue;
}
i++;
len++;
insertPosition = insertPosition || { index: i };
insertPosition = insertPosition || { index: this._pieces.length };
if (pieces.length > 0) {
this._pieces = arrays.arrayInsert(this._pieces, insertPosition.index, pieces);
sparseMultilineTokens.ts ×4
}
// console.log(`I HAVE ${this._pieces.length} pieces`);
// console.log(`${this._pieces.map(p => p.toString()).join('\n')}`);
return range;
}
public isComplete(): boolean {
return this._isComplete;
}
public addSparseTokens(lineNumber: number, aTokens: LineTokens): LineTokens {
return aTokens;
}
const pieces = this._pieces;
if (pieces.length === 0) {
}
const pieceIndex = SparseTokensStore._findFirstPieceWithLine(pieces, lineNumber);
const bTokens = pieces[pieceIndex].getLineTokens(lineNumber);
if (!bTokens) {
}
const aLen = aTokens.getCount();
const bLen = bTokens.getCount();
let aIndex = 0;
const result: number[] = [];
let resultLen = 0;
let lastEndOffset = 0;
const emitToken = (endOffset: number, metadata: number) => {
if (endOffset <= lastEndOffset) {
}
result[resultLen++] = endOffset;
result[resultLen++] = metadata;
};
for (let bIndex = 0; bIndex < bLen; bIndex++) {
// bTokens is not validated yet, but aTokens is. We want to make sure that the LineTokens we return
// are valid, so we clamp the ranges to ensure that.
const bStartCharacter = Math.min(bTokens.getStartCharacter(bIndex), aTokens.getTextLength());
const bEndCharacter = Math.min(bTokens.getEndCharacter(bIndex), aTokens.getTextLength());
const bMetadata = bTokens.getMetadata(bIndex);
const bMask = (
((bMetadata & MetadataConsts.SEMANTIC_USE_ITALIC) ? MetadataConsts.ITALIC_MASK : 0)
| ((bMetadata & MetadataConsts.SEMANTIC_USE_BOLD) ? MetadataConsts.BOLD_MASK : 0)
| ((bMetadata & MetadataConsts.SEMANTIC_USE_UNDERLINE) ? MetadataConsts.UNDERLINE_MASK : 0)
| ((bMetadata & MetadataConsts.SEMANTIC_USE_STRIKETHROUGH) ? MetadataConsts.STRIKETHROUGH_MASK : 0)
| ((bMetadata & MetadataConsts.SEMANTIC_USE_FOREGROUND) ? MetadataConsts.FOREGROUND_MASK : 0)
| ((bMetadata & MetadataConsts.SEMANTIC_USE_BACKGROUND) ? MetadataConsts.BACKGROUND_MASK : 0)
) >>> 0;
const aMask = (~bMask) >>> 0;
// push any token from `a` that is before `b`
while (aIndex < aLen && aTokens.getEndOffset(aIndex) <= bStartCharacter) {
aIndex++;
}
// push the token from `a` if it intersects the token from `b`
if (aIndex < aLen && aTokens.getStartOffset(aIndex) < bStartCharacter) {
emitToken(bStartCharacter, aTokens.getMetadata(aIndex));
}
// skip any tokens from `a` that are contained inside `b`
while (aIndex < aLen && aTokens.getEndOffset(aIndex) < bEndCharacter) {
emitToken(aTokens.getEndOffset(aIndex), (aTokens.getMetadata(aIndex) & aMask) | (bMetadata & bMask));
aIndex++;
}
if (aIndex < aLen) {
emitToken(bEndCharacter, (aTokens.getMetadata(aIndex) & aMask) | (bMetadata & bMask));
if (aTokens.getEndOffset(aIndex) === bEndCharacter) {
aIndex++;
}
const aMergeIndex = Math.min(Math.max(0, aIndex - 1), aLen - 1);
// push the token from `b`
emitToken(bEndCharacter, (aTokens.getMetadata(aMergeIndex) & aMask) | (bMetadata & bMask));
}
// push the remaining tokens from `a`
while (aIndex < aLen) {
aIndex++;
}
return new LineTokens(new Uint32Array(result), aTokens.getLineContent(), this._languageIdCodec);
private static _findFirstPieceWithLine(pieces: SparseMultilineTokens[], lineNumber: number): number {
let high = pieces.length - 1;
while (low < high) {
if (pieces[mid].endLineNumber < lineNumber) {
while (mid > low && pieces[mid - 1].startLineNumber <= lineNumber && lineNumber <= pieces[mid - 1].endLineNumber) {
sparseTokensStore.ts ×2
mid--;
}
}
return low;
public acceptEdit(range: IRange, eolCount: number, firstLineLength: number, lastLineLength: number, firstCharCode: number): void {
piece.acceptEdit(range, eolCount, firstLineLength, lastLineLength, firstCharCode);
if (piece.isEmpty()) {
this._pieces.splice(i, 1);
i--;
}