All Downloads are FREE. Search and download functionalities are using the official Maven repository.

langoustine-lsp_sjs1_3.0.0.8.source-code.JSONRPC.scala Maven / Gradle / Ivy

There is a newer version: 0.0.22
Show newest version
package langoustine.lsp

import jsonrpclib.*
import scala.util.Try

import upickle.default.*

import util.chaining.*

private[lsp] object jsonrpcIntegration:
  given codec[T: Reader: Writer]: Codec[T] =
    new Codec[T]:
      override def decode(
          payload: Option[Payload]
      ): Either[ProtocolError, T] =
        payload
          .map(_.array)
          .flatMap { arr =>
            Try(read[T](arr, trace = true)).toOption
          }
          .toRight(ProtocolError.InvalidParams("oopsie daisy"))
      end decode

      override def encode(a: T): Payload =
        Payload(write(a).getBytes)

  def handlerToEndpoint[F[_]: Monadic, T <: requests.LSPRequest](req: T)(
      f: req.In => F[req.Out]
  ): Endpoint[F] =
    Endpoint(req.requestMethod).simple(f)

  def handlerToNotification[F[_]: Monadic, T <: requests.LSPNotification](
      req: T
  )(
      f: req.In => F[Unit]
  ): Endpoint[F] = Endpoint(req.notificationMethod).notification(f)
end jsonrpcIntegration




© 2015 - 2024 Weber Informatics LLC | Privacy Policy