Skip to content

Commit d9dc2bd

Browse files
committed
Add semantic token test for plain string literal
1 parent 3827e4f commit d9dc2bd

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@ class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticToke
1212
@Test fun `tokenize file`() {
1313
val varLine = 1
1414
val constLine = 2
15-
val classLine = 4
16-
val funLine = 6
17-
val enumLine = 8
15+
val stringLine = 3
16+
val classLine = 5
17+
val funLine = 7
18+
val enumLine = 9
1819

1920
val expectedVar = sequenceOf(
2021
SemanticToken(range(varLine, 5, varLine, 13), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION)), // variable
2122
)
23+
// Neither string literals nor interpolations (which are both
24+
// represented as string templates) are currently emitted as semantic
25+
// tokens. This is to avoid "covering" interpolations with the literal.
26+
// A more sophisticated implementation would slice up the string tokens
27+
// to not include child nodes, but that's for a future implementation.
2228
val expectedConst = sequenceOf(
2329
SemanticToken(range(constLine, 5, constLine, 13), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // constant
2430
SemanticToken(range(constLine, 15, constLine, 21), SemanticTokenType.CLASS), // String
2531
SemanticToken(range(constLine, 30, constLine, 39), SemanticTokenType.INTERPOLATION_ENTRY), // $variable
2632
SemanticToken(range(constLine, 31, constLine, 39), SemanticTokenType.PROPERTY), // variable
2733
)
34+
val expectedString = sequenceOf(
35+
SemanticToken(range(stringLine, 5, stringLine, 11), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // string
36+
)
2837
val expectedClass = sequenceOf(
2938
SemanticToken(range(classLine, 12, classLine, 16), SemanticTokenType.CLASS, setOf(SemanticTokenModifier.DECLARATION)), // Type
3039
SemanticToken(range(classLine, 21, classLine, 29), SemanticTokenType.PARAMETER, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // property
@@ -43,11 +52,11 @@ class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticToke
4352
SemanticToken(range(enumLine, 19, enumLine, 27), SemanticTokenType.ENUM_MEMBER, setOf(SemanticTokenModifier.DECLARATION)) // Variant1
4453
)
4554

46-
val partialExpected = encodeTokens(expectedConst + expectedClass)
55+
val partialExpected = encodeTokens(expectedConst + expectedString + expectedClass)
4756
val partialResponse = languageServer.textDocumentService.semanticTokensRange(semanticTokensRangeParams(file, range(constLine, 0, classLine + 1, 0))).get()!!
4857
assertThat(partialResponse.data, contains(*partialExpected.toTypedArray()))
4958

50-
val fullExpected = encodeTokens(expectedVar + expectedConst + expectedClass + expectedFun + expectedEnum)
59+
val fullExpected = encodeTokens(expectedVar + expectedConst + expectedString + expectedClass + expectedFun + expectedEnum)
5160
val fullResponse = languageServer.textDocumentService.semanticTokensFull(semanticTokensParams(file)).get()!!
5261
assertThat(fullResponse.data, contains(*fullExpected.toTypedArray()))
5362
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var variable = 3
22
val constant: String = "test $variable"
3+
val string = "abc"
34

45
data class Type(val property: Int)
56

67
fun f(x: Int? = null): Int = f(x)
78

8-
enum class Enum { Variant1 }
9+
enum class Enum { Variant1 }

0 commit comments

Comments
 (0)