Code completion with different prefixes

Hello,
I'm writting a plugin in which I use the code completion. But I don't know how to writte such behaviour:
the user types e.g "abc" and press CTRL+SPACE.
Its no probleme to offer him completion variants such "abcd", "abczyx" etc.
But I want to offer him variants like "Prefix1.abcd", "Prefix2.abczyx", "XYZ.abcdef" too. But how?
My method
public CompletionVariant[] findVariants(PsiElement psiElement, CompletionContext completionContext)
returns all desired variants, but in the pop-up the user see are only variants starting with "abc" string.
Can somebody help?
Thanx.

7 comments

Hello Karel,

I'm writting a plugin in which I use the code completion. But I don't
know how to writte such behaviour:

the user types e.g "abc" and press CTRL+SPACE.

Its no probleme to offer him completion variants such "abcd", "abczyx"
etc.

But I want to offer him variants like "Prefix1.abcd",
"Prefix2.abczyx", "XYZ.abcdef" too. But how?

My method

+*public CompletionVariant[] findVariants(PsiElement psiElement,
CompletionContext completionContext)*+

returns all desired variants, but in the pop-up the user see are only
variants starting with "abc" string.


The correct value to handle that is to return variants without a prefix from
your findVariants() method, and to insert the necessary prefix from your
InsertHandler implementation.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

I'm affraid it will not help, because I need to show the prefixes in the pop-up, but the InsertHandler comes to the scene after the user selects one variant from the pop-up.
For example: there are two variants: "ABC.def" and "XYZ.def". If the user writtes "de" and press CTRL+SPACE, i cannot show him only "def" in the pop-up , I must show him both variants and with their prefixes.

0

There can be another partial solution: I can change the variants in the popup to start with the string the user typed: if I have variants "ABC.def" and "XYZ.def" and the user typed "de", I can show him i the popup something like this:
def (ABC.def)
def (XYZ.def)

But in such case I don't know, how to force the InsertionHandler (or something else) to write to the editor the string "def" instead of "ABC.def". I try to call lookupItem.setLookupString("def"), but it still pastes the text "ABC.def"

0

LookupItem.addLookupStrings()

0

But where should I use the LookupItem.addLookupStrings()? In the InsertHandler it is too late, I think, and in the method CompletionData.findVariants(PsiElement psiElement, CompletionContext completionContext) I have no LookupItem instance.

0

Oh, I see, I have to use it in the InsertHandler. I don't know what, but something is wrong with my InsertHandler. I have a class MyInsertHandler implements InsertHandler and its method public void handleInsert is empty (I do nothing there, only System.out.println("xxx"); ). So I expect, that no insertion of the selected variant will be done, but the selected variant is inserted. In the console I see "xxx" so the handleInsert method of MyInsertHandler was executed. There is another InsertHandler that executes the insertion?
So under these circumstances above, if I use LookupItem.addLookupStrings() in MyInsertHandler, it doesn't work. I had to register my own CodeCompletion action and overwritte its method
+ protected CodeInsightActionHandler getHandler() {
  return new CodeCompletionHandler() {
    protected Lookup showLookup(...) {
     lookupItem.addLookupStrings(...)
     }
  }
+

0

When any InsertHandler is executed, the lookup string of selected
variant is already inserted. InsertHandler is needed only when such
behavior doesn't satisfy you and you want to do something better.

You return something from your CompletionVariant, donh't you? I guess,
you use ContextGetter, and return Object[]. Or you return it from
PsiReference.getVariants(). You may return not String's, but
LookupElement's (see LookupElementFactory). And you may cast
LookupElement to LookupItem and add more lookup strings.

0

Please sign in to leave a comment.