Search in sources :

Example 1 with PsiProximityComparator

use of com.intellij.psi.util.proximity.PsiProximityComparator in project intellij-community by JetBrains.

the class GroovyStaticImportMethodFix method getMethodsToImport.

@NotNull
private List<PsiMethod> getMethodsToImport() {
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(myMethodCall.getProject());
    GrMethodCall element = myMethodCall.getElement();
    LOG.assertTrue(element != null);
    GrReferenceExpression reference = getMethodExpression(element);
    LOG.assertTrue(reference != null);
    GrArgumentList argumentList = element.getArgumentList();
    String name = reference.getReferenceName();
    ArrayList<PsiMethod> list = new ArrayList<>();
    if (name == null)
        return list;
    GlobalSearchScope scope = element.getResolveScope();
    PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, 20);
    List<PsiMethod> applicableList = new ArrayList<>();
    for (PsiMethod method : methods) {
        ProgressManager.checkCanceled();
        if (JavaCompletionUtil.isInExcludedPackage(method, false))
            continue;
        if (!method.hasModifierProperty(PsiModifier.STATIC))
            continue;
        PsiFile file = method.getContainingFile();
        if (file instanceof PsiClassOwner && //do not show methods from default package
        !((PsiClassOwner) file).getPackageName().isEmpty() && PsiUtil.isAccessible(element, method)) {
            list.add(method);
            if (PsiUtil.isApplicable(PsiUtil.getArgumentTypes(element, true), method, PsiSubstitutor.EMPTY, element, false)) {
                applicableList.add(method);
            }
        }
    }
    List<PsiMethod> result = applicableList.isEmpty() ? list : applicableList;
    Collections.sort(result, new PsiProximityComparator(argumentList));
    return result;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) ArrayList(java.util.ArrayList) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiProximityComparator(com.intellij.psi.util.proximity.PsiProximityComparator) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 PsiShortNamesCache (com.intellij.psi.search.PsiShortNamesCache)1 PsiProximityComparator (com.intellij.psi.util.proximity.PsiProximityComparator)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1