|
| 1 | +import |
| 2 | +{ |
| 3 | + SemanticTokensBuilder, |
| 4 | + SemanticTokensLegend, |
| 5 | + SemanticTokensClientCapabilities |
| 6 | +} from 'vscode-languageserver'; |
| 7 | +import { TextDocument } from 'vscode-languageserver-textdocument'; |
| 8 | +import { antlrTypeNumToString, tokenize, getTokenTypes } from './parser'; |
| 9 | + |
| 10 | +export type VSCodeToken = { |
| 11 | + line: number |
| 12 | + start: number |
| 13 | + length: number |
| 14 | + tokenType: number |
| 15 | + tokenModifier: number |
| 16 | +} |
| 17 | + |
| 18 | +export class SemanticTokensProvider { |
| 19 | + legend: SemanticTokensLegend; |
| 20 | + /** |
| 21 | + * Keep a separate semantic token builder per file |
| 22 | + * Each builder keeps track of |
| 23 | + */ |
| 24 | + tokenBuilders: Map<string, SemanticTokensBuilder> = new Map(); |
| 25 | + /** |
| 26 | + * A map from EO's g4 grammar token types into |
| 27 | + * token types supported by VS Code |
| 28 | + */ |
| 29 | + tokenTypeMap: Map<string, string> = new Map; |
| 30 | + |
| 31 | + constructor(capability: SemanticTokensClientCapabilities) { |
| 32 | + this.setMap(); // must run before computing legend! |
| 33 | + this.legend = this.computeLegend(capability); |
| 34 | + } |
| 35 | + |
| 36 | + setMap() { |
| 37 | + /* |
| 38 | + Needs update with a proper, fine-tuned mapping |
| 39 | + List of VS Code's token types: |
| 40 | + [ |
| 41 | + 'namespace', 'type', |
| 42 | + 'class', 'enum', |
| 43 | + 'interface', 'struct', |
| 44 | + 'typeParameter', 'parameter', |
| 45 | + 'variable', 'property', |
| 46 | + 'enumMember', 'event', |
| 47 | + 'function', 'method', |
| 48 | + 'macro', 'keyword', |
| 49 | + 'modifier', 'comment', |
| 50 | + 'string', 'number', |
| 51 | + 'regexp', 'operator' |
| 52 | + ] |
| 53 | + */ |
| 54 | + this.tokenTypeMap.set('COMMENT', 'comment'); |
| 55 | + this.tokenTypeMap.set('META', 'macro'); |
| 56 | + this.tokenTypeMap.set('ROOT', 'keyword'); |
| 57 | + this.tokenTypeMap.set('HOME', 'keyword'); |
| 58 | + this.tokenTypeMap.set('STAR', 'operator'); |
| 59 | + this.tokenTypeMap.set('DOTS', 'operator'); |
| 60 | + this.tokenTypeMap.set('CONST', 'keyword'); |
| 61 | + this.tokenTypeMap.set('SLASH', 'operator'); |
| 62 | + this.tokenTypeMap.set('COLON', 'operator'); |
| 63 | + this.tokenTypeMap.set('COPY', 'class'); |
| 64 | + this.tokenTypeMap.set('ARROW', 'method'); |
| 65 | + this.tokenTypeMap.set('VERTEX', 'class'); |
| 66 | + this.tokenTypeMap.set('SIGMA', 'method'); |
| 67 | + this.tokenTypeMap.set('XI', 'method'); |
| 68 | + this.tokenTypeMap.set('PLUS', 'operator'); |
| 69 | + this.tokenTypeMap.set('MINUS', 'operator'); |
| 70 | + this.tokenTypeMap.set('QUESTION', 'operator'); |
| 71 | + this.tokenTypeMap.set('AT', 'method'); |
| 72 | + this.tokenTypeMap.set('RHO', 'method'); |
| 73 | + this.tokenTypeMap.set('HASH', 'keyword'); |
| 74 | + this.tokenTypeMap.set('BYTES', 'number'); |
| 75 | + this.tokenTypeMap.set('BOOL', 'variable'); |
| 76 | + this.tokenTypeMap.set('STRING', 'string'); |
| 77 | + this.tokenTypeMap.set('INT', 'number'); |
| 78 | + this.tokenTypeMap.set('FLOAT', 'number'); |
| 79 | + this.tokenTypeMap.set('HEX', 'number'); |
| 80 | + this.tokenTypeMap.set('NAME', 'variable'); |
| 81 | + this.tokenTypeMap.set('TEXT', 'string'); |
| 82 | + } |
| 83 | + |
| 84 | + computeLegend(capability: SemanticTokensClientCapabilities): SemanticTokensLegend { |
| 85 | + const clientTokenTypes = new Set<string>(capability.tokenTypes); |
| 86 | + |
| 87 | + const tokenTypes: string[] = []; |
| 88 | + getTokenTypes().forEach(el => { |
| 89 | + const type = this.tokenTypeMap.get(el) || ''; |
| 90 | + if (clientTokenTypes.has(type)) { |
| 91 | + tokenTypes.push(type); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + return { tokenTypes: tokenTypes, tokenModifiers: [] }; |
| 96 | + } |
| 97 | + |
| 98 | + tokenize(document: TextDocument) { |
| 99 | + const tokens: VSCodeToken[] = []; |
| 100 | + const antlrTokens = tokenize(document.getText()); |
| 101 | + antlrTokens.forEach(tk => { |
| 102 | + const vscodeTokenType = this.tokenTypeMap.get(antlrTypeNumToString(tk.type)); |
| 103 | + const legendNum = vscodeTokenType ? this.legend.tokenTypes.indexOf(vscodeTokenType) : -1; |
| 104 | + tokens.push({ |
| 105 | + line: tk.line - 1, |
| 106 | + start: tk.charPositionInLine, |
| 107 | + length: tk.stopIndex - tk.startIndex + 1, |
| 108 | + tokenType: legendNum, |
| 109 | + tokenModifier: 0, |
| 110 | + }); |
| 111 | + }); |
| 112 | + return tokens; |
| 113 | + } |
| 114 | + |
| 115 | + getTokenBuilder(document: TextDocument): SemanticTokensBuilder { |
| 116 | + let result = this.tokenBuilders.get(document.uri); |
| 117 | + if (!result) { |
| 118 | + result = new SemanticTokensBuilder(); |
| 119 | + this.tokenBuilders.set(document.uri, result); |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + provideSemanticTokens(document: TextDocument) { |
| 126 | + const builder = this.getTokenBuilder(document); |
| 127 | + this.tokenize(document).forEach((token) => |
| 128 | + { |
| 129 | + builder.push( |
| 130 | + token.line, |
| 131 | + token.start, |
| 132 | + token.length, |
| 133 | + token.tokenType, |
| 134 | + token.tokenModifier |
| 135 | + ); |
| 136 | + }); |
| 137 | + return builder.build(); |
| 138 | + } |
| 139 | + |
| 140 | + provideDeltas(document: TextDocument, resultsId: string) { |
| 141 | + const builder = this.getTokenBuilder(document); |
| 142 | + builder.previousResult(resultsId); |
| 143 | + this.tokenize(document).forEach((token) => |
| 144 | + { |
| 145 | + builder.push( |
| 146 | + token.line, |
| 147 | + token.start, |
| 148 | + token.length, |
| 149 | + token.tokenType, |
| 150 | + token.tokenModifier |
| 151 | + ); |
| 152 | + }); |
| 153 | + return builder.buildEdits(); |
| 154 | + } |
| 155 | +} |
0 commit comments