From 2af541cd4fb2208ca6ace10efd3ddd5df509106c Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 28 Aug 2015 20:25:06 +0300 Subject: [PATCH] Refactor: splict logic to different methods --- .../codeInsight/GotoSuperActionHandler.java | 69 +++++++++++-------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/GotoSuperActionHandler.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/GotoSuperActionHandler.java index 367b229c84b..ad33e43306b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/GotoSuperActionHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/GotoSuperActionHandler.java @@ -32,7 +32,7 @@ import com.intellij.psi.util.PsiUtilCore; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; +import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.idea.JetBundle; import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage; @@ -63,8 +63,45 @@ public class GotoSuperActionHandler implements CodeInsightActionHandler { DeclarationDescriptor descriptor = ResolvePackage.resolveToDescriptor(declaration); + List superDeclarations = findSuperDeclarations(descriptor); + + if (superDeclarations.isEmpty()) return; + if (superDeclarations.size() == 1) { + Navigatable navigatable = EditSourceUtil.getDescriptor(superDeclarations.get(0)); + if (navigatable != null && navigatable.canNavigate()) { + navigatable.navigate(true); + } + } + else { + String message = getTitle(descriptor); + PsiElement[] superDeclarationsArray = PsiUtilCore.toPsiElementArray(superDeclarations); + JBPopup popup = descriptor instanceof ClassDescriptor + ? NavigationUtil.getPsiElementPopup(superDeclarationsArray, message) + : NavigationUtil.getPsiElementPopup(superDeclarationsArray, + new JetFunctionPsiElementCellRenderer(), message); + popup.showInBestPositionFor(editor); + } + } + + @Nullable + private static String getTitle(DeclarationDescriptor descriptor) { + if (descriptor instanceof ClassDescriptor) { + return JetBundle.message("goto.super.class.chooser.title"); + } + + if (descriptor instanceof PropertyDescriptor) { + return JetBundle.message("goto.super.property.chooser.title"); + } + + if (descriptor instanceof SimpleFunctionDescriptor) { + return JetBundle.message("goto.super.function.chooser.title"); + } + + return null; + } + + private static List findSuperDeclarations(DeclarationDescriptor descriptor) { Collection superDescriptors; - String message; if (descriptor instanceof ClassDescriptor) { Collection supertypes = ((ClassDescriptor) descriptor).getTypeConstructor().getSupertypes(); List superclasses = ContainerUtil.mapNotNull(supertypes, new Function() { @@ -79,23 +116,14 @@ public class GotoSuperActionHandler implements CodeInsightActionHandler { }); ContainerUtil.removeDuplicates(superclasses); superDescriptors = superclasses; - message = JetBundle.message("goto.super.class.chooser.title"); } else if (descriptor instanceof CallableMemberDescriptor) { superDescriptors = OverrideResolver.getDirectlyOverriddenDeclarations((CallableMemberDescriptor) descriptor); - if (descriptor instanceof PropertyDescriptor) { - message = JetBundle.message("goto.super.property.chooser.title"); - } - else if (descriptor instanceof SimpleFunctionDescriptor) { - message = JetBundle.message("goto.super.function.chooser.title"); - } - else - throw new IllegalStateException("Unknown member type: " + descriptor.getClass().getName()); } else - return; + return null; - List superDeclarations = ContainerUtil.mapNotNull(superDescriptors, new Function() { + return ContainerUtil.mapNotNull(superDescriptors, new Function() { @Override public PsiElement fun(DeclarationDescriptor descriptor) { if (descriptor instanceof ClassDescriptor && isAny((ClassDescriptor) descriptor)) { @@ -104,21 +132,6 @@ public class GotoSuperActionHandler implements CodeInsightActionHandler { return DescriptorToSourceUtils.descriptorToDeclaration(descriptor); } }); - if (superDeclarations.isEmpty()) return; - if (superDeclarations.size() == 1) { - Navigatable navigatable = EditSourceUtil.getDescriptor(superDeclarations.get(0)); - if (navigatable != null && navigatable.canNavigate()) { - navigatable.navigate(true); - } - } - else { - PsiElement[] superDeclarationsArray = PsiUtilCore.toPsiElementArray(superDeclarations); - JBPopup popup = descriptor instanceof ClassDescriptor - ? NavigationUtil.getPsiElementPopup(superDeclarationsArray, message) - : NavigationUtil.getPsiElementPopup(superDeclarationsArray, - new JetFunctionPsiElementCellRenderer(), message); - popup.showInBestPositionFor(editor); - } } @Override