mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-202262 Wrong cursor position, when IDEA autocompletes List.of(), Set.of() and other Java 9 factory methods for Collections
This commit is contained in:
@@ -64,6 +64,7 @@ public class JavaCompletionSorting {
|
||||
ContainerUtil.addIfNotNull(afterProximity, PreferMostUsedWeigher.create(position));
|
||||
afterProximity.add(new PreferContainingSameWords(expectedTypes));
|
||||
afterProximity.add(new PreferShorter(expectedTypes));
|
||||
afterProximity.add(new DispreferTechnicalOverloads(position));
|
||||
|
||||
CompletionSorter sorter = CompletionSorter.defaultSorter(parameters, result.getPrefixMatcher());
|
||||
if (!smart && afterNew) {
|
||||
@@ -608,6 +609,42 @@ public class JavaCompletionSorting {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sometimes there's core vararg method and a couple of overloads of fixed arity to avoid runtime invocation costs of varargs.
|
||||
* We prefer the vararg method then.
|
||||
*/
|
||||
private static class DispreferTechnicalOverloads extends LookupElementWeigher {
|
||||
private final PsiElement myPlace;
|
||||
|
||||
DispreferTechnicalOverloads(PsiElement place) {
|
||||
super("technicalOverloads");
|
||||
myPlace = place;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Comparable weigh(@NotNull LookupElement element) {
|
||||
Object object = element.getObject();
|
||||
if (object instanceof PsiMethod && element.getUserData(JavaCompletionUtil.FORCE_SHOW_SIGNATURE_ATTR) == null) {
|
||||
PsiMethod method = (PsiMethod)object;
|
||||
PsiClass containingClass = method.getContainingClass();
|
||||
if (!method.isVarArgs() &&
|
||||
containingClass != null &&
|
||||
ContainerUtil.exists(containingClass.findMethodsByName(method.getName(), false), m -> isPurelyVarargOverload(method, m))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isPurelyVarargOverload(PsiMethod original, PsiMethod candidate) {
|
||||
return candidate.hasModifierProperty(PsiModifier.STATIC) == original.hasModifierProperty(PsiModifier.STATIC) &&
|
||||
candidate.isVarArgs() &&
|
||||
candidate.getParameterList().getParametersCount() == 1 &&
|
||||
PsiResolveHelper.SERVICE.getInstance(candidate.getProject()).isAccessible(candidate, myPlace, null);
|
||||
}
|
||||
}
|
||||
|
||||
private static class LiftShorterClasses extends ClassifierFactory<LookupElement> {
|
||||
final ProjectFileIndex fileIndex;
|
||||
private final PsiElement myPosition;
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
class C {
|
||||
void method() {}
|
||||
void method(String s) {}
|
||||
void method(String... s) {}
|
||||
void method1(String... s) {}
|
||||
|
||||
{
|
||||
me<caret>
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
class C {
|
||||
void method() {}
|
||||
void method(String s) {}
|
||||
void method(String... s) {}
|
||||
void method1(String... s) {}
|
||||
|
||||
{
|
||||
method(<caret>);
|
||||
}
|
||||
}
|
||||
+2
@@ -1892,4 +1892,6 @@ class Abc {
|
||||
|
||||
void testNoSuggestionsAfterEnumConstant() { doAntiTest() }
|
||||
|
||||
void testPutCaretInsideParensInFixedPlusVarargOverloads() { doTest('\n') }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user