Skip to content

Commit a113208

Browse files
committed
Prove semantic tokens for function declarations
1 parent e7c2c67 commit a113208

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor
1616
import org.jetbrains.kotlin.descriptors.VariableDescriptor
1717
import org.jetbrains.kotlin.lexer.KtTokens
1818
import org.jetbrains.kotlin.psi.KtClassOrObject
19+
import org.jetbrains.kotlin.psi.KtFunction
1920
import org.jetbrains.kotlin.psi.KtModifierListOwner
2021
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
2122
import org.jetbrains.kotlin.psi.KtVariableDeclaration
@@ -123,6 +124,8 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
123124
val elementRange = range(file.text, element.textRange)
124125

125126
return when (element) {
127+
// References (variables, types, functions, ...)
128+
126129
is KtNameReferenceExpression -> {
127130
val target = bindingContext[BindingContext.REFERENCE_TARGET, element]
128131
val tokenType = when (target) {
@@ -147,12 +150,16 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
147150

148151
SemanticToken(elementRange, tokenType, modifiers)
149152
}
153+
154+
// Declarations (variables, types, functions, ...)
155+
150156
is PsiNameIdentifierOwner -> {
151157
val tokenType = when (element) {
152158
is KtParameter -> SemanticTokenType.PARAMETER
153159
is KtProperty -> SemanticTokenType.PROPERTY
154160
is KtVariableDeclaration -> SemanticTokenType.VARIABLE
155161
is KtClassOrObject -> SemanticTokenType.CLASS
162+
is KtFunction -> SemanticTokenType.FUNCTION
156163
else -> return null
157164
}
158165
val identifierRange = element.nameIdentifier?.let { range(file.text, it.textRange) } ?: return null
@@ -170,6 +177,9 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
170177

171178
SemanticToken(identifierRange, tokenType, modifiers)
172179
}
180+
181+
// Literals and string interpolations
182+
173183
is KtSimpleNameStringTemplateEntry, is KtBlockStringTemplateEntry ->
174184
SemanticToken(elementRange, SemanticTokenType.INTERPOLATION_ENTRY)
175185
is KtStringTemplateExpression -> SemanticToken(elementRange, SemanticTokenType.STRING)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticToke
2424
// SemanticToken(range(4, 21, 4, 29), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // property
2525
// SemanticToken(range(4, 31, 4, 34), SemanticTokenType.CLASS), // Int
2626

27-
// SemanticToken(range(6, 5, 6, 6), SemanticTokenType.FUNCTION, setOf(SemanticTokenModifier.DECLARATION)), // f
27+
SemanticToken(range(6, 5, 6, 6), SemanticTokenType.FUNCTION, setOf(SemanticTokenModifier.DECLARATION)), // f
2828
SemanticToken(range(6, 7, 6, 8), SemanticTokenType.PARAMETER, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // x
2929
SemanticToken(range(6, 10, 6, 13), SemanticTokenType.CLASS), // Int?
3030
SemanticToken(range(6, 24, 6, 27), SemanticTokenType.CLASS), // Int

0 commit comments

Comments
 (0)