Path: blob/main/src/vs/editor/common/languages/supports.ts
3294 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IViewLineTokens, LineTokens } from '../tokens/lineTokens.js';6import { StandardTokenType } from '../encodedTokenAttributes.js';7import { ILanguageIdCodec } from '../languages.js';89export function createScopedLineTokens(context: LineTokens, offset: number): ScopedLineTokens {10const tokenCount = context.getCount();11const tokenIndex = context.findTokenIndexAtOffset(offset);12const desiredLanguageId = context.getLanguageId(tokenIndex);1314let lastTokenIndex = tokenIndex;15while (lastTokenIndex + 1 < tokenCount && context.getLanguageId(lastTokenIndex + 1) === desiredLanguageId) {16lastTokenIndex++;17}1819let firstTokenIndex = tokenIndex;20while (firstTokenIndex > 0 && context.getLanguageId(firstTokenIndex - 1) === desiredLanguageId) {21firstTokenIndex--;22}2324return new ScopedLineTokens(25context,26desiredLanguageId,27firstTokenIndex,28lastTokenIndex + 1,29context.getStartOffset(firstTokenIndex),30context.getEndOffset(lastTokenIndex)31);32}3334export class ScopedLineTokens {35_scopedLineTokensBrand: void = undefined;3637public readonly languageIdCodec: ILanguageIdCodec;38public readonly languageId: string;39private readonly _actual: LineTokens;40private readonly _firstTokenIndex: number;41private readonly _lastTokenIndex: number;42public readonly firstCharOffset: number;43private readonly _lastCharOffset: number;4445constructor(46actual: LineTokens,47languageId: string,48firstTokenIndex: number,49lastTokenIndex: number,50firstCharOffset: number,51lastCharOffset: number52) {53this._actual = actual;54this.languageId = languageId;55this._firstTokenIndex = firstTokenIndex;56this._lastTokenIndex = lastTokenIndex;57this.firstCharOffset = firstCharOffset;58this._lastCharOffset = lastCharOffset;59this.languageIdCodec = actual.languageIdCodec;60}6162public getLineContent(): string {63const actualLineContent = this._actual.getLineContent();64return actualLineContent.substring(this.firstCharOffset, this._lastCharOffset);65}6667public getLineLength(): number {68return this._lastCharOffset - this.firstCharOffset;69}7071public getActualLineContentBefore(offset: number): string {72const actualLineContent = this._actual.getLineContent();73return actualLineContent.substring(0, this.firstCharOffset + offset);74}7576public getTokenCount(): number {77return this._lastTokenIndex - this._firstTokenIndex;78}7980public findTokenIndexAtOffset(offset: number): number {81return this._actual.findTokenIndexAtOffset(offset + this.firstCharOffset) - this._firstTokenIndex;82}8384public getStandardTokenType(tokenIndex: number): StandardTokenType {85return this._actual.getStandardTokenType(tokenIndex + this._firstTokenIndex);86}8788public toIViewLineTokens(): IViewLineTokens {89return this._actual.sliceAndInflate(this.firstCharOffset, this._lastCharOffset, 0);90}91}9293const enum IgnoreBracketsInTokens {94value = StandardTokenType.Comment | StandardTokenType.String | StandardTokenType.RegEx95}9697export function ignoreBracketsInToken(standardTokenType: StandardTokenType): boolean {98return (standardTokenType & IgnoreBracketsInTokens.value) !== 0;99}100101102