diff --git a/base/src/com/google/idea/blaze/base/settings/Blaze.java b/base/src/com/google/idea/blaze/base/settings/Blaze.java index 07566f071fd..d6534006bc6 100644 --- a/base/src/com/google/idea/blaze/base/settings/Blaze.java +++ b/base/src/com/google/idea/blaze/base/settings/Blaze.java @@ -17,14 +17,20 @@ import com.google.idea.blaze.base.bazel.BuildSystemProvider; import com.google.idea.blaze.base.model.BlazeProjectData; +import com.google.idea.blaze.base.projectview.ProjectViewManager; +import com.google.idea.blaze.base.projectview.ProjectViewSet; +import com.google.idea.blaze.base.projectview.section.sections.UseQuerySyncSection; +import com.google.idea.blaze.base.scope.Scope; import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager; -import com.google.idea.blaze.base.qsync.QuerySync; import com.google.idea.blaze.base.settings.BlazeImportSettings.ProjectType; import com.intellij.ide.DataManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManager; + +import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.swing.SwingUtilities; +import java.util.Optional; /** Blaze project utilities. */ public class Blaze { @@ -72,6 +78,38 @@ public static ProjectType getProjectType(@Nullable Project project) { return blazeImportSettings.getProjectType(); } + /** + * This variant allows us to enable and disable Query Sync for already imported project. + * com.google.idea.blaze.base.settings.Blaze#getProjectType(com.intellij.openapi.project.Project) is called quite often + * so we cannot reload project view from for every of such call. + * This is why we have this special case to make sure that Sync respects project view selection if there is any. + */ + public static ProjectType getUpToDateProjectTypeBeforeSync(@Nonnull Project project) { + BlazeImportSettingsManager blazeImportSettingsManager = + BlazeImportSettingsManager.getInstance(project); + if (blazeImportSettingsManager == null) { + return ProjectType.UNKNOWN; + } + BlazeImportSettings blazeImportSettings = blazeImportSettingsManager.getImportSettings(); + if (blazeImportSettings == null) { + return ProjectType.UNKNOWN; + } + ProjectViewSet projectViewSet = Scope.root( + context -> { + return ProjectViewManager.getInstance(project).reloadProjectView(context); + }); + Optional querySyncProjectView = projectViewSet.getScalarValue(UseQuerySyncSection.KEY); + if (querySyncProjectView.isPresent()) { + if (blazeImportSettings.getProjectType() == ProjectType.QUERY_SYNC && !querySyncProjectView.get()) { + blazeImportSettings.setProjectType(ProjectType.ASPECT_SYNC); + } else if (blazeImportSettings.getProjectType() == ProjectType.ASPECT_SYNC && querySyncProjectView.get()) { + blazeImportSettings.setProjectType(ProjectType.QUERY_SYNC); + } + } + + return getProjectType(project); + } + /** * Returns the build system associated with this project, or falls back to the default blaze build * system if the project is null or not a blaze project. diff --git a/base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java b/base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java index 1cf36e739c0..854038b9620 100644 --- a/base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java +++ b/base/src/com/google/idea/blaze/base/sync/BlazeSyncManager.java @@ -50,13 +50,11 @@ import com.google.idea.blaze.base.settings.BlazeUserSettings.FocusBehavior; import com.google.idea.blaze.base.sync.SyncScope.SyncCanceledException; import com.google.idea.blaze.base.sync.SyncScope.SyncFailedException; -import com.google.idea.blaze.base.sync.aspects.strategy.AspectRepositoryProvider; import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager; import com.google.idea.blaze.base.sync.projectview.SyncDirectoriesWarning; import com.google.idea.blaze.base.sync.status.BlazeSyncStatus; import com.google.idea.blaze.base.toolwindow.Task; import com.google.idea.blaze.base.util.SaveUtil; -import com.google.idea.blaze.base.util.TemplateWriter; import com.google.idea.blaze.common.Context; import com.google.idea.blaze.common.PrintOutput; import com.google.idea.blaze.common.PrintOutput.OutputType; @@ -69,7 +67,6 @@ import com.intellij.openapi.util.text.StringUtil; import java.util.Collection; -import java.util.Map; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -97,7 +94,7 @@ public static void printAndLogError(String errorMessage, Context context) { /** Requests a project sync with Blaze. */ public void requestProjectSync(BlazeSyncParams syncParams) { - if (Blaze.getProjectType(project) == ProjectType.QUERY_SYNC) { + if (Blaze.getUpToDateProjectTypeBeforeSync(project) == ProjectType.QUERY_SYNC) { throw new NotSupportedWithQuerySyncException("legacy sync requested"); } if (syncParams.syncMode() == SyncMode.NO_BUILD diff --git a/base/src/com/google/idea/blaze/base/sync/BlazeSyncStartupActivity.java b/base/src/com/google/idea/blaze/base/sync/BlazeSyncStartupActivity.java index 6666bb1fdec..0f1eb98461f 100644 --- a/base/src/com/google/idea/blaze/base/sync/BlazeSyncStartupActivity.java +++ b/base/src/com/google/idea/blaze/base/sync/BlazeSyncStartupActivity.java @@ -38,7 +38,7 @@ public void runActivity(Project project) { if (importSettings == null) { return; } - if (Blaze.getProjectType(project) == ProjectType.QUERY_SYNC) { + if (Blaze.getUpToDateProjectTypeBeforeSync(project) == ProjectType.QUERY_SYNC) { // When query sync is not enabled hasProjectData triggers the load QuerySyncManager.getInstance(project) .onStartup(QuerySyncActionStatsScope.create(getClass(), null)); diff --git a/base/src/com/google/idea/blaze/base/sync/actions/FullSyncProjectAction.java b/base/src/com/google/idea/blaze/base/sync/actions/FullSyncProjectAction.java index 4d108c45023..67ee760d1fa 100644 --- a/base/src/com/google/idea/blaze/base/sync/actions/FullSyncProjectAction.java +++ b/base/src/com/google/idea/blaze/base/sync/actions/FullSyncProjectAction.java @@ -34,7 +34,7 @@ public class FullSyncProjectAction extends BlazeProjectSyncAction { @Override protected void runSync(Project project, AnActionEvent e) { - if (Blaze.getProjectType(project) == ProjectType.QUERY_SYNC) { + if (Blaze.getUpToDateProjectTypeBeforeSync(project) == ProjectType.QUERY_SYNC) { QuerySyncManager.getInstance(project) .fullSync(QuerySyncActionStatsScope.create(getClass(), e), TaskOrigin.USER_ACTION); } else { diff --git a/base/src/com/google/idea/blaze/base/sync/actions/IncrementalSyncProjectAction.java b/base/src/com/google/idea/blaze/base/sync/actions/IncrementalSyncProjectAction.java index b6f86d21f9f..a64d2c9eafa 100644 --- a/base/src/com/google/idea/blaze/base/sync/actions/IncrementalSyncProjectAction.java +++ b/base/src/com/google/idea/blaze/base/sync/actions/IncrementalSyncProjectAction.java @@ -48,17 +48,31 @@ protected void runSync(Project project, AnActionEvent e) { } public static void doIncrementalSync(Class klass, Project project, @Nullable AnActionEvent e) { - if (Blaze.getProjectType(project) == ProjectType.QUERY_SYNC) { + // This is a project type before we refreshed project view + ProjectType currentProjectType = Blaze.getProjectType(project); + // This is a project type after we reloaded project view and actualized it in BlazeImportSettings + ProjectType refreshedProjectType = Blaze.getUpToDateProjectTypeBeforeSync(project); + boolean projectTypeChanged = currentProjectType != refreshedProjectType; + if (refreshedProjectType == ProjectType.QUERY_SYNC) { QuerySyncManager qsm = QuerySyncManager.getInstance(project); QuerySyncActionStatsScope scope = QuerySyncActionStatsScope.create(klass, e); if (!qsm.isProjectLoaded()) { qsm.onStartup(scope); } else { - qsm.deltaSync(scope, TaskOrigin.USER_ACTION); + if (projectTypeChanged) { + qsm.fullSync(scope, TaskOrigin.USER_ACTION); + } else { + qsm.deltaSync(scope, TaskOrigin.USER_ACTION); + } } } else { - BlazeSyncManager.getInstance(project) - .incrementalProjectSync(/* reason= */ "IncrementalSyncProjectAction"); + if (projectTypeChanged) { + BlazeSyncManager.getInstance(project) + .fullProjectSync(/* reason= */ "FullSyncProjectAction"); + } else { + BlazeSyncManager.getInstance(project) + .incrementalProjectSync(/* reason= */ "IncrementalSyncProjectAction"); + } } } diff --git a/base/src/com/google/idea/blaze/base/sync/data/DelegatingBlazeProjectDataManager.java b/base/src/com/google/idea/blaze/base/sync/data/DelegatingBlazeProjectDataManager.java index b19d97b06c9..87abe04408b 100644 --- a/base/src/com/google/idea/blaze/base/sync/data/DelegatingBlazeProjectDataManager.java +++ b/base/src/com/google/idea/blaze/base/sync/data/DelegatingBlazeProjectDataManager.java @@ -28,29 +28,22 @@ /** Stores a cache of blaze project data and issues any side effects when that data is updated. */ public class DelegatingBlazeProjectDataManager implements BlazeProjectDataManager { - private final Supplier delegate; + private final Project project; + private Supplier delegate; + private ProjectType projectType; public DelegatingBlazeProjectDataManager(Project project) { - delegate = - Suppliers.memoize( - () -> { - ProjectType projectType = Blaze.getProjectType(project); - switch (projectType) { - case UNKNOWN: - return new EmptyBlazeProjectDataManager(); - case QUERY_SYNC: - return new QuerySyncProjectDataManager(project); - case ASPECT_SYNC: - return new AspectSyncProjectDataManager(project); - } - throw new AssertionError(projectType); - }); + this.project = project; + initBlazeProjectDataDelegate(project); } @Override @Nullable public BlazeProjectData getBlazeProjectData() { - return delegate.get().getBlazeProjectData(); + if (projectType != Blaze.getProjectType(project)) { + initBlazeProjectDataDelegate(project); + } + return delegate.get().getBlazeProjectData(); } @Nullable @@ -61,6 +54,27 @@ public BlazeProjectData loadProject(BlazeImportSettings importSettings) { @Override public void saveProject(BlazeImportSettings importSettings, BlazeProjectData projectData) { + if (projectType != Blaze.getProjectType(project)) { + initBlazeProjectDataDelegate(project); + } delegate.get().saveProject(importSettings, projectData); } + + private void initBlazeProjectDataDelegate(Project project) { + this.projectType = Blaze.getProjectType(project); + this.delegate = + Suppliers.memoize( + () -> { + switch (projectType) { + case UNKNOWN: + return new EmptyBlazeProjectDataManager(); + case QUERY_SYNC: + return new QuerySyncProjectDataManager(project); + case ASPECT_SYNC: + return new AspectSyncProjectDataManager(project); + } + throw new AssertionError(projectType); + }); + } + }