diff --git a/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/progress.kt b/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/progress.kt index b3690ac20409..9e37f08e405c 100644 --- a/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/progress.kt +++ b/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/progress.kt @@ -1,7 +1,6 @@ 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 @@ -11,19 +10,27 @@ fun interface ProgressReporter { companion object { val NOOP: ProgressReporter = ProgressReporter {} } + fun report(progress: WorkDoneProgress) } -suspend fun LspClient.withProgress( +suspend fun LspClient.withProgress( request: WorkDoneProgressParams, - body: suspend CoroutineScope.(ProgressReporter) -> T -): T = + 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(null) - launch { + val endProgress = launch { state.filterNotNull().collect { p -> notify(LSP.ProgressNotificationType, ProgressParams(token, LSP.json.encodeToJsonElement(WorkDoneProgress.serializer(), p))) delay(100.milliseconds) @@ -31,6 +38,12 @@ suspend fun LspClient.withProgress( }.use { body(ProgressReporter { p -> state.value = p }) } + + notify( + LSP.ProgressNotificationType, + ProgressParams(token, LSP.json.encodeToJsonElement(WorkDoneProgress.serializer(), endProgress)) + ) } } - } \ No newline at end of file + } +} \ No newline at end of file