From 078fa1ab372c0d1aee18099ee04064ef01941e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 3 Oct 2023 01:22:35 +0200 Subject: [PATCH] Scala 3 prep: drop cats-tagless-macros (#341) --- build.sbt | 2 +- .../scala/playground/lsp/LanguageClient.scala | 32 +++++++-- .../scala/playground/lsp/LanguageServer.scala | 72 +++++++++++++++++-- .../playground/plugins/PlaygroundPlugin.scala | 2 +- 4 files changed, 92 insertions(+), 16 deletions(-) diff --git a/build.sbt b/build.sbt index 9d362cf7..cea3e66b 100644 --- a/build.sbt +++ b/build.sbt @@ -157,7 +157,7 @@ lazy val lsp = module("lsp") "org.http4s" %% "http4s-ember-client" % "0.23.23", "org.http4s" %% "http4s-ember-server" % "0.23.23" % Test, "io.get-coursier" %% "coursier" % "2.1.7", - "org.typelevel" %% "cats-tagless-macros" % "0.15.0", + "org.typelevel" %% "cats-tagless-core" % "0.15.0", ), buildInfoPackage := "playground.lsp.buildinfo", buildInfoKeys ++= Seq(version, scalaBinaryVersion), diff --git a/modules/lsp/src/main/scala/playground/lsp/LanguageClient.scala b/modules/lsp/src/main/scala/playground/lsp/LanguageClient.scala index f22f9811..06a86299 100644 --- a/modules/lsp/src/main/scala/playground/lsp/LanguageClient.scala +++ b/modules/lsp/src/main/scala/playground/lsp/LanguageClient.scala @@ -3,16 +3,12 @@ package playground.lsp import cats.FlatMap import cats.effect.kernel.Async import cats.implicits._ -import cats.tagless.Derive -import cats.tagless.FunctorK -import cats.tagless.implicits._ import com.google.gson.JsonElement import org.eclipse.lsp4j.ConfigurationItem import org.eclipse.lsp4j.ConfigurationParams import org.eclipse.lsp4j.MessageParams import org.eclipse.lsp4j.MessageType import playground.language.Feedback -import playground.lsp.util.KleisliOps import java.util.concurrent.CompletableFuture import scala.jdk.CollectionConverters._ @@ -52,8 +48,6 @@ object LanguageClient { implicit F: LanguageClient[F] ): LanguageClient[F] = F - implicit val functorK: FunctorK[LanguageClient] = Derive.functorK - def adapt[F[_]: Async]( client: PlaygroundLanguageClient ): LanguageClient[F] = @@ -107,9 +101,33 @@ object LanguageClient { def refreshDiagnostics: F[Unit] = withClientF(_.refreshDiagnostics()).void } + // courtesy of github copilot + // workaround for https://github.com/typelevel/cats-tagless/pull/401 def defer[F[_]: FlatMap]( fa: F[LanguageClient[F]] - ): LanguageClient[F] = Derive.readerT[LanguageClient, F].mapK(KleisliOps.applyEffectK(fa)) + ): LanguageClient[F] = + new LanguageClient[F] { + + override def configuration[A]( + v: ConfigurationValue[A] + ): F[A] = fa.flatMap(_.configuration(v)) + + override def showMessage( + tpe: MessageType, + msg: String, + ): F[Unit] = fa.flatMap(_.showMessage(tpe, msg)) + + override def refreshDiagnostics: F[Unit] = fa.flatMap(_.refreshDiagnostics) + + override def refreshCodeLenses: F[Unit] = fa.flatMap(_.refreshCodeLenses) + + override def showOutputPanel: F[Unit] = fa.flatMap(_.showOutputPanel) + + override def logOutput( + msg: String + ): F[Unit] = fa.flatMap(_.logOutput(msg)) + + } val NoChangeDetected: String = "No change detected, not rebuilding server" } diff --git a/modules/lsp/src/main/scala/playground/lsp/LanguageServer.scala b/modules/lsp/src/main/scala/playground/lsp/LanguageServer.scala index add0d3bc..4a5b7d37 100644 --- a/modules/lsp/src/main/scala/playground/lsp/LanguageServer.scala +++ b/modules/lsp/src/main/scala/playground/lsp/LanguageServer.scala @@ -7,9 +7,6 @@ import cats.effect.implicits._ import cats.effect.kernel.Async import cats.implicits._ import cats.parse.LocationMap -import cats.tagless.Derive -import cats.tagless.FunctorK -import cats.tagless.implicits._ import cats.~> import com.google.gson.JsonElement import com.google.gson.JsonPrimitive @@ -35,7 +32,6 @@ import playground.language.FormattingProvider import playground.language.TextDocumentProvider import playground.language.Uri import playground.lsp.buildinfo.BuildInfo -import playground.lsp.util.KleisliOps import playground.types._ import smithy4s.dynamic.DynamicSchemaIndex @@ -108,8 +104,6 @@ trait LanguageServer[F[_]] { object LanguageServer { - implicit val functorK: FunctorK[LanguageServer] = Derive.functorK - def notAvailable[F[_]: MonadThrow]: LanguageServer[F] = defer( new Throwable("Server not available").raiseError[F, LanguageServer[F]] ) @@ -358,8 +352,72 @@ object LanguageServer { def exit: F[Unit] = Applicative[F].unit } + // courtesy of github copilot + // workaround for https://github.com/typelevel/cats-tagless/pull/401 def defer[F[_]: FlatMap]( fa: F[LanguageServer[F]] - ): LanguageServer[F] = Derive.readerT[LanguageServer, F].mapK(KleisliOps.applyEffectK(fa)) + ): LanguageServer[F] = + new LanguageServer[F] { + + override def initialize( + params: InitializeParams + ): F[InitializeResult] = fa.flatMap(_.initialize(params)); + + override def initialized( + params: InitializedParams + ): F[Unit] = fa.flatMap(_.initialized(params)); + + override def didChange( + params: DidChangeTextDocumentParams + ): F[Unit] = fa.flatMap(_.didChange(params)); + + override def didOpen( + params: DidOpenTextDocumentParams + ): F[Unit] = fa.flatMap(_.didOpen(params)); + + override def didSave( + params: DidSaveTextDocumentParams + ): F[Unit] = fa.flatMap(_.didSave(params)); + + override def didClose( + params: DidCloseTextDocumentParams + ): F[Unit] = fa.flatMap(_.didClose(params)); + + override def formatting( + params: DocumentFormattingParams + ): F[List[TextEdit]] = fa.flatMap(_.formatting(params)); + + override def completion( + position: CompletionParams + ): F[Either[List[CompletionItem], CompletionList]] = fa.flatMap(_.completion(position)); + + override def diagnostic( + params: DocumentDiagnosticParams + ): F[DocumentDiagnosticReport] = fa.flatMap(_.diagnostic(params)); + + override def codeLens( + params: CodeLensParams + ): F[List[CodeLens]] = fa.flatMap(_.codeLens(params)); + + override def documentSymbol( + params: DocumentSymbolParams + ): F[List[DocumentSymbol]] = fa.flatMap(_.documentSymbol(params)); + + override def didChangeWatchedFiles( + params: DidChangeWatchedFilesParams + ): F[Unit] = fa.flatMap(_.didChangeWatchedFiles(params)); + + override def executeCommand( + params: ExecuteCommandParams + ): F[Unit] = fa.flatMap(_.executeCommand(params)); + + override def runFile( + params: RunFileParams + ): F[Unit] = fa.flatMap(_.runFile(params)); + + override def shutdown: F[Unit] = fa.flatMap(_.shutdown); + + override def exit: F[Unit] = fa.flatMap(_.exit) + } } diff --git a/modules/plugin-core/src/main/scala/playground/plugins/PlaygroundPlugin.scala b/modules/plugin-core/src/main/scala/playground/plugins/PlaygroundPlugin.scala index 03541224..9fa54bc2 100644 --- a/modules/plugin-core/src/main/scala/playground/plugins/PlaygroundPlugin.scala +++ b/modules/plugin-core/src/main/scala/playground/plugins/PlaygroundPlugin.scala @@ -51,7 +51,7 @@ object SimpleHttpBuilder { service: Service[Alg], backend: Client[F], ): Either[UnsupportedProtocolError, FunctorAlgebra[Alg, F]] = - builder(service).client(backend).use + builder(service).client(backend).make }