mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[analyzer] refactor: extract progress to a file
GitOrigin-RevId: 78d779182d536b6f632ce4763ebff631ee2096d5
This commit is contained in:
committed by
intellij-monorepo-bot
parent
584e4094bd
commit
5dc7067bd2
@@ -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 })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user