Search code examples
intellij-ideaintellij-plugin

Manually trigger completion popup in IntelliJ Platform SDK


I am developing a plugin for the IntelliJ Platform. I have created an implementation of CompletionContributor, and it is successfully giving suggestions. I'm currently overriding the invokeAutoPopup method to trigger the completion popup. I'm using an implementation of InsertHandler to add additional text to the document immediately after making the selected insertion.

However, I'd like to make it so that when a user successfully inserts the suggestion, they are immediately prompted with another completion popup. For example, in this InsertHandler (written in Kotlin), after selecting a method, they should be immediately given suggestions about the parameters to that method:

val handler = InsertHandler<LookupElement> { context: InsertionContext, element: LookupElement ->
  val offset = context.tailOffset
  context.document.insertString(offset, "()")
  context.editor.caretModel.moveToOffset(offset + 1)
  TODO("trigger popup")
}

Or immediately after selecting a field, they could be given methods that they can call on that field:

val handler = InsertHandler<LookupElement> { context: InsertionContext, element: LookupElement ->
  val offset = context.tailOffset
  context.document.insertString(offset, ".")
  context.editor.caretModel.moveToOffset(offset + 1)
  TODO("trigger popup")
}

In other words, I'd like to make it so that after inserting suggested text, it is as if the user pressed Ctrl+Space. Is this possible? Am I approaching the problem in the right way? Is there something in my code above that's missing the point? (Solutions in either Java or Kotlin would be welcome.)


Solution

  • If autopopup completion is enough for your needs, you could try invoking com.intellij.codeInsight.AutoPopupController.getInstance(context.project).scheduleAutoPopup(context.editor).