Skip to content

Commit

Permalink
migrate to new dialog impl
Browse files Browse the repository at this point in the history
  • Loading branch information
VISTALL committed Jul 11, 2024
1 parent a85ce44 commit caf6ff7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,31 @@
import consulo.content.library.Library;
import consulo.document.Document;
import consulo.ide.impl.idea.analysis.BaseAnalysisAction;
import consulo.ide.impl.idea.analysis.BaseAnalysisActionDialog;
import consulo.ide.impl.idea.ide.util.PropertiesComponent;
import consulo.ide.impl.idea.openapi.project.ProjectUtil;
import consulo.ide.impl.idea.openapi.roots.libraries.LibraryUtil;
import consulo.ide.impl.idea.util.SequentialModalProgressTask;
import consulo.language.editor.FileModificationService;
import consulo.language.editor.WriteCommandAction;
import consulo.language.editor.refactoring.RefactoringBundle;
import consulo.language.editor.refactoring.localize.RefactoringLocalize;
import consulo.language.editor.scope.AnalysisScope;
import consulo.language.editor.scope.localize.AnalysisScopeLocalize;
import consulo.language.editor.ui.awt.scope.BaseAnalysisActionDialog;
import consulo.language.file.FileViewProvider;
import consulo.language.psi.*;
import consulo.language.psi.scope.GlobalSearchScope;
import consulo.language.util.ModuleUtilCore;
import consulo.localHistory.LocalHistory;
import consulo.localHistory.LocalHistoryAction;
import consulo.localize.LocalizeValue;
import consulo.module.Module;
import consulo.module.content.util.ModuleRootModificationUtil;
import consulo.project.DumbService;
import consulo.project.Project;
import consulo.ui.CheckBox;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.ui.ex.awt.Messages;
import consulo.ui.ex.awt.TitledSeparator;
import consulo.ui.ex.awt.UIUtil;
import consulo.ui.ex.awt.VerticalFlowLayout;
import consulo.ui.layout.VerticalLayout;
import consulo.usage.*;
import consulo.util.collection.ContainerUtil;
import consulo.util.lang.ObjectUtil;
Expand All @@ -65,16 +64,14 @@
import jakarta.annotation.Nullable;
import org.jetbrains.annotations.NonNls;

import javax.swing.*;
import java.util.*;
import java.util.function.Supplier;

