A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://plugins.jetbrains.com/docs/intellij/references-and-resolve.html below:

References and Resolve | IntelliJ Platform Plugin SDK

References and Resolve

One of the most important and tricky parts in implementing a custom language PSI is resolving references. Resolving references allows users to navigate from a PSI element usage (accessing a variable, calling a method, etc.) to the declaration of that element (the variable's definition, a method declaration, and so on).

This feature is required to support the action invoked by Ctrl/Cmd+B or clicking the mouse button while holding Ctrl/Cmd key, and it is a prerequisite for implementing finding usages, rename refactoring, and code completion.

The action is based on the same mechanism, so it becomes automatically available for all references that can be resolved by the language plugin. To customize the exact document range to show in the popup (e.g., include "surrounding" code or comments), provide ImplementationTextSelectioner registered in com.intellij.lang.implementationTextSelectioner extension point.

PSI References

All PSI elements which work as references (for which the action applies) need to implement the PsiElement.getReference() method and to return a PsiReference implementation from that method. The PsiReference can be implemented by the same class as PsiElement, or by a different class. An element can also contain multiple references (for example, a string literal can contain multiple substrings which are valid fully qualified class names), in which case it can implement PsiElement.getReferences() and return the references as an array. To optimize PsiElement.getReferences() performance, consider implementing HintedReferenceHost to provide additional hints.

The primary method of the PsiReference interface is resolve(). It returns the element to which the reference points, or null if it was not possible to resolve the reference to a valid element (for example, should it point to an undefined class). The resolved element should implement the PsiNamedElement interface. To enable more advanced functionality, prefer implementing PsiNameIdentifierOwner over PsiNamedElement where possible.

While the referencing element and the referenced element both may have a name, only the element which introduces the name (e.g., the definition int x = 42) needs to implement PsiNamedElement. The referencing element at the point of usage (e.g., the x in the expression x + 1) should not implement PsiNamedElement since it does not have a name.

A counterpart to the resolve() method is isReferenceTo(), which checks if the reference resolves to the specified element. The latter method can be implemented by calling resolve() and comparing the result with the passed PSI element. Still, additional optimizations are possible (for example, performing the tree walk only if the element text is equal to the text of the reference).

Examples:

Implementing Resolve Logic

There is a set of interfaces that can be used as a base for implementing resolve support, namely the PsiScopeProcessor interface and the PsiElement.processDeclarations() method. These interfaces have several extra complexities that are unnecessary for most custom languages (like support for substituting Java generics types). Still, they are required if the custom language can have references to Java code. If Java interoperability is not required, the plugin can forgo the standard interfaces and provide its own, different implementation of resolve.

See also Cache Results of Heavy Computations.

The implementation of resolve based on the standard helper classes contains the following components:

Resolving to Multiple Targets

PsiPolyVariantReference is an extension of PsiReference and allows a reference to resolve to multiple targets. The targets to which the reference resolves are returned from the multiResolve() method. The action for such references allows the user to choose a navigation target in a popup. The implementation of multiResolve() can be also based on PsiScopeProcessor, and can collect all valid targets for the reference instead of stopping when the first valid target is found.

Consider using PsiPolyVariantReferenceBase as a base class for PsiPolyVariantReference implementations.

Additional Highlighting

Implement HighlightedReference to add additional highlighting for non-obvious places (e.g., inside String literals). Such references will automatically be highlighted using String | Highlighted reference text attributes from .

16 April 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