将 scalac 插件拆分为多个文件
我想将我的 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")
}
}
}
}
}
如何将 Component
和 DivByZeroPhase
放入它们自己的文件中,而不将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个非常古老的项目,我做了同样的事情:
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.
在 Scala 重构库中,我通过使用 CompilerAccess 特征解决了这个问题:
现在需要访问
global
的所有其他特征只需将CompilerAccess
声明为依赖项:然后有一个混合所有这些特征并提供全局实例的类:
这个方案对我来说效果很好。
In the Scala Refactoring library, I solved it by having a trait CompilerAccess:
Now all the other traits that need to access
global
just declareCompilerAccess
as a dependency:and then there's a class that mixes in all these traits and provides an instance of global:
This scheme worked quite well for me.
您可以为您的组件创建一个单独的类并将全局传递给:
You can create a separate class for your component and pass global in: