static CharFilter.Result getLookupAction(final char charTyped, final LookupImpl lookup) {
    final CharFilter.Result filtersDecision = getFiltersDecision(charTyped, lookup);
    if (!Registry.is("ide.completion.allow.finishing.by.chars")
        && filtersDecision == CharFilter.Result.SELECT_ITEM_AND_FINISH_LOOKUP) {
      return CharFilter.Result.HIDE_LOOKUP;
    }

    final LookupElement currentItem = lookup.getCurrentItem();
    if (currentItem != null && charTyped != ' ') {
      String postfix = lookup.getAdditionalPrefix() + charTyped;
      final PrefixMatcher matcher = lookup.itemMatcher(currentItem);
      for (String lookupString : currentItem.getAllLookupStrings()) {
        if (lookupString.startsWith(matcher.getPrefix() + postfix)) {
          return CharFilter.Result.ADD_TO_PREFIX;
        }
      }
    }

    if (filtersDecision != null) {
      return filtersDecision;
    }
    throw new AssertionError(
        "Typed char not handler by char filter: c="
            + charTyped
            + "; prefix="
            + currentItem
            + "; filters="
            + Arrays.toString(getFilters()));
  }
 private boolean prefixMatchersInternal(
     final LookupElement element, final boolean itemCaseInsensitive) {
   for (final String name : element.getAllLookupStrings()) {
     if (itemCaseInsensitive && StringUtil.startsWithIgnoreCase(name, myPrefix)
         || prefixMatches(name)) {
       return true;
     }
     if (itemCaseInsensitive
         && CodeInsightSettings.ALL
             != CodeInsightSettings.getInstance().COMPLETION_CASE_SENSITIVE) {
       if (myCaseInsensitiveMatcher.matches(name)) {
         return true;
       }
     }
   }
   return false;
 }
 @Override
 public boolean isStartMatch(LookupElement element) {
   if (super.isStartMatch(element)) {
     return true;
   }
   if (element.isCaseSensitive()) {
     return false;
   }
   return ContainerUtil.or(
       element.getAllLookupStrings(),
       new Condition<String>() {
         @Override
         public boolean value(String s) {
           return myCaseInsensitiveMatcher.isStartMatch(s);
         }
       });
 }
 private static boolean completeTillTypedCharOccurrence(
     char charTyped, LookupImpl lookup, LookupElement item) {
   PrefixMatcher matcher = lookup.itemMatcher(item);
   final String oldPrefix = matcher.getPrefix() + lookup.getAdditionalPrefix();
   PrefixMatcher expanded = matcher.cloneWithPrefix(oldPrefix + charTyped);
   if (expanded.prefixMatches(item)) {
     for (String s : item.getAllLookupStrings()) {
       if (matcher.prefixMatches(s)) {
         int i = -1;
         while (true) {
           i = s.indexOf(charTyped, i + 1);
           if (i < 0) break;
           final String newPrefix = s.substring(0, i + 1);
           if (expanded.prefixMatches(newPrefix)) {
             lookup.replacePrefix(oldPrefix, newPrefix);
             return true;
           }
         }
       }
     }
   }
   return false;
 }
Ejemplo n.º 5
0
  public void finishLookup(char completionChar, @Nullable final LookupElement item) {
    //noinspection deprecation,unchecked
    if (item == null
        || item instanceof EmptyLookupItem
        || item.getObject() instanceof DeferredUserLookupValue
            && item.as(LookupItem.CLASS_CONDITION_KEY) != null
            && !((DeferredUserLookupValue) item.getObject())
                .handleUserSelection(item.as(LookupItem.CLASS_CONDITION_KEY), myProject)) {
      doHide(false, true);
      fireItemSelected(null, completionChar);
      return;
    }

    if (myDisposed) { // DeferredUserLookupValue could close us in any way
      return;
    }

    final PsiFile file = getPsiFile();
    boolean writableOk =
        file == null || FileModificationService.getInstance().prepareFileForWrite(file);
    if (myDisposed) { // ensureFilesWritable could close us by showing a dialog
      return;
    }

    if (!writableOk) {
      doHide(false, true);
      fireItemSelected(null, completionChar);
      return;
    }

    final String prefix = itemPattern(item);
    boolean plainMatch =
        ContainerUtil.or(
            item.getAllLookupStrings(),
            new Condition<String>() {
              @Override
              public boolean value(String s) {
                return StringUtil.containsIgnoreCase(s, prefix);
              }
            });
    if (!plainMatch) {
      FeatureUsageTracker.getInstance()
          .triggerFeatureUsed(CodeCompletionFeatures.EDITING_COMPLETION_CAMEL_HUMPS);
    }

    myFinishing = true;
    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                myEditor.getDocument().startGuardedBlockChecking();
                try {
                  insertLookupString(item, getPrefixLength(item));
                } finally {
                  myEditor.getDocument().stopGuardedBlockChecking();
                }
              }
            });

    if (myDisposed) { // any document listeners could close us
      return;
    }

    doHide(false, true);

    fireItemSelected(item, completionChar);
  }