com.intellij.codeInsight.completion.InsertHandler Java Examples

The following examples show how to use com.intellij.codeInsight.completion.InsertHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpecLookupElement.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * @param qualifiedName the name of the class to create lookup
 * @param project to find the lookup annotation class
 * @param insertHandler adds custom actions to the insert handling
 * @throws IncorrectOperationException if the qualifiedName does not specify a valid type
 * @return new {@link LookupElement} or cached instance if it was created previously
 */
static LookupElement create(
    String qualifiedName, Project project, InsertHandler<LookupElement> insertHandler)
    throws IncorrectOperationException {
  if (CACHE.containsKey(qualifiedName)) {
    return CACHE.get(qualifiedName);
  }
  PsiClass typeCls = PsiSearchUtils.findClass(project, qualifiedName);
  if (typeCls != null) {
    SpecLookupElement lookupElement = new SpecLookupElement(typeCls, insertHandler);
    CACHE.put(qualifiedName, lookupElement);
    return lookupElement;
  }
  // This is a dummy class, we don't want to cache it.
  typeCls =
      JavaPsiFacade.getInstance(project)
          .getElementFactory()
          .createClass(LithoClassNames.shortName(qualifiedName));
  return new SpecLookupElement(typeCls, insertHandler);
}
 
Example #2
Source File: DefaultTextCompletionValueDescriptor.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public LookupElementBuilder createLookupBuilder(@Nonnull T item) {
  LookupElementBuilder builder = LookupElementBuilder.create(item, getLookupString(item))
          .withIcon(getIcon(item));

  InsertHandler<LookupElement> handler = createInsertHandler(item);
  if (handler != null) {
    builder = builder.withInsertHandler(handler);
  }

  String tailText = getTailText(item);
  if (tailText != null) {
    builder = builder.withTailText(tailText, true);
  }

  String typeText = getTypeText(item);
  if (typeText != null) {
    builder = builder.withTypeText(typeText);
  }
  return builder;
}
 
Example #3
Source File: GoToHashOrRefPopup.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
protected InsertHandler<LookupElement> createInsertHandler(@Nonnull VcsRef item) {
  return (context, item1) -> {
    mySelectedRef = (VcsRef)item1.getObject();
    ApplicationManager.getApplication().invokeLater(() -> {
      // handleInsert is called in the middle of some other code that works with editor
      // (see CodeCompletionHandlerBase.insertItem)
      // for example, scrolls editor
      // problem is that in onOk we make text field not editable
      // by some reason this is done by disposing its editor and creating a new one
      // so editor gets disposed here and CodeCompletionHandlerBase can not finish doing whatever it is doing with it
      // I counter this by invoking onOk in invokeLater
      myTextField.onOk();
    });
  };
}
 
Example #4
Source File: TextFieldWithAutoCompletionListProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
public LookupElementBuilder createLookupBuilder(@Nonnull final T item) {
  LookupElementBuilder builder = LookupElementBuilder.create(item, getLookupString(item))
    .withIcon(getIcon(item));

  final InsertHandler<LookupElement> handler = createInsertHandler(item);
  if (handler != null) {
    builder = builder.withInsertHandler(handler);
  }

  final String tailText = getTailText(item);
  if (tailText != null) {
    builder = builder.withTailText(tailText, true);
  }

  final String typeText = getTypeText(item);
  if (typeText != null) {
    builder = builder.withTypeText(typeText);
  }
  return builder;
}
 
Example #5
Source File: LookupElementBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object o) {
  if (this == o) return true;
  if (o == null || getClass() != o.getClass()) return false;

  LookupElementBuilder that = (LookupElementBuilder)o;

  final InsertHandler<LookupElement> insertHandler = that.myInsertHandler;
  if (myInsertHandler != null && insertHandler != null ? !myInsertHandler.getClass().equals(insertHandler.getClass()) : myInsertHandler != insertHandler) {
    return false;
  }
  if (!myLookupString.equals(that.myLookupString)) return false;
  if (!myObject.equals(that.myObject)) return false;

  final LookupElementRenderer<LookupElement> renderer = that.myRenderer;
  if (myRenderer != null && renderer != null ? !myRenderer.getClass().equals(renderer.getClass()) : myRenderer != renderer) return false;

  return true;
}
 
