CompletionContributor: how to use withPrefixMatcher?


I deeply appreciate the new CompletionContributor API with all the Javadoc, however i still can't figure out how can i override the PrefixMatcher.

Details: i have PsiNamedElement with text "$abc" and getName() for it returns just "abc". I want the user when he/she types "$" and invokes completion to see "abc" in the completion list and not "$abc".
All the variants for completion are found through getVariants() of "$abc" element.

Here's what i'm trying to do, which is wrong i guess:

public boolean fillCompletionVariants(CompletionParameters parameters, CompletionResultSet result) {
    if (parameters.getPosition().getParent() instanceof Variable) {
      result = result.withPrefixMatcher(new VariablePrefixMatcher(result.getPrefixMatcher().getPrefix()));

    return super.fillCompletionVariants(parameters, result);

What should i do instead of this for the code to work?

Thanks, jay


The simplest way is to change reference getRangeInElement() method so
that it doesn't include the leading $ character, and not mess with
prefix matchers at all.


I've did it that way right now, but i still want to know how to do it with the prefixMatcher ;-)


You could create your own CompletionContributor, check the caret
position there, somehow extract the prefix from file text, obtain new
matcher via CompletionResultSet#withPrefixMatcher() and feed your lookup
elements to that new matcher. They would be checked against the
specified new prefix and, if they are matched, eventually show in lookup.


Please sign in to leave a comment.