Skip to content

Commit 03db435

Browse files
committed
Fix semantic tokens encoding
1 parent 861077f commit 03db435

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.eclipse.lsp4j.SemanticTokensLegend
66
import org.eclipse.lsp4j.Range
77
import org.javacs.kt.position.range
88
import org.javacs.kt.util.preOrderTraversal
9-
import org.jetbrains.kotlin.psi.KtProperty
9+
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
1010
import org.jetbrains.kotlin.psi.KtVariableDeclaration
1111
import org.jetbrains.kotlin.psi.KtNamedDeclaration
1212
import com.intellij.psi.PsiElement
@@ -38,9 +38,9 @@ private fun encodeTokens(tokens: Sequence<SemanticToken>): List<Int> {
3838
for (token in tokens) {
3939
// Tokens must be on a single line
4040
if (token.range.start.line == token.range.end.line) {
41-
val deltaLine = token.range.start.line - (last?.let { it.range.start.line } ?: 0)
42-
val deltaStart = token.range.start.character - (last?.let { it.range.start.character } ?: 0)
4341
val length = token.range.end.character - token.range.start.character
42+
val deltaLine = token.range.start.line - (last?.range?.start?.line ?: 0)
43+
val deltaStart = token.range.start.character - (last?.takeIf { deltaLine == 0 }?.range?.start?.character ?: 0)
4444

4545
encoded.add(deltaLine)
4646
encoded.add(deltaStart)
@@ -63,15 +63,16 @@ private fun encodeModifiers(modifiers: Set<SemanticTokenModifier>): Int = modifi
6363

6464
private fun elementTokens(element: PsiElement): Sequence<SemanticToken> = element
6565
.preOrderTraversal()
66-
.mapNotNull { (it as? KtNamedDeclaration)?.nameIdentifier }
66+
// .mapNotNull { (it as? KtNamedDeclaration)?.nameIdentifier }
6767
.mapNotNull { elementToken(it) }
6868

6969
private fun elementToken(element: PsiElement): SemanticToken? {
7070
val file = element.containingFile
7171
val elementRange = range(file.text, element.textRange)
7272
return when (element) {
73-
is KtProperty -> SemanticToken(elementRange, SemanticTokenType.PROPERTY)
74-
is KtVariableDeclaration -> SemanticToken(elementRange, SemanticTokenType.VARIABLE)
73+
is KtNameReferenceExpression -> SemanticToken(elementRange, SemanticTokenType.VARIABLE)
74+
// is KtProperty -> SemanticToken(elementRange, SemanticTokenType.PROPERTY)
75+
// is KtVariableDeclaration -> SemanticToken(elementRange, SemanticTokenType.VARIABLE)
7576
else -> null
7677
}
7778
}

0 commit comments

Comments
 (0)