Skip to content

Commit 7b93dd6

Browse files
authored
Merge pull request #548 from fwcd/remove-string-semantic-tokens
Remove semantic tokens for string literals (templates)
2 parents 382b174 + 52700d9 commit 7b93dd6

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S
187187

188188
// Literals and string interpolations
189189

190-
is KtSimpleNameStringTemplateEntry, is KtBlockStringTemplateEntry ->
190+
is KtSimpleNameStringTemplateEntry ->
191191
SemanticToken(elementRange, SemanticTokenType.INTERPOLATION_ENTRY)
192-
is KtStringTemplateExpression -> SemanticToken(elementRange, SemanticTokenType.STRING)
193192
is PsiLiteralExpression -> {
194193
val tokenType = when (element.type) {
195194
PsiType.INT, PsiType.LONG, PsiType.DOUBLE -> SemanticTokenType.NUMBER

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +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
25-
SemanticToken(range(constLine, 24, constLine, 40), SemanticTokenType.STRING), // "test $variable"
2631
SemanticToken(range(constLine, 30, constLine, 39), SemanticTokenType.INTERPOLATION_ENTRY), // $variable
2732
SemanticToken(range(constLine, 31, constLine, 39), SemanticTokenType.PROPERTY), // variable
2833
)
34+
val expectedString = sequenceOf(
35+
SemanticToken(range(stringLine, 5, stringLine, 11), SemanticTokenType.PROPERTY, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // string
36+
)
2937
val expectedClass = sequenceOf(
3038
SemanticToken(range(classLine, 12, classLine, 16), SemanticTokenType.CLASS, setOf(SemanticTokenModifier.DECLARATION)), // Type
3139
SemanticToken(range(classLine, 21, classLine, 29), SemanticTokenType.PARAMETER, setOf(SemanticTokenModifier.DECLARATION, SemanticTokenModifier.READONLY)), // property
@@ -44,11 +52,11 @@ class SemanticTokensTest : SingleFileTestFixture("semantictokens", "SemanticToke
4452
SemanticToken(range(enumLine, 19, enumLine, 27), SemanticTokenType.ENUM_MEMBER, setOf(SemanticTokenModifier.DECLARATION)) // Variant1
4553
)
4654

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

51-
val fullExpected = encodeTokens(expectedVar + expectedConst + expectedClass + expectedFun + expectedEnum)
59+
val fullExpected = encodeTokens(expectedVar + expectedConst + expectedString + expectedClass + expectedFun + expectedEnum)
5260
val fullResponse = languageServer.textDocumentService.semanticTokensFull(semanticTokensParams(file)).get()!!
5361
assertThat(fullResponse.data, contains(*fullExpected.toTypedArray()))
5462
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var variable = 3
2-
val constant: String = "test $variable"
2+
val constant: String = "test $variable ${12}"
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)