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

Commit 2cf8dfc

Browse files
authored
Merge pull request #54 from ShaneDelmore/load_all_advice_from_resources
Load advice from any all resources and jars named "clippy.json"
2 parents 1a7f496 + 71a46ab commit 2cf8dfc

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ import scala.concurrent.{ExecutionContext, Future}
1010
import scala.io.Source
1111
import scala.tools.nsc.Global
1212
import scala.util.{Failure, Success, Try}
13-
import scala.collection.JavaConversions._
14-
15-
class AdviceLoader(global: Global, url: String, localStoreDir: File, projectAdviceFile: Option[File])(implicit ec: ExecutionContext) {
13+
import scala.collection.JavaConverters._
14+
15+
class AdviceLoader(
16+
global: Global,
17+
url: String,
18+
localStoreDir: File,
19+
projectAdviceFile: Option[File],
20+
localAdviceFiles: List[URL]
21+
)(implicit ec: ExecutionContext) {
1622
private val OneDayMillis = 1000L * 60 * 60 * 24
1723

1824
private val localStore = new File(localStoreDir, "clippy.json.gz")
1925

2026
private val resourcesAdvice: List[Advice] =
21-
getClass.getClassLoader
22-
.getResources("clippy.json")
23-
.toList
24-
.flatMap(loadAdviceFromUrL)
27+
localAdviceFiles.flatMap(loadAdviceFromUrL)
2528

2629
private def loadAdviceFromUrL(url: URL): List[Advice] =
2730
TryWith(url.openStream())(inputStreamToClippy(_).advices) match {

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.softwaremill.clippy
22

33
import java.io.File
4+
import java.net.URL
45
import java.util.concurrent.TimeoutException
56

7+
import scala.collection.JavaConverters._
68
import scala.concurrent.Await
79
import scala.concurrent.duration._
810
import scala.reflect.internal.util.Position
11+
import scala.reflect.internal.util.ScalaClassLoader
912
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
1116

1217
class 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

Comments
 (0)