From cdfdf8d51e2d039eedab19e42e77f48766f6871a Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Thu, 6 Mar 2014 13:02:35 +0400 Subject: [PATCH] IDEA-121662 Unify Groovy and Java Intention behavior --- .../annotator/GrReferenceHighlighter.java | 31 +++++++++---------- .../GroovyStaticImportMethodFix.java | 8 ++--- .../GrUnresolvedAccessInspection.java | 31 ++++++++++++------- .../GroovyHighlightingTest.groovy | 11 +++++++ 4 files changed, 48 insertions(+), 33 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java index 4d23980e30ac..78ae905b5850 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,29 +67,26 @@ public class GrReferenceHighlighter extends TextEditorHighlightingPass { final TextAttributesKey attribute = GrHighlightUtil.getDeclarationHighlightingAttribute(variable, null); if (attribute != null) { final PsiElement nameElement = variable.getNameIdentifierGroovy(); - assert myInfos != null; - HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.INFORMATION).range(nameElement); - HighlightInfo info = builder.needsUpdateOnTyping(false).textAttributes(attribute).create(); - if (info != null) { - myInfos.add(info); - } + addInfo(attribute, nameElement); } } } - private void visit(GrReferenceElement element) { ProgressManager.checkCanceled(); final PsiElement resolved = element.resolve(); final TextAttributesKey attribute = GrHighlightUtil.getDeclarationHighlightingAttribute(resolved, element); if (attribute != null) { final PsiElement refNameElement = GrHighlightUtil.getElementToHighlight(element); - assert myInfos != null; - HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.INFORMATION).range(refNameElement); - HighlightInfo info = builder.needsUpdateOnTyping(false).textAttributes(attribute).create(); - if (info != null) { - myInfos.add(info); - } + addInfo(attribute, refNameElement); + } + } + + private void addInfo(TextAttributesKey attribute, PsiElement nameElement) { + assert myInfos != null; + HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.INFORMATION).range(nameElement).needsUpdateOnTyping(false).textAttributes(attribute).create(); + if (info != null) { + myInfos.add(info); } } }; @@ -100,10 +97,10 @@ public class GrReferenceHighlighter extends TextEditorHighlightingPass { final int size = myInfos.size(); super.visitReferenceExpression(referenceExpression); if (size == myInfos.size()) { - HighlightInfo info = GrUnresolvedAccessInspection.checkReferenceExpression(referenceExpression); - if (info != null) { + List infos = GrUnresolvedAccessInspection.checkReferenceExpression(referenceExpression); + if (infos != null) { assert myInfos != null; - myInfos.add(info); + myInfos.addAll(infos); } } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/GroovyStaticImportMethodFix.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/GroovyStaticImportMethodFix.java index d5b5132f0b9e..8b17f9c4d047 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/GroovyStaticImportMethodFix.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/GroovyStaticImportMethodFix.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import com.intellij.psi.*; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.search.PsiShortNamesCache; import com.intellij.psi.util.PsiFormatUtil; +import com.intellij.psi.util.PsiFormatUtilBase; import com.intellij.psi.util.proximity.PsiProximityComparator; import com.intellij.ui.components.JBList; import com.intellij.util.IncorrectOperationException; @@ -65,7 +66,7 @@ public class GroovyStaticImportMethodFix extends Intention { public String getText() { String text = "Static Import Method"; if (getCandidates().size() == 1) { - final int options = PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_CONTAINING_CLASS | PsiFormatUtil.SHOW_FQ_NAME; + final int options = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_FQ_NAME; text += " '" + PsiFormatUtil.formatMethod(getCandidates().get(0), PsiSubstitutor.EMPTY, options, 0) + "'"; } else { @@ -87,8 +88,7 @@ public class GroovyStaticImportMethodFix extends Intention { public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { myCandidates = null; - return myMethodCall != null && - myMethodCall.getElement() != null && + return myMethodCall.getElement() != null && myMethodCall.getElement().isValid() && getMethodExpression(myMethodCall.getElement()) != null && getMethodExpression(myMethodCall.getElement()).getQualifierExpression() == null && diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/untypedUnresolvedAccess/GrUnresolvedAccessInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/untypedUnresolvedAccess/GrUnresolvedAccessInspection.java index 7259fa7cff67..6b58f99d0ddc 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/untypedUnresolvedAccess/GrUnresolvedAccessInspection.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/untypedUnresolvedAccess/GrUnresolvedAccessInspection.java @@ -88,6 +88,9 @@ import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; import org.jetbrains.plugins.groovy.util.LightCacheKey; import javax.swing.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Map; import static com.intellij.psi.PsiModifier.STATIC; @@ -173,9 +176,9 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo } @Nullable - public static HighlightInfo checkReferenceExpression(GrReferenceExpression ref) { - HighlightInfo info = checkRefInner(ref); - addEmptyIntentionIfNeeded(info); + public static List checkReferenceExpression(GrReferenceExpression ref) { + List info = checkRefInner(ref); + addEmptyIntentionIfNeeded(ContainerUtil.getFirstItem(info)); return info; } @@ -251,7 +254,7 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo } @Nullable - private static HighlightInfo checkRefInner(GrReferenceExpression ref) { + private static List checkRefInner(GrReferenceExpression ref) { PsiElement refNameElement = ref.getReferenceNameElement(); if (refNameElement == null) return null; @@ -263,7 +266,7 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo if (!isStaticOk(resolveResult)) { String message = GroovyBundle.message("cannot.reference.non.static", ref.getReferenceName()); - return createAnnotationForRef(ref, inStaticContext, message); + return Collections.singletonList(createAnnotationForRef(ref, inStaticContext, message)); } return null; @@ -288,9 +291,11 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo HighlightInfo info = createAnnotationForRef(ref, inStaticContext, GroovyBundle.message("cannot.resolve", ref.getReferenceName())); if (info == null) return null; + ArrayList result = ContainerUtil.newArrayList(); + result.add(info); HighlightDisplayKey displayKey = HighlightDisplayKey.find(SHORT_NAME); if (ref.getParent() instanceof GrMethodCall) { - registerStaticImportFix(ref, info, displayKey); + ContainerUtil.addIfNotNull(result, registerStaticImportFix(ref, displayKey)); } else { registerCreateClassByTypeFix(ref, info, displayKey); @@ -300,7 +305,7 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo registerReferenceFixes(ref, info, inStaticContext, displayKey); UnresolvedReferenceQuickFixProvider.registerReferenceFixes(ref, new QuickFixActionRegistrarAdapter(info, displayKey)); OrderEntryFix.registerFixes(new QuickFixActionRegistrarAdapter(info, displayKey), ref); - return info; + return result; } return null; @@ -534,14 +539,16 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo return HighlightInfo.newHighlightInfo(highlightInfoType).range(refNameElement).descriptionAndTooltip(message).create(); } - private static void registerStaticImportFix(@NotNull GrReferenceExpression referenceExpression, - @Nullable HighlightInfo info, - @Nullable final HighlightDisplayKey key) { + private static HighlightInfo registerStaticImportFix(@NotNull GrReferenceExpression referenceExpression, + @Nullable final HighlightDisplayKey key) { final String referenceName = referenceExpression.getReferenceName(); - if (StringUtil.isEmpty(referenceName)) return; - if (referenceExpression.getQualifier() != null) return; + if (StringUtil.isEmpty(referenceName)) return null; + if (referenceExpression.getQualifier() != null) return null; + HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.INFORMATION).range( + referenceExpression.getParent()).createUnconditionally(); QuickFixAction.registerQuickFixAction(info, new GroovyStaticImportMethodFix((GrMethodCall)referenceExpression.getParent()), key); + return info; } private static void registerReferenceFixes(GrReferenceExpression refExpr, diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy index 0405449ee0f5..0b6f00de9d2e 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy @@ -1789,4 +1789,15 @@ public class Bar3 { } ''') } + void testImportStaticFix() { + myFixture.configureByText('a.groovy', ''' +class A { + static void foo(String s){} +} + +foo() +''') + + myFixture.getAvailableIntention("Static Import Method 'A.foo'") + } } \ No newline at end of file