宏注释在 IntelliJ14 中不起作用

Macros annotation not working in IntelliJ14

我在使用 Scala 宏时遇到问题。它一直告诉我

enable macro paradise to expand macro annotations

来自我写的 @compileTimeOnly 消息。我遵循了 Macro annotation documentation and the SBT example 的所有说明。

IDE:IntelliJ 14.1

Scala 版本:2.11.7

Build.scala项目文件夹下:

import sbt._
import sbt.Keys._

object Build extends Build {
  val paradiseVersion = "2.1.0-M5"
  lazy val sm = Project(id = "server-modules", base = file(".")).settings(
    version := "1.0",
    logLevel := Level.Warn,
    scalacOptions ++= Seq(),
    scalaVersion := "2.11.7",
    crossScalaVersions := Seq("2.10.2", "2.10.3", "2.10.4", "2.10.5", "2.11.0", "2.11.1", "2.11.2", "2.11.3", "2.11.4", "2.11.5", "2.11.6", "2.11.7"),
    resolvers += Resolver.sonatypeRepo("snapshots"),
    resolvers += Resolver.sonatypeRepo("releases"),
    addCompilerPlugin("org.scalamacros" % "paradise" % paradiseVersion cross CrossVersion.full),
    libraryDependencies <+= (scalaVersion)("org.scala-lang" % "scala-reflect" % _),
  )
}

代码:

@compileTimeOnly("enable macro paradise to expand macro annotations")
class dmCompile extends StaticAnnotation{
  def macroTransform(annottees: Any*): Any = macro DMCompile.impl
}

object DMCompile {
  def impl(c: whitebox.Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    import c.universe._

    Log.info("Work work work!")

    c.Expr(q"""var x = y""")
  }
}

@dmCompile class Test{}

我到底错过了什么?

这花了我一整天的时间,但我让它工作了。

只需忽略宏天堂的SBT设置并手动将其添加到Preference -> Scala Compiler

就是这样!

对我来说,一个解决方案是在设置中将增量类型从 IDEA 更改为 SBT。这允许使用原生 SBT 的构建引擎而不是 IDEA 的构建引擎。