From bbb0760dbb338c804243b8a73a2d19d4bd4a7f0e Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 6 Jun 2012 13:57:35 +0400 Subject: [PATCH] smart completion: diamonds: forbid non applicable diamonds based on the completion position (IDEA-87053) --- .../completion/JavaInheritorsGetter.java | 16 +++++++++------- .../DiamondNotCollapsedNotApplicable-out.java | 15 +++++++++++++++ .../DiamondNotCollapsedNotApplicable.java | 15 +++++++++++++++ .../completion/SmartType17CompletionTest.java | 4 ++++ 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/DiamondNotCollapsedNotApplicable-out.java create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/DiamondNotCollapsedNotApplicable.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java index bafe000392d2..18cccada0032 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java @@ -19,6 +19,7 @@ import com.intellij.codeInsight.CodeInsightUtil; import com.intellij.codeInsight.ExpectedTypeInfo; import com.intellij.codeInsight.daemon.impl.analysis.HighlightClassUtil; import com.intellij.codeInsight.lookup.*; +import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Condition; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; @@ -154,14 +155,15 @@ public class JavaInheritorsGetter extends CompletionProvider -1) { + final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(originalPosition, PsiMethodCallExpression.class); + if (methodCallExpression != null) { + final PsiNewExpression newExpression = PsiTreeUtil.getParentOfType(originalPosition, PsiNewExpression.class); + if (newExpression != null && ArrayUtil.find(methodCallExpression.getArgumentList().getExpressions(), newExpression) > -1 || + Comparing.equal(originalPosition.getParent(), methodCallExpression.getArgumentList())) { final JavaResolveResult resolveResult = methodCallExpression.resolveMethodGenerics(); - PsiMethod method = (PsiMethod)resolveResult.getElement(); - return method == null || PsiUtil.getApplicabilityLevel(method, resolveResult.getSubstitutor(), types, PsiUtil.getLanguageLevel(originalPosition)) + final PsiMethod method = (PsiMethod)resolveResult.getElement(); + return method == null || + PsiUtil.getApplicabilityLevel(method, resolveResult.getSubstitutor(), types, PsiUtil.getLanguageLevel(originalPosition)) != MethodCandidateInfo.ApplicabilityLevel.NOT_APPLICABLE; } } diff --git a/java/java-tests/testData/codeInsight/completion/smartType/DiamondNotCollapsedNotApplicable-out.java b/java/java-tests/testData/codeInsight/completion/smartType/DiamondNotCollapsedNotApplicable-out.java new file mode 100644 index 000000000000..618fd3388c0c --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/DiamondNotCollapsedNotApplicable-out.java @@ -0,0 +1,15 @@ +import java.util.ArrayList; + +public class TestCompletion { + public static void test() { + A> ref = new A<>(); + ref.set(new ArrayList() ); + } +} + +class A { + A() { + } + + void set(V v){} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartType/DiamondNotCollapsedNotApplicable.java b/java/java-tests/testData/codeInsight/completion/smartType/DiamondNotCollapsedNotApplicable.java new file mode 100644 index 000000000000..3d79a3392a8f --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/DiamondNotCollapsedNotApplicable.java @@ -0,0 +1,15 @@ +import java.util.ArrayList; + +public class TestCompletion { + public static void test() { + A> ref = new A<>(); + ref.set(new ); + } +} + +class A { + A() { + } + + void set(V v){} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType17CompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType17CompletionTest.java index 96dda9979256..cd531b3b91fa 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType17CompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartType17CompletionTest.java @@ -46,6 +46,10 @@ public class SmartType17CompletionTest extends LightFixtureCompletionTestCase { doTest(); } + public void testDiamondNotCollapsedNotApplicable() throws Exception { + doTest(); + } + public void testDiamondNotCollapsedInCaseOfAnonymousClasses() throws Exception { doTest(); }