@@ -20,21 +20,16 @@ import org.jetbrains.kotlin.psi.KtFunction
20
20
import org.jetbrains.kotlin.psi.KtModifierListOwner
21
21
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
22
22
import org.jetbrains.kotlin.psi.KtVariableDeclaration
23
- import org.jetbrains.kotlin.psi.KtNamedDeclaration
24
23
import org.jetbrains.kotlin.psi.KtProperty
25
24
import org.jetbrains.kotlin.psi.KtParameter
26
25
import org.jetbrains.kotlin.psi.KtEnumEntry
27
- import org.jetbrains.kotlin.psi.KtStringTemplateEntry
28
- import org.jetbrains.kotlin.psi.KtStringTemplateExpression
29
26
import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry
30
- import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
31
- import org.jetbrains.kotlin.psi.KtEscapeStringTemplateEntry
32
27
import org.jetbrains.kotlin.resolve.BindingContext
33
28
import com.intellij.psi.PsiElement
34
29
import com.intellij.psi.PsiNameIdentifierOwner
35
30
import com.intellij.psi.PsiLiteralExpression
36
- import com.intellij.psi.PsiType
37
31
import com.intellij.openapi.util.TextRange
32
+ import com.intellij.psi.PsiTypes
38
33
39
34
enum class SemanticTokenType (val typeName : String ) {
40
35
KEYWORD (SemanticTokenTypes .Keyword ),
@@ -62,8 +57,8 @@ enum class SemanticTokenModifier(val modifierName: String) {
62
57
}
63
58
64
59
val 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 }
67
62
)
68
63
69
64
data 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
119
114
// TODO: Ideally we would like to cut-off subtrees outside our range, but this doesn't quite seem to work
120
115
// .preOrderTraversal { elem -> textRange?.let { it.contains(elem.textRange) } ?: true }
121
116
.preOrderTraversal()
122
- .filter { elem -> textRange?.let { it. contains(elem.textRange) } ? : true }
117
+ .filter { elem -> textRange?.contains(elem.textRange) ? : true }
123
118
.mapNotNull { elementToken(it, bindingContext) }
124
119
}
125
120
@@ -129,7 +124,6 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
129
124
130
125
return when (element) {
131
126
// References (variables, types, functions, ...)
132
-
133
127
is KtNameReferenceExpression -> {
134
128
val target = bindingContext[BindingContext .REFERENCE_TARGET , element]
135
129
val tokenType = when (target) {
@@ -158,7 +152,6 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
158
152
}
159
153
160
154
// Declarations (variables, types, functions, ...)
161
-
162
155
is PsiNameIdentifierOwner -> {
163
156
val tokenType = when (element) {
164
157
is KtParameter -> SemanticTokenType .PARAMETER
@@ -172,7 +165,7 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
172
165
val identifierRange = element.nameIdentifier?.let { range(file.text, it.textRange) } ? : return null
173
166
val modifiers = mutableSetOf (SemanticTokenModifier .DECLARATION )
174
167
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 ) {
176
169
modifiers.add(SemanticTokenModifier .READONLY )
177
170
}
178
171
@@ -186,14 +179,13 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
186
179
}
187
180
188
181
// Literals and string interpolations
189
-
190
182
is KtSimpleNameStringTemplateEntry ->
191
183
SemanticToken (elementRange, SemanticTokenType .INTERPOLATION_ENTRY )
192
184
is PsiLiteralExpression -> {
193
185
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
197
189
else -> return null
198
190
}
199
191
SemanticToken (elementRange, tokenType)
0 commit comments