Skip to content

Commit e7c2c67

Browse files
committed
Implement semantic tokens test
1 parent 888a3a7 commit e7c2c67

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fun encodedSemanticTokens(file: CompiledFile, range: Range? = null): List<Int> =
8080
fun semanticTokens(file: CompiledFile, range: Range? = null): Sequence<SemanticToken> =
8181
elementTokens(file.parse, file.compile, range)
8282

83-
private fun encodeTokens(tokens: Sequence<SemanticToken>): List<Int> {
83+
fun encodeTokens(tokens: Sequence<SemanticToken>): List<Int> {
8484
val encoded = mutableListOf<Int>()
8585
var last: SemanticToken? = null
8686

@@ -149,19 +149,17 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
149149
}
150150
is PsiNameIdentifierOwner -> {
151151
val tokenType = when (element) {
152-
is KtProperty -> SemanticTokenType.PROPERTY
153152
is KtParameter -> SemanticTokenType.PARAMETER
153+
is KtProperty -> SemanticTokenType.PROPERTY
154154
is KtVariableDeclaration -> SemanticTokenType.VARIABLE
155155
is KtClassOrObject -> SemanticTokenType.CLASS
156156
else -> return null
157157
}
158158
val identifierRange = element.nameIdentifier?.let { range(file.text, it.textRange) } ?: return null
159159
val modifiers = mutableSetOf(SemanticTokenModifier.DECLARATION)
160160

161-
if (element is KtVariableDeclaration) {
162-
if (!element.isVar() || element.hasModifier(KtTokens.CONST_KEYWORD)) {
163-
modifiers.add(SemanticTokenModifier.READONLY)
164-
}
161+
if (element is KtVariableDeclaration && (!element.isVar() || element.hasModifier(KtTokens.CONST_KEYWORD)) || element is KtParameter) {
162+
modifiers.add(SemanticTokenModifier.READONLY)
165163
}
166164

167165
if (element is KtModifierListOwner) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ abstract class LanguageServerTestFixture(relativeWorkspaceRoot: String) : Langua
6868
fun hoverParams(relativePath: String, line: Int, column: Int): HoverParams =
6969
textDocumentPosition(relativePath, line, column).run { HoverParams(textDocument, position) }
7070

71+
fun semanticTokensParams(relativePath: String): SemanticTokensParams =
72+
textDocumentPosition(relativePath, 0, 0).run { SemanticTokensParams(textDocument) }
73+
7174
fun signatureHelpParams(relativePath: String, line: Int, column: Int): SignatureHelpParams =
7275
textDocumentPosition(relativePath, line, column).run { SignatureHelpParams(textDocument, position) }
7376

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,38 @@ package org.javacs.kt
33
import org.hamcrest.Matchers.*
44
import org.junit.Assert.assertThat
55
import org.junit.Test
6+
import org.javacs.kt.semantictokens.encodeTokens
7+
import org.javacs.kt.semantictokens.SemanticToken
8+
import org.javacs.kt.semantictokens.SemanticTokenType
9+
import org.javacs.kt.semantictokens.SemanticTokenModifier
610

711
class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticTokens.kt") {
8-
// TODO
12+
@Test fun `tokenize entire file`() {
13+
val response = languageServer.textDocumentService.semanticTokensFull(semanticTokensParams(file)).get()!!
14+
val actual = response.data
15+
val expected = encodeTokens(sequenceOf(
16+
// SemanticToken(range(1, 5, 1, 13), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION)), // variable
17+
// SemanticToken(range(2, 5, 2, 13), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // constant
18+
// SemanticToken(range(2, 15, 2, 21), SemanticTokenType.CLASS), // String
19+
// SemanticToken(range(2, 24, 2, 40), SemanticTokenType.STRING), // "test $variable"
20+
// SemanticToken(range(2, 30, 2, 39), SemanticTokenType.INTERPOLATION_ENTRY), // $variable
21+
// SemanticToken(range(2, 31, 2, 39), SemanticTokenType.VARIABLE), // variable
22+
23+
// SemanticToken(range(4, 12, 4, 16), SemanticTokenType.CLASS, setOf(SemanticTokenModifier.DECLARATION)), // Type
24+
// SemanticToken(range(4, 21, 4, 29), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // property
25+
// SemanticToken(range(4, 31, 4, 34), SemanticTokenType.CLASS), // Int
26+
27+
// SemanticToken(range(6, 5, 6, 6), SemanticTokenType.FUNCTION, setOf(SemanticTokenModifier.DECLARATION)), // f
28+
SemanticToken(range(6, 7, 6, 8), SemanticTokenType.PARAMETER, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // x
29+
SemanticToken(range(6, 10, 6, 13), SemanticTokenType.CLASS), // Int?
30+
SemanticToken(range(6, 24, 6, 27), SemanticTokenType.CLASS), // Int
31+
SemanticToken(range(6, 30, 6, 31), SemanticTokenType.FUNCTION), // f
32+
SemanticToken(range(6, 32, 6, 33), SemanticTokenType.VARIABLE, setOf(SemanticTokenModifier.READONLY)), // x
33+
))
34+
35+
println(actual)
36+
println(expected)
37+
38+
assertThat(actual, contains(*expected.toTypedArray()))
39+
}
940
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// var variable = 3
2+
// val constant: String = "test $variable"
3+
4+
// data class Type(val property: Int)
5+
6+
fun f(x: Int? = null): Int = f(x)

0 commit comments

Comments
 (0)