From 1fec75c35a898ba6a7108b72c972922d61d51047 Mon Sep 17 00:00:00 2001 From: "peter.gromov" Date: Mon, 8 Nov 2010 17:55:05 +0300 Subject: [PATCH] suggest the just appeared type variables when writing a method return type (IDEA-60681) --- .../completion/JavaCompletionData.java | 17 +++++++++++++++++ .../normal/ReturningTypeVariable.java | 3 +++ .../normal/ReturningTypeVariable2.java | 3 +++ .../normal/ReturningTypeVariable2_after.java | 3 +++ .../normal/ReturningTypeVariable3.java | 3 +++ .../normal/ReturningTypeVariable3_after.java | 3 +++ .../normal/ReturningTypeVariable_after.java | 3 +++ .../completion/NormalCompletionTest.groovy | 3 +++ .../intellij/patterns/TreeElementPattern.java | 14 ++++++++++++++ 9 files changed, 52 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable2.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable2_after.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable3.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable3_after.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable_after.java 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 d6f982f22825..45a7965df590 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java @@ -37,6 +37,7 @@ import com.intellij.psi.jsp.JspElementType; import com.intellij.psi.scope.ElementClassFilter; import com.intellij.psi.templateLanguages.OuterLanguageElement; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.ProcessingContext; import org.jetbrains.annotations.NonNls; import static com.intellij.patterns.PsiJavaPatterns.*; @@ -598,6 +599,22 @@ public class JavaCompletionData extends JavaAwareCompletionData{ } } + 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)); + } + } + } + } private static LookupElement createKeyword(PsiElement position, String keyword) { diff --git a/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable.java b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable.java new file mode 100644 index 000000000000..49ee189e13f2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable.java @@ -0,0 +1,3 @@ +public class Beda { + public Tooo +} diff --git a/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable2.java b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable2.java new file mode 100644 index 000000000000..9dd705997abc --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable2.java @@ -0,0 +1,3 @@ +public class Beda { + public Tooo x +} diff --git a/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable2_after.java b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable2_after.java new file mode 100644 index 000000000000..4f846ea53dd7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable2_after.java @@ -0,0 +1,3 @@ +public class Beda { + public Toooo x +} diff --git a/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable3.java b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable3.java new file mode 100644 index 000000000000..675ce20a630d --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable3.java @@ -0,0 +1,3 @@ +public class Beda { + public Tooo a() +} diff --git a/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable3_after.java b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable3_after.java new file mode 100644 index 000000000000..c382a57c858a --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable3_after.java @@ -0,0 +1,3 @@ +public class Beda { + public Toooo a() +} diff --git a/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable_after.java b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable_after.java new file mode 100644 index 000000000000..99044b6b2616 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/ReturningTypeVariable_after.java @@ -0,0 +1,3 @@ +public class Beda { + public Toooo +} 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 b8ebfd0ea1db..30f7cdb853d1 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy @@ -597,6 +597,9 @@ public class NormalCompletionTest extends LightFixtureCompletionTestCase { } public void testNoSemicolonAfterExistingParenthesesEspeciallyIfItsACast() throws Throwable { doTest(); } + public void testReturningTypeVariable() throws Throwable { doTest(); } + public void testReturningTypeVariable2() throws Throwable { doTest(); } + public void testReturningTypeVariable3() throws Throwable { doTest(); } public void testCaseTailType() throws Throwable { doTest(); } diff --git a/platform/platform-api/src/com/intellij/patterns/TreeElementPattern.java b/platform/platform-api/src/com/intellij/patterns/TreeElementPattern.java index c4fb4a387d62..1eda3eef4ec1 100644 --- a/platform/platform-api/src/com/intellij/patterns/TreeElementPattern.java +++ b/platform/platform-api/src/com/intellij/patterns/TreeElementPattern.java @@ -173,4 +173,18 @@ public abstract class TreeElementPattern pattern) { + return with(new PatternCondition("afterSibling") { + @Override + public boolean accepts(@NotNull T t, ProcessingContext context) { + final ParentType parent = getParent(t); + if (parent == null) return false; + final ParentType[] children = getChildren(parent); + final int i = Arrays.asList(children).indexOf(t); + if (i <= 0) return false; + return pattern.accepts(children[i - 1], context); + } + }); + } }