diff --git a/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/LspHandlers.kt b/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/LspHandlers.kt index 158554419972..d7bcd552de20 100644 --- a/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/LspHandlers.kt +++ b/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/LspHandlers.kt @@ -51,24 +51,6 @@ class LspNotificationHandler( val handler: suspend context(LspHandlerContext) CoroutineScope.(Params) -> Unit, ) -interface LspHandlersMiddleware { - fun requestHandler(handler: LspRequestHandler): LspRequestHandler - - fun

notificationHandler(handler: LspNotificationHandler

): LspNotificationHandler

- - companion object { - val IDENTITY: LspHandlersMiddleware = object : LspHandlersMiddleware { - override fun requestHandler(handler: LspRequestHandler): LspRequestHandler { - return handler - } - - override fun

notificationHandler(handler: LspNotificationHandler

): LspNotificationHandler

{ - return handler - } - } - } -} - fun lspHandlers(builder: LspHandlersBuilder.() -> Unit): LspHandlers { val requests = mutableMapOf>() val notifications = mutableMapOf>() diff --git a/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/withLsp.kt b/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/withLsp.kt index a739c1825f8d..8d76e8092b5f 100644 --- a/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/withLsp.kt +++ b/fleet/lsp.protocol/srcCommonMain/com/jetbrains/lsp/implementation/withLsp.kt @@ -36,7 +36,6 @@ suspend fun withLsp( incoming: ReceiveChannel, outgoing: SendChannel, handlers: LspHandlers, - middleware: LspHandlersMiddleware = LspHandlersMiddleware.IDENTITY, createCoroutineContext: (LspClient) -> CoroutineContext = { EmptyCoroutineContext }, body: suspend CoroutineScope.(LspClient) -> Unit, ) { @@ -115,7 +114,6 @@ suspend fun withLsp( val request = LSP.json.decodeFromJsonElement(RequestMessage.serializer(), jsonMessage) supervisor.launch(context = CoroutineName("handler for ${request.method}"), start = CoroutineStart.ATOMIC) { val maybeHandler = handlers.requestHandler(request.method) - ?.let { handler -> middleware.requestHandler(handler) } runCatching { val handler = requireNotNull(maybeHandler) { "no handler for request: ${request.method}" @@ -123,13 +121,18 @@ suspend fun withLsp( val deserializedParams = request.params?.let { params -> LSP.json.decodeFromJsonElement(handler.requestType.paramsSerializer, params) } - val result = (handler as LspRequestHandler).handler( - lspHandlerContext, - this, - deserializedParams - ) + + @Suppress("UNCHECKED_CAST") + handler as LspRequestHandler + + val result = handler.handler( + lspHandlerContext, + this, + deserializedParams + ) + LSP.json.encodeToJsonElement( - serializer = handler.requestType.resultSerializer as KSerializer, + serializer = handler.requestType.resultSerializer, value = result ) }.fold( @@ -155,9 +158,11 @@ suspend fun withLsp( code = x.errorCode, message = x.message ?: x::class.simpleName ?: "unknown error", data = runCatching { + @Suppress("UNCHECKED_CAST") val errorSerializer = requireNotNull(maybeHandler) { "we could not have caught LspException if we didn't find the handler" }.requestType.errorSerializer as KSerializer + LSP.json.encodeToJsonElement( serializer = errorSerializer, value = x.payload @@ -206,10 +211,11 @@ suspend fun withLsp( null -> { val result = response.result?.let { result -> runCatching { - LSP.json.decodeFromJsonElement( - client.requestType.resultSerializer as KSerializer, - result - ) + @Suppress("UNCHECKED_CAST") + LSP.json.decodeFromJsonElement( + client.requestType.resultSerializer as KSerializer, + result + ) }.onFailure { error -> currentCoroutineContext().job.ensureActive() LOG.error(error) @@ -226,10 +232,11 @@ suspend fun withLsp( cause = null, payload = error.data?.let { data -> runCatching { - LSP.json.decodeFromJsonElement( - client.requestType.errorSerializer as KSerializer, - data - ) + @Suppress("UNCHECKED_CAST") + LSP.json.decodeFromJsonElement( + client.requestType.errorSerializer as KSerializer, + data + ) }.onFailure { decodingError -> currentCoroutineContext().job.ensureActive() LOG.error(decodingError) @@ -255,15 +262,15 @@ suspend fun withLsp( else -> runCatching { - when (val originalHandler = handlers.notificationHandler(notification.method)) { + when (val handler = handlers.notificationHandler(notification.method)) { null -> LOG.debug("no handler for notification: ${notification.method}") else -> { - val handler = middleware.notificationHandler(originalHandler) val deserializedParams = notification.params?.let { params -> LSP.json.decodeFromJsonElement(handler.notificationType.paramsSerializer, params) } + @Suppress("UNCHECKED_CAST") (handler as LspNotificationHandler).handler(lspHandlerContext, this, deserializedParams) } }