From 6ac6e6712b3cd8c7e72162a6c91a60dc59d55094 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 3 Aug 2016 15:24:48 +0200 Subject: [PATCH] IDEA-157088 The autocomplete to offer not only the implementation of the interface, but the interface itself --- .../codeInsight/completion/JavaInheritorsGetter.java | 7 ++++++- .../normal/SuggestInterfaceArrayWhenObjectIsExpected.java | 5 +++++ .../codeInsight/completion/NormalCompletionTest.groovy | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/completion/normal/SuggestInterfaceArrayWhenObjectIsExpected.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 038b722f83f3..a935b1c66eba 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaInheritorsGetter.java @@ -83,11 +83,16 @@ public class JavaInheritorsGetter extends CompletionProvider { + List classTypes = extractClassTypes(infos); + boolean arraysWelcome = classTypes.stream().anyMatch(t -> t.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)); + processInheritors(parameters, classTypes, prefixMatcher, type -> { final LookupElement element = addExpectedType(type, parameters); if (element != null) { consumer.consume(element); } + if (arraysWelcome) { + consumer.consume(createNewArrayItem(parameters.getPosition(), type.createArrayType())); + } }); } diff --git a/java/java-tests/testData/codeInsight/completion/normal/SuggestInterfaceArrayWhenObjectIsExpected.java b/java/java-tests/testData/codeInsight/completion/normal/SuggestInterfaceArrayWhenObjectIsExpected.java new file mode 100644 index 000000000000..9a449e918869 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/SuggestInterfaceArrayWhenObjectIsExpected.java @@ -0,0 +1,5 @@ +interface MyIntf {} + +class C { + Object[] o = {new MyInt}; +} \ 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 fed893a48af9..ac7bf94e039e 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy @@ -1597,4 +1597,10 @@ class Bar { assert !LookupElementPresentation.renderElement(myFixture.lookup.items[2]).tailText assert LookupElementPresentation.renderElement(myFixture.lookup.items[3]).tailText == ' = 42' } + + public void testSuggestInterfaceArrayWhenObjectIsExpected() { + configure() + assert LookupElementPresentation.renderElement(myFixture.lookup.items[0]).tailText.contains('{...}') + assert LookupElementPresentation.renderElement(myFixture.lookup.items[1]).tailText.contains('[]') + } }