-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: Load exact version of the presentation compiler
- Loading branch information
Showing
7 changed files
with
236 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
scalafix-rules/src/main/scala-3/scalafix/internal/rule/Embedded.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package scalafix.internal.rule | ||
|
||
import java.net.URLClassLoader | ||
import java.nio.file.Path | ||
import java.util.ServiceLoader | ||
|
||
import scala.jdk.CollectionConverters.* | ||
|
||
import scala.meta.pc.PresentationCompiler | ||
|
||
import coursierapi.Dependency | ||
import coursierapi.Fetch | ||
import coursierapi.MavenRepository | ||
|
||
object Embedded { | ||
|
||
def presentationCompiler( | ||
scalaVersion: String | ||
): PresentationCompiler = { | ||
val deps = | ||
scala3PresentationCompilerDependencies(scalaVersion) | ||
val jars = Fetch | ||
.create() | ||
.addDependencies(deps*) | ||
.addRepositories( | ||
MavenRepository.of( | ||
"https://oss.sonatype.org/content/repositories/snapshots" | ||
) | ||
) | ||
.fetch() | ||
.asScala | ||
.map(_.toPath()) | ||
.toSeq | ||
val classloader = newPresentationCompilerClassLoader(jars) | ||
|
||
val presentationCompilerClassname = | ||
if (supportPresentationCompilerInDotty(scalaVersion)) { | ||
"dotty.tools.pc.ScalaPresentationCompiler" | ||
} else { | ||
"scala.meta.pc.ScalaPresentationCompiler" | ||
} | ||
|
||
serviceLoader( | ||
classOf[PresentationCompiler], | ||
presentationCompilerClassname, | ||
classloader | ||
) | ||
} | ||
|
||
private def supportPresentationCompilerInDotty(scalaVersion: String) = { | ||
scalaVersion.split("\\.").take(3).map(_.toInt) match { | ||
case Array(3, minor, patch) => minor > 3 || minor == 3 && patch >= 4 | ||
case _ => false | ||
} | ||
} | ||
|
||
private def scala3PresentationCompilerDependencies(version: String) = | ||
if (supportPresentationCompilerInDotty(version)) | ||
List( | ||
Dependency | ||
.of("org.scala-lang", "scala3-presentation-compiler_3", version) | ||
) | ||
else | ||
List( | ||
// TODO should use build info etc. instead of using 1.3.4 | ||
Dependency.of("org.scalameta", s"mtags_${version}", "1.3.4") | ||
) | ||
|
||
private def serviceLoader[T]( | ||
cls: Class[T], | ||
className: String, | ||
classloader: URLClassLoader | ||
): T = { | ||
val services = ServiceLoader.load(cls, classloader).iterator() | ||
if (services.hasNext) services.next() | ||
else { | ||
val cls = classloader.loadClass(className) | ||
val ctor = cls.getDeclaredConstructor() | ||
ctor.setAccessible(true) | ||
ctor.newInstance().asInstanceOf[T] | ||
} | ||
} | ||
|
||
private def newPresentationCompilerClassLoader( | ||
jars: Seq[Path] | ||
): URLClassLoader = { | ||
val allJars = jars.iterator | ||
val allURLs = allJars.map(_.toUri.toURL).toArray | ||
// Share classloader for a subset of types. | ||
val parent = | ||
new PresentationCompilerClassLoader(this.getClass.getClassLoader) | ||
new URLClassLoader(allURLs, parent) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
scalafix-rules/src/main/scala-3/scalafix/internal/rule/PresentationCompilerClassloader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package scalafix.internal.rule | ||
|
||
/** | ||
* ClassLoader that is used to reflectively invoke presentation compiler APIs. | ||
* | ||
* The presentation compiler APIs are compiled against exact Scala versions of | ||
* the compiler while Scalafix rule only runs in a single Scala version. In | ||
* order to communicate between Scalafix and the reflectively loaded compiler, | ||
* this classloader shares a subset of Java classes that appear in method | ||
* signatures of the `PresentationCompiler` class. | ||
*/ | ||
class PresentationCompilerClassLoader(parent: ClassLoader) | ||
extends ClassLoader(null) { | ||
override def findClass(name: String): Class[?] = { | ||
val isShared = | ||
name.startsWith("org.eclipse.lsp4j") || | ||
name.startsWith("com.google.gson") || | ||
name.startsWith("scala.meta.pc") || | ||
name.startsWith("javax") | ||
if (isShared) { | ||
parent.loadClass(name) | ||
} else { | ||
throw new ClassNotFoundException(name) | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
scalafix-rules/src/main/scala-3/scalafix/internal/rule/PresentationCompilerConfigImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package scalafix.internal.rule | ||
|
||
import java.util.Optional | ||
import java.util.concurrent.TimeUnit | ||
|
||
import scala.meta.pc.PresentationCompilerConfig | ||
import scala.meta.pc.PresentationCompilerConfig.OverrideDefFormat | ||
|
||
case class PresentationCompilerConfigImpl( | ||
debug: Boolean = false, | ||
parameterHintsCommand: Optional[String] = Optional.empty(), | ||
completionCommand: Optional[String] = Optional.empty(), | ||
symbolPrefixes: java.util.Map[String, String] = | ||
PresentationCompilerConfig.defaultSymbolPrefixes(), | ||
overrideDefFormat: OverrideDefFormat = OverrideDefFormat.Ascii, | ||
isCompletionItemDetailEnabled: Boolean = true, | ||
isCompletionItemDocumentationEnabled: Boolean = true, | ||
isHoverDocumentationEnabled: Boolean = true, | ||
snippetAutoIndent: Boolean = true, | ||
isSignatureHelpDocumentationEnabled: Boolean = true, | ||
isCompletionSnippetsEnabled: Boolean = true, | ||
isCompletionItemResolve: Boolean = true, | ||
isStripMarginOnTypeFormattingEnabled: Boolean = true, | ||
timeoutDelay: Long = 20, | ||
timeoutUnit: TimeUnit = TimeUnit.SECONDS, | ||
semanticdbCompilerOptions: java.util.List[String] = | ||
PresentationCompilerConfig.defaultSemanticdbCompilerOptions(), | ||
) extends PresentationCompilerConfig { | ||
|
||
override def isDefaultSymbolPrefixes(): Boolean = false | ||
} |
Oops, something went wrong.