Skip to content

Commit

Permalink
Merge pull request #548 from fwcd/remove-string-semantic-tokens
Browse files Browse the repository at this point in the history
Remove semantic tokens for string literals (templates)
  • Loading branch information
fwcd authored Jan 16, 2024
2 parents 382b174 + 52700d9 commit 7b93dd6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ private fun elementToken(element: PsiElement, bindingContext: BindingContext): S

// Literals and string interpolations

is KtSimpleNameStringTemplateEntry, is KtBlockStringTemplateEntry ->
is KtSimpleNameStringTemplateEntry ->
SemanticToken(elementRange, SemanticTokenType.INTERPOLATION_ENTRY)
is KtStringTemplateExpression -> SemanticToken(elementRange, SemanticTokenType.STRING)
is PsiLiteralExpression -> {
val tokenType = when (element.type) {
PsiType.INT, PsiType.LONG, PsiType.DOUBLE -> SemanticTokenType.NUMBER
Expand Down
20 changes: 14 additions & 6 deletions server/src/test/kotlin/org/javacs/kt/SemanticTokensTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +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, 24, constLine, 40), SemanticTokenType.STRING), // "test $variable"
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
Expand All @@ -44,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()))
}
Expand Down
5 changes: 3 additions & 2 deletions server/src/test/resources/semantictokens/SemanticTokens.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var variable = 3
val constant: String = "test $variable"
val constant: String = "test $variable ${12}"
val string = "abc"

data class Type(val property: Int)

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

enum class Enum { Variant1 }
enum class Enum { Variant1 }

0 comments on commit 7b93dd6

Please sign in to comment.