11package com .softwaremill .clippy
22
33import java .io .File
4+ import java .net .URL
45import java .util .concurrent .TimeoutException
56
7+ import scala .collection .JavaConverters ._
68import scala .concurrent .Await
79import scala .concurrent .duration ._
810import scala .reflect .internal .util .Position
11+ import scala .reflect .internal .util .ScalaClassLoader
912import scala .tools .nsc .Global
10- import scala .tools .nsc .plugins .{Plugin , PluginComponent }
13+ import scala .tools .nsc .plugins .Plugin
14+ import scala .tools .nsc .plugins .PluginComponent
15+ import scala .tools .util .PathResolver
1116
1217class ClippyPlugin (val global : Global ) extends Plugin {
1318
@@ -22,11 +27,17 @@ class ClippyPlugin(val global: Global) extends Plugin {
2227 var localStoreDir = DefaultStoreDir
2328 var projectRoot : Option [File ] = None
2429
30+ lazy val localAdviceFiles = {
31+ val classPathURLs = new PathResolver (global.settings).resultAsURLs
32+ val classLoader = ScalaClassLoader .fromURLs(classPathURLs, getClass.getClassLoader)
33+ classLoader.getResources(" clippy.json" ).asScala.toList
34+ }
35+
2536 def handleError (pos : Position , msg : String ): String = {
26- val advices = loadAdvices(url, localStoreDir, projectRoot)
37+ val advices = loadAdvices(url, localStoreDir, projectRoot, localAdviceFiles )
2738 val parsedMsg = CompilationErrorParser .parse(msg)
2839 val matchers = advices.map(_.errMatching.lift)
29- val matches = matchers.flatMap(pf => parsedMsg.flatMap(pf))
40+ val matches = matchers.flatMap(pf => parsedMsg.flatMap(pf)).distinct
3041
3142 matches.size match {
3243 case 0 =>
@@ -146,13 +157,13 @@ class ClippyPlugin(val global: Global) extends Plugin {
146157 private def localStoreDirFromOptions (options : List [String ]): File =
147158 options.find(_.startsWith(" store=" )).map(_.substring(6 )).map(new File (_)).getOrElse(DefaultStoreDir )
148159
149- private def loadAdvices (url : String , localStoreDir : File , projectAdviceFile : Option [File ]): List [Advice ] = {
160+ private def loadAdvices (url : String , localStoreDir : File , projectAdviceFile : Option [File ], localAdviceFiles : List [ URL ] ): List [Advice ] = {
150161 implicit val ec = scala.concurrent.ExecutionContext .Implicits .global
151162
152163 try {
153164 Await
154165 .result(
155- new AdviceLoader (global, url, localStoreDir, projectAdviceFile).load(),
166+ new AdviceLoader (global, url, localStoreDir, projectAdviceFile, localAdviceFiles ).load(),
156167 10 .seconds
157168 )
158169 .advices
0 commit comments