diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 0bab53ff..05d97e42 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -1,3 +1,6 @@ # Scala Steward: Reformat with scalafmt 3.1.2 fb6cfb8aea15a1b339e3ed69e1e96acd7df4cae6 8e25745c77e488b3ff99d6a459d42916a0af1834 + +# Scala Steward: Reformat with scalafmt 3.7.10 +f281e3260dc8f2d207b24cdd62cfaae941badf2d diff --git a/.scalafmt.conf b/.scalafmt.conf index b3d59fe7..3acda551 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,3 +1,3 @@ runner.dialect = "scala213" -version = "3.7.5" +version = "3.7.10" maxColumn = 80 diff --git a/domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala b/domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala index 7c3c6761..0e45a623 100644 --- a/domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala +++ b/domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala @@ -14,16 +14,18 @@ import scala.language.implicitConversions import scala.util.control.Exception.Catcher import scala.util.control.NonFatal -/** Contains interpreters to enable [[Dsl.Keyword#unary_$bang !-notation]] - * for [[keywords.Monadic]] and other keywords - * in code blocks whose type support [[cats.FlatMap]] and [[cats.MonadError]]. - * - * @example [[cats.free.Trampoline Trampoline]] is a monadic data type that performs tail call optimization. - * It can be built from a `@[[Dsl.reset reset]]` code block within some [[Dsl.Keyword#unary_$bang !-notation]], - * similar to the [[com.thoughtworks.each.Monadic.EachOps#each each]] method in - * [[https://github.com/ThoughtWorksInc/each ThoughtWorks Each]]. - * - * {{{ +/** Contains interpreters to enable [[Dsl.Keyword#unary_$bang !-notation]] for + * [[keywords.Monadic]] and other keywords in code blocks whose type support + * [[cats.FlatMap]] and [[cats.MonadError]]. + * + * @example + * [[cats.free.Trampoline Trampoline]] is a monadic data type that performs + * tail call optimization. It can be built from a `@[[Dsl.reset reset]]` code + * block within some [[Dsl.Keyword#unary_$bang !-notation]], similar to the + * [[com.thoughtworks.each.Monadic.EachOps#each each]] method in + * [[https://github.com/ThoughtWorksInc/each ThoughtWorks Each]]. + * + * {{{ * import _root_.cats.free.Trampoline * import _root_.cats.instances.function._ * import com.thoughtworks.dsl.keywords.Monadic._ @@ -37,13 +39,13 @@ import scala.util.control.NonFatal * }: @reset * * dslSquare.run should be("This string is produced by a trampoline: 9") - * }}} + * }}} * - * `!trampoline3` is a shortcut of `!Monadic(trampoline3)`, - * which will be converted to `flatMap` calls by our DSL interpreter. - * Thus, the method `dslSquare` is equivalent to the following code in [[cats.syntax]]: + * `!trampoline3` is a shortcut of `!Monadic(trampoline3)`, which will be + * converted to `flatMap` calls by our DSL interpreter. Thus, the method + * `dslSquare` is equivalent to the following code in [[cats.syntax]]: * - * {{{ + * {{{ * * def catsSyntaxSquare = trampoline3.flatMap { tmp1 => * trampoline3.flatMap { tmp2 => @@ -54,15 +56,17 @@ import scala.util.control.NonFatal * } * * catsSyntaxSquare.run should be("This string is produced by a trampoline: 9") - * }}} - * @example A `@[[Dsl.reset reset]]` code block can contain `try` / `catch` / `finally` - * if the monadic data type supports [[cats.MonadError]]. + * }}} + * @example + * A `@[[Dsl.reset reset]]` code block can contain `try` / `catch` / + * `finally` if the monadic data type supports [[cats.MonadError]]. * - * For example, [[cats.effect.IO]] is a monadic data type that supports [[cats.MonadError]], - * therefore `try` / `catch` / `finally` expressions can be used inside a `@[[Dsl.reset reset]]` code block - * whose return type is [[cats.effect.IO]]. + * For example, [[cats.effect.IO]] is a monadic data type that supports + * [[cats.MonadError]], therefore `try` / `catch` / `finally` expressions can + * be used inside a `@[[Dsl.reset reset]]` code block whose return type is + * [[cats.effect.IO]]. * - * {{{ + * {{{ * import com.thoughtworks.dsl.keywords.Monadic._ * import com.thoughtworks.dsl.domains.cats._ * import _root_.cats.effect.IO @@ -80,11 +84,12 @@ import scala.util.control.NonFatal * }: @reset * * dslTryCatch.unsafeRunSync() should be("Cannot divide 0 by itself") - * }}} + * }}} * - * The above `dslTryCatch` method is equivalent to the following code in [[cats.syntax]]: + * The above `dslTryCatch` method is equivalent to the following code in + * [[cats.syntax]]: * - * {{{ + * {{{ * def catsSyntaxTryCatch: IO[String] = { * import _root_.cats.syntax.applicativeError._ * io0.flatMap { tmp0 => @@ -102,19 +107,23 @@ import scala.util.control.NonFatal * } * * catsSyntaxTryCatch.unsafeRunSync() should be("Cannot divide 0 by itself") - * }}} - * @author 杨博 (Yang Bo) + * }}} + * @author + * 杨博 (Yang Bo) */ object cats { protected type MonadThrowable[F[_]] = MonadError[F, Throwable] - implicit def catsReturnDsl[F[_], A, B](implicit applicative: Applicative[F], - restReturnDsl: Dsl[Return[A], B, Nothing]): Dsl[Return[A], F[B], Nothing] = { + implicit def catsReturnDsl[F[_], A, B](implicit + applicative: Applicative[F], + restReturnDsl: Dsl[Return[A], B, Nothing] + ): Dsl[Return[A], F[B], Nothing] = { (keyword: Return[A], handler: Nothing => F[B]) => applicative.pure(restReturnDsl.cpsApply(keyword, identity)) } - @inline private def catchNativeException[F[_], A](continuation: F[A] !! A)( - implicit monadThrowable: MonadThrowable[F]): F[A] = { + @inline private def catchNativeException[F[_], A]( + continuation: F[A] !! A + )(implicit monadThrowable: MonadThrowable[F]): F[A] = { try { continuation(monadThrowable.pure(_)) } catch { @@ -123,27 +132,41 @@ object cats { } } - implicit def catsTryFinally[F[_], A, B]( - implicit monadError: MonadThrowable[F]): TryFinally[A, F[B], F[A], F[Unit]] = { - (block: F[A] !! A, finalizer: F[Unit] !! Unit, outerSuccessHandler: A => F[B]) => + implicit def catsTryFinally[F[_], A, B](implicit + monadError: MonadThrowable[F] + ): TryFinally[A, F[B], F[A], F[Unit]] = { + ( + block: F[A] !! A, + finalizer: F[Unit] !! Unit, + outerSuccessHandler: A => F[B] + ) => @inline def injectFinalizer[A](f: Unit => F[A]): F[A] = { monadError.flatMap(catchNativeException(finalizer))(f) } - monadError.flatMap(monadError.handleErrorWith(catchNativeException(block)) { e: Throwable => - injectFinalizer { _: Unit => - monadError.raiseError(e) + monadError.flatMap( + monadError.handleErrorWith(catchNativeException(block)) { + e: Throwable => + injectFinalizer { _: Unit => + monadError.raiseError(e) + } } - }) { a => + ) { a => injectFinalizer { _: Unit => outerSuccessHandler(a) } } } - implicit def catsTryCatch[F[_], A, B](implicit monadError: MonadThrowable[F]): TryCatch[A, F[B], F[A]] = { - (block: F[A] !! A, catcher: Catcher[F[A] !! A], outerSuccessHandler: A => F[B]) => + implicit def catsTryCatch[F[_], A, B](implicit + monadError: MonadThrowable[F] + ): TryCatch[A, F[B], F[A]] = { + ( + block: F[A] !! A, + catcher: Catcher[F[A] !! A], + outerSuccessHandler: A => F[B] + ) => def errorHandler(e: Throwable): F[A] = { (try { catcher.lift(e) @@ -157,10 +180,14 @@ object cats { catchNativeException(recovered) } } - monadError.flatMap(monadError.handleErrorWith(catchNativeException(block))(errorHandler))(outerSuccessHandler) + monadError.flatMap( + monadError.handleErrorWith(catchNativeException(block))(errorHandler) + )(outerSuccessHandler) } - private[dsl] def catsCatchDsl[F[_], A, B](implicit monadError: MonadThrowable[F]): CatchDsl[F[A], F[B], A] = { + private[dsl] def catsCatchDsl[F[_], A, B](implicit + monadError: MonadThrowable[F] + ): CatchDsl[F[A], F[B], A] = { (block: F[A] !! A, catcher: Catcher[F[A] !! A], handler: A => F[B]) => val fa = monadError.flatMap(monadError.pure(block))(_(monadError.pure)) val protectedFa = monadError.handleErrorWith(fa) { @@ -172,7 +199,9 @@ object cats { monadError.flatMap(protectedFa)(handler) } - implicit def catsMonadicDsl[F[_], A, B](implicit flatMap: FlatMap[F]): Dsl[Monadic[F, A], F[B], A] = { + implicit def catsMonadicDsl[F[_], A, B](implicit + flatMap: FlatMap[F] + ): Dsl[Monadic[F, A], F[B], A] = { (keyword: Monadic[F, A], handler: A => F[B]) => flatMap.flatMap(keyword.fa)(handler) } diff --git a/domains-cats/src/test/scala/com/thoughtworks/dsl/domains/catsSpec.scala b/domains-cats/src/test/scala/com/thoughtworks/dsl/domains/catsSpec.scala index 59c2c976..12cdfd1d 100644 --- a/domains-cats/src/test/scala/com/thoughtworks/dsl/domains/catsSpec.scala +++ b/domains-cats/src/test/scala/com/thoughtworks/dsl/domains/catsSpec.scala @@ -6,8 +6,8 @@ import com.thoughtworks.dsl.domains.cats._ import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers -/** - * @author 杨博 (Yang Bo) +/** @author + * 杨博 (Yang Bo) */ class catsSpec extends AnyFreeSpec with Matchers { @@ -34,23 +34,56 @@ class catsSpec extends AnyFreeSpec with Matchers { generator should be( Seq( /**/ "Entering generator", - /****/ "Fork thread 0", - /******/ "Entering asyncFunction", - /********/ "Fork sub-thread 0", - /**********/ "Leaving asyncFunction", - /**********/ "Leaving generator", - /********/ "Fork sub-thread 1", - /**********/ "Leaving asyncFunction", - /**********/ "Leaving generator", - /****/ "Fork thread 1", - /******/ "Entering asyncFunction", - /********/ "Fork sub-thread 0", - /**********/ "Leaving asyncFunction", - /**********/ "Leaving generator", - /********/ "Fork sub-thread 1", - /**********/ "Leaving asyncFunction", - /**********/ "Leaving generator" - )) + /** * + */ + "Fork thread 0", + /** *** + */ + "Entering asyncFunction", + /** ***** + */ + "Fork sub-thread 0", + /** ******* + */ + "Leaving asyncFunction", + /** ******* + */ + "Leaving generator", + /** ***** + */ + "Fork sub-thread 1", + /** ******* + */ + "Leaving asyncFunction", + /** ******* + */ + "Leaving generator", + /** * + */ + "Fork thread 1", + /** *** + */ + "Entering asyncFunction", + /** ***** + */ + "Fork sub-thread 0", + /** ******* + */ + "Leaving asyncFunction", + /** ******* + */ + "Leaving generator", + /** ***** + */ + "Fork sub-thread 1", + /** ******* + */ + "Leaving asyncFunction", + /** ******* + */ + "Leaving generator" + ) + ) } }