Skip to content

Commit

Permalink
Use AnalysisCallback2 to avoid dropping CodeActions
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed Aug 2, 2023
1 parent 039f658 commit 35a23d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ val jnaDep = "net.java.dev.jna" % "jna"
val jlineDeps = Seq(jlineDep, jnaDep)
val testInterfaceDep = "org.scala-sbt" % "test-interface" % "1.0"
val diffUtilsDep = "io.github.java-diff-utils" % "java-diff-utils" % "4.12"
val compilerInterfaceDep = "org.scala-sbt" % "compiler-interface" % "1.9.2"
val compilerInterfaceDep = "org.scala-sbt" % "compiler-interface" % "1.9.3"

val projectFolder = settingKey[String]("subfolder in src when using configureAsSubproject, else the project name")

Expand Down
31 changes: 12 additions & 19 deletions src/sbt-bridge/scala/tools/xsbt/CompilerBridge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
package scala.tools
package xsbt

import xsbti.{ AnalysisCallback, Logger, Problem, Reporter, VirtualFile }
import xsbti.{AnalysisCallback, AnalysisCallback2, Logger, Problem, Reporter, VirtualFile}
import xsbti.compile._

import scala.tools.nsc.Settings
import scala.collection.mutable
import scala.reflect.io.AbstractFile
import scala.tools.nsc.CompilerCommand
import Log.debug
import java.io.File
import java.util.{Collections, Optional}

/**
* This is the entry point for the compiler bridge (implementation of CompilerInterface)
Expand Down Expand Up @@ -154,6 +156,14 @@ private final class CachedCompiler0(
compileProgress: CompileProgress
): Unit = {

lazy val callback2 =
try callback.asInstanceOf[AnalysisCallback2]
catch { case _: NoClassDefFoundError => null}

def callbackProblem(p: Problem) =
if (callback2 != null) callback2.problem2(p.category, p.position, p.message, p.severity, true, p.rendered, p.diagnosticCode, p.diagnosticRelatedInformation, p.actions)
else callback.problem(p.category, p.position, p.message, p.severity, true)

if (command.shouldStopWithInfo) {
underlyingReporter.info(null, command.getInfoMessage(compiler), true)
throw new InterfaceCompileFailed(args, Array(), StopInfoError)
Expand All @@ -165,10 +175,7 @@ private final class CachedCompiler0(
val run = new compiler.ZincRun(compileProgress)

run.compileFiles(sources)
processUnreportedWarnings(run)
underlyingReporter.problems.foreach(p =>
callback.problem(p.category, p.position, p.message, p.severity, true)
)
underlyingReporter.problems.foreach(callbackProblem)
}

underlyingReporter.printSummary()
Expand All @@ -192,18 +199,4 @@ private final class CachedCompiler0(
debug(log, "Compilation cancelled (CompilerInterface)")
throw new InterfaceCompileCancelled(args, "Compilation has been cancelled")
}

def processUnreportedWarnings(run: compiler.Run): Unit = {
// allConditionalWarnings and the ConditionalWarning class are only in 2.10+
final class CondWarnCompat(
val what: String,
val warnings: mutable.ListBuffer[(compiler.Position, String)]
)
implicit def compat(run: AnyRef): Compat = new Compat
final class Compat { def allConditionalWarnings = List[CondWarnCompat]() }

val warnings = run.allConditionalWarnings
if (warnings.nonEmpty)
compiler.logUnreportedWarnings(warnings.map(cw => ("" /*cw.what*/, cw.warnings.toList)))
}
}
19 changes: 14 additions & 5 deletions test/junit/scala/tools/xsbt/TestCallback.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package scala.tools.xsbt

import xsbti.{AnalysisCallback, UseScope, VirtualFile, VirtualFileRef}
import xsbti.api.{ClassLike, DependencyContext}
import xsbti.{Action, AnalysisCallback2, DiagnosticCode, DiagnosticRelatedInformation, Position, Severity, UseScope, VirtualFile, VirtualFileRef}

import java.io.File
import java.nio.file.Path
import java.util
import java.util.Optional
import xsbti.api.{ClassLike, DependencyContext}

import scala.collection.mutable.ArrayBuffer

class TestCallback extends AnalysisCallback {
class TestCallback extends AnalysisCallback2 {
case class TestUsedName(name: String, scopes: util.EnumSet[UseScope])

val classDependencies = new ArrayBuffer[(String, String, DependencyContext)]
Expand Down Expand Up @@ -122,6 +121,16 @@ class TestCallback extends AnalysisCallback {
reported: Boolean
): Unit = ()

override def problem2(what: String,
pos: Position,
msg: String,
severity: Severity,
reported: Boolean,
rendered: Optional[String],
diagnosticCode: Optional[DiagnosticCode],
diagnosticRelatedInformation: util.List[DiagnosticRelatedInformation],
actions: util.List[Action]): Unit = ()

override def dependencyPhaseCompleted(): Unit = {}

override def apiPhaseCompleted(): Unit = {}
Expand Down Expand Up @@ -153,7 +162,7 @@ object TestCallback {
}

private def pairsToMultiMap[A, B](pairs: Seq[(A, B)]): Map[A, Set[B]] = {
import scala.collection.{ mutable => m }
import scala.collection.{mutable => m}
val emptyMultiMap = new m.HashMap[A, m.Set[B]]
val multiMap = pairs.foldLeft(emptyMultiMap) {
case (acc, (key, value)) => acc.get(key) match {
Expand Down

0 comments on commit 35a23d6

Please sign in to comment.