Search in sources :

Example 1 with ProximityLocation

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

the class PsiProximityComparator method getProximity.

@Nullable
public static WeighingComparable<PsiElement, ProximityLocation> getProximity(final Computable<PsiElement> elementComputable, final PsiElement context, ProcessingContext processingContext) {
    PsiElement element = elementComputable.compute();
    if (element == null)
        return null;
    if (element instanceof MetadataPsiElementBase)
        return null;
    if (context == null)
        return null;
    Module contextModule = processingContext.get(MODULE_BY_LOCATION);
    if (contextModule == null) {
        contextModule = ModuleUtilCore.findModuleForPsiElement(context);
        processingContext.put(MODULE_BY_LOCATION, contextModule);
    }
    if (contextModule == null)
        return null;
    return new WeighingComparable<>(elementComputable, new ProximityLocation(context, contextModule, processingContext), PROXIMITY_WEIGHERS);
}
Also used : MetadataPsiElementBase(com.intellij.extapi.psi.MetadataPsiElementBase) Module(com.intellij.openapi.module.Module) WeighingComparable(com.intellij.psi.WeighingComparable) PsiElement(com.intellij.psi.PsiElement) ProximityLocation(com.intellij.psi.util.ProximityLocation) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ProximityLocation

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

the class PsiProximityComparator method compare.

@Override
public int compare(final Object o1, final Object o2) {
    PsiElement element1 = o1 instanceof PsiElement ? (PsiElement) o1 : null;
    PsiElement element2 = o2 instanceof PsiElement ? (PsiElement) o2 : null;
    if (element1 == null)
        return element2 == null ? 0 : 1;
    if (element2 == null)
        return -1;
    if (myContext != null && myContextModule != null) {
        final ProximityLocation location = new ProximityLocation(myContext, myContextModule);
        StatisticsInfo info1 = StatisticsManager.serialize(STATISTICS_KEY, element1, location);
        StatisticsInfo info2 = StatisticsManager.serialize(STATISTICS_KEY, element2, location);
        if (info1 != null && info2 != null) {
            StatisticsManager statisticsManager = StatisticsManager.getInstance();
            int count1 = statisticsManager.getLastUseRecency(info1);
            int count2 = statisticsManager.getLastUseRecency(info2);
            if (count1 != count2) {
                return count1 < count2 ? -1 : 1;
            }
        }
    }
    final WeighingComparable<PsiElement, ProximityLocation> proximity1 = myProximities.get(element1);
    final WeighingComparable<PsiElement, ProximityLocation> proximity2 = myProximities.get(element2);
    if (proximity1 == null || proximity2 == null) {
        return 0;
    }
    return -proximity1.compareTo(proximity2);
}
Also used : StatisticsInfo(com.intellij.psi.statistics.StatisticsInfo) StatisticsManager(com.intellij.psi.statistics.StatisticsManager) PsiElement(com.intellij.psi.PsiElement) ProximityLocation(com.intellij.psi.util.ProximityLocation)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 ProximityLocation (com.intellij.psi.util.ProximityLocation)2 MetadataPsiElementBase (com.intellij.extapi.psi.MetadataPsiElementBase)1 Module (com.intellij.openapi.module.Module)1 WeighingComparable (com.intellij.psi.WeighingComparable)1 StatisticsInfo (com.intellij.psi.statistics.StatisticsInfo)1 StatisticsManager (com.intellij.psi.statistics.StatisticsManager)1 Nullable (org.jetbrains.annotations.Nullable)1