IDEA-121662 Unify Groovy and Java Intention behavior

This commit is contained in:
Max Medvedev
2014-03-06 17:31:35 +04:00
parent 82a4f0d0e6
commit cdfdf8d51e
4 changed files with 48 additions and 33 deletions
@@ -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<HighlightInfo> infos = GrUnresolvedAccessInspection.checkReferenceExpression(referenceExpression);
if (infos != null) {
assert myInfos != null;
myInfos.add(info);
myInfos.addAll(infos);
}
}
}
@@ -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 &&
@@ -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<HighlightInfo> checkReferenceExpression(GrReferenceExpression ref) {
List<HighlightInfo> 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<HighlightInfo> 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<HighlightInfo> 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,
@@ -1789,4 +1789,15 @@ public class Bar3 { }
''')
}
void testImportStaticFix() {
myFixture.configureByText('a.groovy', '''
class A {
static void foo(String s){}
}
foo(<caret>)
''')
myFixture.getAvailableIntention("Static Import Method 'A.foo'")
}
}