From 9007758412bcb637e004fbf06f5394fb0f593713 Mon Sep 17 00:00:00 2001 From: Peter Gromov Date: Wed, 5 Feb 2020 20:33:43 +0100 Subject: [PATCH] IDEA-CR-53066 java smart completion: autoinsert likely generic methods GitOrigin-RevId: 6dcca0e0c39758de0a9c3bb77ded8b2c61ad4cf0 --- .../codeInsight/completion/JavaCompletionSorting.java | 6 +++++- .../AutoInsertMethodReturningClassTypeParam-out.java | 6 ++++++ .../smartType/AutoInsertMethodReturningClassTypeParam.java | 6 ++++++ .../codeInsight/completion/SmartTypeCompletionTest.java | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/AutoInsertMethodReturningClassTypeParam-out.java create mode 100644 java/java-tests/testData/codeInsight/completion/smartType/AutoInsertMethodReturningClassTypeParam.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java index c29df79dd17b..539c6785d272 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java @@ -501,7 +501,11 @@ public class JavaCompletionSorting { type = callItem.getSubstitutor().substitute(type); } - return type instanceof PsiClassType && ((PsiClassType)type).resolve() instanceof PsiTypeParameter; + if (type instanceof PsiClassType) { + PsiClass target = ((PsiClassType)type).resolve(); + return target instanceof PsiTypeParameter && ((PsiTypeParameter)target).getOwner() instanceof PsiMethod; + } + return false; } private static class PreferSimple extends LookupElementWeigher { diff --git a/java/java-tests/testData/codeInsight/completion/smartType/AutoInsertMethodReturningClassTypeParam-out.java b/java/java-tests/testData/codeInsight/completion/smartType/AutoInsertMethodReturningClassTypeParam-out.java new file mode 100644 index 000000000000..7c0c89abc323 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/AutoInsertMethodReturningClassTypeParam-out.java @@ -0,0 +1,6 @@ +abstract class K { + protected abstract T get(); + private void m() { + T t = get(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartType/AutoInsertMethodReturningClassTypeParam.java b/java/java-tests/testData/codeInsight/completion/smartType/AutoInsertMethodReturningClassTypeParam.java new file mode 100644 index 000000000000..032bc3096c7f --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/AutoInsertMethodReturningClassTypeParam.java @@ -0,0 +1,6 @@ +abstract class K { + protected abstract T get(); + private void m() { + T t = g + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionTest.java index 93b0d3f7ef5e..2b5ecb907db4 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionTest.java @@ -606,6 +606,8 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase { myFixture.assertPreferredCompletionItems(0, "valueOf"); } + public void testAutoInsertMethodReturningClassTypeParam() { doActionTest(); } + public void testIntPlusLongNotDouble() { doTest(); } public void testNestedAssignments() { doTest(); }