Example #6
Source File: MyLookupExpression.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static LookupElement[] initLookupItems(LinkedHashSet<String> names,
                                               PsiNamedElement elementToRename, 
                                               PsiElement nameSuggestionContext,
                                               final boolean shouldSelectAll) {
  if (names == null) {
    names = new LinkedHashSet<String>();
    for (NameSuggestionProvider provider : Extensions.getExtensions(NameSuggestionProvider.EP_NAME)) {
      final SuggestedNameInfo suggestedNameInfo = provider.getSuggestedNames(elementToRename, nameSuggestionContext, names);
      if (suggestedNameInfo != null &&
          provider instanceof PreferrableNameSuggestionProvider &&
          !((PreferrableNameSuggestionProvider)provider).shouldCheckOthers()) {
        break;
      }
    }
  }
  final LookupElement[] lookupElements = new LookupElement[names.size()];
  final Iterator<String> iterator = names.iterator();
  for (int i = 0; i < lookupElements.length; i++) {
    final String suggestion = iterator.next();
    lookupElements[i] = LookupElementBuilder.create(suggestion).withInsertHandler(new InsertHandler<LookupElement>() {
      @Override
      public void handleInsert(InsertionContext context, LookupElement item) {
        if (shouldSelectAll) return;
        final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.getEditor());
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor);
        if (templateState != null) {
          final TextRange range = templateState.getCurrentVariableRange();
          if (range != null) {
            topLevelEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), suggestion);
          }
        }
      }
    });
  }
  return lookupElements;
}
 
Example #7
Source File: CSharpStatementCompletionContributor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static InsertHandler<LookupElement> buildInsertHandler(final IElementType elementType)
{
	char open = '(';
	char close = ')';

	if(elementType == DO_KEYWORD || elementType == TRY_KEYWORD || elementType == CATCH_KEYWORD || elementType == UNSAFE_KEYWORD || elementType == FINALLY_KEYWORD)
	{
		open = '{';
		close = '}';
	}

	return new ExpressionOrStatementInsertHandler<>(open, close);
}
 
Example #8
Source File: BuiltInFunctionCompletionContributor.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static InsertHandler<LookupElement> getInsertHandler(
    String ruleName, @Nullable BuildLanguageSpec spec) {
  if (spec == null) {
    return ParenthesesInsertHandler.getInstance(true);
  }
  return RulesTemplates.templateForRule(ruleName, spec)
      .map(BuiltInFunctionCompletionContributor::createTemplateInsertHandler)
      .orElse(ParenthesesInsertHandler.getInstance(true));
}
 
Example #9
Source File: CommonMacroCompletionContributor.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static InsertHandler<LookupElement> getInsertHandler(
    BuildFile file, String packageLocation, String symbol) {
  ParenthesesInsertHandler<LookupElement> base =
      ParenthesesInsertHandler.getInstance(/* hasParameters= */ true);
  return (context, item) -> {
    base.handleInsert(context, item);
    insertLoadStatement(context, file, packageLocation, symbol);
  };
}
 
Example #10
Source File: ProjectViewKeywordCompletionContributor.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static InsertHandler<LookupElement> insertDivider(SectionParser parser) {
  return (context, item) -> {
    Editor editor = context.getEditor();
    Document document = editor.getDocument();
    context.commitDocument();

    String nextTokenText = findNextTokenText(context);
    if (nextTokenText == null || nextTokenText.equals("\n")) {
      document.insertString(context.getTailOffset(), getDivider(parser));
      editor.getCaretModel().moveToOffset(context.getTailOffset());
    }
  };
}
 
Example #11
Source File: LookupElementBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
private LookupElementBuilder(@Nonnull String lookupString,
                             @Nonnull Object object,
                             @Nullable InsertHandler<LookupElement> insertHandler,
                             @Nullable LookupElementRenderer<LookupElement> renderer,
                             @Nullable LookupElementPresentation hardcodedPresentation,
                             @Nonnull Set<String> allLookupStrings,
                             boolean caseSensitive) {
  myLookupString = lookupString;
  myObject = object;
  myInsertHandler = insertHandler;
  myRenderer = renderer;
  myHardcodedPresentation = hardcodedPresentation;
  myAllLookupStrings = Collections.unmodifiableSet(allLookupStrings);
  myCaseSensitive = caseSensitive;
}
 
Example #12
Source File: LookupItem.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void handleInsert(final InsertionContext context) {
  final InsertHandler<? extends LookupElement> handler = getInsertHandler();
  if (handler != null) {
    //noinspection unchecked
    ((InsertHandler)handler).handleInsert(context, this);
  }
  if (getTailType() != TailType.UNKNOWN && myInsertHandler == null) {
    context.setAddCompletionChar(false);
    final TailType type = handleCompletionChar(context.getEditor(), this, context.getCompletionChar());
    type.processTail(context.getEditor(), context.getTailOffset());
  }
}
 
Example #13
Source File: ReplacingConsumer.java    From litho with Apache License 2.0 5 votes vote down vote up
ReplacingConsumer(
    Collection<String> replacedQualifiedNames,
    CompletionResultSet result,
    InsertHandler<LookupElement> insertHandler) {
  this.replacedQualifiedNames = new HashSet<>(replacedQualifiedNames);
  this.result = result;
  this.insertHandler = insertHandler;
}
 
