Skip to content

Commit b7f1b66

Browse files
committed
Merge PR fwcd#316 (Semantic highlighting for enum members)
2 parents 6cbe12d + 351792e commit b7f1b66

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.psi.KtVariableDeclaration
2323
import org.jetbrains.kotlin.psi.KtNamedDeclaration
2424
import org.jetbrains.kotlin.psi.KtProperty
2525
import org.jetbrains.kotlin.psi.KtParameter
26+
import org.jetbrains.kotlin.psi.KtEnumEntry
2627
import org.jetbrains.kotlin.psi.KtStringTemplateEntry
2728
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
2829
import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry
@@ -135,11 +136,13 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
135136
is PropertyDescriptor -> SemanticTokenType.PROPERTY
136137
is VariableDescriptor -> SemanticTokenType.VARIABLE
137138
is ConstructorDescriptor -> when (target.constructedClass.kind) {
139+
ClassKind.ENUM_ENTRY -> SemanticTokenType.ENUM_MEMBER
138140
ClassKind.ANNOTATION_CLASS -> SemanticTokenType.TYPE // annotations look nicer this way
139141
else -> SemanticTokenType.FUNCTION
140142
}
141143
is FunctionDescriptor -> SemanticTokenType.FUNCTION
142144
is ClassDescriptor -> when (target.kind) {
145+
ClassKind.ENUM_ENTRY -> SemanticTokenType.ENUM_MEMBER
143146
ClassKind.CLASS -> SemanticTokenType.CLASS
144147
ClassKind.OBJECT -> SemanticTokenType.CLASS
145148
ClassKind.INTERFACE -> SemanticTokenType.INTERFACE
@@ -160,6 +163,7 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
160163
val tokenType = when (element) {
161164
is KtParameter -> SemanticTokenType.PARAMETER
162165
is KtProperty -> SemanticTokenType.PROPERTY
166+
is KtEnumEntry -> SemanticTokenType.ENUM_MEMBER
163167
is KtVariableDeclaration -> SemanticTokenType.VARIABLE
164168
is KtClassOrObject -> SemanticTokenType.CLASS
165169
is KtFunction -> SemanticTokenType.FUNCTION

server/src/test/kotlin/org/javacs/kt/SemanticTokensTest.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticToke
1414
val constLine = 2
1515
val classLine = 4
1616
val funLine = 6
17+
val enumLine = 8
1718

1819
val expectedVar = sequenceOf(
1920
SemanticToken(range(varLine, 5, varLine, 13), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION)), // variable
@@ -38,12 +39,16 @@ class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticToke
3839
SemanticToken(range(funLine, 30, funLine, 31), SemanticTokenType.FUNCTION), // f
3940
SemanticToken(range(funLine, 32, funLine, 33), SemanticTokenType.VARIABLE, setOf(SemanticTokenModifier.READONLY)), // x
4041
)
42+
val expectedEnum = sequenceOf(
43+
SemanticToken(range(enumLine, 12, enumLine, 16), SemanticTokenType.CLASS, setOf(SemanticTokenModifier.DECLARATION)), // Enum
44+
SemanticToken(range(enumLine, 19, enumLine, 27), SemanticTokenType.ENUM_MEMBER, setOf(SemanticTokenModifier.DECLARATION)) // Variant1
45+
)
4146

4247
val partialExpected = encodeTokens(expectedConst + expectedClass)
4348
val partialResponse = languageServer.textDocumentService.semanticTokensRange(semanticTokensRangeParams(file, range(constLine, 0, classLine + 1, 0))).get()!!
4449
assertThat(partialResponse.data, contains(*partialExpected.toTypedArray()))
4550

46-
val fullExpected = encodeTokens(expectedVar + expectedConst + expectedClass + expectedFun)
51+
val fullExpected = encodeTokens(expectedVar + expectedConst + expectedClass + expectedFun + expectedEnum)
4752
val fullResponse = languageServer.textDocumentService.semanticTokensFull(semanticTokensParams(file)).get()!!
4853
assertThat(fullResponse.data, contains(*fullExpected.toTypedArray()))
4954
}

server/src/test/resources/semantictokens/SemanticTokens.kt

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ val constant: String = "test $variable"
44
data class Type(val property: Int)
55

66
fun f(x: Int? = null): Int = f(x)
7+
8+
enum class Enum { Variant1 }

0 commit comments

Comments
 (0)