Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Feb 13, 2024
1 parent 376823c commit dd965c4
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 45 deletions.
14 changes: 13 additions & 1 deletion metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Expand Up @@ -112,6 +112,8 @@ class Compilers(
)

private val worksheetsDigests = new TrieMap[AbsolutePath, String]()
private val wasSuccessfullyCompiled =
new TrieMap[BuildTargetIdentifier, Boolean]()

private val cache = jcache.asScala
private def buildTargetPCFromCache(
Expand Down Expand Up @@ -272,6 +274,11 @@ class Compilers(
val isSuccessful = report.getErrors == 0
buildTargetPCFromCache(report.getTarget).foreach(_.restart(isSuccessful))

wasSuccessfullyCompiled.updateWith(report.getTarget()) {
case Some(true) => Some(true)
case _ => Some(isSuccessful)
}

if (isSuccessful) {
// Restart PC for all build targets that depend on this target since the classfiles
// may have changed.
Expand Down Expand Up @@ -1188,14 +1195,19 @@ class Compilers(
classpath: Seq[Path],
search: SymbolSearch,
): PresentationCompiler = {
newCompiler(
val pc = newCompiler(
mtags,
target.scalac.getOptions().asScala.toSeq,
classpath,
search,
target.scalac.getTarget.getUri,
).withBuildTargetName(target.displayName)
.withCompilerFiles(new TargetCompilerFiles(target.id))

wasSuccessfullyCompiled
.get(target.scalac.getTarget())
.map(pc.withWasSuccessfullyCompiled)
.getOrElse(pc)
}

def newCompiler(
Expand Down
Expand Up @@ -324,4 +324,8 @@ public abstract PresentationCompiler newInstance(String buildTargetIdentifier, L
*/
public abstract String scalaVersion();

public PresentationCompiler withWasSuccessfullyCompiled(boolean wasSuccessful) {
return this;
}

}
Expand Up @@ -28,5 +28,5 @@ case class OutlineFiles(
)

object OutlineFiles {
def empty = OutlineFiles(Nil)
def empty: OutlineFiles = OutlineFiles(Nil)
}
10 changes: 7 additions & 3 deletions mtags/src/main/scala-2.13/scala/meta/internal/pc/Compat.scala
Expand Up @@ -28,22 +28,26 @@ trait Compat { this: MetalsGlobal =>

def runOutline(files: OutlineFiles): Unit = {
runOutline(files.files)
if(files.firstCompileSubstitute) {
if (files.firstCompileSubstitute) {
// if first compilation substitute we compile all files twice
// first to emit symbols, second so signatures have information about those symbols
// this isn't a perfect strategy but much better than single compile
runOutline(files.files, forceNewUnit = true)
}
}

private def runOutline(files: List[VirtualFileParams], forceNewUnit: Boolean = false): Unit = {
private def runOutline(
files: List[VirtualFileParams],
forceNewUnit: Boolean = false
): Unit = {
this.settings.Youtline.value = true
files.foreach { params =>
val unit = this.addCompilationUnit(
params.text(),
params.uri.toString(),
cursor = None,
isOutline = true
isOutline = true,
forceNew = forceNewUnit
)
this.typeCheck(unit)
this.richCompilationCache.put(params.uri().toString(), unit)
Expand Down
10 changes: 6 additions & 4 deletions mtags/src/main/scala-2/scala/meta/internal/pc/MetalsGlobal.scala
Expand Up @@ -66,7 +66,8 @@ class MetalsGlobal(

val logger: Logger = Logger.getLogger(classOf[MetalsGlobal].getName)

val richCompilationCache: TrieMap[String,RichCompilationUnit] = TrieMap.empty[String, RichCompilationUnit]
val richCompilationCache: TrieMap[String, RichCompilationUnit] =
TrieMap.empty[String, RichCompilationUnit]

// for those paths units were
val fullyCompiled: mutable.Set[String] = mutable.Set.empty[String]
Expand Down Expand Up @@ -652,7 +653,8 @@ class MetalsGlobal(
filename: String,
cursor: Option[Int],
cursorName: String = CURSOR,
isOutline: Boolean = false
isOutline: Boolean = false,
forceNew: Boolean = false
): RichCompilationUnit = {
val codeWithCursor = cursor match {
case Some(offset) =>
Expand All @@ -671,11 +673,11 @@ class MetalsGlobal(
if util.Arrays.equals(
value.source.content,
richUnit.source.content
) && (isOutline || fullyCompiled(filename)) =>
) && (isOutline || fullyCompiled(filename)) && !forceNew =>
value
case _ =>
unitOfFile(richUnit.source.file) = richUnit
if(!isOutline) {
if (!isOutline) {
fullyCompiled += filename
} else {
fullyCompiled -= filename
Expand Down
Expand Up @@ -9,7 +9,6 @@ import scala.util.control.NonFatal

import scala.meta.internal.metals.ReportContext
import scala.meta.pc.PresentationCompilerConfig
import scala.meta.pc.VirtualFileParams

class ScalaCompilerWrapper(global: MetalsGlobal)
extends CompilerWrapper[StoreReporter, MetalsGlobal] {
Expand Down
Expand Up @@ -67,10 +67,14 @@ case class ScalaPresentationCompiler(
config: PresentationCompilerConfig = PresentationCompilerConfigImpl(),
folderPath: Option[Path] = None,
reportsLevel: ReportLevel = ReportLevel.Info,
compilerFiles: Option[CompilerFiles] = None
compilerFiles: Option[CompilerFiles] = None,
wasSuccessfullyCompiledInitial: Option[Boolean] = None
) extends PresentationCompiler {

private val wasFirstCompilationSuccessful = new util.concurrent.atomic.AtomicReference[Option[Boolean]](None)
private val wasSuccessfullyCompiled =
new util.concurrent.atomic.AtomicReference[Option[Boolean]](
wasSuccessfullyCompiledInitial
)

implicit val executionContext: ExecutionContextExecutor = ec

Expand Down Expand Up @@ -148,12 +152,13 @@ case class ScalaPresentationCompiler(
def restart(): Unit = restoreOutlineAndRestart()

override def restart(wasSuccessful: java.lang.Boolean): Unit = {
wasFirstCompilationSuccessful.updateAndGet {
wasSuccessfullyCompiled.updateAndGet {
case None => Some(wasSuccessful)
case _ if wasSuccessful => Some(true)
case value => value
}

if(wasSuccessful) {
if (wasSuccessful) {
changedDocuments.clear()
compilerAccess.shutdownCurrentCompiler()
} else {
Expand All @@ -168,26 +173,25 @@ case class ScalaPresentationCompiler(
EmptyCancelToken
) { pc =>
/* we will still want outline recompiled if the compilation was not succesful */
pc.compiler().richCompilationCache.foreach {
case (uriString, unit) =>
try {
val text = unit.source.content.mkString
val uri = uriString.toAbsolutePath.toURI
val params =
CompilerOffsetParams(uri, text, 0, EmptyCancelToken)
changedDocuments += uri -> params
} catch {
case NonFatal(error) =>
reportContex.incognito.create(
Report(
"restoring_cache",
"Error while restoring outline compiler cache",
error
)
pc.compiler().richCompilationCache.foreach { case (uriString, unit) =>
try {
val text = unit.source.content.mkString
val uri = uriString.toAbsolutePath.toURI
val params =
CompilerOffsetParams(uri, text, 0, EmptyCancelToken)
changedDocuments += uri -> params
} catch {
case NonFatal(error) =>
reportContex.incognito.create(
Report(
"restoring_cache",
"Error while restoring outline compiler cache",
error
)
logger
.log(util.logging.Level.SEVERE, error.getMessage(), error)
}
)
logger
.log(util.logging.Level.SEVERE, error.getMessage(), error)
}
}
}
.get()
Expand All @@ -212,8 +216,15 @@ case class ScalaPresentationCompiler(
private def outlineFiles(
current: VirtualFileParams
): OutlineFiles = {
val shouldCompileAll = wasSuccessfullyCompiled
.getAndUpdate {
case Some(false) => Some(true)
case value => value
}
.exists(!_)

val result =
if (wasFirstCompilationSuccessful.getAndSet(Some(true)).exists(!_)) {
if (shouldCompileAll) {
// if first compilation was unsuccessful we want to outline all files
compilerFiles.iterator.flatMap(_.allPaths().asScala).foreach { path =>
val uri = path.toUri()
Expand All @@ -222,9 +233,13 @@ case class ScalaPresentationCompiler(
changedDocuments += uri -> CompilerOffsetParams(uri, text, 0)
}
}
OutlineFiles(changedDocuments.values.toList, firstCompileSubstitute = true)
OutlineFiles(
changedDocuments.values.toList,
firstCompileSubstitute = true
)
} else {
val files = changedDocuments.values.filterNot(_.uri() == current.uri()).toList
val files =
changedDocuments.values.filterNot(_.uri() == current.uri()).toList
OutlineFiles(files)
}

Expand Down Expand Up @@ -282,14 +297,15 @@ case class ScalaPresentationCompiler(

override def complete(
params: OffsetParams
): CompletableFuture[CompletionList] =
): CompletableFuture[CompletionList] = {
compilerAccess.withInterruptableCompiler(Some(params))(
EmptyCompletionList(),
params.token
) { pc =>
new CompletionProvider(pc.compiler(outlineFiles(params)), params)
.completions()
}
}

override def implementAbstractMembers(
params: OffsetParams
Expand Down Expand Up @@ -579,4 +595,11 @@ case class ScalaPresentationCompiler(
.toList
.asJava
}

override def withWasSuccessfullyCompiled(
wasSuccessful: Boolean
): PresentationCompiler = {
this.copy(wasSuccessfullyCompiledInitial = Some(wasSuccessful))
}

}
Expand Up @@ -26,7 +26,7 @@ trait WorkspaceSymbolSearch { compiler: MetalsGlobal =>
def matches = if (sym.isType)
CompletionFuzzy.matchesSubCharacters(query, sym.name.toString())
else CompletionFuzzy.matches(query, sym.name.toString())
if (sym.exists && matches) {
if (sym != null && sym.exists && matches) {
try {
visitMember(sym)
} catch {
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/src/test/scala/tests/SemanticTokensLspSuite.scala
Expand Up @@ -266,13 +266,13 @@ class SemanticTokensLspSuite extends BaseLspSuite("SemanticTokens") {
for {
_ <- initialize(
s"""/metals.json
|{"a":{}}
|/a/src/main/scala/a/Main.scala
|
|/a/src/main/scala/a/OtherFile.scala
|package a
|object A
|""".stripMargin,
|{"a":{}}
|/a/src/main/scala/a/Main.scala
|
|/a/src/main/scala/a/OtherFile.scala
|package a
|object A
|""".stripMargin,
expectError = true,
)
_ <- server.didChangeConfiguration(
Expand Down

0 comments on commit dd965c4

Please sign in to comment.