Skip to content

Commit

Permalink
improvement: ignore create or modify file watcher event, when content…
Browse files Browse the repository at this point in the history
… of the file is not changed
  • Loading branch information
kasiaMarek committed Dec 5, 2024
1 parent c291bd2 commit f083b46
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package scala.meta.internal.metals

import java.io.IOException
import java.util
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.atomic.AtomicReference

import scala.collection.concurrent.TrieMap
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContextExecutorService
import scala.concurrent.Future
Expand All @@ -20,6 +22,7 @@ import scala.meta.internal.builds.BuildServerProvider
import scala.meta.internal.builds.BuildTools
import scala.meta.internal.builds.ScalaCliBuildTool
import scala.meta.internal.builds.ShellRunner
import scala.meta.internal.io.FileIO
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ammonite.Ammonite
import scala.meta.internal.metals.clients.language.ConfiguredLanguageClient
Expand All @@ -28,6 +31,7 @@ import scala.meta.internal.metals.doctor.MetalsServiceInfo
import scala.meta.internal.metals.watcher.FileWatcherEvent
import scala.meta.internal.metals.watcher.FileWatcherEvent.EventType
import scala.meta.internal.metals.watcher.ProjectFileWatcher
import scala.meta.internal.mtags.MD5
import scala.meta.internal.mtags.SemanticdbPath
import scala.meta.internal.mtags.Semanticdbs
import scala.meta.internal.tvp.FolderTreeViewProvider
Expand Down Expand Up @@ -411,7 +415,8 @@ class ProjectMetalsLspService(
isScalaOrJava &&
!path.isDirectory &&
!savedFiles.isRecentlyActive(path) &&
!buffers.contains(path)
!buffers.contains(path) &&
didContentChange(event)
) {
event.eventType match {
case EventType.CreateOrModify => onCreate(path)
Expand All @@ -436,6 +441,34 @@ class ProjectMetalsLspService(
}
}

private val previousCreateOrModify = TrieMap[AbsolutePath, String]()

// If code is genereted we might get a lot of `CreateOrModify`
// events, even if the code doesn't really change
private def didContentChange(event: FileWatcherEvent): Boolean = {
val path = AbsolutePath(event.path)
event.eventType match {
case EventType.CreateOrModify =>
try {
val md5 = MD5.bytesToHex(FileIO.readAllBytes(path))
previousCreateOrModify.synchronized {
if (previousCreateOrModify.get(path).exists(_ == md5)) false
else {
previousCreateOrModify.put(path, md5)
true
}
}
} catch {
case e: IOException =>
scribe.warn(s"Failed to read contents of $path", e)
false
}
case _ =>
previousCreateOrModify.remove(path)
true
}
}

private val ammonite: Ammonite = register {
val amm = new Ammonite(
buffers,
Expand Down

0 comments on commit f083b46

Please sign in to comment.