Skip to content

Commit 9561f35

Browse files
committed
Highlight abstract classes in semantic tokens
1 parent 2dac9ea commit 9561f35

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

server/src/main/kotlin/org/javacs/kt/semantictokens/SemanticTokens.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
1313
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
1414
import org.jetbrains.kotlin.descriptors.VariableDescriptor
1515
import org.jetbrains.kotlin.lexer.KtTokens
16+
import org.jetbrains.kotlin.psi.KtClassOrObject
17+
import org.jetbrains.kotlin.psi.KtModifierListOwner
1618
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
1719
import org.jetbrains.kotlin.psi.KtVariableDeclaration
1820
import org.jetbrains.kotlin.psi.KtNamedDeclaration
@@ -39,7 +41,6 @@ private enum class SemanticTokenModifier(val modifierName: String) {
3941
DECLARATION(SemanticTokenModifiers.Declaration),
4042
DEFINITION(SemanticTokenModifiers.Definition),
4143
ABSTRACT(SemanticTokenModifiers.Abstract),
42-
STATIC(SemanticTokenModifiers.Static),
4344
READONLY(SemanticTokenModifiers.Readonly)
4445
}
4546

@@ -90,6 +91,7 @@ private fun elementTokens(element: PsiElement, bindingContext: BindingContext):
9091
private fun elementToken(element: PsiElement, bindingContext: BindingContext): SemanticToken? {
9192
val file = element.containingFile
9293
val elementRange = range(file.text, element.textRange)
94+
9395
return when (element) {
9496
is KtNameReferenceExpression -> {
9597
val target = bindingContext[BindingContext.REFERENCE_TARGET, element]
@@ -108,21 +110,32 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
108110
}
109111
val isConstant = (target as? VariableDescriptor)?.let { !it.isVar() || it.isConst() } ?: false
110112
val modifiers = if (isConstant) setOf(SemanticTokenModifier.READONLY) else setOf()
113+
111114
SemanticToken(elementRange, tokenType, modifiers)
112115
}
113116
is PsiNameIdentifierOwner -> {
114117
val tokenType = when (element) {
115118
is KtProperty -> SemanticTokenType.PROPERTY
116119
is KtParameter -> SemanticTokenType.PARAMETER
117120
is KtVariableDeclaration -> SemanticTokenType.VARIABLE
121+
is KtClassOrObject -> SemanticTokenType.CLASS
118122
else -> return null
119123
}
120124
val identifierRange = element.nameIdentifier?.let { range(file.text, it.textRange) } ?: return null
121125
val modifiers = mutableSetOf(SemanticTokenModifier.DECLARATION)
122-
val isConstant = (element as? KtVariableDeclaration)?.let { !it.isVar() || it.hasModifier(KtTokens.CONST_KEYWORD) } ?: false
123-
if (isConstant) {
124-
modifiers.add(SemanticTokenModifier.READONLY)
126+
127+
if (element is KtVariableDeclaration) {
128+
if (!element.isVar() || element.hasModifier(KtTokens.CONST_KEYWORD)) {
129+
modifiers.add(SemanticTokenModifier.READONLY)
130+
}
125131
}
132+
133+
if (element is KtModifierListOwner) {
134+
if (element.hasModifier(KtTokens.ABSTRACT_KEYWORD)) {
135+
modifiers.add(SemanticTokenModifier.ABSTRACT)
136+
}
137+
}
138+
126139
SemanticToken(identifierRange, tokenType, modifiers)
127140
}
128141
else -> null

0 commit comments

Comments
 (0)