From 41dc01ecc4c6b5a81e377f058cfa439ec4b14817 Mon Sep 17 00:00:00 2001 From: dkashyn Date: Wed, 13 Sep 2023 18:09:06 -0400 Subject: [PATCH] Trying to workaround "unsynced" case for newly added files. --- .../autosync/ProjectTargetManagerImpl.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/base/src/com/google/idea/blaze/base/sync/autosync/ProjectTargetManagerImpl.java b/base/src/com/google/idea/blaze/base/sync/autosync/ProjectTargetManagerImpl.java index 561393cd0df..497205f6224 100644 --- a/base/src/com/google/idea/blaze/base/sync/autosync/ProjectTargetManagerImpl.java +++ b/base/src/com/google/idea/blaze/base/sync/autosync/ProjectTargetManagerImpl.java @@ -34,6 +34,8 @@ import com.intellij.ide.projectView.ProjectView; import com.intellij.openapi.project.Project; import java.io.File; +import java.util.Arrays; +import java.util.List; import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; @@ -79,6 +81,35 @@ public SyncStatus getSyncStatus(File source) { return syncedTargets.stream().anyMatch(t -> syncInProgress(t.getLabel())) ? SyncStatus.RESYNCING : SyncStatus.SYNCED; + } else { + // For newly added files there is no way to have them synced even though they are, in the majority of cases, are + // identical to the rest of files in the folder. + // So we are checking 5 other files from the same folder and if TargetKey collection is the same for all + // then we are using the same for file that marked as `(unsynced)`. + List> alternativeSources = + Arrays.stream(source.getParentFile().listFiles()) + .filter(f -> f.isFile() && !SourceToTargetMap.getInstance(project).getRulesForSourceFile(f).isEmpty()) + .limit(5) + .map(f -> SourceToTargetMap.getInstance(project).getRulesForSourceFile(f)) + .toList(); + ImmutableCollection consensusSource = null; + for (ImmutableCollection alternativeSource : alternativeSources) { + if (consensusSource == null) { + consensusSource = alternativeSource; + } else { + // If at least one is different we cannot use this approach. + if (!alternativeSource.equals(consensusSource)) { + consensusSource = null; + break; + } + } + } + if (consensusSource != null && !consensusSource.isEmpty()) { + return consensusSource.stream().anyMatch(t -> syncInProgress(t.getLabel())) + ? SyncStatus.RESYNCING + : SyncStatus.SYNCED; + + } } Label label = WorkspaceHelper.getBuildLabel(project, source); if (label == null) {