@@ -20,21 +20,16 @@ import org.jetbrains.kotlin.psi.KtFunction
2020import org.jetbrains.kotlin.psi.KtModifierListOwner
2121import org.jetbrains.kotlin.psi.KtNameReferenceExpression
2222import org.jetbrains.kotlin.psi.KtVariableDeclaration
23- import org.jetbrains.kotlin.psi.KtNamedDeclaration
2423import org.jetbrains.kotlin.psi.KtProperty
2524import org.jetbrains.kotlin.psi.KtParameter
2625import org.jetbrains.kotlin.psi.KtEnumEntry
27- import org.jetbrains.kotlin.psi.KtStringTemplateEntry
28- import org.jetbrains.kotlin.psi.KtStringTemplateExpression
2926import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry
30- import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
31- import org.jetbrains.kotlin.psi.KtEscapeStringTemplateEntry
3227import org.jetbrains.kotlin.resolve.BindingContext
3328import com.intellij.psi.PsiElement
3429import com.intellij.psi.PsiNameIdentifierOwner
3530import com.intellij.psi.PsiLiteralExpression
36- import com.intellij.psi.PsiType
3731import com.intellij.openapi.util.TextRange
32+ import com.intellij.psi.PsiTypes
3833
3934enum class SemanticTokenType (val typeName : String ) {
4035 KEYWORD (SemanticTokenTypes .Keyword ),
@@ -62,8 +57,8 @@ enum class SemanticTokenModifier(val modifierName: String) {
6257}
6358
6459val semanticTokensLegend = SemanticTokensLegend (
65- SemanticTokenType .values() .map { it.typeName },
66- SemanticTokenModifier .values() .map { it.modifierName }
60+ SemanticTokenType .entries .map { it.typeName },
61+ SemanticTokenModifier .entries .map { it.modifierName }
6762)
6863
6964data class SemanticToken (val range : Range , val type : SemanticTokenType , val modifiers : Set <SemanticTokenModifier > = setOf())
@@ -119,7 +114,7 @@ private fun elementTokens(element: PsiElement, bindingContext: BindingContext, r
119114 // TODO: Ideally we would like to cut-off subtrees outside our range, but this doesn't quite seem to work
120115 // .preOrderTraversal { elem -> textRange?.let { it.contains(elem.textRange) } ?: true }
121116 .preOrderTraversal()
122- .filter { elem -> textRange?.let { it. contains(elem.textRange) } ? : true }
117+ .filter { elem -> textRange?.contains(elem.textRange) ? : true }
123118 .mapNotNull { elementToken(it, bindingContext) }
124119}
125120
@@ -129,7 +124,6 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
129124
130125 return when (element) {
131126 // References (variables, types, functions, ...)
132-
133127 is KtNameReferenceExpression -> {
134128 val target = bindingContext[BindingContext .REFERENCE_TARGET , element]
135129 val tokenType = when (target) {
@@ -158,7 +152,6 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
158152 }
159153
160154 // Declarations (variables, types, functions, ...)
161-
162155 is PsiNameIdentifierOwner -> {
163156 val tokenType = when (element) {
164157 is KtParameter -> SemanticTokenType .PARAMETER
@@ -172,7 +165,7 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
172165 val identifierRange = element.nameIdentifier?.let { range(file.text, it.textRange) } ? : return null
173166 val modifiers = mutableSetOf (SemanticTokenModifier .DECLARATION )
174167
175- if (element is KtVariableDeclaration && (! element.isVar() || element.hasModifier(KtTokens .CONST_KEYWORD )) || element is KtParameter ) {
168+ if (element is KtVariableDeclaration && (! element.isVar || element.hasModifier(KtTokens .CONST_KEYWORD )) || element is KtParameter ) {
176169 modifiers.add(SemanticTokenModifier .READONLY )
177170 }
178171
@@ -186,14 +179,13 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
186179 }
187180
188181 // Literals and string interpolations
189-
190182 is KtSimpleNameStringTemplateEntry ->
191183 SemanticToken (elementRange, SemanticTokenType .INTERPOLATION_ENTRY )
192184 is PsiLiteralExpression -> {
193185 val tokenType = when (element.type) {
194- PsiType . INT , PsiType . LONG , PsiType . DOUBLE -> SemanticTokenType .NUMBER
195- PsiType . CHAR -> SemanticTokenType .STRING
196- PsiType . BOOLEAN , PsiType . NULL -> SemanticTokenType .KEYWORD
186+ PsiTypes .intType(), PsiTypes .longType(), PsiTypes .doubleType() -> SemanticTokenType .NUMBER
187+ PsiTypes .charType() -> SemanticTokenType .STRING
188+ PsiTypes .booleanType(), PsiTypes .nullType() -> SemanticTokenType .KEYWORD
197189 else -> return null
198190 }
199191 SemanticToken (elementRange, tokenType)
0 commit comments