Path: blob/main/src/vs/editor/common/encodedTokenAttributes.ts
3292 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*--------------------------------------------------------------------------------------------*/45/**6* Open ended enum at runtime7*/8export const enum LanguageId {9Null = 0,10PlainText = 111}1213/**14* A font style. Values are 2^x such that a bit mask can be used.15*/16export const enum FontStyle {17NotSet = -1,18None = 0,19Italic = 1,20Bold = 2,21Underline = 4,22Strikethrough = 8,23}2425/**26* Open ended enum at runtime27*/28export const enum ColorId {29None = 0,30DefaultForeground = 1,31DefaultBackground = 232}3334/**35* A standard token type.36*/37export const enum StandardTokenType {38Other = 0,39Comment = 1,40String = 2,41RegEx = 342}4344/**45* Helpers to manage the "collapsed" metadata of an entire StackElement stack.46* The following assumptions have been made:47* - languageId < 256 => needs 8 bits48* - unique color count < 512 => needs 9 bits49*50* The binary format is:51* - -------------------------------------------52* 3322 2222 2222 1111 1111 1100 0000 000053* 1098 7654 3210 9876 5432 1098 7654 321054* - -------------------------------------------55* xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx56* bbbb bbbb ffff ffff fFFF FBTT LLLL LLLL57* - -------------------------------------------58* - L = LanguageId (8 bits)59* - T = StandardTokenType (2 bits)60* - B = Balanced bracket (1 bit)61* - F = FontStyle (4 bits)62* - f = foreground color (9 bits)63* - b = background color (8 bits)64*65*/66export const enum MetadataConsts {67LANGUAGEID_MASK /* */ = 0b00000000_00000000_00000000_11111111,68TOKEN_TYPE_MASK /* */ = 0b00000000_00000000_00000011_00000000,69BALANCED_BRACKETS_MASK /* */ = 0b00000000_00000000_00000100_00000000,70FONT_STYLE_MASK /* */ = 0b00000000_00000000_01111000_00000000,71FOREGROUND_MASK /* */ = 0b00000000_11111111_10000000_00000000,72BACKGROUND_MASK /* */ = 0b11111111_00000000_00000000_00000000,7374ITALIC_MASK /* */ = 0b00000000_00000000_00001000_00000000,75BOLD_MASK /* */ = 0b00000000_00000000_00010000_00000000,76UNDERLINE_MASK /* */ = 0b00000000_00000000_00100000_00000000,77STRIKETHROUGH_MASK /* */ = 0b00000000_00000000_01000000_00000000,7879// Semantic tokens cannot set the language id, so we can80// use the first 8 bits for control purposes81SEMANTIC_USE_ITALIC /* */ = 0b00000000_00000000_00000000_00000001,82SEMANTIC_USE_BOLD /* */ = 0b00000000_00000000_00000000_00000010,83SEMANTIC_USE_UNDERLINE /* */ = 0b00000000_00000000_00000000_00000100,84SEMANTIC_USE_STRIKETHROUGH /* */ = 0b00000000_00000000_00000000_00001000,85SEMANTIC_USE_FOREGROUND /* */ = 0b00000000_00000000_00000000_00010000,86SEMANTIC_USE_BACKGROUND /* */ = 0b00000000_00000000_00000000_00100000,8788LANGUAGEID_OFFSET = 0,89TOKEN_TYPE_OFFSET = 8,90BALANCED_BRACKETS_OFFSET = 10,91FONT_STYLE_OFFSET = 11,92FOREGROUND_OFFSET = 15,93BACKGROUND_OFFSET = 2494}9596/**97*/98export class TokenMetadata {99100public static getLanguageId(metadata: number): LanguageId {101return (metadata & MetadataConsts.LANGUAGEID_MASK) >>> MetadataConsts.LANGUAGEID_OFFSET;102}103104public static getTokenType(metadata: number): StandardTokenType {105return (metadata & MetadataConsts.TOKEN_TYPE_MASK) >>> MetadataConsts.TOKEN_TYPE_OFFSET;106}107108public static containsBalancedBrackets(metadata: number): boolean {109return (metadata & MetadataConsts.BALANCED_BRACKETS_MASK) !== 0;110}111112public static getFontStyle(metadata: number): FontStyle {113return (metadata & MetadataConsts.FONT_STYLE_MASK) >>> MetadataConsts.FONT_STYLE_OFFSET;114}115116public static getForeground(metadata: number): ColorId {117return (metadata & MetadataConsts.FOREGROUND_MASK) >>> MetadataConsts.FOREGROUND_OFFSET;118}119120public static getBackground(metadata: number): ColorId {121return (metadata & MetadataConsts.BACKGROUND_MASK) >>> MetadataConsts.BACKGROUND_OFFSET;122}123124public static getClassNameFromMetadata(metadata: number): string {125const foreground = this.getForeground(metadata);126let className = 'mtk' + foreground;127128const fontStyle = this.getFontStyle(metadata);129if (fontStyle & FontStyle.Italic) {130className += ' mtki';131}132if (fontStyle & FontStyle.Bold) {133className += ' mtkb';134}135if (fontStyle & FontStyle.Underline) {136className += ' mtku';137}138if (fontStyle & FontStyle.Strikethrough) {139className += ' mtks';140}141142return className;143}144145public static getInlineStyleFromMetadata(metadata: number, colorMap: string[]): string {146const foreground = this.getForeground(metadata);147const fontStyle = this.getFontStyle(metadata);148149let result = `color: ${colorMap[foreground]};`;150if (fontStyle & FontStyle.Italic) {151result += 'font-style: italic;';152}153if (fontStyle & FontStyle.Bold) {154result += 'font-weight: bold;';155}156let textDecoration = '';157if (fontStyle & FontStyle.Underline) {158textDecoration += ' underline';159}160if (fontStyle & FontStyle.Strikethrough) {161textDecoration += ' line-through';162}163if (textDecoration) {164result += `text-decoration:${textDecoration};`;165166}167return result;168}169170public static getPresentationFromMetadata(metadata: number): ITokenPresentation {171const foreground = this.getForeground(metadata);172const fontStyle = this.getFontStyle(metadata);173174return {175foreground: foreground,176italic: Boolean(fontStyle & FontStyle.Italic),177bold: Boolean(fontStyle & FontStyle.Bold),178underline: Boolean(fontStyle & FontStyle.Underline),179strikethrough: Boolean(fontStyle & FontStyle.Strikethrough),180};181}182}183184/**185*/186export interface ITokenPresentation {187foreground: ColorId;188italic: boolean;189bold: boolean;190underline: boolean;191strikethrough: boolean;192}193194195