Skip to content

Commit 8361f9b

Browse files
committed
Look up descriptor type for semantic tokens
1 parent 03db435 commit 8361f9b

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ class KotlinTextDocumentService(
231231

232232
reportTime {
233233
val uri = parseURI(params.textDocument.uri)
234-
val parsed = sp.parsedFile(uri)
234+
val file = sp.latestCompiledVersion(uri)
235235

236-
val tokens = semanticTokens(parsed)
236+
val tokens = semanticTokens(file)
237237
LOG.info("Found {} tokens", tokens.size)
238238

239239
SemanticTokens(tokens)

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@ import org.eclipse.lsp4j.SemanticTokenTypes
44
import org.eclipse.lsp4j.SemanticTokenModifiers
55
import org.eclipse.lsp4j.SemanticTokensLegend
66
import org.eclipse.lsp4j.Range
7+
import org.javacs.kt.CompiledFile
78
import org.javacs.kt.position.range
89
import org.javacs.kt.util.preOrderTraversal
10+
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
11+
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
12+
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
13+
import org.jetbrains.kotlin.descriptors.VariableDescriptor
914
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
1015
import org.jetbrains.kotlin.psi.KtVariableDeclaration
1116
import org.jetbrains.kotlin.psi.KtNamedDeclaration
17+
import org.jetbrains.kotlin.resolve.BindingContext
1218
import com.intellij.psi.PsiElement
1319

1420
private enum class SemanticTokenType(val typeName: String) {
1521
VARIABLE(SemanticTokenTypes.Variable),
22+
FUNCTION(SemanticTokenTypes.Function),
1623
PROPERTY(SemanticTokenTypes.Property),
17-
ENUM_MEMBER(SemanticTokenTypes.EnumMember)
24+
ENUM_MEMBER(SemanticTokenTypes.EnumMember),
25+
TYPE(SemanticTokenTypes.Type)
1826
}
1927

2028
private enum class SemanticTokenModifier(val modifierName: String) {
@@ -29,7 +37,8 @@ val semanticTokensLegend = SemanticTokensLegend(
2937

3038
private data class SemanticToken(val range: Range, val type: SemanticTokenType, val modifiers: Set<SemanticTokenModifier> = setOf())
3139

32-
fun semanticTokens(element: PsiElement): List<Int> = encodeTokens(elementTokens(element))
40+
fun semanticTokens(file: CompiledFile): List<Int> =
41+
encodeTokens(elementTokens(file.parse, file.compile))
3342

3443
private fun encodeTokens(tokens: Sequence<SemanticToken>): List<Int> {
3544
val encoded = mutableListOf<Int>()
@@ -61,18 +70,25 @@ private fun encodeModifiers(modifiers: Set<SemanticTokenModifier>): Int = modifi
6170
.map { 1 shl it.ordinal }
6271
.fold(0, Int::or)
6372

64-
private fun elementTokens(element: PsiElement): Sequence<SemanticToken> = element
73+
private fun elementTokens(element: PsiElement, bindingContext: BindingContext): Sequence<SemanticToken> = element
6574
.preOrderTraversal()
66-
// .mapNotNull { (it as? KtNamedDeclaration)?.nameIdentifier }
67-
.mapNotNull { elementToken(it) }
75+
.mapNotNull { elementToken(it, bindingContext) }
6876

69-
private fun elementToken(element: PsiElement): SemanticToken? {
77+
private fun elementToken(element: PsiElement, bindingContext: BindingContext): SemanticToken? {
7078
val file = element.containingFile
7179
val elementRange = range(file.text, element.textRange)
7280
return when (element) {
73-
is KtNameReferenceExpression -> SemanticToken(elementRange, SemanticTokenType.VARIABLE)
74-
// is KtProperty -> SemanticToken(elementRange, SemanticTokenType.PROPERTY)
75-
// is KtVariableDeclaration -> SemanticToken(elementRange, SemanticTokenType.VARIABLE)
81+
is KtNameReferenceExpression -> {
82+
val target = bindingContext[BindingContext.REFERENCE_TARGET, element]
83+
val tokenType = when (target) {
84+
is PropertyDescriptor -> SemanticTokenType.PROPERTY
85+
is VariableDescriptor -> SemanticTokenType.VARIABLE
86+
is FunctionDescriptor -> SemanticTokenType.FUNCTION
87+
is ClassifierDescriptor -> SemanticTokenType.TYPE
88+
else -> return null
89+
}
90+
SemanticToken(elementRange, tokenType)
91+
}
7692
else -> null
7793
}
7894
}

0 commit comments

Comments
 (0)