Skip to content

Instantly share code, notes, and snippets.

@wangzaixiang
Forked from lihaoyi/Plugin.scala
Created December 4, 2016 23:59
Show Gist options
  • Save wangzaixiang/ca96eefd6be1471fb7074fd7ed54eccd to your computer and use it in GitHub Desktop.
Save wangzaixiang/ca96eefd6be1471fb7074fd7ed54eccd to your computer and use it in GitHub Desktop.
package demo
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.{Global, Phase}
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
class DemoPlugin(val global: Global) extends Plugin {
import global._
override def init(options: List[String], error: String => Unit): Boolean = true
val name = "demo"
val description = "a plugin"
val components = List[PluginComponent](DemoComponent)
private object DemoComponent extends PluginComponent {
val global = DemoPlugin.this.global
import global._
override val runsAfter = List("parser")
override val runsBefore = List("namer")
val phaseName = "Demo"
override def newPhase(prev: Phase) = new GlobalPhase(prev) {
override def run() = {
val count = global.currentRun.units.length
val x: Iterator[Seq[String]] = global.currentRun.units.map(_.source.file.path.split("/"))
var start = x.next()
for(next <- x){
for(i <- next.indices){
if (i < start.length && start.lift(i) != next.lift(i)){
start = start.take(i)
}
}
}
val cwd = new java.io.File("").getAbsolutePath.split("/")
if (start.startsWith(cwd)) start = start.drop(cwd.length)
val msg = s"Compiling $count files in ${start.mkString("/")}"
val spaces = " " * ((90 - msg.length) / 2)
global.reporter.echo(
scala.Console.BLUE_B +
spaces + msg + spaces +
scala.Console.RESET
)
}
def name: String = phaseName
def apply(unit: global.CompilationUnit): Unit = {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment