将 scalac 插件拆分为多个文件

发布于 2024-10-31 16:24:55 字数 1403 浏览 4 评论 0原文

我想将我的 scalac 插件拆分为多个文件。这听起来很容易,但由于 import global._ 行产生的路径相关类型问题,我还没有成功实现它。

这是 Lex Spoon 的示例插件:

package localhost

import scala.tools.nsc
import nsc.Global
import nsc.Phase
import nsc.plugins.Plugin
import nsc.plugins.PluginComponent

class DivByZero(val global: Global) extends Plugin {
  import global._

  val name = "divbyzero"
  val description = "checks for division by zero"
  val components = List[PluginComponent](Component)

  private object Component extends PluginComponent {
    val global: DivByZero.this.global.type = DivByZero.this.global
    val runsAfter = "refchecks"
    // Using the Scala Compiler 2.8.x the runsAfter should be written as below
    // val runsAfter = List[String]("refchecks");
    val phaseName = DivByZero.this.name
    def newPhase(_prev: Phase) = new DivByZeroPhase(_prev)    

    class DivByZeroPhase(prev: Phase) extends StdPhase(prev) {
      override def name = DivByZero.this.name
      def apply(unit: CompilationUnit) {
        for ( tree @ Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0)))) <- unit.body;
             if rcvr.tpe <:< definitions.IntClass.tpe) 
          {
            unit.error(tree.pos, "definitely division by zero")
          }
      }
    }
  }
}

如何将 ComponentDivByZeroPhase 放入它们自己的文件中,而不将 import global._ 纳入范围内?

I'd like to split my scalac plugin into multiple files. This sounds easy but I haven't managed to pull it off due to path-dependent type issues stemming from the import global._ line.

Here's Lex Spoon's sample plugin:

package localhost

import scala.tools.nsc
import nsc.Global
import nsc.Phase
import nsc.plugins.Plugin
import nsc.plugins.PluginComponent

class DivByZero(val global: Global) extends Plugin {
  import global._

  val name = "divbyzero"
  val description = "checks for division by zero"
  val components = List[PluginComponent](Component)

  private object Component extends PluginComponent {
    val global: DivByZero.this.global.type = DivByZero.this.global
    val runsAfter = "refchecks"
    // Using the Scala Compiler 2.8.x the runsAfter should be written as below
    // val runsAfter = List[String]("refchecks");
    val phaseName = DivByZero.this.name
    def newPhase(_prev: Phase) = new DivByZeroPhase(_prev)    

    class DivByZeroPhase(prev: Phase) extends StdPhase(prev) {
      override def name = DivByZero.this.name
      def apply(unit: CompilationUnit) {
        for ( tree @ Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0)))) <- unit.body;
             if rcvr.tpe <:< definitions.IntClass.tpe) 
          {
            unit.error(tree.pos, "definitely division by zero")
          }
      }
    }
  }
}

How can I put Component and DivByZeroPhase in their own files without having the import global._ in scope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苯莒 2024-11-07 16:24:55

这是一个非常古老的项目,我做了同样的事情:

https://github.com/jsuereth/osgi-scalac-plugin/blob/master/src/main/scala/scala/osgi/compiler/OsgiPlugin.scala

如果您不需要从全局传递路径相关类型,则不必担心尝试保持其相关的“this.global”部分。

Here's a really old project where I've done the same thing:

https://github.com/jsuereth/osgi-scalac-plugin/blob/master/src/main/scala/scala/osgi/compiler/OsgiPlugin.scala

If you don't need to pass path-dependent types from the global, don't worry about trying to keep the "this.global" portions of it relevant.

池予 2024-11-07 16:24:55

在 Scala 重构库中,我通过使用 CompilerAccess 特征解决了这个问题:

trait CompilerAccess { 
  val global: tools.nsc.Global
}

现在需要访问 global 的所有其他特征只需将 CompilerAccess 声明为依赖项:

trait TreeTraverser {
  this: CompilerAccess =>
  import global._

  ...
}

然后有一个混合所有这些特征并提供全局实例的类:

trait SomeRefactoring extends TreeTraverser with OtherTrait with MoreTraits {
  val global = //wherever you get your global from
}

这个方案对我来说效果很好。

In the Scala Refactoring library, I solved it by having a trait CompilerAccess:

trait CompilerAccess { 
  val global: tools.nsc.Global
}

Now all the other traits that need to access global just declare CompilerAccess as a dependency:

trait TreeTraverser {
  this: CompilerAccess =>
  import global._

  ...
}

and then there's a class that mixes in all these traits and provides an instance of global:

trait SomeRefactoring extends TreeTraverser with OtherTrait with MoreTraits {
  val global = //wherever you get your global from
}

This scheme worked quite well for me.

万人眼中万个我 2024-11-07 16:24:55

您可以为您的组件创建一个单独的类并将全局传递给:

class TemplateComponent(val global: Global) extends PluginComponent {

  import global._

  val runsAfter = List[String]("refchecks")

  val phaseName = "plugintemplate"

  def newPhase(prev: Phase) = new StdPhase(prev) {

    override def name = phaseName

    def apply(unit:CompilationUnit) = {
    }    
  }
}

You can create a separate class for your component and pass global in:

class TemplateComponent(val global: Global) extends PluginComponent {

  import global._

  val runsAfter = List[String]("refchecks")

  val phaseName = "plugintemplate"

  def newPhase(prev: Phase) = new StdPhase(prev) {

    override def name = phaseName

    def apply(unit:CompilationUnit) = {
    }    
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文