From 9c616208230524a5a467ab907c32bdd04c4039cc Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 14 Jan 2020 14:28:44 +0300 Subject: [PATCH] fix IDEA-230545 'Search Accessors' dialog is shown simultaneously with 'Show Usages' results popup GitOrigin-RevId: 7ba3dfafc77c5ef1c18bd3dff6d82ee1ea2b5dfd --- .../findUsages/JavaFindUsagesHandler.java | 14 +++--- .../ide/util/SuperMethodWarningUtil.java | 49 ++++++++++--------- .../intellij/psi/util/PropertyUtilBase.java | 37 +++++--------- .../find/actions/ShowUsagesAction.java | 8 ++- .../src/messages/FindBundle.properties | 4 +- .../src/messages/IdeBundle.properties | 6 +-- 6 files changed, 56 insertions(+), 62 deletions(-) diff --git a/java/java-impl/src/com/intellij/find/findUsages/JavaFindUsagesHandler.java b/java/java-impl/src/com/intellij/find/findUsages/JavaFindUsagesHandler.java index 369bb2f944ee..7100e1d07e95 100644 --- a/java/java-impl/src/com/intellij/find/findUsages/JavaFindUsagesHandler.java +++ b/java/java-impl/src/com/intellij/find/findUsages/JavaFindUsagesHandler.java @@ -162,11 +162,10 @@ public class JavaFindUsagesHandler extends FindUsagesHandler{ final String propertyName = JavaCodeStyleManager.getInstance(getProject()).variableNameToPropertyName(fieldName, VariableKind.FIELD); Set accessors = new THashSet<>(); boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC); - PsiMethod getter = PropertyUtilBase.findPropertyGetterWithType(propertyName, isStatic, field.getType(), - ContainerUtil.iterate(containingClass.getMethods())); + Collection methods = Arrays.asList(containingClass.getMethods()); + PsiMethod getter = PropertyUtilBase.findPropertyGetterWithType(propertyName, isStatic, field.getType(), methods); if (getter != null) accessors.add(getter); - PsiMethod setter = PropertyUtilBase.findPropertySetterWithType(propertyName, isStatic, field.getType(), - ContainerUtil.iterate(containingClass.getMethods())); + PsiMethod setter = PropertyUtilBase.findPropertySetterWithType(propertyName, isStatic, field.getType(), methods); if (setter != null) accessors.add(setter); accessors.addAll(PropertyUtilBase.getAccessors(containingClass, fieldName)); accessors.removeIf(accessor -> field != PropertyUtilBase.findPropertyFieldByMember(accessor)); @@ -186,11 +185,11 @@ public class JavaFindUsagesHandler extends FindUsagesHandler{ return super.getSecondaryElements(); } - private static boolean askShouldSearchAccessors(String fieldName) { + private static boolean askShouldSearchAccessors(@NotNull String fieldName) { return Messages.showOkCancelDialog(FindBundle.message("find.field.accessors.prompt", fieldName), FindBundle.message("find.field.accessors.title"), - CommonBundle.getYesButtonText(), - CommonBundle.getNoButtonText(), Messages.getQuestionIcon()) == Messages.OK; + "&Include accessors", + "&Exclude accessors", Messages.getQuestionIcon()) == Messages.OK; } @Override @@ -265,6 +264,7 @@ public class JavaFindUsagesHandler extends FindUsagesHandler{ return super.findReferencesToHighlight(target, searchScope); } + @NotNull protected static String getActionString() { return FindBundle.message("find.super.method.warning.action.verb"); } diff --git a/java/java-impl/src/com/intellij/ide/util/SuperMethodWarningUtil.java b/java/java-impl/src/com/intellij/ide/util/SuperMethodWarningUtil.java index 6e869a102226..a37010911408 100644 --- a/java/java-impl/src/com/intellij/ide/util/SuperMethodWarningUtil.java +++ b/java/java-impl/src/com/intellij/ide/util/SuperMethodWarningUtil.java @@ -69,14 +69,15 @@ public class SuperMethodWarningUtil { parentInterface |= isInterface; } - switch (showDialog( + int shouldIncludeBase = showDialog( method.getProject(), DescriptiveNameUtil.getDescriptiveName(method), actionString, superAbstract, parentInterface, method.getContainingClass().isInterface(), - ArrayUtilRt.toStringArray(superClasses))) { + ArrayUtilRt.toStringArray(superClasses)); + switch (shouldIncludeBase) { case Messages.YES: return methodTargetCandidates; case Messages.NO: @@ -124,15 +125,16 @@ public class SuperMethodWarningUtil { PsiClass containingClass = superMethod.getContainingClass(); - switch (showDialog( - method.getProject(), - DescriptiveNameUtil.getDescriptiveName(method), - actionString, - containingClass.isInterface() || superMethod.hasModifierProperty(PsiModifier.ABSTRACT), - containingClass.isInterface(), - aClass.isInterface(), - containingClass.getQualifiedName() - )) { + int useSuperMethod = showDialog( + method.getProject(), + DescriptiveNameUtil.getDescriptiveName(method), + actionString, + containingClass.isInterface() || superMethod.hasModifierProperty(PsiModifier.ABSTRACT), + containingClass.isInterface(), + aClass.isInterface(), + containingClass.getQualifiedName() + ); + switch (useSuperMethod) { case Messages.YES: return superMethod; case Messages.NO: return method; default: return null; @@ -199,22 +201,25 @@ public class SuperMethodWarningUtil { @Messages.YesNoCancelResult private static int showDialog(@NotNull Project project, - @NotNull String name, - @NotNull String actionString, - boolean isSuperAbstract, - boolean isParentInterface, - boolean isContainedInInterface, - @NotNull String... classNames) { - return Messages.showYesNoCancelDialog(project, getDialogMessage(name, actionString, isSuperAbstract, isParentInterface, isContainedInInterface, classNames), "Method Overrides", Messages.getQuestionIcon()); + @NotNull String name, + @NotNull String actionString, + boolean isSuperAbstract, + boolean isParentInterface, + boolean isContainedInInterface, + @NotNull String... classNames) { + String message = getDialogMessage(name, actionString, isSuperAbstract, isParentInterface, isContainedInInterface, classNames); + return Messages.showYesNoCancelDialog(project, + message, "Super Method Found", "&Base method", "&Current method", + Messages.getCancelButton(), Messages.getQuestionIcon()); } @NotNull - private static String getDialogMessage(String name, - String actionString, + private static String getDialogMessage(@NotNull String name, + @NotNull String actionString, boolean isSuperAbstract, boolean isParentInterface, boolean isContainedInInterface, - String[] classNames) { + @NotNull String[] classNames) { StringBuilder labelText = new StringBuilder(); String classType = isParentInterface ? IdeBundle.message("element.of.interface") : IdeBundle.message("element.of.class"); String methodString = IdeBundle.message("element.method"); @@ -230,7 +235,7 @@ public class SuperMethodWarningUtil { labelText.append(IdeBundle.message("label.implements.method.of_interfaces")); for (final String className : classNames) { - labelText.append("
 ").append(className); + labelText.append("
 '").append(className).append("'"); } } diff --git a/java/java-psi-api/src/com/intellij/psi/util/PropertyUtilBase.java b/java/java-psi-api/src/com/intellij/psi/util/PropertyUtilBase.java index a0c6f5dea059..be5990b5611f 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/PropertyUtilBase.java +++ b/java/java-psi-api/src/com/intellij/psi/util/PropertyUtilBase.java @@ -217,38 +217,23 @@ public class PropertyUtilBase { return null; } - @Nullable - public static PsiMethod findPropertyGetterWithType(String propertyName, boolean isStatic, PsiType type, Iterator methods) { - while (methods.hasNext()) { - PsiMethod method = methods.next(); - if (method.hasModifierProperty(PsiModifier.STATIC) != isStatic) continue; - if (isSimplePropertyGetter(method)) { - if (getPropertyNameByGetter(method).equals(propertyName)) { - if (type.equals(method.getReturnType())) return method; - } - } - } - return null; + public static PsiMethod findPropertyGetterWithType(@NotNull String propertyName, boolean isStatic, PsiType type, @NotNull Collection methods) { + return ContainerUtil.find(methods, method -> + method.hasModifierProperty(PsiModifier.STATIC) == isStatic && + isSimplePropertyGetter(method) && + getPropertyNameByGetter(method).equals(propertyName) && + type.equals(method.getReturnType())); } public static boolean isSimplePropertyAccessor(PsiMethod method) { return isSimplePropertyGetter(method) || isSimplePropertySetter(method); } - @Nullable - public static PsiMethod findPropertySetterWithType(String propertyName, boolean isStatic, PsiType type, Iterator methods) { - while (methods.hasNext()) { - PsiMethod method = methods.next(); - if (method.hasModifierProperty(PsiModifier.STATIC) != isStatic) continue; - - if (isSimplePropertySetter(method)) { - if (getPropertyNameBySetter(method).equals(propertyName)) { - PsiType methodType = method.getParameterList().getParameters()[0].getType(); - if (type.equals(methodType)) return method; - } - } - } - return null; + public static PsiMethod findPropertySetterWithType(@NotNull String propertyName, boolean isStatic, PsiType type, @NotNull Collection methods) { + return ContainerUtil.find(methods, method-> + method.hasModifierProperty(PsiModifier.STATIC) == isStatic && + isSimplePropertySetter(method) && + getPropertyNameBySetter(method).equals(propertyName) && type.equals(method.getParameterList().getParameters()[0].getType())); } public enum GetterFlavour { diff --git a/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java b/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java index 49d98a7b237a..5bb9d80d6f8a 100644 --- a/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java +++ b/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java @@ -247,6 +247,10 @@ public class ShowUsagesAction extends AnAction implements PopupAction { boolean isPreviewMode = Boolean.TRUE == PreviewManager.SERVICE.preview(handler.getProject(), UsagesPreviewPanelProvider.ID, Pair.create(usageView, table), false); Runnable itemChosenCallback = table.prepareTable(editor, popupPosition, handler, maxUsages, options, isPreviewMode, this); + // show super method warning dialogs before starting finding usages + PsiElement[] primaryElements = handler.getPrimaryElements(); + PsiElement[] secondaryElements = handler.getSecondaryElements(); + JBPopup popup = isPreviewMode ? null : createUsagePopup(usages, visibleNodes, handler, editor, popupPosition, maxUsages, usageView, options, table, itemChosenCallback, presentation, processIcon, minWidth); if (popup != null) { @@ -312,8 +316,8 @@ public class ShowUsagesAction extends AnAction implements PopupAction { return true; }; - final ProgressIndicator indicator = FindUsagesManager.startProcessUsages(handler, handler.getPrimaryElements(), handler.getSecondaryElements(), collect, - options, ()-> ApplicationManager.getApplication().invokeLater(() -> { + final ProgressIndicator indicator = FindUsagesManager.startProcessUsages(handler, primaryElements, secondaryElements, collect, + options, ()-> ApplicationManager.getApplication().invokeLater(() -> { Disposer.dispose(processIcon); Container parent = processIcon.getParent(); if (parent != null) { diff --git a/platform/platform-resources-en/src/messages/FindBundle.properties b/platform/platform-resources-en/src/messages/FindBundle.properties index 55ce5ea0f57e..fed363b77c3a 100644 --- a/platform/platform-resources-en/src/messages/FindBundle.properties +++ b/platform/platform-resources-en/src/messages/FindBundle.properties @@ -43,8 +43,8 @@ cannot.search.for.usages.title=Cannot Search for Usages find.super.method.warning.action.verb=to find usages of find.parameter.usages.in.overriding.methods.prompt=Do you want to search usages of parameter ''{0}'' in overriding methods? find.parameter.usages.in.overriding.methods.title=Search in Overriding Methods -find.field.accessors.prompt=Do you want to search for accessors of ''{0}''? -find.field.accessors.title=Search Accessors +find.field.accessors.prompt=Search for accessors of field ''{0}''? +find.field.accessors.title=Field Accessors Found diff --git a/platform/platform-resources-en/src/messages/IdeBundle.properties b/platform/platform-resources-en/src/messages/IdeBundle.properties index 4fc1f7bac529..154ebae59690 100644 --- a/platform/platform-resources-en/src/messages/IdeBundle.properties +++ b/platform/platform-resources-en/src/messages/IdeBundle.properties @@ -909,13 +909,13 @@ checkbox.search.in.comments.and.strings=&Search in comments and strings checkbox.search.in.non.java.files=S&earch for non-code usages # label.method=Method ''{0}'' -label.overrides.method.of_class_or_interface.name=overrides {0} {1} {2}. -label.implements.method.of_class_or_interface.name=implements {0} {1} {2}. +label.overrides.method.of_class_or_interface.name=overrides {0} {1} ''{2}''. +label.implements.method.of_class_or_interface.name=implements {0} {1} ''{2}''. label.implements.method.of_interfaces=implements methods of the following classes/interfaces: element.method=method element.of.interface=of interface element.of.class=of class -prompt.do.you.want.to.action_verb.the.method.from_class=Do you want {0} the base {1,choice,1#method|2#methods}? +prompt.do.you.want.to.action_verb.the.method.from_class=Do you want {0} the base {1,choice,1#method|2#methods} instead? help.topic.not.found.error=Help topic \"{0}\" not found