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?
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.
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.
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) = {
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With