Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update semantic tokens asynchronously #298

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,10 @@ class KotlinTextDocumentService(

reportTime {
val uri = parseURI(params.textDocument.uri)
val file = sp.currentVersion(uri)
val parse = sp.parsedFile(uri)
val file = sp.latestCompiledVersion(uri)

val tokens = encodedSemanticTokens(file)
val tokens = encodedSemanticTokens(parse, file.compile)
LOG.info("Found {} tokens", tokens.size)

SemanticTokens(tokens)
Expand All @@ -245,9 +246,10 @@ class KotlinTextDocumentService(

reportTime {
val uri = parseURI(params.textDocument.uri)
val file = sp.currentVersion(uri)
val parse = sp.parsedFile(uri)
val file = sp.latestCompiledVersion(uri)

val tokens = encodedSemanticTokens(file, params.range)
val tokens = encodedSemanticTokens(parse, file.compile, params.range)
LOG.info("Found {} tokens", tokens.size)

SemanticTokens(tokens)
Expand Down Expand Up @@ -288,6 +290,7 @@ class KotlinTextDocumentService(
val context = sp.compileFiles(files)
if (!cancelCallback.invoke()) {
reportDiagnostics(files, context.diagnostics)
client.refreshSemanticTokens()
}
lintCount++
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
import org.jetbrains.kotlin.psi.KtEscapeStringTemplateEntry
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
Expand Down Expand Up @@ -71,15 +72,15 @@ data class SemanticToken(val range: Range, val type: SemanticTokenType, val modi
* Computes LSP-encoded semantic tokens for the given range in the
* document. No range means the entire document.
*/
fun encodedSemanticTokens(file: CompiledFile, range: Range? = null): List<Int> =
encodeTokens(semanticTokens(file, range))
fun encodedSemanticTokens(parse: KtFile, bindingContext: BindingContext, range: Range? = null): List<Int> =
encodeTokens(semanticTokens(parse, bindingContext, range))

/**
* Computes semantic tokens for the given range in the document.
* No range means the entire document.
*/
fun semanticTokens(file: CompiledFile, range: Range? = null): Sequence<SemanticToken> =
elementTokens(file.parse, file.compile, range)
fun semanticTokens(parse: KtFile, bindingContext: BindingContext, range: Range? = null): Sequence<SemanticToken> =
elementTokens(parse, bindingContext, range)

fun encodeTokens(tokens: Sequence<SemanticToken>): List<Int> {
val encoded = mutableListOf<Int>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ abstract class LanguageServerTestFixture(relativeWorkspaceRoot: String) : Langua
println(`object`.toString())
}

// We don't implement this since we are waiting for the linter to complete anyway
// before performing the semantic tokens test (and for the other tests, semantic
// tokens aren't relevant, so we can save time by not performing this request)
override fun refreshSemanticTokens(): CompletableFuture<Void> = CompletableFuture.completedFuture(null)

override fun logMessage(message: MessageParams?) = printMessage(message)

override fun showMessage(message: MessageParams?) = printMessage(message)
Expand Down