public class InferNullityAnnotationsAction extends BaseAnalysisAction {
@NonNls
private static final String INFER_NULLITY_ANNOTATIONS = "Infer Nullity Annotations";
@NonNls
private static final String ANNOTATE_LOCAL_VARIABLES = "annotate.local.variables";
private JCheckBox myAnnotateLocalVariablesCb;

private CheckBox myAnnotateLocalVariablesCb;

public InferNullityAnnotationsAction() {
super("Infer Nullity", INFER_NULLITY_ANNOTATIONS);
Expand All @@ -83,7 +80,9 @@ public InferNullityAnnotationsAction() {
@Override
@RequiredUIAccess
protected void analyze(@Nonnull final Project project, @Nonnull final AnalysisScope scope) {
PropertiesComponent.getInstance().setValue(ANNOTATE_LOCAL_VARIABLES, myAnnotateLocalVariablesCb.isSelected());
boolean annotateLocaVars = myAnnotateLocalVariablesCb.getValueOrError();
PropertiesComponent.getInstance().setValue(ANNOTATE_LOCAL_VARIABLES, annotateLocaVars);
myAnnotateLocalVariablesCb = null;

final ProgressManager progressManager = ProgressManager.getInstance();
final Set<Module> modulesWithoutAnnotations = new HashSet<>();
Expand All @@ -109,11 +108,12 @@ public void visitFile(PsiFile file) {
if (!(file instanceof PsiJavaFile)) {
return;
}
final Module module = ModuleUtilCore.findModuleForPsiElement(file);
final Module module = file.getModule();
if (module != null && processed.add(module)) {
if (PsiUtil.getLanguageLevel(file).compareTo(LanguageLevel.JDK_1_5) < 0) {
modulesWithLL.add(module);
} else if (javaPsiFacade.findClass(defaultNullable, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)) == null) {
}
else if (javaPsiFacade.findClass(defaultNullable, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)) == null) {
modulesWithoutAnnotations.add(module);
}
}
Expand All @@ -136,19 +136,20 @@ public void visitFile(PsiFile file) {
return;
}
PsiDocumentManager.getInstance(project).commitAllDocuments();
final UsageInfo[] usageInfos = findUsages(project, scope, fileCount[0]);
final UsageInfo[] usageInfos = findUsages(annotateLocaVars, project, scope, fileCount[0]);
if (usageInfos == null) {
return;
}

processUsages(project, scope, usageInfos);
processUsages(annotateLocaVars, project, scope, usageInfos);
}

protected void processUsages(@Nonnull Project project, @Nonnull AnalysisScope scope, @Nonnull UsageInfo[] usageInfos) {
protected void processUsages(boolean annotateLocaVars, @Nonnull Project project, @Nonnull AnalysisScope scope, @Nonnull UsageInfo[] usageInfos) {
if (usageInfos.length < 5) {
applyRunnable(project, () -> usageInfos).run();
} else {
showUsageView(project, usageInfos, scope);
}
else {
showUsageView(annotateLocaVars, project, usageInfos, scope);
}
}

Expand All @@ -170,11 +171,11 @@ public static boolean addAnnotationsDependency(
message += (modulesWithoutAnnotations.size() == 1 ? "y" : "ies") + " now?";
if (Messages.showOkCancelDialog(project, message, title, UIUtil.getErrorIcon()) == Messages.OK) {
project.getApplication().runWriteAction(() ->
{
for (Module module : modulesWithoutAnnotations) {
ModuleRootModificationUtil.addDependency(module, annotationsLib);
}
});
{
for (Module module : modulesWithoutAnnotations) {
ModuleRootModificationUtil.addDependency(module, annotationsLib);
}
});
return true;
}
return false;
Expand All @@ -193,8 +194,8 @@ public static boolean addAnnotationsDependency(
}

@Nullable
protected UsageInfo[] findUsages(@Nonnull final Project project, @Nonnull final AnalysisScope scope, final int fileCount) {
final NullityInferrer inferrer = new NullityInferrer(isAnnotateLocalVariables(), project);
protected UsageInfo[] findUsages(boolean annotateLocaVars, @Nonnull final Project project, @Nonnull final AnalysisScope scope, final int fileCount) {
final NullityInferrer inferrer = new NullityInferrer(annotateLocaVars, project);
final PsiManager psiManager = PsiManager.getInstance(project);
final Runnable searchForUsages = () -> scope.accept(new PsiElementVisitor() {
int myFileCount;
Expand All @@ -211,7 +212,7 @@ public void visitFile(final PsiFile file) {
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
if (progressIndicator != null) {
progressIndicator.setText2(ProjectUtil.calcRelativeToProjectPath(virtualFile, project));
progressIndicator.setFraction(((double) myFileCount) / fileCount);
progressIndicator.setFraction(((double)myFileCount) / fileCount);
}
if (file instanceof PsiJavaFile) {
inferrer.collect(file);
Expand All @@ -220,10 +221,11 @@ public void visitFile(final PsiFile file) {
});
if (project.getApplication().isDispatchThread()) {
if (!ProgressManager.getInstance()
.runProcessWithProgressSynchronously(searchForUsages, INFER_NULLITY_ANNOTATIONS, true, project)) {
.runProcessWithProgressSynchronously(searchForUsages, INFER_NULLITY_ANNOTATIONS, true, project)) {
return null;
}
} else {
}
else {
searchForUsages.run();
}

Expand All @@ -232,10 +234,6 @@ public void visitFile(final PsiFile file) {
return usages.toArray(new UsageInfo[usages.size()]);
}

protected boolean isAnnotateLocalVariables() {
return myAnnotateLocalVariablesCb.isSelected();
}

private static Runnable applyRunnable(final Project project, final Computable<UsageInfo[]> computable) {
return () ->
{
Expand All @@ -262,12 +260,14 @@ protected void run(@Nonnull Result result) throws Throwable {
progressTask.setMinIterationTime(200);
progressTask.setTask(new AnnotateTask(project, progressTask, infos));
ProgressManager.getInstance().run(progressTask);
} else {
}
else {
NullityInferrer.nothingFoundMessage(project);
}
}
}.execute();
} finally {
}
finally {
action.finish();
}
};
Expand All @@ -285,7 +285,7 @@ protected void restartAnalysis(final Project project, final AnalysisScope scope)
});
}

private void showUsageView(@Nonnull Project project, final UsageInfo[] usageInfos, @Nonnull AnalysisScope scope) {
private void showUsageView(boolean annotateLocaVars, @Nonnull Project project, final UsageInfo[] usageInfos, @Nonnull AnalysisScope scope) {
final UsageTarget[] targets = UsageTarget.EMPTY_ARRAY;
final Ref<Usage[]> convertUsagesRef = new Ref<>();
if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(
Expand All @@ -308,26 +308,29 @@ private void showUsageView(@Nonnull Project project, final UsageInfo[] usageInfo
presentation.setShowCancelButton(true);
presentation.setUsagesString(RefactoringLocalize.usageviewUsagestext().get());

final UsageView usageView = UsageViewManager.getInstance(project).showUsages(targets, usages, presentation, rerunFactory(project, scope));
final UsageView usageView =
UsageViewManager.getInstance(project).showUsages(targets, usages, presentation, rerunFactory(annotateLocaVars, project, scope));

final Runnable refactoringRunnable = applyRunnable(project, () ->
{
final Set<UsageInfo> infos = UsageViewUtil.getNotExcludedUsageInfos(usageView);
return infos.toArray(new UsageInfo[infos.size()]);
});

String canNotMakeString = "Cannot perform operation.\nThere were changes in code after usages have been found.\nPlease perform operation search again.";
String canNotMakeString =
"Cannot perform operation.\nThere were changes in code after usages have been found.\nPlease perform operation search again.";

usageView.addPerformOperationAction(refactoringRunnable, INFER_NULLITY_ANNOTATIONS, canNotMakeString, INFER_NULLITY_ANNOTATIONS, false);
}

@Nonnull
private Supplier<UsageSearcher> rerunFactory(@Nonnull final Project project, @Nonnull final AnalysisScope scope) {
private Supplier<UsageSearcher> rerunFactory(boolean annotateLocaVars, @Nonnull final Project project, @Nonnull final AnalysisScope scope) {
return () -> new UsageInfoSearcherAdapter() {
@Nonnull
@Override
protected UsageInfo[] findUsages() {
return ObjectUtil.notNull(InferNullityAnnotationsAction.this.findUsages(project, scope, scope.getFileCount()), UsageInfo.EMPTY_ARRAY);
return ObjectUtil.notNull(InferNullityAnnotationsAction.this.findUsages(annotateLocaVars, project, scope, scope.getFileCount()),
UsageInfo.EMPTY_ARRAY);
}

@Override
Expand All @@ -337,14 +340,13 @@ public void generate(@Nonnull Processor<Usage> processor) {
};
}

@RequiredUIAccess
@Override
protected JComponent getAdditionalActionSettings(Project project, BaseAnalysisActionDialog dialog) {
final JPanel panel = new JPanel(new VerticalFlowLayout());
panel.add(new TitledSeparator());
protected void extendMainLayout(BaseAnalysisActionDialog dialog, VerticalLayout layout, Project project) {
myAnnotateLocalVariablesCb =
new JCheckBox("Annotate local variables", PropertiesComponent.getInstance().getBoolean(ANNOTATE_LOCAL_VARIABLES));
panel.add(myAnnotateLocalVariablesCb);
return panel;
CheckBox.create(LocalizeValue.localizeTODO("Annotate local variables"));
myAnnotateLocalVariablesCb.setValue(PropertiesComponent.getInstance().getBoolean(ANNOTATE_LOCAL_VARIABLES));
layout.add(myAnnotateLocalVariablesCb);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import com.intellij.java.impl.javadoc.JavadocGenerationManager;
import com.intellij.java.language.JavadocBundle;
import consulo.ide.impl.idea.analysis.BaseAnalysisAction;
import consulo.ide.impl.idea.analysis.BaseAnalysisActionDialog;
import consulo.language.editor.scope.AnalysisScope;
import consulo.language.editor.ui.awt.scope.BaseAnalysisActionDialog;
import consulo.project.Project;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.ui.ex.awt.event.DocumentAdapter;
import consulo.ui.ex.awtUnsafe.TargetAWT;
import consulo.ui.layout.VerticalLayout;
import jakarta.annotation.Nonnull;

import javax.swing.*;
Expand All @@ -42,8 +45,9 @@ protected void analyze(@Nonnull Project project, AnalysisScope scope) {
dispose();
}

@RequiredUIAccess
@Override
protected JComponent getAdditionalActionSettings(Project project, final BaseAnalysisActionDialog dialog) {
protected void extendMainLayout(BaseAnalysisActionDialog dialog, VerticalLayout layout, Project project) {
myConfigurable = new JavadocConfigurable(JavadocGenerationManager.getInstance(project).getConfiguration());
final JComponent component = myConfigurable.createComponent();
myConfigurable.reset();
Expand All @@ -54,7 +58,8 @@ protected void textChanged(DocumentEvent e) {
}
});
updateAvailability(dialog);
return component;

layout.add(TargetAWT.wrap(component));
}

private void updateAvailability(BaseAnalysisActionDialog dialog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import consulo.language.editor.refactoring.move.MoveCallback;
import consulo.language.editor.refactoring.move.MoveHandlerDelegate;
import consulo.language.editor.refactoring.move.fileOrDirectory.MoveFilesOrDirectoriesUtil;
import consulo.language.editor.refactoring.ui.RadioUpDownListener;
import consulo.language.editor.refactoring.util.CommonRefactoringUtil;
import consulo.language.editor.ui.RadioUpDownListener;
import consulo.language.psi.PsiDirectory;
import consulo.language.psi.PsiElement;
import consulo.language.psi.PsiFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import consulo.language.editor.refactoring.localize.RefactoringLocalize;
import consulo.language.editor.refactoring.move.MoveCallback;
import consulo.language.editor.refactoring.move.MoveHandlerDelegate;
import consulo.language.editor.refactoring.ui.RadioUpDownListener;
import consulo.language.editor.ui.RadioUpDownListener;
import consulo.language.psi.PsiElement;
import consulo.language.psi.PsiReference;
import consulo.project.Project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import consulo.application.util.function.Computable;
import consulo.codeEditor.Editor;
import consulo.dataContext.DataContext;
import consulo.ide.impl.idea.analysis.AnalysisUIOptions;
import consulo.ide.impl.idea.analysis.BaseAnalysisActionDialog;
import consulo.ide.impl.idea.openapi.project.ProjectUtil;
import consulo.language.codeStyle.PostprocessReformattingAspect;
import consulo.language.editor.refactoring.ContextAwareActionHandler;
Expand All @@ -40,6 +38,8 @@
import consulo.language.editor.refactoring.localize.RefactoringLocalize;
import consulo.language.editor.refactoring.util.CommonRefactoringUtil;
import consulo.language.editor.scope.AnalysisScope;
import consulo.language.editor.ui.awt.scope.BaseAnalysisActionDialog;
import consulo.language.editor.ui.scope.AnalysisUIOptions;
import consulo.language.psi.PsiElement;
import consulo.language.psi.PsiFile;
import consulo.language.psi.PsiRecursiveElementVisitor;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void invoke(@Nonnull final Project project, final Editor editor, PsiFile
@Override
public void run(@Nonnull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
invokeOnScope(project, member, dlg.getScope(AnalysisUIOptions.getInstance(project), scope, project, module));
invokeOnScope(project, member, dlg.getScope(scope));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
package com.intellij.java.impl.slicer;

import consulo.language.editor.scope.AnalysisScope;
import consulo.ide.impl.idea.analysis.AnalysisUIOptions;
import consulo.ide.impl.idea.analysis.BaseAnalysisActionDialog;
import consulo.language.editor.ui.awt.scope.BaseAnalysisActionDialog;
import consulo.language.editor.ui.scope.AnalysisUIOptions;
import consulo.language.psi.PsiElement;
import consulo.module.Module;
import consulo.language.util.ModuleUtilCore;
import consulo.project.Project;
import consulo.language.psi.PsiElement;

import javax.swing.*;

Expand All @@ -36,7 +35,7 @@ public SliceForwardHandler() {
@Override
public SliceAnalysisParams askForParams(PsiElement element, boolean dataFlowToThis, SliceManager.StoredSettingsBean storedSettingsBean, String dialogTitle) {
AnalysisScope analysisScope = new AnalysisScope(element.getContainingFile());
Module module = ModuleUtilCore.findModuleForPsiElement(element);
Module module = element.getModule();
String name = module == null ? null : module.getName();

Project myProject = element.getProject();
Expand All @@ -60,7 +59,7 @@ protected JComponent getAdditionalActionSettings(Project project) {
storedSettingsBean.analysisUIOptions.save(analysisUIOptions);
storedSettingsBean.showDereferences = form.isToShowDerefs();

AnalysisScope scope = dialog.getScope(analysisUIOptions, analysisScope, myProject, module);
AnalysisScope scope = dialog.getScope(analysisScope);

SliceAnalysisParams params = new SliceAnalysisParams();
params.scope = scope;
Expand Down
Loading

0 comments on commit caf6ff7

Please sign in to comment.