From e69b6f5dab8f4047f0507502513ec7fe6d1e8fde Mon Sep 17 00:00:00 2001 From: Krzysztof Ciesielski Date: Fri, 13 Jan 2017 11:27:06 +0100 Subject: [PATCH 1/2] Restore original reporter after compilation - In multi-module sbt projects the build system needs its original reporter back between subsequent module compilations. --- .../softwaremill/clippy/ClippyPlugin.scala | 2 +- .../softwaremill/clippy/RestoreReporter.scala | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 plugin/src/main/scala/com/softwaremill/clippy/RestoreReporter.scala diff --git a/plugin/src/main/scala/com/softwaremill/clippy/ClippyPlugin.scala b/plugin/src/main/scala/com/softwaremill/clippy/ClippyPlugin.scala index c0f4a6f..89347c8 100644 --- a/plugin/src/main/scala/com/softwaremill/clippy/ClippyPlugin.scala +++ b/plugin/src/main/scala/com/softwaremill/clippy/ClippyPlugin.scala @@ -54,7 +54,7 @@ class ClippyPlugin(val global: Global) extends Plugin { } } - override val components: List[PluginComponent] = List(new InjectReporter(handleError, global)) + override val components: List[PluginComponent] = List(new InjectReporter(handleError, global), new RestoreReporter(global)) private def prettyPrintTypeMismatchError(tme: TypeMismatchError[ExactT], msg: String): String = { val plain = new StringDiff(tme.required.toString, tme.found.toString) diff --git a/plugin/src/main/scala/com/softwaremill/clippy/RestoreReporter.scala b/plugin/src/main/scala/com/softwaremill/clippy/RestoreReporter.scala new file mode 100644 index 0000000..fa4c0e6 --- /dev/null +++ b/plugin/src/main/scala/com/softwaremill/clippy/RestoreReporter.scala @@ -0,0 +1,26 @@ +package com.softwaremill.clippy + +import scala.reflect.internal.util.Position +import scala.tools.nsc.plugins.PluginComponent +import scala.tools.nsc.{Global, Phase} + +class RestoreReporter(val global: Global) extends PluginComponent { + + val originalReporter = global.reporter + + override val runsAfter = List[String]("jvm") + override val runsBefore = List[String]("terminal") + override val phaseName = "restore-original-reporter" + + override def newPhase(prev: Phase) = new Phase(prev) { + + override def name = phaseName + + override def description = "Switches the reporter from Clippy's DelegatingReporter back to original one" + + override def run(): Unit = { + global.reporter = originalReporter + } + } + +} From 0eb9728dd8514b50f055ad27ba433f14caebc478 Mon Sep 17 00:00:00 2001 From: Krzysztof Ciesielski Date: Fri, 13 Jan 2017 15:57:34 +0100 Subject: [PATCH 2/2] Add some scaladoc --- .../main/scala/com/softwaremill/clippy/InjectReporter.scala | 3 +++ .../scala/com/softwaremill/clippy/RestoreReporter.scala | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/scala/com/softwaremill/clippy/InjectReporter.scala b/plugin/src/main/scala/com/softwaremill/clippy/InjectReporter.scala index 9ad7b20..87b792c 100644 --- a/plugin/src/main/scala/com/softwaremill/clippy/InjectReporter.scala +++ b/plugin/src/main/scala/com/softwaremill/clippy/InjectReporter.scala @@ -4,6 +4,9 @@ import scala.reflect.internal.util.Position import scala.tools.nsc.plugins.PluginComponent import scala.tools.nsc.{Global, Phase} +/** + * Responsible for replacing the global reporter with our custom Clippy reporter after the first phase of compilation. + */ class InjectReporter(handleError: (Position, String) => String, superGlobal: Global) extends PluginComponent { override val global = superGlobal diff --git a/plugin/src/main/scala/com/softwaremill/clippy/RestoreReporter.scala b/plugin/src/main/scala/com/softwaremill/clippy/RestoreReporter.scala index fa4c0e6..b139f88 100644 --- a/plugin/src/main/scala/com/softwaremill/clippy/RestoreReporter.scala +++ b/plugin/src/main/scala/com/softwaremill/clippy/RestoreReporter.scala @@ -1,9 +1,13 @@ package com.softwaremill.clippy -import scala.reflect.internal.util.Position import scala.tools.nsc.plugins.PluginComponent import scala.tools.nsc.{Global, Phase} +/** + * Replaces global reporter back with the original global reporter. Sbt uses its own xsbt.DelegatingReporter + * which we cannot replace outside of Scala compilation phases. This component makes sure that before the compilation + * is over, original reporter gets reassigned to the global field. + */ class RestoreReporter(val global: Global) extends PluginComponent { val originalReporter = global.reporter