[analyzer] refactor: extract progress to a file

GitOrigin-RevId: 78d779182d536b6f632ce4763ebff631ee2096d5
This commit is contained in:
Andrey Zaytsev
2025-10-30 03:19:54 +00:00
committed by intellij-monorepo-bot
parent 584e4094bd
commit 5dc7067bd2
2 changed files with 36 additions and 26 deletions
@@ -53,33 +53,7 @@ class LspHandlerContext(
context(context: LspHandlerContext)
val lspClient: LspClient get() = context.lspClient
fun interface ProgressReporter {
companion object {
val NOOP: ProgressReporter = ProgressReporter {}
}
fun report(progress: WorkDoneProgress)
}
suspend fun<T> LspClient.withProgress(
request: WorkDoneProgressParams,
body: suspend CoroutineScope.(ProgressReporter) -> T
): T =
coroutineScope {
when (val token = request.workDoneToken) {
null -> body(ProgressReporter.NOOP)
else -> {
val state = MutableStateFlow<WorkDoneProgress?>(null)
launch {
state.filterNotNull().collect { p ->
notify(ProgressNotificationType, ProgressParams(token, LSP.json.encodeToJsonElement(WorkDoneProgress.serializer(), p)))
delay(100.milliseconds)
}
}.use {
body(ProgressReporter { p -> state.value = p })
}
}
}
}
class LspRequestHandler<Params, Result, Error>(
val requestType: RequestType<Params, Result, Error>,
@@ -0,0 +1,36 @@
package com.jetbrains.lsp.implementation
import com.jetbrains.lsp.protocol.*
import com.jetbrains.lsp.implementation.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlin.time.Duration.Companion.milliseconds
fun interface ProgressReporter {
companion object {
val NOOP: ProgressReporter = ProgressReporter {}
}
fun report(progress: WorkDoneProgress)
}
suspend fun<T> LspClient.withProgress(
request: WorkDoneProgressParams,
body: suspend CoroutineScope.(ProgressReporter) -> T
): T =
coroutineScope {
when (val token = request.workDoneToken) {
null -> body(ProgressReporter.NOOP)
else -> {
val state = MutableStateFlow<WorkDoneProgress?>(null)
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 })
}
}
}
}