【问题标题】:Generated Source with Macros in a Scala 2.13 and Mill project not found in Intellij在 Scala 2.13 和 Mill 项目中使用宏生成的源代码在 Intellij 中找不到
【发布时间】:2019-11-25 21:07:05
【问题描述】:

我在 Scala 2.13 / Mill 项目中使用 zio-macros

这里是例子:

@accessible
@mockable
trait AccountObserver {
  val accountObserver: AccountObserver.Service[Any]
}

object AccountObserver {
  trait Service[R] {
    def processEvent(event: String): ZIO[R, Nothing, Unit]
  }

  // autogenerated `object Service { ... }`
  // autogenerated `object > extends Service[AccountObserver] { ... }`
  // autogenerated `implicit val mockable: Mockable[AccountObserver] = ...`
}

我可以运行测试,它会找到自动生成的代码。

问题是在 Intellij 中对自动生成代码的引用编译。

我是否必须配置一些东西或缺少什么?

【问题讨论】:

    标签: scala intellij-idea scala-macros zio mill


    【解决方案1】:

    以下项目编译

    build.sc

    import mill._, scalalib._
    
    object root extends ScalaModule {
      def scalaVersion = "2.13.1"
      val zioMacrosV = "0.4.0"
      def ivyDeps = Agg(
        ivy"dev.zio::zio-macros-core:${zioMacrosV}",
        ivy"dev.zio::zio-macros-access:${zioMacrosV}",
        ivy"dev.zio::zio-macros-mock:${zioMacrosV}"
      )
      def scalacOptions = Seq(
        "-Ymacro-annotations",
        "-Ymacro-debug-lite"
      )
    }
    

    root/src/App.scala

    import zio.ZIO
    import zio.macros.access.accessible
    import zio.macros.mock.mockable
    
    @accessible
    @mockable
    trait AccountObserver {
      val accountObserver: AccountObserver.Service[Any]
    }
    
    object AccountObserver {
      trait Service[R] {
        def processEvent(event: String): ZIO[R, Nothing, Unit]
      }
    }
    

    命令mill root.compile编译项目

    //{
    //  abstract trait AccountObserver extends scala.AnyRef {
    //    val accountObserver: AccountObserver.Service[Any]
    //  };
    //  object AccountObserver extends scala.AnyRef {
    //    def <init>() = {
    //      super.<init>();
    //      ()
    //    };
    //    abstract trait Service[R] extends scala.AnyRef {
    //      def processEvent(event: String): ZIO[R, Nothing, Unit]
    //    };
    //    object Service extends scala.AnyRef {
    //      def <init>() = {
    //        super.<init>();
    //        ()
    //      };
    //      case object processEvent extends _root_.zio.test.mock.Method[String, Unit] with scala.Product with scala.Serializable {
    //        def <init>() = {
    //          super.<init>();
    //          ()
    //        }
    //      }
    //    };
    //    implicit val mockable: _root_.zio.test.mock.Mockable[AccountObserver] = ((mock: _root_.zio.test.mock.Mock) => {
    //      final class $anon extends AccountObserver {
    //        def <init>() = {
    //          super.<init>();
    //          ()
    //        };
    //        val accountObserver = {
    //          final class $anon extends Service[Any] {
    //            def <init>() = {
    //              super.<init>();
    //              ()
    //            };
    //            def processEvent(event: String): _root_.zio.IO[Nothing, Unit] = mock(Service.processEvent, event)
    //          };
    //          new $anon()
    //        }
    //      };
    //      new $anon()
    //    });
    //    object $greater extends Service[AccountObserver] {
    //      def <init>() = {
    //        super.<init>();
    //        ()
    //      };
    //      def processEvent(event: String): _root_.zio.IO[Nothing, Unit] = _root_.zio.ZIO.accessM(<empty> match {
    //        case (env @ (_: AccountObserver)) => env.accountObserver.processEvent(event)
    //      })
    //    }
    //  };
    //  ()
    //}
    

    创建 IntelliJ 文件

    mill mill.scalalib.GenIdea/idea
    

    然后在 IntelliJ 中打开项目(不要导入它)。

    现在 Ctrl+Shift+F9 编译项目(与上面类似)。

    您可以使用例如对对象AccountObserver.Service 的引用(此编译)。 IntelliJ 肯定会以红色突出显示 Service,但这并不重要。

    【讨论】:

    • “IntelliJ 肯定会以红色突出显示服务,但这并不重要。”。但这正是如果您在源代码中到处都有编译异常,很难编码的问题。
    • @pme 这不是编译异常,只是错误突出显示。
    • @pme 您应该习惯 IntelliJ 有时会错误地突出显示 Scala 代码。宏对于 IntelliJ 来说太棘手了。这不取决于您如何构建项目,使用 sbt 或 mill。
    • 但代码完成和代码导航也不起作用(与 VS Code 相同的问题)
    • 红色标记问题。我修复了项目生成器的那一部分。要么运行最新的 mill 快照,要么等待下一个 mill 版本 > 0.5.2。
    【解决方案2】:

    更新:这个答案没有解决问题。


    这是 Mill 0.5.2 及更早版本的 IntelliJ IDEA 项目生成器中的错误。自 0.5.2-9-ea4f04 以来的 Mill 版本将包含针对该特定问题的修复程序。 (供参考:#729#728

    解决方法:在您的项目中添加一个 .mill-version 文件,其中包含该版本号(当然也可以是更高版本),然后重新运行 IDEA 项目生成器。

    $ echo -n "0.5.2-9-ea4f04" > .mill-version
    $ mill mill.scalalib.GenIdea/idea
    

    【讨论】:

    • 此修复没有帮助。但我也尝试了一个 sbt 项目 - 同样的问题。这是编译但在 intellij 中显示错误的类:github.com/pme123/zio-examples/blob/master/macros/src/pme123/…
    • 我可以看到,它不是生成的 source 代码,所以我在回答中谈到的问题并不完全相同,因为没有 IntelliJ 的新文件IDEA需要考虑。
    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 2016-02-15
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多