CodeCompletion not working with prefixes

Hi, I'm getting this error while trying to get the completion list:

    Method threw 'com.intellij.openapi.progress.ProcessCanceledException' exception.

I'm adding my own code completion contributor for some XML files, and it's working fine.
This error only happens when I'm requesting the completion list after something like "<t"
If I request the list in a white space it works as expeced.
If I request the list after "<" it works but it tries to insert a new "<".

Do you know what could be the cause of this behavior?
I'm adding a string like "<sometag></sometag>" with the LookupElement.
So I was expecting if I write "<s" and press Ctrl+Space then have the autocomplete for "<sometag></sometag>"

My results are calculated correctly and my completion method is finishing without throwing any error.
I've tried adding "_result.stophere();" but it stills throwing the error (I don't see the error in the IDE, I'm seeing only debugging, the IDE just doesn't show any completion list)

This is my code for the LookupElement:

String name = entry.getKey();

LookupElement element = LookupElementBuilder.create(entry.getValue())    // entry.getValue() == "<sometag></sometag>"
//LookupElement element = LookupElementBuilder.create("I was thinking in using this string inside my weigher", entry.getValue())
   .setPresentableText(name)
   .setIcon(myIcon)
   .setInsertHandler(QUOTE_EATER);

_result.addElement(element);



If I execute the code before (LookupElementBuilder without Object), the behavior is a bit different.
I'm not getting the list when I request it after "<s" but when I request it after "<" it's not adding another "<" instantly (without selecting any item in the completion list)
But If I choose the item from the completion list (with enter, click or tab, it doesn't matter) it inserts something like: "<<sometag></sometag>" (note the duplicated "<")

Please tell me what I'm doing wrong, I don't know how to solve this :(

Thanks !!

3 comments
Comment actions Permalink

I think I found something:
I guess we need to make the prefix filtering by ourself (and then Idea is doing it again...)

If I do this:

final String prefix = _result.getPrefixMatcher().getPrefix();
...
if( prefix == null || value.startsWith(prefix, 1))
{
                                LookupElement element = LookupElementBuilder.create(prefix+value)
                                        .setPresentableText(name)
                                        .setIcon(myIcon)
                                        .setInsertHandler(INSERT_HANDLER);

                                _result.addElement(element);
}


Then I'm getting the completion list when I have something like "<s"

But I don't get how this works.
Also, the completion is not nice, because after selecting the item in the list, I get again "<s<sometag></sometag>"

I think this is because the "prefix" is not getting the initial "<", that is, if I write "<" and press Ctrl+Space the prefix is null, and if write "<s" and press ctrl+space the prefix is "s".

Can you help me to understand this and to do it in the right way?
Because I think I could "hack" these things, and even make some "fixes" in my INSERT_HANDLER, but I think I'm taking the wrong and then hard way.
I'm sure there's a better solution for this, am I right?

0
Comment actions Permalink

I assume you tried the hint given in javadoc for com.intellij.codeInsight.completion.CompletionContributor ?

0
Comment actions Permalink

This is what I'm doing right now:

// force prefix to begin with "<" if it's a new tag
                final PsiElement currentElement = parameters.getPosition();
                PsiElement prevSibling = currentElement.getPrevSibling();
                if( prevSibling != null && ! prefix.startsWith("<") )
                {
                    if( prevSibling.getText().equals("<") ||
                        prevSibling.getNode().getElementType().toString() == "XML_START_TAG_START" )
                    {
                        result = _result.withPrefixMatcher("<"+prefix);
                    }
                }


And it works, but I don't know, I feel I'm doing this wrong.
Can you help me to go in the right direction?

Thanks !

0

Please sign in to leave a comment.