Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit f322a4c

Browse files
authored
Merge pull request #33 from kciesielski/restore-reporter
Restore original reporter after compilation
2 parents 701ea95 + 0eb9728 commit f322a4c

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

plugin/src/main/scala/com/softwaremill/clippy/ClippyPlugin.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ClippyPlugin(val global: Global) extends Plugin {
5454
}
5555
}
5656

57-
override val components: List[PluginComponent] = List(new InjectReporter(handleError, global))
57+
override val components: List[PluginComponent] = List(new InjectReporter(handleError, global), new RestoreReporter(global))
5858

5959
private def prettyPrintTypeMismatchError(tme: TypeMismatchError[ExactT], msg: String): String = {
6060
val plain = new StringDiff(tme.required.toString, tme.found.toString)

plugin/src/main/scala/com/softwaremill/clippy/InjectReporter.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import scala.reflect.internal.util.Position
44
import scala.tools.nsc.plugins.PluginComponent
55
import scala.tools.nsc.{Global, Phase}
66

7+
/**
8+
* Responsible for replacing the global reporter with our custom Clippy reporter after the first phase of compilation.
9+
*/
710
class InjectReporter(handleError: (Position, String) => String, superGlobal: Global) extends PluginComponent {
811

912
override val global = superGlobal
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.softwaremill.clippy
2+
3+
import scala.tools.nsc.plugins.PluginComponent
4+
import scala.tools.nsc.{Global, Phase}
5+
6+
/**
7+
* Replaces global reporter back with the original global reporter. Sbt uses its own xsbt.DelegatingReporter
8+
* which we cannot replace outside of Scala compilation phases. This component makes sure that before the compilation
9+
* is over, original reporter gets reassigned to the global field.
10+
*/
11+
class RestoreReporter(val global: Global) extends PluginComponent {
12+
13+
val originalReporter = global.reporter
14+
15+
override val runsAfter = List[String]("jvm")
16+
override val runsBefore = List[String]("terminal")
17+
override val phaseName = "restore-original-reporter"
18+
19+
override def newPhase(prev: Phase) = new Phase(prev) {
20+
21+
override def name = phaseName
22+
23+
override def description = "Switches the reporter from Clippy's DelegatingReporter back to original one"
24+
25+
override def run(): Unit = {
26+
global.reporter = originalReporter
27+
}
28+
}
29+
30+
}

0 commit comments

Comments
 (0)