|
| 1 | +import { SemanticTokens, SemanticTokensParams, TextDocuments, Range, Connection } from 'vscode-languageserver' |
| 2 | +import MatlabLifecycleManager from '../../lifecycle/MatlabLifecycleManager' |
| 3 | +import { TextDocument } from 'vscode-languageserver-textdocument' |
| 4 | +import FileInfoIndex, { MatlabFunctionScopeInfo, MatlabGlobalScopeInfo } from '../../indexing/FileInfoIndex' |
| 5 | +import DocumentIndexer from '../../indexing/DocumentIndexer' |
| 6 | + |
| 7 | +interface SemanticToken { |
| 8 | + range: Range |
| 9 | + typeIndex: number |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Handles requests for semantic tokens for a document. |
| 14 | + */ |
| 15 | +class SemanticTokensProvider { |
| 16 | + constructor ( |
| 17 | + protected readonly matlabLifecycleManager: MatlabLifecycleManager, |
| 18 | + protected readonly documentIndexer: DocumentIndexer, |
| 19 | + protected readonly fileInfoIndex: FileInfoIndex |
| 20 | + ) { } |
| 21 | + |
| 22 | + async handleSemanticTokensRequest ( |
| 23 | + params: SemanticTokensParams, |
| 24 | + documentManager: TextDocuments<TextDocument> |
| 25 | + ): Promise<SemanticTokens | null> { |
| 26 | + // This provider will be called constantly, should not connect to MATLAB just because it was called |
| 27 | + const matlabConnection = await this.matlabLifecycleManager.getMatlabConnection(false) |
| 28 | + // If MATLAB is not connected, fall back to default highlighting |
| 29 | + if (matlabConnection == null) return null |
| 30 | + |
| 31 | + const textDocument = documentManager.get(params.textDocument.uri) |
| 32 | + if (textDocument == null) return null |
| 33 | + |
| 34 | + const codeInfo = this.fileInfoIndex.codeInfoCache.get(params.textDocument.uri) |
| 35 | + if (codeInfo == null) return null |
| 36 | + |
| 37 | + const tokens: SemanticToken[] = [] |
| 38 | + this.collectSemanticTokens(codeInfo.globalScopeInfo, tokens) |
| 39 | + |
| 40 | + // Sort tokens by their position in the document (line and character) |
| 41 | + // This is necessary to encode them using relative positions |
| 42 | + tokens.sort((a, b) => { |
| 43 | + const lineDiff = a.range.start.line - b.range.start.line |
| 44 | + if (lineDiff !== 0) return lineDiff |
| 45 | + |
| 46 | + return a.range.start.character - b.range.start.character |
| 47 | + }) |
| 48 | + |
| 49 | + const data: number[] = [] |
| 50 | + let prevLine = 0 |
| 51 | + let prevStart = 0 |
| 52 | + |
| 53 | + // Encode semantic tokens using relative line and character positions |
| 54 | + for (const token of tokens) { |
| 55 | + const line = token.range.start.line |
| 56 | + const start = token.range.start.character |
| 57 | + const length = token.range.end.character - token.range.start.character |
| 58 | + |
| 59 | + const deltaLine = line - prevLine |
| 60 | + const deltaStart = deltaLine === 0 ? start - prevStart : start |
| 61 | + |
| 62 | + data.push(deltaLine, deltaStart, length, token.typeIndex, 0) |
| 63 | + prevLine = line |
| 64 | + prevStart = start |
| 65 | + } |
| 66 | + |
| 67 | + return { data } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Recursively collects semantic tokens for a given scope and its nested scopes. |
| 72 | + * Tokens are appended to 'tokens' in-place. |
| 73 | + * @param scope The scope from which semantic tokens should be collected |
| 74 | + * @param tokens The array to which collected semantic tokens are appended |
| 75 | + */ |
| 76 | + private collectSemanticTokens ( |
| 77 | + scope: MatlabGlobalScopeInfo | MatlabFunctionScopeInfo, |
| 78 | + tokens: SemanticToken[] |
| 79 | + ): void { |
| 80 | + // Variables: highlight only the first component as variable |
| 81 | + for (const item of scope.variables.values()) { |
| 82 | + for (const ref of item.references) { |
| 83 | + tokens.push({ range: ref.components[0].range, typeIndex: 1 }) // variable |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Functions/unbound: highlight only the first component as function |
| 88 | + for (const item of scope.functionOrUnboundReferences.values()) { |
| 89 | + for (const ref of item.references) { |
| 90 | + tokens.push({ range: ref.components[0].range, typeIndex: 0 }) // function |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Class scope |
| 95 | + const classScope = (scope as MatlabGlobalScopeInfo).classScope; |
| 96 | + if (classScope != null) { |
| 97 | + for (const nestedFunc of classScope.functionScopes.values()) { |
| 98 | + if (nestedFunc.functionScopeInfo != null) { |
| 99 | + this.collectSemanticTokens(nestedFunc.functionScopeInfo, tokens); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Function scopes |
| 105 | + for (const nestedFunc of scope.functionScopes.values()) { |
| 106 | + if (nestedFunc.functionScopeInfo != null) { |
| 107 | + this.collectSemanticTokens(nestedFunc.functionScopeInfo, tokens) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export const SEMANTIC_TOKEN_TYPES = ['function', 'variable'] |
| 114 | +export const SEMANTIC_TOKEN_MODIFIERS: string[] = [] |
| 115 | +export default SemanticTokensProvider |
| 116 | + |
| 117 | +/** |
| 118 | + * Wires semantic token invalidation to document indexing. |
| 119 | + * |
| 120 | + * When indexing completes, this schedules a debounced refresh request |
| 121 | + * so the client re-requests semantic tokens and updates highlighting. |
| 122 | + */ |
| 123 | +export function setupSemanticTokensRefresh ( |
| 124 | + connection: Connection, |
| 125 | + documentIndexer: DocumentIndexer |
| 126 | +): void { |
| 127 | + let refreshTimer: NodeJS.Timeout | undefined |
| 128 | + |
| 129 | + documentIndexer.setOnIndexed(() => { |
| 130 | + if (refreshTimer != null) clearTimeout(refreshTimer) |
| 131 | + |
| 132 | + refreshTimer = setTimeout(() => { |
| 133 | + void connection.sendRequest('workspace/semanticTokens/refresh') |
| 134 | + }, 150) |
| 135 | + }) |
| 136 | +} |
0 commit comments