Trigger custom `CompletionContributor` by some specific characters

Answered

Hi all! I’m developing a plugin and trying to make some additions to the code completion. I created a custom `CompletionContributor` and I want this custom `CompletionContributor` to be only triggered by some specific characters. For example:

// Provide default completions triggered by `.`
foo.ba
    ^ bar()

// Provide custom completions triggered by `..`
foo..ba
     ^ customBar()

// Accepting a completion triggered by `..` replace the trigger characters with `.`
foo.customBar()

These trigger characters are purely for separating my custom completions from the default ones. After accepting a custom completion, the trigger characters must be replaced to make the code valid (`..` to `.` as in the above example).

I’d love to have some advice and directions on how to do this.

Many thanks!

0
4 comments

Why exactly do you need this separation between your completion items and default ones? What alternatives have you considered?

0

Sorry for my late reply. 

The problem I'm facing is that I’m expecting to have quite a lot of completion results from my custom `CompletionContributor`. These custom completion results are of a different kind(s) (think of how basic completion is a different kind of code completion than postfix completion or live template) than the default completion results. Having them mixed with the default results can make it too long for the user to scroll through and pollute the final result set with too many different kinds of completion.

So, I think it’d be logical to separate my custom completion results into a different list. If this is possible I’m also wanting to split my custom completion results into smaller ones based on their kinds and have them triggered by different characters/syntaxes.

I hope this clarifies my question.

0

IMHO the approach to use distinct “completion prefix characters” that effectively are not part of the language is a bit tricky to implement - and to understand by users.

I’d suggest to try using com.intellij.codeInsight.completion.CompletionParameters#getInvocationCount and show more/different groups of items depending on the number of “Complete” action invocations by users. This could also be supported by showing corresponding hint in completion popup via com.intellij.codeInsight.completion.CompletionResultSet#addLookupAdvertisement.

There are also various ways to group and rank completion items in the final combined completion list to increase usability for users, see CompletionContributor javadoc for some hints. Also, completion items themselves could be styled (e.g. icons or color/bold font...).

0

Thanks for your reply. I'll have a look into `com.intellij.codeInsight.completion.CompletionParameters#getInvocationCount`.

0

Please sign in to leave a comment.