4

I am working on a plugin on IntelliJ. I have a problem like this:

It's known to us that if we are typing a key word, IntelliJ will using its Code Completion to give our tips like the picture shows.

enter image description here

Now I have a JTextField in my plugin on IntelliJ which need such function like this: when I fill some words to the JTextField, it will provide Code Completion tips like the picture shows. Does IntelliJ has some open api to do that? I do lots of work to investigate how to do that. but nothing gained.

0

1 Answer 1

2

For JTextField, IntelliJ IDEA has no such API. IDEA's completion requires IDEA's Editor to work. You can create one using EditorFactory (and don't forget to release it from the same EditorFactory after it's not needed). If your editor is one-line, you might consider using EditorTextField instead which makes things easier. Both have getComponent() methods that return usual Swing components that can be embedded into your GUI.

Then you can either show/hide/handle the popup manually using LookupManager methods, or rely on the generic IDE completion machinery which will do many things automatically. But the latter requires you to associate the editor's document with a PsiFile (probably a plain text one, see PsiFileFactory.createFileFromText) and creating a CompletionContributor that would provide suggestion variants depending on the position in your file.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer. It seems what I need. BTW, how can I using LookupManager enable function of Code Completion in EditorTextField? I am trying to use LookupManager.getInstance(project).showLookup(...), but noting happened when I fill some word in the EditorTextField, something wrong with it?
Most likely something else needs to be done on your side. But I can't tell you right away what it is. Please ensure that the editor is already shown on the screen when you call showLookup. showLookup should return a non-null Lookup. If that's all the case, I can suggest putting breakpoints in the source, around showLookup method and LookupImpl.doHide. Maybe this will help figure the issue out.
@PeterGromov Thanks for the explanation. A question, the Java completion popup adds items "asynchronously"; does using LookupManager produce the same effect or is it a blocking component?
@LppEdd LookupManager is just an API to manage the popup (Lookup) itself. Lookup in turn supports item addition via LookupImpl#addItem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.