Two 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 completion types ( and ), and can be used, for example, to implement keyword completion.
Reference CompletionTo 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.
A common way to implement getVariants()
is to use the same function for walking up the tree as in PsiReference.resolve()
using a PsiScopeProcessor
which collects all declarations passed to its execute()
method and returns them as an array for filling the completion list.
This API is currently in development and thus in experimental state. See also References.
To provide completion variants by a PsiSymbolReference
implement PsiCompletableReference
.
Implementing CompletionContributor
gives the greatest control over the operation of code completion. Register in com.intellij.completion.contributor
extension point and specify the language
attribute (unless it works on any supported language).
Note that the Javadoc of that class contains a detailed FAQ for implementing code completion.
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. The CompletionProvider
then adds 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.
Items shown in the completion list are represented by instances of the LookupElement
interface. These instances are typically created through the LookupElementBuilder
class.
If there's only one lookup item to be shown, the behavior can be customized via AutoCompletionPolicy
.
Shown left-aligned.
Text color/grayed text, Bold, Italic, Underlined, Strikeout
Shown next to the main item text (not used for prefix matching). Can be grayed text. Example: parameter list of a method
Shown right-aligned in the lookup list. Can be grayed text. Example: containing class of a method
See Working with Icons.
The InsertHandler
is called when the item is selected and performs additional modifications of the inserted text. It can also show the completion popup after insertion. Example: add parentheses for a method call (ParenthesesInsertHandler
)
The item presentation can also be performed via (re-usable) LookupElementRenderer
.
For expensive calculations (for example, the presentation of an item to be shown depends on other items), use LookupElementBuilder.withExpensiveRenderer()
to invoke the renderer in the background while the completion list is already populating.
Use PrioritizedLookupElement
to control item sorting if all lookup items are guaranteed to be provided by this CompletionContributor
.
To skip the completion popup in the current context (for example, inside comments), implement CompletionConfidence
registered in com.intellij.completion.confidence
extension point.
This can also be used to prevent skipping provided by other plugins by returning ThreeState.NO
and registering the extension with order="first"
.
To trigger completion upon typing a specific character in the editor, override TypedHandlerDelegate.checkAutoPopup()
registered in com.intellij.typedHandler
extension point.
If all conditions match, invoke AutoPopupController.scheduleAutoPopup()
and return Result.STOP
.
Use LookupListener
to receive notifications about completion popup lifecycle events.
19 May 2025
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4