|
| 1 | +import { |
| 2 | + AbstractSemanticTokenProvider, |
| 3 | + AllSemanticTokenTypes, |
| 4 | + AstNode, |
| 5 | + hasContainerOfType, |
| 6 | + SemanticTokenAcceptor, |
| 7 | + DefaultSemanticTokenOptions, |
| 8 | +} from 'langium'; |
| 9 | +import { |
| 10 | + isSdsAnnotation, |
| 11 | + isSdsAnnotationCall, |
| 12 | + isSdsArgument, |
| 13 | + isSdsAttribute, |
| 14 | + isSdsClass, |
| 15 | + isSdsDeclaration, |
| 16 | + isSdsEnum, |
| 17 | + isSdsEnumVariant, |
| 18 | + isSdsFunction, |
| 19 | + isSdsImport, |
| 20 | + isSdsImportedDeclaration, |
| 21 | + isSdsModule, |
| 22 | + isSdsNamedType, |
| 23 | + isSdsParameter, |
| 24 | + isSdsPipeline, |
| 25 | + isSdsPlaceholder, |
| 26 | + isSdsReference, |
| 27 | + isSdsSegment, |
| 28 | + isSdsTypeArgument, |
| 29 | + isSdsTypeParameter, |
| 30 | + isSdsTypeParameterConstraint, |
| 31 | +} from '../generated/ast.js'; |
| 32 | +import { SemanticTokenModifiers, SemanticTokenTypes } from 'vscode-languageserver-types'; |
| 33 | +import { SafeDsServices } from '../safe-ds-module.js'; |
| 34 | +import { SafeDsClasses } from '../builtins/safe-ds-classes.js'; |
| 35 | + |
| 36 | +// Add a new semantic token type for decorators, which is missing in langium v2.0.2 |
| 37 | +if (!AllSemanticTokenTypes[SemanticTokenTypes.decorator]) { |
| 38 | + const maxValue = Math.max(...Object.values(AllSemanticTokenTypes)); |
| 39 | + AllSemanticTokenTypes[SemanticTokenTypes.decorator] = maxValue + 1; |
| 40 | + |
| 41 | + DefaultSemanticTokenOptions.legend.tokenTypes = Object.keys(AllSemanticTokenTypes); |
| 42 | +} |
| 43 | + |
| 44 | +export class SafeDsSemanticTokenProvider extends AbstractSemanticTokenProvider { |
| 45 | + private readonly builtinClasses: SafeDsClasses; |
| 46 | + |
| 47 | + constructor(services: SafeDsServices) { |
| 48 | + super(services); |
| 49 | + |
| 50 | + this.builtinClasses = services.builtins.Classes; |
| 51 | + } |
| 52 | + |
| 53 | + protected highlightElement(node: AstNode, acceptor: SemanticTokenAcceptor): void { |
| 54 | + if (isSdsAnnotationCall(node)) { |
| 55 | + acceptor({ |
| 56 | + node, |
| 57 | + keyword: '@', |
| 58 | + type: SemanticTokenTypes.decorator, |
| 59 | + }); |
| 60 | + acceptor({ |
| 61 | + node, |
| 62 | + property: 'annotation', |
| 63 | + type: SemanticTokenTypes.decorator, |
| 64 | + }); |
| 65 | + } else if (isSdsArgument(node)) { |
| 66 | + if (node.parameter) { |
| 67 | + acceptor({ |
| 68 | + node, |
| 69 | + property: 'parameter', |
| 70 | + type: SemanticTokenTypes.parameter, |
| 71 | + }); |
| 72 | + } |
| 73 | + } else if (isSdsDeclaration(node)) { |
| 74 | + const info = this.computeSemanticTokenInfoForDeclaration(node, [SemanticTokenModifiers.declaration]); |
| 75 | + if (info) { |
| 76 | + acceptor({ |
| 77 | + node, |
| 78 | + property: 'name', |
| 79 | + ...info, |
| 80 | + }); |
| 81 | + } |
| 82 | + } else if (isSdsImport(node)) { |
| 83 | + acceptor({ |
| 84 | + node, |
| 85 | + property: 'package', |
| 86 | + type: SemanticTokenTypes.namespace, |
| 87 | + }); |
| 88 | + } else if (isSdsImportedDeclaration(node)) { |
| 89 | + const info = this.computeSemanticTokenInfoForDeclaration(node.declaration.ref); |
| 90 | + if (info) { |
| 91 | + acceptor({ |
| 92 | + node, |
| 93 | + property: 'declaration', |
| 94 | + ...info, |
| 95 | + }); |
| 96 | + } |
| 97 | + } else if (isSdsNamedType(node)) { |
| 98 | + const info = this.computeSemanticTokenInfoForDeclaration(node.declaration.ref); |
| 99 | + if (info) { |
| 100 | + acceptor({ |
| 101 | + node, |
| 102 | + property: 'declaration', |
| 103 | + ...info, |
| 104 | + }); |
| 105 | + } |
| 106 | + } else if (isSdsReference(node)) { |
| 107 | + const info = this.computeSemanticTokenInfoForDeclaration(node.target.ref); |
| 108 | + if (info) { |
| 109 | + acceptor({ |
| 110 | + node, |
| 111 | + property: 'target', |
| 112 | + ...info, |
| 113 | + }); |
| 114 | + } |
| 115 | + } else if (isSdsTypeArgument(node)) { |
| 116 | + if (node.typeParameter) { |
| 117 | + acceptor({ |
| 118 | + node, |
| 119 | + property: 'typeParameter', |
| 120 | + type: SemanticTokenTypes.typeParameter, |
| 121 | + }); |
| 122 | + } |
| 123 | + } else if (isSdsTypeParameterConstraint(node)) { |
| 124 | + acceptor({ |
| 125 | + node, |
| 126 | + property: 'leftOperand', |
| 127 | + type: SemanticTokenTypes.typeParameter, |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private computeSemanticTokenInfoForDeclaration( |
| 133 | + node: AstNode | undefined, |
| 134 | + additionalModifiers: SemanticTokenModifiers[] = [], |
| 135 | + ): SemanticTokenInfo | void { |
| 136 | + /* c8 ignore start */ |
| 137 | + if (!node) { |
| 138 | + return; |
| 139 | + } |
| 140 | + /* c8 ignore stop */ |
| 141 | + |
| 142 | + if (isSdsAnnotation(node)) { |
| 143 | + return { |
| 144 | + type: SemanticTokenTypes.decorator, |
| 145 | + modifier: additionalModifiers, |
| 146 | + }; |
| 147 | + } else if (isSdsAttribute(node)) { |
| 148 | + const modifier = [SemanticTokenModifiers.readonly, ...additionalModifiers]; |
| 149 | + if (node.isStatic) { |
| 150 | + modifier.push(SemanticTokenModifiers.static); |
| 151 | + } |
| 152 | + |
| 153 | + return { |
| 154 | + type: SemanticTokenTypes.property, |
| 155 | + modifier, |
| 156 | + }; |
| 157 | + } else if (isSdsClass(node)) { |
| 158 | + const isBuiltinClass = this.builtinClasses.isBuiltinClass(node); |
| 159 | + return { |
| 160 | + type: SemanticTokenTypes.class, |
| 161 | + modifier: isBuiltinClass |
| 162 | + ? [SemanticTokenModifiers.defaultLibrary, ...additionalModifiers] |
| 163 | + : additionalModifiers, |
| 164 | + }; |
| 165 | + } else if (isSdsEnum(node)) { |
| 166 | + return { |
| 167 | + type: SemanticTokenTypes.enum, |
| 168 | + modifier: additionalModifiers, |
| 169 | + }; |
| 170 | + } else if (isSdsEnumVariant(node)) { |
| 171 | + return { |
| 172 | + type: SemanticTokenTypes.enumMember, |
| 173 | + modifier: additionalModifiers, |
| 174 | + }; |
| 175 | + } else if (isSdsFunction(node)) { |
| 176 | + if (hasContainerOfType(node, isSdsClass)) { |
| 177 | + return { |
| 178 | + type: SemanticTokenTypes.method, |
| 179 | + modifier: node.isStatic |
| 180 | + ? [SemanticTokenModifiers.static, ...additionalModifiers] |
| 181 | + : additionalModifiers, |
| 182 | + }; |
| 183 | + } else { |
| 184 | + return { |
| 185 | + type: SemanticTokenTypes.function, |
| 186 | + modifier: additionalModifiers, |
| 187 | + }; |
| 188 | + } |
| 189 | + } else if (isSdsModule(node)) { |
| 190 | + return { |
| 191 | + type: SemanticTokenTypes.namespace, |
| 192 | + modifier: additionalModifiers, |
| 193 | + }; |
| 194 | + } else if (isSdsParameter(node)) { |
| 195 | + return { |
| 196 | + type: SemanticTokenTypes.parameter, |
| 197 | + modifier: additionalModifiers, |
| 198 | + }; |
| 199 | + } else if (isSdsPipeline(node)) { |
| 200 | + return { |
| 201 | + type: SemanticTokenTypes.function, |
| 202 | + modifier: additionalModifiers, |
| 203 | + }; |
| 204 | + } else if (isSdsPlaceholder(node)) { |
| 205 | + return { |
| 206 | + type: SemanticTokenTypes.variable, |
| 207 | + modifier: [SemanticTokenModifiers.readonly, ...additionalModifiers], |
| 208 | + }; |
| 209 | + } else if (isSdsSegment(node)) { |
| 210 | + return { |
| 211 | + type: SemanticTokenTypes.function, |
| 212 | + modifier: additionalModifiers, |
| 213 | + }; |
| 214 | + } else if (isSdsTypeParameter(node)) { |
| 215 | + return { |
| 216 | + type: SemanticTokenTypes.typeParameter, |
| 217 | + modifier: additionalModifiers, |
| 218 | + }; |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +interface SemanticTokenInfo { |
| 224 | + type: SemanticTokenTypes; |
| 225 | + modifier?: SemanticTokenModifiers | SemanticTokenModifiers[]; |
| 226 | +} |
0 commit comments