Example #14
Source File: SymfonyBundleFileLookupElement.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public SymfonyBundleFileLookupElement(BundleFile bundleFile, InsertHandler<LookupElement> insertHandler) {
    this(bundleFile);
    this.insertHandler = insertHandler;
}
 
Example #15
Source File: LookupElementBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public InsertHandler<LookupElement> getInsertHandler() {
  return myInsertHandler;
}
 
Example #16
Source File: AssetLookupElement.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public AssetLookupElement withInsertHandler(InsertHandler<AssetLookupElement> insertHandler) {
    this.insertHandler = insertHandler;
    return this;
}
 
Example #17
Source File: TranslatorLookupElement.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
public TranslatorLookupElement(@NotNull String translationString, @NotNull String domain, @NotNull InsertHandler<TranslatorLookupElement> insertHandler) {
    this.translationString = translationString;
    this.domain = domain;
    this.insertHandler = insertHandler;
}
 
Example #18
Source File: PhpClassAnnotationLookupElement.java    From idea-php-annotation-plugin with MIT License 4 votes vote down vote up
public PhpClassAnnotationLookupElement withInsertHandler(InsertHandler<LookupElement> insertHandler) {
    this.insertHandler = insertHandler;
    return this;
}
 
Example #19
Source File: GenericFileLookupElement.java    From idea-php-advanced-autocomplete with MIT License 4 votes vote down vote up
public GenericFileLookupElement(String fileName, PsiFileSystemItem psiFile, PsiElement psiElement, InsertHandler<LookupElement> insertHandler) {
    this.fileName = fileName;
    this.psiFile = psiFile;
    this.insertHandler = insertHandler;
    this.psiElement = psiElement;
}
 
Example #20
Source File: IgnoredLookupElement.java    From yiistorm with MIT License 4 votes vote down vote up
public IgnoredLookupElement(String title, String filePath, PsiElement psiElement, @Nullable InsertHandler<LookupElement> insertHandler) {
    this.title = title;
    this.insertHandler = insertHandler;
    this.psiElement = psiElement;
}
 
Example #21
Source File: DefaultTextCompletionValueDescriptor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
protected InsertHandler<LookupElement> createInsertHandler(@Nonnull final T item) {
  return null;
}
 
Example #22
Source File: FolderLookupElement.java    From yiistorm with MIT License 4 votes vote down vote up
public FolderLookupElement(String title, String filePath, PsiElement psiElement, @Nullable InsertHandler<LookupElement> insertHandler) {
    this.title = title;
    this.insertHandler = insertHandler;
    this.psiElement = psiElement;
}
 
Example #23
Source File: TextFieldWithAutoCompletionListProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
protected InsertHandler<LookupElement> createInsertHandler(@Nonnull final T item) {
  return null;
}
 
Example #24
Source File: LookupItem.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public LookupItem<T> setInsertHandler(@Nonnull final InsertHandler<? extends LookupElement> handler) {
  myInsertHandler = handler;
  return this;
}
 
Example #25
Source File: LookupElementDecorator.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static <T extends LookupElement> LookupElementDecorator<T> withInsertHandler(@Nonnull T element,
                                                                                    @Nonnull final InsertHandler<? super LookupElementDecorator<T>> insertHandler) {
  return new InsertingDecorator<T>(element, insertHandler);
}
 
Example #26
Source File: LookupElementDecorator.java    From consulo with Apache License 2.0 4 votes vote down vote up
public InsertingDecorator(T element, InsertHandler<? super LookupElementDecorator<T>> insertHandler) {
  super(element);
  myInsertHandler = insertHandler;
}
 
Example #27
Source File: LookupItem.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public InsertHandler<? extends LookupItem> getInsertHandler(){
  return myInsertHandler;
}
 
Example #28
Source File: LookupElementBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * @deprecated use {@link #withInsertHandler(InsertHandler)}
 */
@Contract(value = "", pure = true)
public LookupElementBuilder setInsertHandler(@Nullable InsertHandler<LookupElement> insertHandler) {
  return withInsertHandler(insertHandler);
}
 
Example #29
Source File: LookupElementBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Contract(value = "", pure = true)
public LookupElementBuilder withInsertHandler(@Nullable InsertHandler<LookupElement> insertHandler) {
  return new LookupElementBuilder(myLookupString, myObject, insertHandler, myRenderer, myHardcodedPresentation, myAllLookupStrings, myCaseSensitive);
}
 
Example #30
Source File: YamlTraversal.java    From intellij-swagger with MIT License 4 votes vote down vote up
@Override
public InsertHandler<LookupElement> createInsertFieldHandler(final Field field) {
  return new YamlInsertFieldHandler(field);
}