|
| 1 | +/* |
| 2 | + * semantic-tokens.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2025 by Posit Software, PBC |
| 5 | + * |
| 6 | + * Unless you have received this program directly from Posit Software pursuant |
| 7 | + * to the terms of a commercial license agreement with Posit Software, then |
| 8 | + * this program is licensed to you under the terms of version 3 of the |
| 9 | + * GNU Affero General Public License. This program is distributed WITHOUT |
| 10 | + * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, |
| 11 | + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the |
| 12 | + * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +import { |
| 17 | + CancellationToken, |
| 18 | + commands, |
| 19 | + Position, |
| 20 | + SemanticTokens, |
| 21 | + SemanticTokensBuilder, |
| 22 | + TextDocument, |
| 23 | + Uri, |
| 24 | + window, |
| 25 | +} from "vscode"; |
| 26 | +import { DocumentSemanticsTokensSignature } from "vscode-languageclient"; |
| 27 | +import { MarkdownEngine } from "../markdown/engine"; |
| 28 | +import { isQuartoDoc } from "../core/doc"; |
| 29 | +import { unadjustedSemanticTokens, virtualDoc, withVirtualDocUri } from "../vdoc/vdoc"; |
| 30 | +import { QUARTO_SEMANTIC_TOKEN_LEGEND } from "quarto-core"; |
| 31 | + |
| 32 | +/** |
| 33 | + * Decode semantic tokens from delta-encoded format to absolute positions |
| 34 | + * |
| 35 | + * Semantic tokens are encoded as [deltaLine, deltaStartChar, length, tokenType, tokenModifiers, ...] |
| 36 | + * This function converts them to absolute line/character positions for easier manipulation. |
| 37 | + */ |
| 38 | +export function decodeSemanticTokens(tokens: SemanticTokens): Array<{ |
| 39 | + line: number; |
| 40 | + startChar: number; |
| 41 | + length: number; |
| 42 | + tokenType: number; |
| 43 | + tokenModifiers: number; |
| 44 | +}> { |
| 45 | + const decoded: Array<{ |
| 46 | + line: number; |
| 47 | + startChar: number; |
| 48 | + length: number; |
| 49 | + tokenType: number; |
| 50 | + tokenModifiers: number; |
| 51 | + }> = []; |
| 52 | + |
| 53 | + let currentLine = 0; |
| 54 | + let currentChar = 0; |
| 55 | + |
| 56 | + for (let i = 0; i < tokens.data.length; i += 5) { |
| 57 | + const deltaLine = tokens.data[i]; |
| 58 | + const deltaStartChar = tokens.data[i + 1]; |
| 59 | + const length = tokens.data[i + 2]; |
| 60 | + const tokenType = tokens.data[i + 3]; |
| 61 | + const tokenModifiers = tokens.data[i + 4]; |
| 62 | + |
| 63 | + // Update absolute position |
| 64 | + currentLine += deltaLine; |
| 65 | + if (deltaLine > 0) { |
| 66 | + currentChar = deltaStartChar; |
| 67 | + } else { |
| 68 | + currentChar += deltaStartChar; |
| 69 | + } |
| 70 | + |
| 71 | + decoded.push({ |
| 72 | + line: currentLine, |
| 73 | + startChar: currentChar, |
| 74 | + length, |
| 75 | + tokenType, |
| 76 | + tokenModifiers |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + return decoded; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Encode semantic tokens from absolute positions to delta-encoded format |
| 85 | + * |
| 86 | + * Uses VS Code's built-in SemanticTokensBuilder for proper delta encoding. |
| 87 | + */ |
| 88 | +export function encodeSemanticTokens( |
| 89 | + tokens: Array<{ |
| 90 | + line: number; |
| 91 | + startChar: number; |
| 92 | + length: number; |
| 93 | + tokenType: number; |
| 94 | + tokenModifiers: number; |
| 95 | + }>, |
| 96 | + resultId?: string |
| 97 | +): SemanticTokens { |
| 98 | + const builder = new SemanticTokensBuilder(); |
| 99 | + |
| 100 | + for (const token of tokens) { |
| 101 | + builder.push( |
| 102 | + token.line, |
| 103 | + token.startChar, |
| 104 | + token.length, |
| 105 | + token.tokenType, |
| 106 | + token.tokenModifiers |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return builder.build(resultId); |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Build a map from source type/modifier names to target indices |
| 115 | + */ |
| 116 | +function buildLegendMap( |
| 117 | + sourceNames: string[], |
| 118 | + targetNames: string[] |
| 119 | +): Map<number, number> { |
| 120 | + const map = new Map<number, number>(); |
| 121 | + |
| 122 | + for (let i = 0; i < sourceNames.length; i++) { |
| 123 | + const targetIndex = targetNames.indexOf(sourceNames[i]); |
| 124 | + if (targetIndex >= 0) { |
| 125 | + map.set(i, targetIndex); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return map; |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Remap a modifier bitfield from source indices to target indices |
| 134 | + */ |
| 135 | +function remapModifierBitfield( |
| 136 | + sourceModifiers: number, |
| 137 | + modifierMap: Map<number, number> |
| 138 | +): number { |
| 139 | + let targetModifiers = 0; |
| 140 | + |
| 141 | + // Check each bit in the source bitfield |
| 142 | + for (const [sourceBit, targetBit] of modifierMap) { |
| 143 | + if (sourceModifiers & (1 << sourceBit)) { |
| 144 | + targetModifiers |= (1 << targetBit); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return targetModifiers; |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Remap token type/modifier indices from source legend to target legend |
| 153 | + * Only maps types that exist in both legends (standard types only) |
| 154 | + */ |
| 155 | +function remapTokenIndices( |
| 156 | + tokens: SemanticTokens, |
| 157 | + sourceLegend: { tokenTypes: string[]; tokenModifiers: string[]; }, |
| 158 | + targetLegend: { tokenTypes: string[]; tokenModifiers: string[]; } |
| 159 | +): SemanticTokens { |
| 160 | + // Build mappings once |
| 161 | + const typeMap = buildLegendMap(sourceLegend.tokenTypes, targetLegend.tokenTypes); |
| 162 | + const modifierMap = buildLegendMap(sourceLegend.tokenModifiers, targetLegend.tokenModifiers); |
| 163 | + |
| 164 | + // Decode, filter, and remap tokens |
| 165 | + const decoded = decodeSemanticTokens(tokens); |
| 166 | + const remapped = decoded |
| 167 | + .filter(token => typeMap.has(token.tokenType)) |
| 168 | + .map(token => ({ |
| 169 | + ...token, |
| 170 | + tokenType: typeMap.get(token.tokenType)!, |
| 171 | + tokenModifiers: remapModifierBitfield(token.tokenModifiers, modifierMap) |
| 172 | + })); |
| 173 | + |
| 174 | + return encodeSemanticTokens(remapped, tokens.resultId); |
| 175 | +} |
| 176 | + |
| 177 | +export function embeddedSemanticTokensProvider(engine: MarkdownEngine) { |
| 178 | + return async ( |
| 179 | + document: TextDocument, |
| 180 | + token: CancellationToken, |
| 181 | + next: DocumentSemanticsTokensSignature |
| 182 | + ): Promise<SemanticTokens | null | undefined> => { |
| 183 | + // Only handle Quarto documents |
| 184 | + if (!isQuartoDoc(document, true)) { |
| 185 | + return await next(document, token); |
| 186 | + } |
| 187 | + |
| 188 | + const editor = window.activeTextEditor; |
| 189 | + const activeDocument = editor?.document; |
| 190 | + if (!editor || activeDocument?.uri.toString() !== document.uri.toString()) { |
| 191 | + // Not the active document, delegate to default |
| 192 | + return await next(document, token); |
| 193 | + } |
| 194 | + |
| 195 | + const line = editor.selection.active.line; |
| 196 | + const position = new Position(line, 0); |
| 197 | + const vdoc = await virtualDoc(document, position, engine); |
| 198 | + |
| 199 | + if (!vdoc) { |
| 200 | + return await next(document, token); |
| 201 | + } |
| 202 | + |
| 203 | + return await withVirtualDocUri(vdoc, document.uri, "semanticTokens", async (uri: Uri) => { |
| 204 | + try { |
| 205 | + // Get the legend from the embedded language provider |
| 206 | + const legend = await commands.executeCommand<any>( |
| 207 | + "vscode.provideDocumentSemanticTokensLegend", |
| 208 | + uri |
| 209 | + ); |
| 210 | + |
| 211 | + const tokens = await commands.executeCommand<SemanticTokens>( |
| 212 | + "vscode.provideDocumentSemanticTokens", |
| 213 | + uri |
| 214 | + ); |
| 215 | + |
| 216 | + if (!tokens || tokens.data.length === 0) { |
| 217 | + return tokens; |
| 218 | + } |
| 219 | + |
| 220 | + // Remap token indices from embedded provider's legend to our universal legend |
| 221 | + let remappedTokens = tokens; |
| 222 | + if (legend) { |
| 223 | + remappedTokens = remapTokenIndices(tokens, legend, QUARTO_SEMANTIC_TOKEN_LEGEND); |
| 224 | + } |
| 225 | + |
| 226 | + // Adjust token positions from virtual doc to real doc coordinates |
| 227 | + return unadjustedSemanticTokens(vdoc.language, remappedTokens); |
| 228 | + } catch (error) { |
| 229 | + return undefined; |
| 230 | + } |
| 231 | + }); |
| 232 | + }; |
| 233 | +} |
0 commit comments