Permalink
Browse files

[scalac-plugin] Fix compatibility warning between 2.10 & 2.11

  • Loading branch information...
cchantep
cchantep committed Aug 10, 2014
1 parent e9c5c47 commit 193d3bf1118dcd617cdd6bc915a597b2c550c5a1
Showing with 48 additions and 21 deletions.
  1. +1 −1 project/Acolyte.scala
  2. +40 −15 project/ScalacPlugin.scala
  3. +7 −5 scalac-plugin/src/main/scala/acolyte/AcolytePlugin.scala
View
@@ -13,7 +13,7 @@ object Acolyte extends Build with Dependencies
version in ThisBuild := "1.0.23",
javaOptions in ThisBuild ++= Seq("-source", "1.6", "-target", "1.6"),
scalaVersion in ThisBuild := "2.10.4",
crossScalaVersions in ThisBuild := Seq("2.10.4", "2.11.0"),
crossScalaVersions in ThisBuild := Seq("2.10.4", "2.11.2"),
publishTo in ThisBuild := Some(Resolver.file("file",
new File(Path.userHome.absolutePath+"/.m2/repository"))),
pomExtra in ThisBuild := (
View
@@ -11,20 +11,45 @@ trait ScalacPlugin { deps: Dependencies ⇒
compile in Test <<= (compile in Test).dependsOn(
packageBin in Compile/* make sure plugin.jar is available */),
scalacOptions in Compile ++= Seq("-feature", "-deprecation"),
scalacOptions in Test <++= (version in ThisBuild).
zip(scalaVersion in ThisBuild).zip(baseDirectory in Compile).
zip(name in Compile) map { d =>
val (((v, sv), b), n) = d
val msv =
if (sv startsWith "2.10") "2.10"
else if (sv startsWith "2.11") "2.11"
else sv
val td = b / "target" / "scala-%s".format(msv)
val j = td / "%s_%s-%s.jar".format(n, msv, v)
Seq("-feature", "-deprecation", "-P:acolyte:debug",
"-Xplugin:%s".format(j.getAbsolutePath))
})
sourceGenerators in Compile <+=
scalaVersion zip (sourceManaged in Compile) map (d
{ val (ver, dir) = d; generateUtility(ver, dir) }),
scalacOptions in Test ++= {
val v = version.value
val sv = scalaVersion.value
val b = (baseDirectory in Compile).value
val n = (name in Compile).value
val msv =
if (sv startsWith "2.10") "2.10"
else if (sv startsWith "2.11") "2.11"
else sv
val td = b / "target" / s"scala-$msv"
val j = td / s"${n}_$msv-$v.jar"
Seq("-feature", "-deprecation", "-P:acolyte:debug",
s"-Xplugin:${j.getAbsolutePath}")
})
private def generateUtility(ver: String, managed: File): Seq[File] = {
val f = managed / "acolyte" / "CompilerUtility.scala"
val withSource =
if (ver startsWith "2.11") "withSource(f).withShift(shift)"
else "withSource(f, shift)"
IO.writer[Seq[File]](f, "", IO.defaultCharset, false) { w
w append {
s"""package acolyte
import scala.tools.nsc.Global
import scala.reflect.internal.util.BatchSourceFile
object CompilerUtility {
@inline def withSource(global: Global)(pos: global.Position, f: BatchSourceFile, shift: Int): global.Position = pos.$withSource
}"""
}
Seq(f)
}
}
}
@@ -4,6 +4,8 @@ import scala.tools.nsc.Global
import scala.tools.nsc.plugins.{ Plugin, PluginComponent }
import scala.tools.nsc.transform.Transform
import scala.reflect.internal.util.BatchSourceFile
class AcolytePlugin(val global: Global) extends Plugin {
val name = "acolyte"
val description = "Syntax extensions: Extractors with arguments"
@@ -20,6 +22,8 @@ class AcolytePlugin(val global: Global) extends Plugin {
override val optionsHelp: Option[String] = Some(
" -P:acolyte:debug Enable debug")
@inline private def withSource(pos: global.Position)(f: BatchSourceFile, shift: Int) = CompilerUtility.withSource(global)(pos, f, shift)
private object ExtractorComponent extends PluginComponent with Transform {
val global: AcolytePlugin.this.global.type = AcolytePlugin.this.global
override val runsRightAfter = Some("parser")
@@ -51,7 +55,6 @@ class AcolytePlugin(val global: Global) extends Plugin {
ValDef
}
import scala.reflect.io.VirtualFile
import scala.reflect.internal.util.BatchSourceFile
override def transform(tree: Tree): Tree = refactor(tree)
@@ -84,8 +87,8 @@ class AcolytePlugin(val global: Global) extends Plugin {
val nc = CaseDef(tx.transform(pat), g, refactor(by))
val cdc = s"${global show nc} // generated from ln ${ocp.line}, col ${ocp.column - 5}"
val cdp = ocp.withPoint(0).
withSource(new BatchSourceFile(file, cdc), 0)
val cdp = withSource(ocp.withPoint(0))(
new BatchSourceFile(file, cdc), 0)
global.atPos(cdp)(nc)
}
@@ -151,8 +154,7 @@ class AcolytePlugin(val global: Global) extends Plugin {
val vdc =
s"${show(vd)} // generated from ln ${xp.line}, col ${xp.column - 1}"
val vdp = xp.withPoint(0).withSource(new BatchSourceFile(file, vdc), 0)
// TODO: Scala 2.11 -> withSource(new BatchSourceFile(file, vdc))
val vdp = withSource(xp.withPoint(0))(new BatchSourceFile(file, vdc), 0)
(atPos(vdp)(vd), Apply(Ident(xn), ua))
}

0 comments on commit 193d3bf

Please sign in to comment.