Skip to content

Commit

Permalink
Scala 3 prep: drop cats-tagless-macros (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz authored Oct 2, 2023
1 parent ef8880e commit 078fa1a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
32 changes: 25 additions & 7 deletions modules/lsp/src/main/scala/playground/lsp/LanguageClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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] =
Expand Down Expand Up @@ -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"
}
72 changes: 65 additions & 7 deletions modules/lsp/src/main/scala/playground/lsp/LanguageServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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]]
)
Expand Down Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

Expand Down

0 comments on commit 078fa1a

Please sign in to comment.