Code Completion
Two main types of code completion can be provided by custom language plugins: reference completion and contributor-based completion.
Reference completion is easier to implement, but supports only the basic completion action. Contributor-based completion provides more features, supports all three completion types (basic, smart, and class name), and can be used, for example, to implement keyword completion.
Reference Completion
To fill the completion list, the IDE calls PsiReference.getVariants()
either on the reference at the caret location or on a dummy reference that would be placed at the caret. This method needs to return an array of objects containing either strings, PsiElement
instances or instances of the LookupElement
class (see Lookup Items below). If a PsiElement
instance is returned in the array, the completion list shows the icon for the element.
The most common way to implement getVariants()
is to use the same function for walking up the tree as in PsiReference.resolve()
, and a different implementation of PsiScopeProcessor
which collects all declarations passed to its execute()
method and returns them as an array for filling the completion list.
Symbol Reference Completion
To provide completion variants by a PsiSymbolReference
implement PsiCompletableReference
.
Contributor-Based Completion
Implementing the CompletionContributor
interface gives you the greatest control over the operation of code completion for your language. Register in com.intellij.completion.contributor
extension point and specify language
attribute (unless it works on any supported language).
The core scenario of using CompletionContributor
consists of calling the extend()
method and passing in the Element Pattern specifying the context in which this completion variant is applicable, as well as a completion provider which generates the items to show in the completion list.
Keep in mind that the pattern is checked against the leaf PSI element. If you want to match a composite element, use withParent()
or withSuperParent()
methods.
If completion items do not depend on indexes (e.g., keywords), it can be marked as dumb aware.
Examples:
CompletionContributor
for completing keywords in MANIFEST.MF files.
Lookup Items
Items shown in the completion list are represented by instances of the LookupElement
interface. These instances are typically created through the LookupElementBuilder
class.
For every lookup element, you can specify the following attributes:
Text Shown left-aligned.
Tail text Shown next to the main item text, is not used for prefix matching, and can be used, for example, to show the parameter list of the method.
Type text Shown right-aligned in the lookup list and can be used to show the return type or containing class of a method, for example.
Icon
Text attributes Bold, Strikeout, etc.
Insert handler The insert handler is a callback which is called when the item is selected and can be used to perform additional modifications of the text (for example, to put in the parentheses for a method call)
Code Completion FAQ
Showing Completion Popup Programmatically
Completion Popup Events
Use LookupListener
to receive notifications about completion popup lifecycle events.