From 367fcd17a88317595d8c1ada7ae54235aa97fc02 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 16 Sep 2011 14:00:18 +0200 Subject: [PATCH] IDEA-73345 Completion does not propose generic parameters of a method until the header is complete --- .../completion/JavaCompletionData.java | 35 +++++++++++-------- .../JavaPsiClassReferenceElement.java | 6 +++- .../normal/UnfinishedMethodTypeParameter.java | 5 +++ .../UnfinishedMethodTypeParameter2.java | 5 +++ .../completion/NormalCompletionTest.groovy | 10 +++++- .../GroovyCompletionContributor.java | 23 ++++++++++++ .../completion/GroovyCompletionTest.groovy | 8 +++++ .../UnfinishedMethodTypeParameter.groovy | 3 ++ .../UnfinishedMethodTypeParameter2.groovy | 3 ++ 9 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/normal/UnfinishedMethodTypeParameter.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/UnfinishedMethodTypeParameter2.java create mode 100644 plugins/groovy/testdata/groovy/completion/UnfinishedMethodTypeParameter.groovy create mode 100644 plugins/groovy/testdata/groovy/completion/UnfinishedMethodTypeParameter2.groovy diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java index b67b7a9b0662..10567e4ff20f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java @@ -522,21 +522,7 @@ public class JavaCompletionData extends JavaAwareCompletionData{ result.addElement(createKeyword(position, PsiKeyword.CLASS)); } - final ProcessingContext context = new ProcessingContext(); - if (psiElement().afterLeaf( - psiElement().withText(">").withParent( - psiElement(PsiTypeParameterList.class).withParent(PsiErrorElement.class).save("typeParameterList"))).accepts(position, context)) { - final PsiTypeParameterList list = (PsiTypeParameterList)context.get("typeParameterList"); - PsiElement current = list.getParent().getParent(); - if (current instanceof PsiField) { - current = current.getParent(); - } - if (current instanceof PsiClass) { - for (PsiTypeParameter typeParameter : list.getTypeParameters()) { - result.addElement(new JavaPsiClassReferenceElement(typeParameter)); - } - } - } + addUnfinishedMethodTypeParameters(position, result); if (JavaSmartCompletionContributor.INSIDE_EXPRESSION.accepts(position) && !BasicExpressionCompletionContributor.AFTER_DOT.accepts(position) && @@ -553,6 +539,25 @@ public class JavaCompletionData extends JavaAwareCompletionData{ } } + private static void addUnfinishedMethodTypeParameters(PsiElement position, CompletionResultSet result) { + final ProcessingContext context = new ProcessingContext(); + if (psiElement().inside( + psiElement(PsiTypeElement.class).afterLeaf( + psiElement().withText(">").withParent( + psiElement(PsiTypeParameterList.class).withParent(PsiErrorElement.class).save("typeParameterList")))).accepts(position, context)) { + final PsiTypeParameterList list = (PsiTypeParameterList)context.get("typeParameterList"); + PsiElement current = list.getParent().getParent(); + if (current instanceof PsiField) { + current = current.getParent(); + } + if (current instanceof PsiClass) { + for (PsiTypeParameter typeParameter : list.getTypeParameters()) { + result.addElement(new JavaPsiClassReferenceElement(typeParameter)); + } + } + } + } + static boolean isAfterPrimitiveOrArrayType(PsiElement element) { return psiElement().withParent( psiReferenceExpression().withFirstChild( diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaPsiClassReferenceElement.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaPsiClassReferenceElement.java index 05d7f522ded9..4a78370e5c70 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaPsiClassReferenceElement.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaPsiClassReferenceElement.java @@ -74,7 +74,11 @@ public class JavaPsiClassReferenceElement extends LookupItem { final JavaPsiClassReferenceElement that = (JavaPsiClassReferenceElement)o; - return Comparing.equal(myQualifiedName, that.myQualifiedName); + if (myQualifiedName != null) { + return myQualifiedName.equals(that.myQualifiedName); + } + + return Comparing.equal(myClass, that.myClass); } public String getQualifiedName() { diff --git a/java/java-tests/testData/codeInsight/completion/normal/UnfinishedMethodTypeParameter.java b/java/java-tests/testData/codeInsight/completion/normal/UnfinishedMethodTypeParameter.java new file mode 100644 index 000000000000..9dfc817d7d91 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/UnfinishedMethodTypeParameter.java @@ -0,0 +1,5 @@ +import java.util.ArrayList; + +class A { + public static ArrayList> +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/normal/UnfinishedMethodTypeParameter2.java b/java/java-tests/testData/codeInsight/completion/normal/UnfinishedMethodTypeParameter2.java new file mode 100644 index 000000000000..c82489a04c64 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/UnfinishedMethodTypeParameter2.java @@ -0,0 +1,5 @@ +import java.util.ArrayList; + +class A { + public static My> +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy index d00936071fa9..bca25ee8d32a 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy @@ -917,7 +917,15 @@ public class NormalCompletionTest extends LightFixtureCompletionTestCase { public void testMethodParameterTypeDot() throws Throwable { doAntiTest() } public void testNewGenericClass() throws Throwable { doTest('\n') } public void testNewGenericInterface() throws Throwable { doTest() } - //public void testUnfinishedMethodTypeParameter() throws Throwable { doTest() } + + public void testUnfinishedMethodTypeParameter() throws Throwable { + configure() + assertStringItems("MyParameter", "MySecondParameter") + } + public void testUnfinishedMethodTypeParameter2() throws Throwable { + configure() + assertStringItems("MyParameter", "MySecondParameter") + } public void testSuperProtectedMethod() throws Throwable { myFixture.addClass """package foo; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionContributor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionContributor.java index d41996aa6e4f..cb7c85a56efb 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionContributor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/completion/GroovyCompletionContributor.java @@ -61,6 +61,8 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAc import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod; import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement; import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement; +import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement; +import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList; import org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils; import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil; import org.jetbrains.plugins.groovy.refactoring.inline.InlineMethodConflictSolver; @@ -287,6 +289,8 @@ public class GroovyCompletionContributor extends CompletionContributor { suggestVariableNames(position, result); + addUnfinishedMethodTypeParameters(position, result); + final PsiElement parent = position.getParent(); if (parent instanceof GrReferenceElement) { GrReferenceElement reference = (GrReferenceElement)parent; @@ -339,6 +343,25 @@ public class GroovyCompletionContributor extends CompletionContributor { } + private static void addUnfinishedMethodTypeParameters(PsiElement position, CompletionResultSet result) { + final ProcessingContext context = new ProcessingContext(); + if (PsiJavaPatterns.psiElement().inside( + PsiJavaPatterns.psiElement(GrTypeElement.class).afterLeaf( + PsiJavaPatterns.psiElement().withText(">").withParent( + PsiJavaPatterns.psiElement(GrTypeParameterList.class).withParent(PsiErrorElement.class).save("typeParameterList")))).accepts( + position, context)) { + final GrTypeParameterList list = (GrTypeParameterList)context.get("typeParameterList"); + PsiElement current = list.getParent().getParent(); + if (current instanceof PsiField) { + current = current.getParent(); + } + if (current instanceof GrTypeDefinitionBody) { + for (PsiTypeParameter typeParameter : list.getTypeParameters()) { + result.addElement(new JavaPsiClassReferenceElement(typeParameter)); + } + } + } + } @Override public void fillCompletionVariants(CompletionParameters parameters, CompletionResultSet result) { diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy index 6cce5f841bb3..7cd6f547c5fa 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy @@ -90,6 +90,14 @@ public class GroovyCompletionTest extends GroovyCompletionTestBase { doVariantableTest("hahaha", "hohoho"); } + public void testUnfinishedMethodTypeParameter() throws Throwable { + doVariantableTest("MyParameter", "MySecondParameter"); + } + + public void testUnfinishedMethodTypeParameter2() throws Throwable { + doVariantableTest("MyParameter", "MySecondParameter"); + } + public void testInstanceofHelpsDetermineType() throws Throwable { doBasicTest(); } diff --git a/plugins/groovy/testdata/groovy/completion/UnfinishedMethodTypeParameter.groovy b/plugins/groovy/testdata/groovy/completion/UnfinishedMethodTypeParameter.groovy new file mode 100644 index 000000000000..8fb6d048448c --- /dev/null +++ b/plugins/groovy/testdata/groovy/completion/UnfinishedMethodTypeParameter.groovy @@ -0,0 +1,3 @@ +class A { + public static ArrayList> +} \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/completion/UnfinishedMethodTypeParameter2.groovy b/plugins/groovy/testdata/groovy/completion/UnfinishedMethodTypeParameter2.groovy new file mode 100644 index 000000000000..e3b191a6d0d6 --- /dev/null +++ b/plugins/groovy/testdata/groovy/completion/UnfinishedMethodTypeParameter2.groovy @@ -0,0 +1,3 @@ +class A { + public static My +} \ No newline at end of file