@@ -13,40 +13,49 @@ class ClippyPlugin(val global: Global) extends Plugin {
1313
1414 override val name : String = " clippy"
1515
16- override val components : List [PluginComponent ] = Nil
17-
1816 override val description : String = " gives good advice"
1917
20- override def processOptions (options : List [String ], error : (String ) => Unit ) = {
21- val r = global.reporter
18+ var url : String = " "
19+ var enableColors = false
20+ var testMode = false
21+ val DefaultStoreDir = new File (System .getProperty(" user.home" ), " .clippy" )
22+ var localStoreDir = DefaultStoreDir
23+ var projectRoot : Option [File ] = None
2224
23- val url = urlFromOptions(options)
24- val enableColors = colorsFromOptions(options)
25- val localStoreDir = localStoreDirFromOptions(options)
26- val projectRoot = projectRootFromOptions(options)
25+ def handleError (pos : Position , msg : String ): String = {
2726 val advices = loadAdvices(url, localStoreDir, projectRoot)
27+ val parsedMsg = CompilationErrorParser .parse(msg)
28+ val matchers = advices.map(_.errMatching.lift)
29+ val matches = matchers.flatMap(pf => parsedMsg.flatMap(pf))
30+
31+ matches.size match {
32+ case 0 =>
33+ parsedMsg match {
34+ case Some (tme : TypeMismatchError [ExactT ]) if enableColors => prettyPrintTypeMismatchError(tme, msg)
35+ case _ => msg
36+ }
37+ case 1 =>
38+ matches.mkString(s " $msg\n Clippy advises: " , " " , " " )
39+ case _ =>
40+ matches.mkString(s " $msg\n Clippy advises you to try one of these solutions: \n " , " \n or\n " , " " )
41+ }
42+ }
43+
44+ override def processOptions (options : List [String ], error : (String ) => Unit ): Unit = {
45+ enableColors = colorsFromOptions(options)
46+ url = urlFromOptions(options)
47+ testMode = testModeFromOptions(options)
48+ localStoreDir = localStoreDirFromOptions(options)
49+ projectRoot = projectRootFromOptions(options)
2850
29- global.reporter = new DelegatingReporter (r, handleError)
30-
31- def handleError (pos : Position , msg : String ): String = {
32- val parsedMsg = CompilationErrorParser .parse(msg)
33- val matchers = advices.map(_.errMatching.lift)
34- val matches = matchers.flatMap(pf => parsedMsg.flatMap(pf))
35-
36- matches.size match {
37- case 0 =>
38- parsedMsg match {
39- case Some (tme : TypeMismatchError [ExactT ]) if enableColors => prettyPrintTypeMismatchError(tme, msg)
40- case _ => msg
41- }
42- case 1 =>
43- matches.mkString(s " $msg\n Clippy advises: " , " " , " " )
44- case _ =>
45- matches.mkString(s " $msg\n Clippy advises you to try one of these solutions: \n " , " \n or\n " , " " )
46- }
51+ if (testMode) {
52+ val r = global.reporter
53+ global.reporter = new DelegatingReporter (r, handleError)
4754 }
4855 }
4956
57+ override val components : List [PluginComponent ] = List (new InjectReporter (handleError, global))
58+
5059 private def prettyPrintTypeMismatchError (tme : TypeMismatchError [ExactT ], msg : String ): String = {
5160 val plain = new StringDiff (tme.required.toString, tme.found.toString)
5261 val expands = new StringDiff (tme.requiredExpandsTo.toString, tme.foundExpandsTo.toString)
@@ -64,8 +73,12 @@ class ClippyPlugin(val global: Global) extends Plugin {
6473 private def urlFromOptions (options : List [String ]): String =
6574 options.find(_.startsWith(" url=" )).map(_.substring(4 )).getOrElse(" https://www.scala-clippy.org" ) + " /api/advices"
6675
67- private def colorsFromOptions (options : List [String ]): Boolean =
68- options.find(_.startsWith(" colors=" )).map(_.substring(7 ))
76+ private def colorsFromOptions (options : List [String ]): Boolean = boolFromOptions(options, " colors" )
77+
78+ private def testModeFromOptions (options : List [String ]): Boolean = boolFromOptions(options, " testmode" )
79+
80+ private def boolFromOptions (options : List [String ], option : String ): Boolean =
81+ options.find(_.startsWith(s " $option= " )).map(_.substring(option.length + 1 ))
6982 .getOrElse(" false" )
7083 .toBoolean
7184
@@ -75,9 +88,7 @@ class ClippyPlugin(val global: Global) extends Plugin {
7588 .filter(_.exists())
7689
7790 private def localStoreDirFromOptions (options : List [String ]): File =
78- options.find(_.startsWith(" store=" )).map(_.substring(6 )).map(new File (_)).getOrElse {
79- new File (System .getProperty(" user.home" ), " .clippy" )
80- }
91+ options.find(_.startsWith(" store=" )).map(_.substring(6 )).map(new File (_)).getOrElse(DefaultStoreDir )
8192
8293 private def loadAdvices (url : String , localStoreDir : File , projectAdviceFile : Option [File ]): List [Advice ] = {
8394 implicit val ec = scala.concurrent.ExecutionContext .Implicits .global
0 commit comments