Files
Leonid Shalupov 188b7ef96f IJI-3282 Optimize imports
GitOrigin-RevId: 3b5c00d8ff20b8d0bb6005bc450921085d2da9b2
2026-01-31 17:03:42 +00:00

55 lines
1.8 KiB
Kotlin

package com.jetbrains.lsp.implementation
import com.jetbrains.lsp.protocol.LSP
import com.jetbrains.lsp.protocol.ProgressParams
import com.jetbrains.lsp.protocol.WorkDoneProgress
import com.jetbrains.lsp.protocol.WorkDoneProgressParams
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.milliseconds
fun interface ProgressReporter {
companion object {
val NOOP: ProgressReporter = ProgressReporter {}
}
fun report(progress: WorkDoneProgress)
}
suspend fun LspClient.withProgress(
request: WorkDoneProgressParams,
beginTitle: String,
body: suspend CoroutineScope.(ProgressReporter) -> WorkDoneProgress.End,
) {
coroutineScope {
when (val token = request.workDoneToken) {
null -> body(ProgressReporter.NOOP)
else -> {
val beginProgress = WorkDoneProgress.Begin(title = beginTitle)
notify(
LSP.ProgressNotificationType,
ProgressParams(token, LSP.json.encodeToJsonElement(WorkDoneProgress.serializer(), beginProgress)),
)
val state = MutableStateFlow<WorkDoneProgress?>(null)
val endProgress = launch {
state.filterNotNull().collect { p ->
notify(LSP.ProgressNotificationType, ProgressParams(token, LSP.json.encodeToJsonElement(WorkDoneProgress.serializer(), p)))
delay(100.milliseconds)
}
}.use {
body(ProgressReporter { p -> state.value = p })
}
notify(
LSP.ProgressNotificationType,
ProgressParams(token, LSP.json.encodeToJsonElement(WorkDoneProgress.serializer(), endProgress))
)
}
}
}
}