|
| 1 | +package org.javacs.kt.semantictokens |
| 2 | + |
| 3 | +import org.eclipse.lsp4j.SemanticTokenTypes |
| 4 | +import org.eclipse.lsp4j.SemanticTokenModifiers |
| 5 | +import org.eclipse.lsp4j.SemanticTokensLegend |
| 6 | +import org.eclipse.lsp4j.Range |
| 7 | +import org.javacs.kt.CompiledFile |
| 8 | +import org.javacs.kt.position.range |
| 9 | +import org.javacs.kt.position.offset |
| 10 | +import org.javacs.kt.util.preOrderTraversal |
| 11 | +import org.jetbrains.kotlin.descriptors.ClassDescriptor |
| 12 | +import org.jetbrains.kotlin.descriptors.ClassKind |
| 13 | +import org.jetbrains.kotlin.descriptors.ConstructorDescriptor |
| 14 | +import org.jetbrains.kotlin.descriptors.FunctionDescriptor |
| 15 | +import org.jetbrains.kotlin.descriptors.PropertyDescriptor |
| 16 | +import org.jetbrains.kotlin.descriptors.VariableDescriptor |
| 17 | +import org.jetbrains.kotlin.lexer.KtTokens |
| 18 | +import org.jetbrains.kotlin.psi.KtClassOrObject |
| 19 | +import org.jetbrains.kotlin.psi.KtFunction |
| 20 | +import org.jetbrains.kotlin.psi.KtModifierListOwner |
| 21 | +import org.jetbrains.kotlin.psi.KtNameReferenceExpression |
| 22 | +import org.jetbrains.kotlin.psi.KtVariableDeclaration |
| 23 | +import org.jetbrains.kotlin.psi.KtNamedDeclaration |
| 24 | +import org.jetbrains.kotlin.psi.KtProperty |
| 25 | +import org.jetbrains.kotlin.psi.KtParameter |
| 26 | +import org.jetbrains.kotlin.psi.KtStringTemplateEntry |
| 27 | +import org.jetbrains.kotlin.psi.KtStringTemplateExpression |
| 28 | +import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry |
| 29 | +import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry |
| 30 | +import org.jetbrains.kotlin.psi.KtEscapeStringTemplateEntry |
| 31 | +import org.jetbrains.kotlin.resolve.BindingContext |
| 32 | +import com.intellij.psi.PsiElement |
| 33 | +import com.intellij.psi.PsiNameIdentifierOwner |
| 34 | +import com.intellij.psi.PsiLiteralExpression |
| 35 | +import com.intellij.psi.PsiType |
| 36 | +import com.intellij.openapi.util.TextRange |
| 37 | + |
| 38 | +enum class SemanticTokenType(val typeName: String) { |
| 39 | + KEYWORD(SemanticTokenTypes.Keyword), |
| 40 | + VARIABLE(SemanticTokenTypes.Variable), |
| 41 | + FUNCTION(SemanticTokenTypes.Function), |
| 42 | + PROPERTY(SemanticTokenTypes.Property), |
| 43 | + PARAMETER(SemanticTokenTypes.Parameter), |
| 44 | + ENUM_MEMBER(SemanticTokenTypes.EnumMember), |
| 45 | + CLASS(SemanticTokenTypes.Class), |
| 46 | + INTERFACE(SemanticTokenTypes.Interface), |
| 47 | + ENUM(SemanticTokenTypes.Enum), |
| 48 | + TYPE(SemanticTokenTypes.Type), |
| 49 | + STRING(SemanticTokenTypes.String), |
| 50 | + NUMBER(SemanticTokenTypes.Number), |
| 51 | + // Since LSP does not provide a token type for string interpolation |
| 52 | + // entries, we use Variable as a fallback here for now |
| 53 | + INTERPOLATION_ENTRY(SemanticTokenTypes.Variable) |
| 54 | +} |
| 55 | + |
| 56 | +enum class SemanticTokenModifier(val modifierName: String) { |
| 57 | + DECLARATION(SemanticTokenModifiers.Declaration), |
| 58 | + DEFINITION(SemanticTokenModifiers.Definition), |
| 59 | + ABSTRACT(SemanticTokenModifiers.Abstract), |
| 60 | + READONLY(SemanticTokenModifiers.Readonly) |
| 61 | +} |
| 62 | + |
| 63 | +val semanticTokensLegend = SemanticTokensLegend( |
| 64 | + SemanticTokenType.values().map { it.typeName }, |
| 65 | + SemanticTokenModifier.values().map { it.modifierName } |
| 66 | +) |
| 67 | + |
| 68 | +data class SemanticToken(val range: Range, val type: SemanticTokenType, val modifiers: Set<SemanticTokenModifier> = setOf()) |
| 69 | + |
| 70 | +/** |
| 71 | + * Computes LSP-encoded semantic tokens for the given range in the |
| 72 | + * document. No range means the entire document. |
| 73 | + */ |
| 74 | +fun encodedSemanticTokens(file: CompiledFile, range: Range? = null): List<Int> = |
| 75 | + encodeTokens(semanticTokens(file, range)) |
| 76 | + |
| 77 | +/** |
| 78 | + * Computes semantic tokens for the given range in the document. |
| 79 | + * No range means the entire document. |
| 80 | + */ |
| 81 | +fun semanticTokens(file: CompiledFile, range: Range? = null): Sequence<SemanticToken> = |
| 82 | + elementTokens(file.parse, file.compile, range) |
| 83 | + |
| 84 | +fun encodeTokens(tokens: Sequence<SemanticToken>): List<Int> { |
| 85 | + val encoded = mutableListOf<Int>() |
| 86 | + var last: SemanticToken? = null |
| 87 | + |
| 88 | + for (token in tokens) { |
| 89 | + // Tokens must be on a single line |
| 90 | + if (token.range.start.line == token.range.end.line) { |
| 91 | + val length = token.range.end.character - token.range.start.character |
| 92 | + val deltaLine = token.range.start.line - (last?.range?.start?.line ?: 0) |
| 93 | + val deltaStart = token.range.start.character - (last?.takeIf { deltaLine == 0 }?.range?.start?.character ?: 0) |
| 94 | + |
| 95 | + encoded.add(deltaLine) |
| 96 | + encoded.add(deltaStart) |
| 97 | + encoded.add(length) |
| 98 | + encoded.add(encodeType(token.type)) |
| 99 | + encoded.add(encodeModifiers(token.modifiers)) |
| 100 | + |
| 101 | + last = token |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return encoded |
| 106 | +} |
| 107 | + |
| 108 | +private fun encodeType(type: SemanticTokenType): Int = type.ordinal |
| 109 | + |
| 110 | +private fun encodeModifiers(modifiers: Set<SemanticTokenModifier>): Int = modifiers |
| 111 | + .map { 1 shl it.ordinal } |
| 112 | + .fold(0, Int::or) |
| 113 | + |
| 114 | +private fun elementTokens(element: PsiElement, bindingContext: BindingContext, range: Range? = null): Sequence<SemanticToken> { |
| 115 | + val file = element.containingFile |
| 116 | + val textRange = range?.let { TextRange(offset(file.text, it.start), offset(file.text, it.end)) } |
| 117 | + return element |
| 118 | + // TODO: Ideally we would like to cut-off subtrees outside our range, but this doesn't quite seem to work |
| 119 | + // .preOrderTraversal { elem -> textRange?.let { it.contains(elem.textRange) } ?: true } |
| 120 | + .preOrderTraversal() |
| 121 | + .filter { elem -> textRange?.let { it.contains(elem.textRange) } ?: true } |
| 122 | + .mapNotNull { elementToken(it, bindingContext) } |
| 123 | +} |
| 124 | + |
| 125 | +private fun elementToken(element: PsiElement, bindingContext: BindingContext): SemanticToken? { |
| 126 | + val file = element.containingFile |
| 127 | + val elementRange = range(file.text, element.textRange) |
| 128 | + |
| 129 | + return when (element) { |
| 130 | + // References (variables, types, functions, ...) |
| 131 | + |
| 132 | + is KtNameReferenceExpression -> { |
| 133 | + val target = bindingContext[BindingContext.REFERENCE_TARGET, element] |
| 134 | + val tokenType = when (target) { |
| 135 | + is PropertyDescriptor -> SemanticTokenType.PROPERTY |
| 136 | + is VariableDescriptor -> SemanticTokenType.VARIABLE |
| 137 | + is ConstructorDescriptor -> when (target.constructedClass.kind) { |
| 138 | + ClassKind.ANNOTATION_CLASS -> SemanticTokenType.TYPE // annotations look nicer this way |
| 139 | + else -> SemanticTokenType.FUNCTION |
| 140 | + } |
| 141 | + is FunctionDescriptor -> SemanticTokenType.FUNCTION |
| 142 | + is ClassDescriptor -> when (target.kind) { |
| 143 | + ClassKind.CLASS -> SemanticTokenType.CLASS |
| 144 | + ClassKind.OBJECT -> SemanticTokenType.CLASS |
| 145 | + ClassKind.INTERFACE -> SemanticTokenType.INTERFACE |
| 146 | + ClassKind.ENUM_CLASS -> SemanticTokenType.ENUM |
| 147 | + else -> SemanticTokenType.TYPE |
| 148 | + } |
| 149 | + else -> return null |
| 150 | + } |
| 151 | + val isConstant = (target as? VariableDescriptor)?.let { !it.isVar() || it.isConst() } ?: false |
| 152 | + val modifiers = if (isConstant) setOf(SemanticTokenModifier.READONLY) else setOf() |
| 153 | + |
| 154 | + SemanticToken(elementRange, tokenType, modifiers) |
| 155 | + } |
| 156 | + |
| 157 | + // Declarations (variables, types, functions, ...) |
| 158 | + |
| 159 | + is PsiNameIdentifierOwner -> { |
| 160 | + val tokenType = when (element) { |
| 161 | + is KtParameter -> SemanticTokenType.PARAMETER |
| 162 | + is KtProperty -> SemanticTokenType.PROPERTY |
| 163 | + is KtVariableDeclaration -> SemanticTokenType.VARIABLE |
| 164 | + is KtClassOrObject -> SemanticTokenType.CLASS |
| 165 | + is KtFunction -> SemanticTokenType.FUNCTION |
| 166 | + else -> return null |
| 167 | + } |
| 168 | + val identifierRange = element.nameIdentifier?.let { range(file.text, it.textRange) } ?: return null |
| 169 | + val modifiers = mutableSetOf(SemanticTokenModifier.DECLARATION) |
| 170 | + |
| 171 | + if (element is KtVariableDeclaration && (!element.isVar() || element.hasModifier(KtTokens.CONST_KEYWORD)) || element is KtParameter) { |
| 172 | + modifiers.add(SemanticTokenModifier.READONLY) |
| 173 | + } |
| 174 | + |
| 175 | + if (element is KtModifierListOwner) { |
| 176 | + if (element.hasModifier(KtTokens.ABSTRACT_KEYWORD)) { |
| 177 | + modifiers.add(SemanticTokenModifier.ABSTRACT) |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + SemanticToken(identifierRange, tokenType, modifiers) |
| 182 | + } |
| 183 | + |
| 184 | + // Literals and string interpolations |
| 185 | + |
| 186 | + is KtSimpleNameStringTemplateEntry, is KtBlockStringTemplateEntry -> |
| 187 | + SemanticToken(elementRange, SemanticTokenType.INTERPOLATION_ENTRY) |
| 188 | + is KtStringTemplateExpression -> SemanticToken(elementRange, SemanticTokenType.STRING) |
| 189 | + is PsiLiteralExpression -> { |
| 190 | + val tokenType = when (element.type) { |
| 191 | + PsiType.INT, PsiType.LONG, PsiType.DOUBLE -> SemanticTokenType.NUMBER |
| 192 | + PsiType.CHAR -> SemanticTokenType.STRING |
| 193 | + PsiType.BOOLEAN, PsiType.NULL -> SemanticTokenType.KEYWORD |
| 194 | + else -> return null |
| 195 | + } |
| 196 | + SemanticToken(elementRange, tokenType) |
| 197 | + } |
| 198 | + else -> null |
| 199 | + } |
| 200 | +} |
0 commit comments