Skip to content

Commit 308e6d7

Browse files
committed
Expose function for computing non-encoded semantic tokens
...this will be useful for testing
1 parent ca55e42 commit 308e6d7

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

server/src/main/kotlin/org/javacs/kt/KotlinTextDocumentService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.javacs.kt.position.offset
1414
import org.javacs.kt.position.extractRange
1515
import org.javacs.kt.position.position
1616
import org.javacs.kt.references.findReferences
17-
import org.javacs.kt.semantictokens.semanticTokens
17+
import org.javacs.kt.semantictokens.encodedSemanticTokens
1818
import org.javacs.kt.signaturehelp.fetchSignatureHelpAt
1919
import org.javacs.kt.symbols.documentSymbols
2020
import org.javacs.kt.util.noResult
@@ -233,7 +233,7 @@ class KotlinTextDocumentService(
233233
val uri = parseURI(params.textDocument.uri)
234234
val file = sp.currentVersion(uri)
235235

236-
val tokens = semanticTokens(file)
236+
val tokens = encodedSemanticTokens(file)
237237
LOG.info("Found {} tokens", tokens.size)
238238

239239
SemanticTokens(tokens)
@@ -247,7 +247,7 @@ class KotlinTextDocumentService(
247247
val uri = parseURI(params.textDocument.uri)
248248
val file = sp.currentVersion(uri)
249249

250-
val tokens = semanticTokens(file, params.range)
250+
val tokens = encodedSemanticTokens(file, params.range)
251251
LOG.info("Found {} tokens", tokens.size)
252252

253253
SemanticTokens(tokens)

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import com.intellij.psi.PsiElement
2626
import com.intellij.psi.PsiNameIdentifierOwner
2727
import com.intellij.openapi.util.TextRange
2828

29-
private enum class SemanticTokenType(val typeName: String) {
29+
enum class SemanticTokenType(val typeName: String) {
3030
KEYWORD(SemanticTokenTypes.Keyword),
3131
VARIABLE(SemanticTokenTypes.Variable),
3232
FUNCTION(SemanticTokenTypes.Function),
@@ -39,7 +39,7 @@ private enum class SemanticTokenType(val typeName: String) {
3939
TYPE(SemanticTokenTypes.Type)
4040
}
4141

42-
private enum class SemanticTokenModifier(val modifierName: String) {
42+
enum class SemanticTokenModifier(val modifierName: String) {
4343
DECLARATION(SemanticTokenModifiers.Declaration),
4444
DEFINITION(SemanticTokenModifiers.Definition),
4545
ABSTRACT(SemanticTokenModifiers.Abstract),
@@ -51,14 +51,21 @@ val semanticTokensLegend = SemanticTokensLegend(
5151
SemanticTokenModifier.values().map { it.modifierName }
5252
)
5353

54-
private data class SemanticToken(val range: Range, val type: SemanticTokenType, val modifiers: Set<SemanticTokenModifier> = setOf())
54+
data class SemanticToken(val range: Range, val type: SemanticTokenType, val modifiers: Set<SemanticTokenModifier> = setOf())
55+
56+
/**
57+
* Computes LSP-encoded semantic tokens for the given range in the
58+
* document. No range means the entire document.
59+
*/
60+
fun encodedSemanticTokens(file: CompiledFile, range: Range? = null): List<Int> =
61+
encodeTokens(semanticTokens(file, range))
5562

5663
/**
5764
* Computes semantic tokens for the given range in the document.
5865
* No range means the entire document.
5966
*/
60-
fun semanticTokens(file: CompiledFile, range: Range? = null): List<Int> =
61-
encodeTokens(elementTokens(file.parse, file.compile, range))
67+
fun semanticTokens(file: CompiledFile, range: Range? = null): Sequence<SemanticToken> =
68+
elementTokens(file.parse, file.compile, range)
6269

6370
private fun encodeTokens(tokens: Sequence<SemanticToken>): List<Int> {
6471
val encoded = mutableListOf<Int>()

0 commit comments

Comments
 (0)