Skip to content
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

Reload project view, and make sure that BazelImportSettings respect p… #7088

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion base/src/com/google/idea/blaze/base/settings/Blaze.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Boolean> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlazeProjectDataManager> delegate;
private final Project project;
private Supplier<BlazeProjectDataManager> 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
Expand All @@ -61,6 +54,27 @@ public BlazeProjectData loadProject(BlazeImportSettings importSettings) {

@Override
public void saveProject(BlazeImportSettings importSettings, BlazeProjectData projectData) {
if (projectType != Blaze.getProjectType(project)) {
initBlazeProjectDataDelegate(project);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we need to call save here for the old model... But this is a thing only for Aspect one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 50% of non-test impls not using saveProject this interface definitely needs some cleanup...

// TODO(mathewi) Tidy up the interface to remove this unnecessary stuff.

}
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);
});
}

}
Loading