1
1
package com .softwaremill .clippy
2
2
3
3
import java .io .File
4
+ import java .net .URL
4
5
import java .util .concurrent .TimeoutException
5
6
7
+ import scala .collection .JavaConverters ._
6
8
import scala .concurrent .Await
7
9
import scala .concurrent .duration ._
8
10
import scala .reflect .internal .util .Position
11
+ import scala .reflect .internal .util .ScalaClassLoader
9
12
import 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
11
16
12
17
class ClippyPlugin (val global : Global ) extends Plugin {
13
18
@@ -22,11 +27,17 @@ class ClippyPlugin(val global: Global) extends Plugin {
22
27
var localStoreDir = DefaultStoreDir
23
28
var projectRoot : Option [File ] = None
24
29
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
+
25
36
def handleError (pos : Position , msg : String ): String = {
26
- val advices = loadAdvices(url, localStoreDir, projectRoot)
37
+ val advices = loadAdvices(url, localStoreDir, projectRoot, localAdviceFiles )
27
38
val parsedMsg = CompilationErrorParser .parse(msg)
28
39
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
30
41
31
42
matches.size match {
32
43
case 0 =>
@@ -146,13 +157,13 @@ class ClippyPlugin(val global: Global) extends Plugin {
146
157
private def localStoreDirFromOptions (options : List [String ]): File =
147
158
options.find(_.startsWith(" store=" )).map(_.substring(6 )).map(new File (_)).getOrElse(DefaultStoreDir )
148
159
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 ] = {
150
161
implicit val ec = scala.concurrent.ExecutionContext .Implicits .global
151
162
152
163
try {
153
164
Await
154
165
.result(
155
- new AdviceLoader (global, url, localStoreDir, projectAdviceFile).load(),
166
+ new AdviceLoader (global, url, localStoreDir, projectAdviceFile, localAdviceFiles ).load(),
156
167
10 .seconds
157
168
)
158
169
.advices
0 commit comments