|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +/** |
| 7 | + * Open ended enum at runtime |
| 8 | + */ |
| 9 | +export const enum LanguageId { |
| 10 | + Null = 0, |
| 11 | + PlainText = 1 |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * A font style. Values are 2^x such that a bit mask can be used. |
| 16 | + */ |
| 17 | +export const enum FontStyle { |
| 18 | + NotSet = -1, |
| 19 | + None = 0, |
| 20 | + Italic = 1, |
| 21 | + Bold = 2, |
| 22 | + Underline = 4, |
| 23 | + Strikethrough = 8, |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Open ended enum at runtime |
| 28 | + */ |
| 29 | +export const enum ColorId { |
| 30 | + None = 0, |
| 31 | + DefaultForeground = 1, |
| 32 | + DefaultBackground = 2 |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * A standard token type. |
| 37 | + */ |
| 38 | +export const enum StandardTokenType { |
| 39 | + Other = 0, |
| 40 | + Comment = 1, |
| 41 | + String = 2, |
| 42 | + RegEx = 3 |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Helpers to manage the "collapsed" metadata of an entire StackElement stack. |
| 47 | + * The following assumptions have been made: |
| 48 | + * - languageId < 256 => needs 8 bits |
| 49 | + * - unique color count < 512 => needs 9 bits |
| 50 | + * |
| 51 | + * The binary format is: |
| 52 | + * - ------------------------------------------- |
| 53 | + * 3322 2222 2222 1111 1111 1100 0000 0000 |
| 54 | + * 1098 7654 3210 9876 5432 1098 7654 3210 |
| 55 | + * - ------------------------------------------- |
| 56 | + * xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx |
| 57 | + * bbbb bbbb ffff ffff fFFF FBTT LLLL LLLL |
| 58 | + * - ------------------------------------------- |
| 59 | + * - L = LanguageId (8 bits) |
| 60 | + * - T = StandardTokenType (2 bits) |
| 61 | + * - B = Balanced bracket (1 bit) |
| 62 | + * - F = FontStyle (4 bits) |
| 63 | + * - f = foreground color (9 bits) |
| 64 | + * - b = background color (9 bits) |
| 65 | + * |
| 66 | + */ |
| 67 | +export const enum MetadataConsts { |
| 68 | + LANGUAGEID_MASK = 0b00000000000000000000000011111111, |
| 69 | + TOKEN_TYPE_MASK = 0b00000000000000000000001100000000, |
| 70 | + BALANCED_BRACKETS_MASK = 0b00000000000000000000010000000000, |
| 71 | + FONT_STYLE_MASK = 0b00000000000000000111100000000000, |
| 72 | + FOREGROUND_MASK = 0b00000000111111111000000000000000, |
| 73 | + BACKGROUND_MASK = 0b11111111000000000000000000000000, |
| 74 | + |
| 75 | + ITALIC_MASK = 0b00000000000000000000100000000000, |
| 76 | + BOLD_MASK = 0b00000000000000000001000000000000, |
| 77 | + UNDERLINE_MASK = 0b00000000000000000010000000000000, |
| 78 | + STRIKETHROUGH_MASK = 0b00000000000000000100000000000000, |
| 79 | + |
| 80 | + // Semantic tokens cannot set the language id, so we can |
| 81 | + // use the first 8 bits for control purposes |
| 82 | + SEMANTIC_USE_ITALIC = 0b00000000000000000000000000000001, |
| 83 | + SEMANTIC_USE_BOLD = 0b00000000000000000000000000000010, |
| 84 | + SEMANTIC_USE_UNDERLINE = 0b00000000000000000000000000000100, |
| 85 | + SEMANTIC_USE_STRIKETHROUGH = 0b00000000000000000000000000001000, |
| 86 | + SEMANTIC_USE_FOREGROUND = 0b00000000000000000000000000010000, |
| 87 | + SEMANTIC_USE_BACKGROUND = 0b00000000000000000000000000100000, |
| 88 | + |
| 89 | + LANGUAGEID_OFFSET = 0, |
| 90 | + TOKEN_TYPE_OFFSET = 8, |
| 91 | + BALANCED_BRACKETS_OFFSET = 10, |
| 92 | + FONT_STYLE_OFFSET = 11, |
| 93 | + FOREGROUND_OFFSET = 15, |
| 94 | + BACKGROUND_OFFSET = 24 |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + */ |
| 99 | +export class TokenMetadata { |
| 100 | + |
| 101 | + public static getLanguageId(metadata: number): LanguageId { |
| 102 | + return (metadata & MetadataConsts.LANGUAGEID_MASK) >>> MetadataConsts.LANGUAGEID_OFFSET; |
| 103 | + } |
| 104 | + |
| 105 | + public static getTokenType(metadata: number): StandardTokenType { |
| 106 | + return (metadata & MetadataConsts.TOKEN_TYPE_MASK) >>> MetadataConsts.TOKEN_TYPE_OFFSET; |
| 107 | + } |
| 108 | + |
| 109 | + public static containsBalancedBrackets(metadata: number): boolean { |
| 110 | + return (metadata & MetadataConsts.BALANCED_BRACKETS_MASK) !== 0; |
| 111 | + } |
| 112 | + |
| 113 | + public static getFontStyle(metadata: number): FontStyle { |
| 114 | + return (metadata & MetadataConsts.FONT_STYLE_MASK) >>> MetadataConsts.FONT_STYLE_OFFSET; |
| 115 | + } |
| 116 | + |
| 117 | + public static getForeground(metadata: number): ColorId { |
| 118 | + return (metadata & MetadataConsts.FOREGROUND_MASK) >>> MetadataConsts.FOREGROUND_OFFSET; |
| 119 | + } |
| 120 | + |
| 121 | + public static getBackground(metadata: number): ColorId { |
| 122 | + return (metadata & MetadataConsts.BACKGROUND_MASK) >>> MetadataConsts.BACKGROUND_OFFSET; |
| 123 | + } |
| 124 | + |
| 125 | + public static getClassNameFromMetadata(metadata: number): string { |
| 126 | + const foreground = this.getForeground(metadata); |
| 127 | + let className = 'mtk' + foreground; |
| 128 | + |
| 129 | + const fontStyle = this.getFontStyle(metadata); |
| 130 | + if (fontStyle & FontStyle.Italic) { |
| 131 | + className += ' mtki'; |
| 132 | + } |
| 133 | + if (fontStyle & FontStyle.Bold) { |
| 134 | + className += ' mtkb'; |
| 135 | + } |
| 136 | + if (fontStyle & FontStyle.Underline) { |
| 137 | + className += ' mtku'; |
| 138 | + } |
| 139 | + if (fontStyle & FontStyle.Strikethrough) { |
| 140 | + className += ' mtks'; |
| 141 | + } |
| 142 | + |
| 143 | + return className; |
| 144 | + } |
| 145 | + |
| 146 | + public static getInlineStyleFromMetadata(metadata: number, colorMap: string[]): string { |
| 147 | + const foreground = this.getForeground(metadata); |
| 148 | + const fontStyle = this.getFontStyle(metadata); |
| 149 | + |
| 150 | + let result = `color: ${colorMap[foreground]};`; |
| 151 | + if (fontStyle & FontStyle.Italic) { |
| 152 | + result += 'font-style: italic;'; |
| 153 | + } |
| 154 | + if (fontStyle & FontStyle.Bold) { |
| 155 | + result += 'font-weight: bold;'; |
| 156 | + } |
| 157 | + let textDecoration = ''; |
| 158 | + if (fontStyle & FontStyle.Underline) { |
| 159 | + textDecoration += ' underline'; |
| 160 | + } |
| 161 | + if (fontStyle & FontStyle.Strikethrough) { |
| 162 | + textDecoration += ' line-through'; |
| 163 | + } |
| 164 | + if (textDecoration) { |
| 165 | + result += `text-decoration:${textDecoration};`; |
| 166 | + |
| 167 | + } |
| 168 | + return result; |
| 169 | + } |
| 170 | + |
| 171 | + public static getPresentationFromMetadata(metadata: number): ITokenPresentation { |
| 172 | + const foreground = this.getForeground(metadata); |
| 173 | + const fontStyle = this.getFontStyle(metadata); |
| 174 | + |
| 175 | + return { |
| 176 | + foreground: foreground, |
| 177 | + italic: Boolean(fontStyle & FontStyle.Italic), |
| 178 | + bold: Boolean(fontStyle & FontStyle.Bold), |
| 179 | + underline: Boolean(fontStyle & FontStyle.Underline), |
| 180 | + strikethrough: Boolean(fontStyle & FontStyle.Strikethrough), |
| 181 | + }; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + */ |
| 187 | +export interface ITokenPresentation { |
| 188 | + foreground: ColorId; |
| 189 | + italic: boolean; |
| 190 | + bold: boolean; |
| 191 | + underline: boolean; |
| 192 | + strikethrough: boolean; |
| 193 | +} |
0 commit comments