java: add static import: don't pretend only one method is available when popup would be shown afterwards (IDEA-223655)

only applicable method is returned from `getMembersToImport(false, StaticMembersProcessor.SearchMode.MAX_2_MEMBERS)` when one of it is applicable and another is not. At the same time when we already calculate 100 first applicable methods, we can just reuse them instead the recalculation

GitOrigin-RevId: e31fe962bd8180850e8e5c06aa1f93efb7654aa5
This commit is contained in:
Anna Kozlova
2021-04-29 07:38:21 +00:00
committed by intellij-monorepo-bot
parent e32a42a8a0
commit 11606b6f47
3 changed files with 87 additions and 6 deletions
@@ -45,15 +45,15 @@ public abstract class StaticImportMemberFix<T extends PsiMember, R extends PsiEl
private final List<T> candidates;
protected final SmartPsiElementPointer<R> myRef;
@SuppressWarnings("AbstractMethodCallInConstructor")
StaticImportMemberFix(@NotNull PsiFile file, @NotNull R reference) {
myRef = SmartPointerManager.getInstance(file.getProject()).createSmartPsiElementPointer(reference);
// search for suitable candidates here, in the background thread
//noinspection AbstractMethodCallInConstructor
candidates = getMembersToImport(false, StaticMembersProcessor.SearchMode.MAX_2_MEMBERS);
List<T> applicableCandidates = getMembersToImport(true, StaticMembersProcessor.SearchMode.MAX_100_MEMBERS);
candidates = !applicableCandidates.isEmpty() ? applicableCandidates
: getMembersToImport(false, StaticMembersProcessor.SearchMode.MAX_2_MEMBERS);
//noinspection AbstractMethodCallInConstructor
myApplicableCandidates = ContainerUtil.map(getMembersToImport(true, StaticMembersProcessor.SearchMode.MAX_100_MEMBERS),
SmartPointerManager::createPointer);
myApplicableCandidates = ContainerUtil.map(applicableCandidates, SmartPointerManager::createPointer);
}
@NotNull
@@ -116,7 +116,9 @@ public abstract class StaticImportMemberFix<T extends PsiMember, R extends PsiEl
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
ApplicationManager.getApplication().runWriteAction(() -> {
final List<T> methodsToImport = getMembersToImport(false, StaticMembersProcessor.SearchMode.MAX_100_MEMBERS);
List<T> applicableCandidates = ContainerUtil.mapNotNull(myApplicableCandidates, SmartPsiElementPointer::getElement);
final List<T> methodsToImport = !applicableCandidates.isEmpty() ? applicableCandidates
: getMembersToImport(false, StaticMembersProcessor.SearchMode.MAX_100_MEMBERS);
if (methodsToImport.isEmpty()) return;
createQuestionAction(methodsToImport, project, editor).execute();
});
@@ -0,0 +1,41 @@
// "Import static method..." "true"
package p;
import static p.FFF.myEqualTo;
public class X {
public void test() throws Exception {
assertMe("", myEqualTo(""));
}
<V> void assertMe(V v, M<V> m) {
}
}
class M<T> {
}
class FFF {
public static <T> M<T> myEqualTo(T operand) {
return null;
}
}
class LLL {
public static M<String> myEqualTo(int string) {
return null;
}
}
class XXX {
public static <T> M<T> myEqualTo(T operand) {
return null;
}
}
class YYY {
public static <T> M<T> myEqualTo(T operand) {
return null;
}
}
@@ -0,0 +1,38 @@
// "Import static method..." "true"
package p;
public class X {
public void test() throws Exception {
assertMe("", my<caret>EqualTo(""));
}
<V> void assertMe(V v, M<V> m) {
}
}
class M<T> {
}
class FFF {
public static <T> M<T> myEqualTo(T operand) {
return null;
}
}
class LLL {
public static M<String> myEqualTo(int string) {
return null;
}
}
class XXX {
public static <T> M<T> myEqualTo(T operand) {
return null;
}
}
class YYY {
public static <T> M<T> myEqualTo(T operand) {
return null;
}
}