-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improvement: ignore create or modify file watcher event when content did not change #7006
base: main
Are you sure you want to change the base?
Changes from 2 commits
2d9cd16
0f0c33f
9f7899a
9fb26b1
94e4052
6ad93dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package scala.meta.internal.metals | ||
|
||
import java.io.IOException | ||
|
||
import scala.collection.concurrent.TrieMap | ||
|
||
import scala.meta.internal.io.FileIO | ||
import scala.meta.internal.metals.MetalsEnrichments._ | ||
import scala.meta.internal.mtags.MD5 | ||
import scala.meta.io.AbsolutePath | ||
|
||
class SavedFileSignatures { | ||
private val previousCreateOrModify = TrieMap[AbsolutePath, String]() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we reuse MutableMd5Fingerprints somehow? Maybe just save them in didSave with a flag to MutableMd5Fingerprints and then we can get the last saved one instead of calculating again? We can still have a separate collection in Compilations so that it's possible to clear it and force new compilations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For |
||
|
||
def didSavedContentChanged(pathWithContent: PathWithContent): Boolean = { | ||
val path = pathWithContent.path | ||
pathWithContent | ||
.getSignature() | ||
.map { md5 => | ||
synchronized { | ||
if (previousCreateOrModify.get(path) == md5) false | ||
else { | ||
md5 match { | ||
case None => previousCreateOrModify.remove(path) | ||
case Some(md5) => previousCreateOrModify.put(path, md5) | ||
} | ||
true | ||
} | ||
} | ||
} | ||
.getOrElse(true) | ||
} | ||
|
||
def cancel(): Unit = previousCreateOrModify.clear() | ||
} | ||
|
||
class PathWithContent( | ||
val path: AbsolutePath, | ||
optContent: Option[PathWithContent.Content], | ||
) { | ||
def getSignature(): Either[IOException, Option[String]] = { | ||
optContent | ||
.map(_.map(content => MD5.bytesToHex(content))) | ||
.map(Right[IOException, Option[String]](_)) | ||
.getOrElse { | ||
try { | ||
if (path.exists) | ||
Right(Some(MD5.bytesToHex(FileIO.readAllBytes(path)))) | ||
else Right(None) | ||
} catch { | ||
case e: IOException => | ||
scribe.warn(s"Failed to read contents of $path", e) | ||
Left(e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
object PathWithContent { | ||
// None if the file doesn't exist | ||
type Content = Option[Array[Byte]] | ||
def apply(path: AbsolutePath) = new PathWithContent(path, None) | ||
def apply(path: AbsolutePath, content: Array[Byte]) = | ||
new PathWithContent(path, Some(Some(content))) | ||
def deleted(path: AbsolutePath) = new PathWithContent(path, Some(None)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could just find first that changed in a given target?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, though I doubt it ever too many files at once. Only file watcher passes more than one file.