diff --git a/metals/src/main/scala/scala/meta/internal/metals/InteractiveSemanticdbs.scala b/metals/src/main/scala/scala/meta/internal/metals/InteractiveSemanticdbs.scala index f4ae0f62178..fe8d9a78050 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/InteractiveSemanticdbs.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/InteractiveSemanticdbs.scala @@ -8,7 +8,6 @@ import scala.util.Success import scala.util.Try import scala.meta.internal.builds.SbtBuildTool -import scala.meta.internal.io.FileIO import scala.meta.internal.metals.Messages._ import scala.meta.internal.metals.MetalsEnrichments._ import scala.meta.internal.metals.clients.language.MetalsLanguageClient @@ -42,6 +41,7 @@ final class InteractiveSemanticdbs( clientConfig: ClientConfiguration, semanticdbIndexer: () => SemanticdbIndexer, javaInteractiveSemanticdb: Option[JavaInteractiveSemanticdb], + buffers: Buffers, ) extends Cancelable with Semanticdbs { @@ -87,18 +87,25 @@ final class InteractiveSemanticdbs( val result = textDocumentCache.compute( source, (path, existingDoc) => { - val text = unsavedContents.getOrElse(FileIO.slurp(source, charset)) - val sha = MD5.compute(text) - if (existingDoc == null || existingDoc.md5 != sha) { - Try(compile(path, text)) match { - case Success(doc) if doc != null => - if (!source.isDependencySource(workspace)) - semanticdbIndexer().onChange(source, doc) - doc - case _ => null - } - } else - existingDoc + unsavedContents.orElse(buffers.get(source).orElse { + if (source.exists) Some(source.readText(charset)) + else None + }) match { + case None => null + case Some(text) => + val sha = MD5.compute(text) + if (existingDoc == null || existingDoc.md5 != sha) { + Try(compile(path, text)) match { + case Success(doc) if doc != null => + if (!source.isDependencySource(workspace)) + semanticdbIndexer().onChange(source, doc) + doc + case _ => null + } + } else + existingDoc + } + }, ) TextDocumentLookup.fromOption(source, Option(result)) diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala index 11df0204c71..a98a9308318 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala @@ -367,6 +367,7 @@ class MetalsLspService( clientConfig, () => semanticDBIndexer, javaInteractiveSemanticdb, + buffers, ) ) } diff --git a/mtags/src/main/scala/scala/meta/internal/mtags/ScalametaCommonEnrichments.scala b/mtags/src/main/scala/scala/meta/internal/mtags/ScalametaCommonEnrichments.scala index 29968293341..42c53dd498f 100644 --- a/mtags/src/main/scala/scala/meta/internal/mtags/ScalametaCommonEnrichments.scala +++ b/mtags/src/main/scala/scala/meta/internal/mtags/ScalametaCommonEnrichments.scala @@ -1,6 +1,7 @@ package scala.meta.internal.mtags import java.net.URI +import java.nio.charset.Charset import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.Path @@ -285,8 +286,12 @@ trait ScalametaCommonEnrichments extends CommonMtagsEnrichments { } } + def readText(charset: Charset): String = { + FileIO.slurp(path, charset) + } + def readText: String = { - FileIO.slurp(path, StandardCharsets.UTF_8) + readText(StandardCharsets.UTF_8) } def readTextOpt: Option[String] = {