-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f0c33f
commit 9f7899a
Showing
12 changed files
with
192 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
metals/src/main/scala/scala/meta/internal/metals/FileChanges.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package scala.meta.internal.metals | ||
|
||
import scala.collection.concurrent.TrieMap | ||
import scala.collection.mutable | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
|
||
import scala.meta.internal.metals.MetalsEnrichments._ | ||
import scala.meta.io.AbsolutePath | ||
|
||
import ch.epfl.scala.bsp4j.BuildTargetIdentifier | ||
|
||
class FileChanges(buildTargets: BuildTargets, workspace: () => AbsolutePath)( | ||
implicit ec: ExecutionContext | ||
) { | ||
private val previousCreateOrModify = TrieMap[AbsolutePath, String]() | ||
private val dirtyBuildTargets = mutable.Set[BuildTargetIdentifier]() | ||
|
||
def buildTargetsToCompile( | ||
paths: Seq[(AbsolutePath, Fingerprint)], | ||
focusedDocumentBuildTarget: Option[BuildTargetIdentifier], | ||
): Future[Seq[BuildTargetIdentifier]] = | ||
for { | ||
toCompile <- paths.foldLeft( | ||
Future.successful(Seq.empty[BuildTargetIdentifier]) | ||
) { case (toCompile, (path, fingerprint)) => | ||
toCompile.flatMap { acc => | ||
findBuildTargetIfShouldCompile(path, Some(fingerprint)).map(acc ++ _) | ||
} | ||
} | ||
} yield { | ||
val allToCompile = | ||
if (focusedDocumentBuildTarget.exists(dirtyBuildTargets(_))) | ||
toCompile ++ focusedDocumentBuildTarget | ||
else toCompile | ||
willCompile(allToCompile) | ||
allToCompile | ||
} | ||
|
||
def buildTargetToCompile( | ||
path: AbsolutePath, | ||
fingerprint: Option[Fingerprint], | ||
assumeDidNotChange: Boolean, | ||
): Future[Option[BuildTargetIdentifier]] = { | ||
for { | ||
toCompile <- findBuildTargetIfShouldCompile( | ||
path, | ||
fingerprint, | ||
assumeDidNotChange, | ||
) | ||
} yield { | ||
willCompile(toCompile.toSeq) | ||
toCompile | ||
} | ||
} | ||
|
||
def willCompile(ids: Seq[BuildTargetIdentifier]): Unit = | ||
buildTargets | ||
.buildTargetTransitiveDependencies(ids.toList) | ||
.foreach(dirtyBuildTargets.remove) | ||
|
||
private def findBuildTargetIfShouldCompile( | ||
path: AbsolutePath, | ||
fingerprint: Option[Fingerprint], | ||
assumeDidNotChange: Boolean = false, | ||
): Future[Option[BuildTargetIdentifier]] = { | ||
expand(path).map( | ||
_.filter(bt => | ||
dirtyBuildTargets.contains( | ||
bt | ||
) || (!assumeDidNotChange && didContentChange(path, fingerprint, bt)) | ||
) | ||
) | ||
} | ||
|
||
private def expand( | ||
path: AbsolutePath | ||
): Future[Option[BuildTargetIdentifier]] = { | ||
val isCompilable = | ||
(path.isScalaOrJava || path.isSbt) && | ||
!path.isDependencySource(workspace()) && | ||
!path.isInTmpDirectory(workspace()) | ||
|
||
if (isCompilable) { | ||
val targetOpt = buildTargets.inverseSourcesBsp(path) | ||
targetOpt.foreach { | ||
case tgts if tgts.isEmpty => scribe.warn(s"no build target for: $path") | ||
case _ => | ||
} | ||
|
||
targetOpt | ||
} else | ||
Future.successful(None) | ||
} | ||
|
||
private def didContentChange( | ||
path: AbsolutePath, | ||
fingerprint: Option[Fingerprint], | ||
buildTarget: BuildTargetIdentifier, | ||
): Boolean = { | ||
val didChange = didContentChange(path, fingerprint) | ||
if (didChange) { | ||
buildTargets | ||
.allInverseDependencies(buildTarget) | ||
.foreach { bt => | ||
if (bt != buildTarget) dirtyBuildTargets.add(bt) | ||
} | ||
} | ||
didChange | ||
} | ||
|
||
private def didContentChange( | ||
path: AbsolutePath, | ||
fingerprint: Option[Fingerprint], | ||
): Boolean = { | ||
fingerprint | ||
.map { fingerprint => | ||
synchronized { | ||
if (previousCreateOrModify.getOrElse(path, null) == fingerprint.md5) | ||
false | ||
else { | ||
previousCreateOrModify.put(path, fingerprint.md5) | ||
true | ||
} | ||
} | ||
} | ||
.getOrElse(true) | ||
} | ||
|
||
def cancel(): Unit = previousCreateOrModify.clear() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.