From d9dc2bd0c227a76ae39e7c2eecbb533636145249 Mon Sep 17 00:00:00 2001 From: fwcd Date: Tue, 16 Jan 2024 01:17:17 +0000 Subject: [PATCH] Add semantic token test for plain string literal --- .../org/javacs/kt/SemanticTokensTest.kt | 19 ++++++++++++++----- .../semantictokens/SemanticTokens.kt | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/server/src/test/kotlin/org/javacs/kt/SemanticTokensTest.kt b/server/src/test/kotlin/org/javacs/kt/SemanticTokensTest.kt index 029fe33db..f18d15114 100644 --- a/server/src/test/kotlin/org/javacs/kt/SemanticTokensTest.kt +++ b/server/src/test/kotlin/org/javacs/kt/SemanticTokensTest.kt @@ -12,19 +12,28 @@ class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticToke @Test fun `tokenize file`() { val varLine = 1 val constLine = 2 - val classLine = 4 - val funLine = 6 - val enumLine = 8 + val stringLine = 3 + val classLine = 5 + val funLine = 7 + val enumLine = 9 val expectedVar = sequenceOf( SemanticToken(range(varLine, 5, varLine, 13), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION)), // variable ) + // Neither string literals nor interpolations (which are both + // represented as string templates) are currently emitted as semantic + // tokens. This is to avoid "covering" interpolations with the literal. + // A more sophisticated implementation would slice up the string tokens + // to not include child nodes, but that's for a future implementation. val expectedConst = sequenceOf( SemanticToken(range(constLine, 5, constLine, 13), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // constant SemanticToken(range(constLine, 15, constLine, 21), SemanticTokenType.CLASS), // String SemanticToken(range(constLine, 30, constLine, 39), SemanticTokenType.INTERPOLATION_ENTRY), // $variable SemanticToken(range(constLine, 31, constLine, 39), SemanticTokenType.PROPERTY), // variable ) + val expectedString = sequenceOf( + SemanticToken(range(stringLine, 5, stringLine, 11), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // string + ) val expectedClass = sequenceOf( SemanticToken(range(classLine, 12, classLine, 16), SemanticTokenType.CLASS, setOf(SemanticTokenModifier.DECLARATION)), // Type SemanticToken(range(classLine, 21, classLine, 29), SemanticTokenType.PARAMETER, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // property @@ -43,11 +52,11 @@ class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticToke SemanticToken(range(enumLine, 19, enumLine, 27), SemanticTokenType.ENUM_MEMBER, setOf(SemanticTokenModifier.DECLARATION)) // Variant1 ) - val partialExpected = encodeTokens(expectedConst + expectedClass) + val partialExpected = encodeTokens(expectedConst + expectedString + expectedClass) val partialResponse = languageServer.textDocumentService.semanticTokensRange(semanticTokensRangeParams(file, range(constLine, 0, classLine + 1, 0))).get()!! assertThat(partialResponse.data, contains(*partialExpected.toTypedArray())) - val fullExpected = encodeTokens(expectedVar + expectedConst + expectedClass + expectedFun + expectedEnum) + val fullExpected = encodeTokens(expectedVar + expectedConst + expectedString + expectedClass + expectedFun + expectedEnum) val fullResponse = languageServer.textDocumentService.semanticTokensFull(semanticTokensParams(file)).get()!! assertThat(fullResponse.data, contains(*fullExpected.toTypedArray())) } diff --git a/server/src/test/resources/semantictokens/SemanticTokens.kt b/server/src/test/resources/semantictokens/SemanticTokens.kt index d1aa6eba7..1a9ef04ac 100644 --- a/server/src/test/resources/semantictokens/SemanticTokens.kt +++ b/server/src/test/resources/semantictokens/SemanticTokens.kt @@ -1,8 +1,9 @@ var variable = 3 val constant: String = "test $variable" +val string = "abc" data class Type(val property: Int) fun f(x: Int? = null): Int = f(x) -enum class Enum { Variant1 } \ No newline at end of file +enum class Enum { Variant1 }