IDEA-156379 Suggested "collect(Collectors.toList())" ignores static Collectors import

This commit is contained in:
peter
2016-05-24 15:21:50 +02:00
parent 817581a5ba
commit dc64af7f22
4 changed files with 38 additions and 6 deletions
@@ -78,14 +78,14 @@ class CollectConversion {
}
if (listType != null) {
consumer.consume(new MyLookupElement("toList", listType));
consumer.consume(new MyLookupElement("toList", listType, ref));
}
if (setType != null) {
consumer.consume(new MyLookupElement("toSet", setType));
consumer.consume(new MyLookupElement("toSet", setType, ref));
}
if (expectedTypes.isEmpty() || hasIterable) {
consumer.consume(new MyLookupElement("toCollection", factory.createType(collection, component)));
consumer.consume(new MyLookupElement("toCollection", factory.createType(collection, component), ref));
}
}
@@ -94,12 +94,22 @@ class CollectConversion {
private final String myTypeText;
private final String myMethodName;
@NotNull private final PsiType myExpectedType;
private final boolean myHasImport;
MyLookupElement(String methodName, @NotNull PsiType expectedType) {
MyLookupElement(String methodName, @NotNull PsiType expectedType, @NotNull PsiElement context) {
myMethodName = methodName;
myExpectedType = expectedType;
myLookupString = "collect(Collectors." + myMethodName + "())";
myTypeText = myExpectedType.getPresentableText();
PsiMethodCallExpression call = (PsiMethodCallExpression)
JavaPsiFacade.getElementFactory(context.getProject()).createExpressionFromText(methodName + "()", context);
myHasImport = ContainerUtil.or(call.getMethodExpression().multiResolve(true), result -> {
PsiElement element = result.getElement();
return element instanceof PsiMember &&
(JAVA_UTIL_STREAM_COLLECTORS + "." + myMethodName).equals(PsiUtil.getMemberQualifiedName((PsiMember)element));
});
myLookupString = "collect(" + (myHasImport ? "" : "Collectors.") + myMethodName + "())";
}
@NotNull
@@ -143,7 +153,7 @@ class CollectConversion {
@NotNull
private String getInsertString() {
return "collect(" + JAVA_UTIL_STREAM_COLLECTORS + "." + myMethodName + "())";
return "collect(" + (myHasImport ? "" : JAVA_UTIL_STREAM_COLLECTORS + ".") + myMethodName + "())";
}
@Override
@@ -0,0 +1,8 @@
import java.util.*;
import static java.util.stream.Collectors.*;
class Foo {
void m() {
List<CharSequence> l = Arrays.asList("a", "b").stream().colle<caret>
}
}
@@ -0,0 +1,8 @@
import java.util.*;
import static java.util.stream.Collectors.*;
class Foo {
void m() {
List<CharSequence> l = Arrays.asList("a", "b").stream().collect(toList());<caret>
}
}
@@ -222,6 +222,12 @@ class Test88 {
checkResultByFile(getTestName(false) + "_after.java")
}
public void testStaticallyImportedCollectorsToList() {
configureByTestName()
selectItem(myItems.find { it.lookupString.contains('collect(toList())') })
checkResultByFile(getTestName(false) + "_after.java")
}
public void testAllCollectors() {
configureByTestName()
myFixture.assertPreferredCompletionItems 0, 'collect', 'collect', 'collect(Collectors.toCollection())', 'collect(Collectors.toList())', 'collect(Collectors.toSet())'