Skip to content

Commit

Permalink
bugfix: MAke sure rules can be run on JDK 8
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Aug 9, 2024
1 parent 4a25c25 commit 385b1b1
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ sealed trait ScalaVersion {
case Major(major) => s"${major.value}"
case Minor(major, minorVersion) => s"${major.value}.${minorVersion}"
case Patch(major, minorVersion, patchVersion) =>
s"${major.value}.${minorVersion}.$patchVersion"
s"${major.value}.${minorVersion}.$patchVersion"
case RC(major, minorVersion, patchVersion, rc) =>
s"${major.value}.${minorVersion}.$patchVersion-RC$rc"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package scalafix.internal.rule

import scala.util.control.NonFatal

import scala.meta._
import scala.meta.internal.pc.ScalafixGlobal

import buildinfo.RulesBuildInfo
import metaconfig.Configured
import scalafix.internal.compat.CompilerCompat._
Expand All @@ -8,10 +13,6 @@ import scalafix.internal.v1.LazyValue
import scalafix.patch.Patch
import scalafix.v1._

import scala.meta._
import scala.meta.internal.pc.ScalafixGlobal
import scala.util.control.NonFatal

final class ExplicitResultTypes(
val config: ExplicitResultTypesConfig,
global: LazyValue[Option[ScalafixGlobal]],
Expand Down Expand Up @@ -105,32 +106,32 @@ final class ExplicitResultTypes(

}

class Scala2Printer(
globalPrinter: Option[CompilerTypePrinter],
fallback: LazyValue[Option[ExplicitResultTypesFallback]]
) extends Printer {
def defnType(
defn: Defn,
replace: Token,
space: String
)(implicit
ctx: SemanticDocument
): Option[Patch] = {

globalPrinter match {
case Some(types) =>
for {
name <- ExplicitResultTypesBase.defnName(defn)
defnSymbol <- name.symbol.asNonEmpty
patch <- types.toPatch(name.pos, defnSymbol, replace, defn, space)
} yield {
patch
}
case None =>
fallback.value.flatMap { fallbackExplicit =>
fallbackExplicit.defnType(replace)
}
}
class Scala2Printer(
globalPrinter: Option[CompilerTypePrinter],
fallback: LazyValue[Option[ExplicitResultTypesFallback]]
) extends Printer {
def defnType(
defn: Defn,
replace: Token,
space: String
)(implicit
ctx: SemanticDocument
): Option[Patch] = {

globalPrinter match {
case Some(types) =>
for {
name <- ExplicitResultTypesBase.defnName(defn)
defnSymbol <- name.symbol.asNonEmpty
patch <- types.toPatch(name.pos, defnSymbol, replace, defn, space)
} yield {
patch
}
case None =>
fallback.value.flatMap { fallbackExplicit =>
fallbackExplicit.defnType(replace)
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scalafix.internal.pc
import java.net.URI
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage

import scala.meta.pc.CancelToken
import scala.meta.pc.OffsetParams

Expand All @@ -21,4 +22,4 @@ case class CompilerOffsetParams(

}

}
}
32 changes: 22 additions & 10 deletions scalafix-rules/src/main/scala/scalafix/internal/pc/Embedded.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ object Embedded {
def presentationCompiler(
scalaVersion: String
): PresentationCompiler = {
val deps =
val deps =
scala3PresentationCompilerDependencies(scalaVersion)
val jars = Fetch
.create()
.addDependencies(deps:_*)
.addDependencies(deps: _*)
.addRepositories(
MavenRepository.of(
"https://oss.sonatype.org/content/repositories/snapshots"
Expand All @@ -48,19 +48,29 @@ object Embedded {
}

private def supportPresentationCompilerInDotty(scalaVersion: String) = {
scalaVersion.replaceAll(raw"-RC\d+", "").split("\\.").take(3).map(_.toInt) match {
scalaVersion
.replaceAll(raw"-RC\d+", "")
.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
if (supportPresentationCompilerInDotty(version)) {
val dep = Dependency
.of("org.scala-lang", "scala3-presentation-compiler_3", version)

// some versions of the presentation compiler depend on versions only build for JDK 11
dep.addExclusion("org.eclipse.lsp4j", "org.eclipse.lsp4j")
dep.addExclusion("org.eclipse.lsp4j", "org.eclipse.lsp4j.jsonrpc")
// last built with JDK 8
val lsp4jDep =
Dependency.of("org.eclipse.lsp4j", "org.eclipse.lsp4j", "0.20.1")
List(dep, lsp4jDep)
} else
List(
// TODO should use build info etc. instead of using 1.3.4
Dependency.of("org.scalameta", s"mtags_${version}", "1.3.4")
Expand Down Expand Up @@ -88,7 +98,9 @@ object Embedded {
val allURLs = allJars.map(_.toUri.toURL).toArray
// Share classloader for a subset of types.
val parent =
new scalafix.internal.pc.PresentationCompilerClassLoader(this.getClass.getClassLoader)
new scalafix.internal.pc.PresentationCompilerClassLoader(
this.getClass.getClassLoader
)
new URLClassLoader(allURLs, parent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import scalafix.patch.Patch.empty
import scalafix.v1._

/**
* Fallback tries to download and use existing presentation compiler either from
* Metals or the Scala compiler itself.
*
* @param pc
*/
* Fallback tries to download and use existing presentation compiler either from
* Metals or the Scala compiler itself.
*
* @param pc
*/
final class ExplicitResultTypesFallback private[ExplicitResultTypesFallback] (
pc: LazyValue[Option[PresentationCompiler]]
) {

def shutdownCompiler(): Unit = {
pc.value.foreach{
pc.value.foreach {
_.shutdown()
}
}
Expand Down Expand Up @@ -87,7 +87,7 @@ final class ExplicitResultTypesFallback private[ExplicitResultTypesFallback] (
}

object ExplicitResultTypesFallback {
def apply(config: Configuration) = {
def apply(config: Configuration): ExplicitResultTypesFallback = {
val symbolReplacements =
config.conf.dynamic.ExplicitResultTypes.symbolReplacements
.as[Map[String, String]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ case class PresentationCompilerConfigImpl(
timeoutDelay: Long = 20,
timeoutUnit: TimeUnit = TimeUnit.SECONDS,
semanticdbCompilerOptions: java.util.List[String] =
PresentationCompilerConfig.defaultSemanticdbCompilerOptions(),
PresentationCompilerConfig.defaultSemanticdbCompilerOptions()
) extends PresentationCompilerConfig {

override def isDefaultSymbolPrefixes(): Boolean = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package scalafix.internal.rule

import scala.meta._
import scala.meta.contrib._
import scalafix.v1._
import scala.meta.contrib.Extract
import scalafix.util.TokenOps
import scala.meta.contrib._

import scalafix.util.TokenOps
import scalafix.v1._

trait Printer {
def defnType(
Expand All @@ -18,7 +18,6 @@ trait Printer {

}


trait ExplicitResultTypesBase[P <: Printer] {

val config: ExplicitResultTypesConfig
Expand All @@ -33,7 +32,6 @@ trait ExplicitResultTypesBase[P <: Printer] {
case _ => false
}


protected def defnBody(defn: Defn): Option[Term] = Option(defn).collect {
case Defn.Val(_, _, _, term) => term
case Defn.Var(_, _, _, Some(term)) => term
Expand Down Expand Up @@ -162,12 +160,11 @@ trait ExplicitResultTypesBase[P <: Printer] {
}
}


object ExplicitResultTypesBase{
object ExplicitResultTypesBase {

def defnName(defn: Defn): Option[Name] = Option(defn).collect {
case Defn.Val(_, Pat.Var(name) :: Nil, _, _) => name
case Defn.Var(_, Pat.Var(name) :: Nil, _, _) => name
case Defn.Def(_, name, _, _, _, _) => name
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ case class SimpleDefinitions(kinds: Set[String]) {
case t: m.Term.Select => isSimpleRef(t.qual)
case _ => false
}

def isSimpleDefinition(body: m.Term): Boolean = {
val kind =
if (body.is[m.Lit]) Some("Lit")
Expand Down

0 comments on commit 385b1b1

Please sign in to comment.