CompletionContributor: How to show multiple entries with same name?

Answered

Suppose I have a CompletionContributor that produces LookupElements with same name, but for distinct symbols. Example:

public class MyLanguageCompletionContributor extends CompletionContributor {

    public MyLanguageCompletionContributor() {
        extend(CompletionType.BASIC, PlatformPatterns.psiElement().withLanguage(MyLanguage.INSTANCE),
                new CompletionProvider<>() {
                    @Override
                    protected void addCompletions(@NotNull CompletionParameters parameters,
                                                  @NotNull ProcessingContext context,
                                                  @NotNull CompletionResultSet result) {

/*
* 'funcTest2' exists on both 'MyModule1' and 'MyModule2' modules
*/

                        LookupElement element = LookupElementBuilder.create("funcTest2")
                                .withTailText("(): void")
                                .withTypeText("MyModule1")
                                .withIcon(PlatformIcons.FUNCTION_ICON)
                                .withAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE);

                        result.addElement(element);

                        element = LookupElementBuilder.create("funcTest2")
                                .withTailText("(): void")
                                .withTypeText("MyModule2")
                                .withIcon(PlatformIcons.FUNCTION_ICON)
                                .withAutoCompletionPolicy(AutoCompletionPolicy.NEVER_AUTOCOMPLETE);;

                        result.addElement(element);
                    }
                });
    }

}

Intellij is always grouping these entries and showing only the first one:

 

So, how can I configure my LookupElement or CompletionContributor to show both entries?

Thanks!

2 comments
Comment actions Permalink

Hi Luiz,

Please check the LookupElementBuilder.equals() method:
https://github.com/JetBrains/intellij-community/blob/master/platform/analysis-api/src/com/intellij/codeInsight/lookup/LookupElementBuilder.java#L391

It doesn't depend on presentation/typeText which is the only difference in your elements.

This example seems as playing with the API and in the real world, you may want to set e.g., insertHandler which would import Module1 or Module2 in your case, and it would make the completion popup contain both items.

1
Comment actions Permalink

Hi Karol,

Thank you for pointing out the equals() implementation of LookupElementBuilder.

My real CompletionContributor already have an InsertHandler to deal with the auto-import logic, but my mistake was not providing a lookupObject with a correct implementation of hashCode and equals.

//was LookupElementBuilder.create(symbol.getName())
LookupElementBuilder element = LookupElementBuilder.create(symbol, symbol.getName());

Fixing it was enough to solve the problem

 

Thanks!

0

Please sign in to leave a comment.