private static int compareMembers(PsiMember m1, PsiMember m2, PsiExpression context) { ProgressManager.checkCanceled(); int result = JavaStatisticsManager.createInfo(null, m2).getUseCount() - JavaStatisticsManager.createInfo(null, m1).getUseCount(); if (result != 0) return result; final PsiClass aClass = m1.getContainingClass(); final PsiClass bClass = m2.getContainingClass(); if (aClass != null && bClass != null) { result = JavaStatisticsManager.createInfo(null, bClass).getUseCount() - JavaStatisticsManager.createInfo(null, aClass).getUseCount(); if (result != 0) return result; } WeighingComparable<PsiElement,ProximityLocation> proximity1 = PsiProximityComparator.getProximity(m1, context); WeighingComparable<PsiElement,ProximityLocation> proximity2 = PsiProximityComparator.getProximity(m2, context); if (proximity1 != null && proximity2 != null) { result = proximity2.compareTo(proximity1); if (result != 0) return result; } String name1 = PsiUtil.getMemberQualifiedName(m1); String name2 = PsiUtil.getMemberQualifiedName(m2); return Comparing.compare(name1, name2); }
@Nullable protected Preference getPreferredCondition(@NotNull ProximityLocation location) { PsiElement position = location.getPosition(); if (INSIDE_REFERENCE_LIST.accepts(position)) { assert position != null; PsiReferenceList list = (PsiReferenceList)position.getParent().getParent(); PsiReferenceList.Role role = list.getRole(); if (shouldContainInterfaces(list, role)) { return Preference.Interfaces; } if (role == PsiReferenceList.Role.EXTENDS_LIST) { return Preference.Classes; } if (role == PsiReferenceList.Role.THROWS_LIST) { return Preference.Exceptions; } } return null; }
@Override public Integer weigh(@NotNull PsiElement element, @NotNull ProximityLocation location) { if (element instanceof PsiClass) { Preference condition = getPreferredCondition(location); PsiClass aClass = (PsiClass)element; if (condition == Preference.Interfaces) return aClass.isInterface() ? 1 : -1; if (condition == Preference.Classes) { if (aClass.isInterface()) return -1; if (aClass.getName().endsWith("TestCase")) { VirtualFile vFile = aClass.getContainingFile().getVirtualFile(); if (vFile != null && ProjectFileIndex.SERVICE.getInstance(location.getProject()).isInTestSourceContent(vFile)) { return 2; } } return 1; } if (condition == Preference.Exceptions) return InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_LANG_THROWABLE) ? 1 : -1; } return 0; }
@Override public Comparable weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) { PsiElement position = location.getPosition(); if (position == null){ return null; } if (!INSIDE_PSI_MEMBER.getValue(location)) { return 0; } if (element.isPhysical()) { position = PHYSICAL_POSITION.getValue(location); } final PsiMember member = PsiTreeUtil.getContextOfType(PsiTreeUtil.findCommonContext(position, element), PsiMember.class, false); if (member instanceof PsiClass) return 1; if (member != null) return 2; return 0; }
@NotNull @Override public Set<String> fun(ProximityLocation location) { final HashSet<String> result = new HashSet<String>(); PsiClass contextClass = PsiTreeUtil.getContextOfType(location.getPosition(), PsiClass.class, false); Processor<PsiClass> processor = new Processor<PsiClass>() { @Override public boolean process(PsiClass psiClass) { ContainerUtilRt.addIfNotNull(result, psiClass.getQualifiedName()); return true; } }; while (contextClass != null) { InheritanceUtil.processSupers(contextClass, true, processor); contextClass = contextClass.getContainingClass(); } return result; }
@Override public Comparable weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) { if (location.getPosition() == null || !(element instanceof PsiClass)) { return null; } if (isTooGeneral((PsiClass)element)) return false; Set<String> superClasses = PLACE_SUPER_CLASSES.getValue(location); if (superClasses.isEmpty()) { return false; } final PsiElement position = location.getPosition(); PsiClass placeClass = findPlaceClass(element, position); if (placeClass == null) return false; PsiClass elementClass = placeClass; while (elementClass != null) { if (superClasses.contains(elementClass.getQualifiedName())) { return true; } elementClass = elementClass.getContainingClass(); } return false; }
@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<PsiElement,ProximityLocation>(elementComputable, new ProximityLocation(context, contextModule, processingContext), PROXIMITY_WEIGHERS); }
@Override protected Preference getPreferredCondition(@NotNull ProximityLocation location) { PsiElement position = location.getPosition(); if (INSIDE_REFERENCE_LIST.accepts(position)) { assert position != null; GrReferenceList list = (GrReferenceList)position.getParent().getParent(); PsiElement parent = list.getParent(); if (parent instanceof GrTypeDefinition) { GrTypeDefinition cls = (GrTypeDefinition)parent; if (cls.isInterface() && list == cls.getExtendsClause() || list == cls.getImplementsClause()) { return Preference.Interfaces; } if (list == cls.getExtendsClause()) { return Preference.Classes; } } if (parent instanceof GrMethod && ((GrMethod)parent).getThrowsList() == list) { return Preference.Exceptions; } } return null; }
private static int compareMembers(PsiMember m1, PsiMember m2, PsiExpression context) { ProgressManager.checkCanceled(); int result = JavaStatisticsManager.createInfo(null, m2).getUseCount() - JavaStatisticsManager.createInfo(null, m1).getUseCount(); if (result != 0) return result; final PsiClass aClass = m1.getContainingClass(); final PsiClass bClass = m2.getContainingClass(); if (aClass != null && bClass != null) { result = JavaStatisticsManager.createInfo(null, bClass).getUseCount() - JavaStatisticsManager.createInfo(null, aClass).getUseCount(); if (result != 0) return result; } WeighingComparable<PsiElement,ProximityLocation> proximity1 = PsiProximityComparator.getProximity(m1, context); WeighingComparable<PsiElement,ProximityLocation> proximity2 = PsiProximityComparator.getProximity(m2, context); if (proximity1 != null && proximity2 != null) { result = proximity2.compareTo(proximity1); if (result != 0) return result; } String name1 = PsiUtil.getMemberQualifiedName(m1); String name2 = PsiUtil.getMemberQualifiedName(m2); return name1 == null || name2 == null ? 0 : name1.compareTo(name2); }
@Nullable protected Preference getPreferredCondition(@NotNull ProximityLocation location) { PsiElement position = location.getPosition(); if (PlatformPatterns.psiElement().withParents(PsiJavaCodeReferenceElement.class, PsiReferenceList.class).accepts(position)) { assert position != null; PsiReferenceList list = (PsiReferenceList)position.getParent().getParent(); PsiReferenceList.Role role = list.getRole(); if (shouldContainInterfaces(list, role)) { return Preference.Interfaces; } if (role == PsiReferenceList.Role.EXTENDS_LIST) { return Preference.Classes; } if (role == PsiReferenceList.Role.THROWS_LIST) { return Preference.Exceptions; } } return null; }
@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; final WeighingComparable<PsiElement, ProximityLocation> proximity1 = myProximities.get(element1); final WeighingComparable<PsiElement, ProximityLocation> proximity2 = myProximities.get(element2); if (proximity1 == null || proximity2 == null) { return 0; } if (!proximity1.equals(proximity2)) { return - proximity1.compareTo(proximity2); } if (myContext == null) return 0; Module contextModule = ModuleUtil.findModuleForPsiElement(myContext); if (contextModule == null) return 0; StatisticsManager statisticsManager = StatisticsManager.getInstance(); final ProximityLocation location = new ProximityLocation(myContext, contextModule); int count1 = statisticsManager.getUseCount(STATISTICS_KEY, element1, location); int count2 = statisticsManager.getUseCount(STATISTICS_KEY, element1, location); return count2 - count1; }
@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 = ModuleUtil.findModuleForPsiElement(context); processingContext.put(MODULE_BY_LOCATION, contextModule); } if (contextModule == null) return null; return new WeighingComparable<PsiElement,ProximityLocation>(elementComputable, new ProximityLocation(context, contextModule, processingContext), PROXIMITY_WEIGHERS); }
@Override protected Preference getPreferredCondition(@NotNull ProximityLocation location) { PsiElement position = location.getPosition(); if (PlatformPatterns.psiElement().withParents(GrCodeReferenceElement.class, GrReferenceList.class).accepts(position)) { assert position != null; GrReferenceList list = (GrReferenceList)position.getParent().getParent(); PsiElement parent = list.getParent(); if (parent instanceof GrTypeDefinition) { GrTypeDefinition cls = (GrTypeDefinition)parent; if (cls.isInterface() && list == cls.getExtendsClause() || list == cls.getImplementsClause()) { return Preference.Interfaces; } if (list == cls.getExtendsClause()) { return Preference.Classes; } } if (parent instanceof GrMethod && ((GrMethod)parent).getThrowsList() == list) { return Preference.Exceptions; } } return null; }
@Nullable private static Integer getTestFrameworkWeight(@NotNull PsiElement element, @NotNull ProximityLocation location, @NotNull Project project) { if(element instanceof PsiClass) { final String qualifiedName = ((PsiClass) element).getQualifiedName(); if(qualifiedName != null) { if(qualifiedName.startsWith("org.testng.internal")) { return -1; } VirtualFile locationFile = PsiUtilCore.getVirtualFile(location.getPosition()); if(locationFile != null && ProjectFileIndex.SERVICE.getInstance(project).isInTestSourceContent(locationFile) && (qualifiedName.contains("junit") || qualifiedName.contains("test"))) { return 1; } } } return null; }
@NotNull @Override public Set<String> fun(ProximityLocation location) { final HashSet<String> result = new HashSet<>(); PsiClass contextClass = PsiTreeUtil.getContextOfType(location.getPosition(), PsiClass.class, false); Processor<PsiClass> processor = psiClass -> { ContainerUtilRt.addIfNotNull(result, psiClass.getQualifiedName()); return true; }; while(contextClass != null) { InheritanceUtil.processSupers(contextClass, true, processor); contextClass = contextClass.getContainingClass(); } return result; }
@Override public PsiPackage fun(ProximityLocation location) { PsiElement position = location.getPosition(); if (position == null) return null; return getContextPackage(position); }
@NotNull @Override public List<String> fun(ProximityLocation location) { final PsiJavaFile psiJavaFile = PsiTreeUtil.getContextOfType(location.getPosition(), PsiJavaFile.class, false); final PsiImportList importList = psiJavaFile == null ? null : psiJavaFile.getImportList(); if (importList == null) return Collections.emptyList(); List<String> importedNames = ContainerUtil.newArrayList(); for (final PsiImportStatement importStatement : importList.getImportStatements()) { ContainerUtil.addIfNotNull(importedNames, importStatement.getQualifiedName()); } return importedNames; }
@Override public Comparable weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) { if (location.getPosition() == null){ return null; } final LogicalRoot contextRoot = LOGICAL_ROOT_KEY.getValue(location); if (contextRoot == null) { return false; } return contextRoot.equals(findLogicalRoot(element)); }
@Override public Comparable weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) { if (location.getProject() == null){ return null; } final PsiFile psiFile = element.getContainingFile(); if (psiFile == null) return false; final VirtualFile virtualFile = psiFile.getOriginalFile().getVirtualFile(); return virtualFile != null && ArrayUtil.find(OPENED_EDITORS.getValue(location), virtualFile) != -1; }
@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); }
@Nullable public static WeighingComparable<PsiElement, ProximityLocation> getProximity(final PsiElement element, final PsiElement context) { if (element == null) return null; if (element instanceof MetadataPsiElementBase) return null; final Module contextModule = context != null ? ModuleUtilCore.findModuleForPsiElement(context) : null; return WeighingService.weigh(WEIGHER_KEY, element, new ProximityLocation(context, contextModule)); }
@Override public Comparable weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) { final Module elementModule = ModuleUtil.findModuleForPsiElement(element); if (location.getPositionModule() == elementModule) { return 2; } if (elementModule != null) { return 1; // in project => still not bad } return 0; }
@Override public Boolean weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) { if (location.getPosition() == null) { return Boolean.TRUE; } final PsiDirectory placeDirectory = PLACE_DIRECTORY.getValue(location); return placeDirectory != null && placeDirectory.equals(getParentDirectory(element)); }
@Nullable public static WeighingComparable<PsiElement, ProximityLocation> getProximity(final PsiElement element, final PsiElement context) { if (element == null) return null; if (element instanceof MetadataPsiElementBase) return null; final Module contextModule = context != null ? ModuleUtil.findModuleForPsiElement(context) : null; return WeighingService.weigh(WEIGHER_KEY, element, new ProximityLocation(context, contextModule)); }
@RequiredReadAction @Override public Comparable weigh(@NotNull PsiElement psiElement, @NotNull ProximityLocation proximityLocation) { if(DotNetAttributeUtil.hasAttribute(psiElement, DotNetTypes.System.ObsoleteAttribute)) { return Access.OBSOLETE; } return Access.NORMAL; }
@Override public Comparable weigh(@NotNull PsiElement psiElement, @NotNull ProximityLocation proximityLocation) { if(psiElement instanceof DotNetGenericParameterListOwner && psiElement.getLanguage() == CSharpLanguage.INSTANCE) { return -((DotNetGenericParameterListOwner) psiElement).getGenericParametersCount(); } return 0; }
@Override public Comparable weigh(@Nonnull final PsiElement element, @Nonnull final ProximityLocation location) { if (location.getPosition() == null){ return null; } final LogicalRoot contextRoot = LOGICAL_ROOT_KEY.getValue(location); if (contextRoot == null) { return false; } return contextRoot.equals(findLogicalRoot(element)); }
@Override public Comparable weigh(@Nonnull final PsiElement element, @Nonnull final ProximityLocation location) { if (location.getProject() == null){ return null; } final PsiFile psiFile = element.getContainingFile(); if (psiFile == null) return false; final VirtualFile virtualFile = psiFile.getOriginalFile().getVirtualFile(); return virtualFile != null && ArrayUtil.find(OPENED_EDITORS.getValue(location), virtualFile) != -1; }