Java 类com.intellij.psi.util.proximity.PsiProximityComparator 实例源码

项目:intellij-ce-playground    文件:CreateFromUsageUtils.java   
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);
}
项目:tools-idea    文件:CreateFromUsageUtils.java   
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);
}
项目:intellij-ce-playground    文件:JavaDocReferenceInspection.java   
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
  final PsiElement element = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiJavaCodeReferenceElement.class);
  if (element instanceof PsiJavaCodeReferenceElement) {
    final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)element;
    Collections.sort(originalClasses, new PsiProximityComparator(referenceElement.getElement()));
    final JList list = new JBList(originalClasses.toArray(new PsiClass[originalClasses.size()]));
    list.setCellRenderer(new FQNameCellRenderer());
    final Runnable runnable = new Runnable() {
      @Override
      public void run() {
        if (!element.isValid()) return;
        final int index = list.getSelectedIndex();
        if (index < 0) return;
        new WriteCommandAction(project, element.getContainingFile()){
          @Override
          protected void run(@NotNull final Result result) throws Throwable {
            final PsiClass psiClass = originalClasses.get(index);
            if (psiClass.isValid()) {
              PsiDocumentManager.getInstance(project).commitAllDocuments();
              referenceElement.bindToElement(psiClass);
            }
          }
        }.execute();
      }
    };
    final AsyncResult<DataContext> asyncResult = DataManager.getInstance().getDataContextFromFocus();
    asyncResult.doWhenDone(new Consumer<DataContext>() {
      @Override
      public void consume(DataContext dataContext) {
        new PopupChooserBuilder(list).
          setTitle(QuickFixBundle.message("add.qualifier.original.class.chooser.title")).
          setItemChoosenCallback(runnable).
          createPopup().
          showInBestPositionFor(dataContext);
      }
    });
  }
}
项目:intellij-ce-playground    文件:LookupElementProximityWeigher.java   
@Override
public Comparable weigh(@NotNull final LookupElement item, @NotNull final CompletionLocation location) {
  if (item.getObject() instanceof PsiElement) {
    return PsiProximityComparator.getProximity(new NullableComputable<PsiElement>() {
      @Override
      public PsiElement compute() {
        return item.getPsiElement();
      }
    }, location.getCompletionParameters().getPosition(), location.getProcessingContext());
  }
  return null;
}
项目:intellij-ce-playground    文件:GroovyStaticImportMethodFix.java   
@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<PsiMethod>();
  if (name == null) return list;
  GlobalSearchScope scope = element.getResolveScope();
  PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, 20);
  List<PsiMethod> applicableList = new ArrayList<PsiMethod>();
  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;
}
项目:tools-idea    文件:JavaDocReferenceInspection.java   
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
  final PsiElement element = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiJavaCodeReferenceElement.class);
  if (element instanceof PsiJavaCodeReferenceElement) {
    final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)element;
    Collections.sort(originalClasses, new PsiProximityComparator(referenceElement.getElement()));
    final JList list = new JBList(originalClasses.toArray(new PsiClass[originalClasses.size()]));
    list.setCellRenderer(new FQNameCellRenderer());
    final Runnable runnable = new Runnable() {
      @Override
      public void run() {
        if (!element.isValid()) return;
        final int index = list.getSelectedIndex();
        if (index < 0) return;
        new WriteCommandAction(project, element.getContainingFile()){
          @Override
          protected void run(final Result result) throws Throwable {
            final PsiClass psiClass = originalClasses.get(index);
            if (psiClass.isValid()) {
              PsiDocumentManager.getInstance(project).commitAllDocuments();
              referenceElement.bindToElement(psiClass);
            }
          }
        }.execute();
      }
    };
    final AsyncResult<DataContext> asyncResult = DataManager.getInstance().getDataContextFromFocus();
    asyncResult.doWhenDone(new AsyncResult.Handler<DataContext>() {
      @Override
      public void run(DataContext dataContext) {
        new PopupChooserBuilder(list).
          setTitle(QuickFixBundle.message("add.qualifier.original.class.chooser.title")).
          setItemChoosenCallback(runnable).
          createPopup().
          showInBestPositionFor(dataContext);
      }
    });
  }
}
项目:tools-idea    文件:LookupElementProximityWeigher.java   
@Override
public Comparable weigh(@NotNull final LookupElement item, @NotNull final CompletionLocation location) {
  if (item.getObject() instanceof PsiElement) {
    return PsiProximityComparator.getProximity(new NullableComputable<PsiElement>() {
      @Override
      public PsiElement compute() {
        return item.getPsiElement();
      }
    }, location.getCompletionParameters().getPosition(), location.getProcessingContext());
  }
  return null;
}
项目:tools-idea    文件:GroovyStaticImportMethodFix.java   
@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<PsiMethod>();
  if (name == null) return list;
  GlobalSearchScope scope = element.getResolveScope();
  PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, 20);
  List<PsiMethod> applicableList = new ArrayList<PsiMethod>();
  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().length() != 0 && 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;
}
项目:consulo    文件:LookupElementProximityWeigher.java   
@Override
public Comparable weigh(@Nonnull final LookupElement item, @Nonnull final CompletionLocation location) {
  if (item.getObject() instanceof PsiElement) {
    return PsiProximityComparator.getProximity((NullableComputable<PsiElement>)() -> item.getPsiElement(), location.getCompletionParameters().getPosition(), location.getProcessingContext());
  }
  return null;
}
项目:consulo-java    文件:CodeInsightUtil.java   
public static <T extends PsiMember & PsiDocCommentOwner> Comparator<T> createSortIdenticalNamedMembersComparator(PsiElement place)
{
    final PsiProximityComparator proximityComparator = new PsiProximityComparator(place);
    return (o1, o2) ->
    {
        boolean deprecated1 = JavaCompletionUtil.isEffectivelyDeprecated(o1);
        boolean deprecated2 = JavaCompletionUtil.isEffectivelyDeprecated(o2);
        if(deprecated1 && !deprecated2)
        {
            return 1;
        }
        if(!deprecated1 && deprecated2)
        {
            return -1;
        }
        int compare = proximityComparator.compare(o1, o2);
        if(compare != 0)
        {
            return compare;
        }

        String qname1 = o1 instanceof PsiClass ? ((PsiClass) o1).getQualifiedName() : null;
        String qname2 = o2 instanceof PsiClass ? ((PsiClass) o2).getQualifiedName() : null;
        if(qname1 == null || qname2 == null)
        {
            return 0;
        }
        return qname1.compareToIgnoreCase(qname2);
    };
}
项目:intellij-ce-playground    文件:DefaultChooseByNameItemProvider.java   
private PathProximityComparator(@Nullable final PsiElement context) {
  myProximityComparator = new PsiProximityComparator(context);
}
项目:tools-idea    文件:CodeInsightUtil.java   
public static void sortIdenticalShortNameClasses(PsiClass[] classes, @NotNull PsiReference context) {
  if (classes.length <= 1) return;

  PsiElement leaf = context.getElement().getFirstChild(); // the same proximity weighers are used in completion, where the leafness is critical
  Arrays.sort(classes, new PsiProximityComparator(leaf));
}
项目:tools-idea    文件:DefaultChooseByNameItemProvider.java   
private PathProximityComparator(@Nullable final PsiElement context) {
  myProximityComparator = new PsiProximityComparator(context);
}
项目:consulo    文件:DefaultChooseByNameItemProvider.java   
private PathProximityComparator(@Nullable final PsiElement context) {
  myProximityComparator = new PsiProximityComparator(context);
}
项目:consulo-java    文件:JavaDocReferenceInspection.java   
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor)
{
    final PsiElement element = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiJavaCodeReferenceElement.class);
    if(element instanceof PsiJavaCodeReferenceElement)
    {
        final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement) element;
        Collections.sort(originalClasses, new PsiProximityComparator(referenceElement.getElement()));
        final JList list = new JBList(originalClasses.toArray(new PsiClass[originalClasses.size()]));
        list.setCellRenderer(new FQNameCellRenderer());
        final Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                if(!element.isValid())
                {
                    return;
                }
                final int index = list.getSelectedIndex();
                if(index < 0)
                {
                    return;
                }
                new WriteCommandAction(project, element.getContainingFile())
                {
                    @Override
                    protected void run(final Result result) throws Throwable
                    {
                        final PsiClass psiClass = originalClasses.get(index);
                        if(psiClass.isValid())
                        {
                            PsiDocumentManager.getInstance(project).commitAllDocuments();
                            referenceElement.bindToElement(psiClass);
                        }
                    }
                }.execute();
            }
        };
        final AsyncResult<DataContext> asyncResult = DataManager.getInstance().getDataContextFromFocus();
        asyncResult.doWhenDone(dataContext -> {
            new PopupChooserBuilder(list).
                    setTitle(JavaQuickFixBundle.message("add.qualifier.original.class.chooser.title")).
                    setItemChoosenCallback(runnable).
                    createPopup().
                    showInBestPositionFor(dataContext);
        });
    }
}