示例#1
0
 protected PsiReference[] getReferences(final PsiMultiReference multiReference) {
   final PsiReference[] references = multiReference.getReferences();
   final List<PsiReference> hard =
       ContainerUtil.findAll(
           references,
           new Condition<PsiReference>() {
             public boolean value(final PsiReference object) {
               return !object.isSoft();
             }
           });
   if (!hard.isEmpty()) {
     return hard.toArray(new PsiReference[hard.size()]);
   }
   return references;
 }
  @Override
  public Pair<List<LookupElement>, Integer> arrangeItems(
      @NotNull Lookup lookup, boolean onExplicitAction) {
    List<LookupElement> items = getMatchingItems();
    MultiMap<CompletionSorterImpl, LookupElement> itemsBySorter = groupItemsBySorter(items);

    LookupElement relevantSelection = findMostRelevantItem(itemsBySorter);
    List<LookupElement> listModel =
        isAlphaSorted()
            ? sortByPresentation(items, lookup)
            : fillModelByRelevance((LookupImpl) lookup, items, itemsBySorter, relevantSelection);

    int toSelect =
        getItemToSelect((LookupImpl) lookup, listModel, onExplicitAction, relevantSelection);
    LOG.assertTrue(toSelect >= 0);

    addDummyItems(items.size() - listModel.size(), listModel);

    return new Pair<List<LookupElement>, Integer>(listModel, toSelect);
  }
示例#3
0
  protected void defineScopeEquivalence(Class scopeClass, Class equivClass) {
    final Iterator<CompletionVariant> iter = myCompletionVariants.iterator();
    if (isScopeFinal(scopeClass)) {
      declareFinalScope(equivClass);
    }

    while (iter.hasNext()) {
      final CompletionVariant variant = iter.next();
      if (variant.isScopeClassAcceptable(scopeClass)) {
        variant.includeScopeClass(equivClass, variant.isScopeClassFinal(scopeClass));
      }
    }
  }
  private static int getItemToSelect(
      LookupImpl lookup,
      List<LookupElement> items,
      boolean onExplicitAction,
      @Nullable LookupElement mostRelevant) {
    if (items.isEmpty() || lookup.getFocusDegree() == LookupImpl.FocusDegree.UNFOCUSED) {
      return 0;
    }

    if (lookup.isSelectionTouched() || !onExplicitAction) {
      final LookupElement lastSelection = lookup.getCurrentItem();
      int old = ContainerUtil.indexOfIdentity(items, lastSelection);
      if (old >= 0) {
        return old;
      }

      Object selectedValue = ((LookupImpl) lookup).getList().getSelectedValue();
      if (selectedValue instanceof EmptyLookupItem
          && ((EmptyLookupItem) selectedValue).isLoading()) {
        int index = ((LookupImpl) lookup).getList().getSelectedIndex();
        if (index >= 0 && index < items.size()) {
          return index;
        }
      }

      for (int i = 0; i < items.size(); i++) {
        String invariant = PRESENTATION_INVARIANT.get(items.get(i));
        if (invariant != null && invariant.equals(PRESENTATION_INVARIANT.get(lastSelection))) {
          return i;
        }
      }
    }

    String selectedText = lookup.getEditor().getSelectionModel().getSelectedText();
    for (int i = 0; i < items.size(); i++) {
      LookupElement item = items.get(i);
      if (isPrefixItem(lookup, item, true) && !isLiveTemplate(item)
          || item.getLookupString().equals(selectedText)) {
        return i;
      }
    }

    return Math.max(0, ContainerUtil.indexOfIdentity(items, mostRelevant));
  }
示例#5
0
 /**
  * @deprecated
  * @see com.intellij.codeInsight.completion.CompletionContributor
  */
 protected void registerVariant(CompletionVariant variant) {
   myCompletionVariants.add(variant);
 }
 @Override
 public void prefixChanged(Lookup lookup) {
   myPrefixChanges++;
   myFrozenItems.clear();
   super.prefixChanged(lookup);
 }
 private void addFrozenItems(List<LookupElement> items, LinkedHashSet<LookupElement> model) {
   myFrozenItems.retainAll(items);
   model.addAll(myFrozenItems);
 }
 private void freezeTopItems(LookupImpl lookup, LinkedHashSet<LookupElement> model) {
   myFrozenItems.clear();
   if (lookup.isShown()) {
     myFrozenItems.addAll(model);
   }
 }
 private static void addDummyItems(int count, List<LookupElement> listModel) {
   EmptyLookupItem dummy = new EmptyLookupItem("loading...", true);
   for (int i = count; i > 0; i--) {
     listModel.add(dummy);
   }
 }