Skip to content

Commit

Permalink
bugfix: Don't read file for Interactivedb if it doesn't exist
Browse files Browse the repository at this point in the history
Previously, we would assume that any file that we want to calculate InteractiveSemanticdb for exists on the filesystem, which is not true. For example renamed files if renamed from terminal will stick in the editor and cause exceptions to be throw. Now, we also try to read it from buffers and from filesystem as a fallback if it exists on the filesystem.
  • Loading branch information
tgodzik committed Oct 13, 2023
1 parent 885705f commit b91dfda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package scala.meta.internal.metals

import java.nio.charset.Charset
import java.util.Collections
import java.util.concurrent.atomic.AtomicReference

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
Expand All @@ -34,14 +32,14 @@ import org.eclipse.{lsp4j => l}
final class InteractiveSemanticdbs(
workspace: AbsolutePath,
buildTargets: BuildTargets,
charset: Charset,
client: MetalsLanguageClient,
tables: Tables,
statusBar: StatusBar,
compilers: () => Compilers,
clientConfig: ClientConfiguration,
semanticdbIndexer: () => SemanticdbIndexer,
javaInteractiveSemanticdb: Option[JavaInteractiveSemanticdb],
buffers: Buffers,
) extends Cancelable
with Semanticdbs {

Expand Down Expand Up @@ -87,18 +85,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)
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,14 @@ class MetalsLspService(
new InteractiveSemanticdbs(
folder,
buildTargets,
charset,
languageClient,
tables,
statusBar,
() => compilers,
clientConfig,
() => semanticDBIndexer,
javaInteractiveSemanticdb,
buffers,
)
)
}
Expand Down

0 comments on commit b91dfda

Please sign in to comment.