Declarations and References
WARNING This API is available starting from 2020.3 and currently in development and thus in experimental state.
Each symbol may be declared in zero or more places, for example: - a C# partial class is a symbol with several declarations; - a property key is a symbol possibly declared in several files simultaneously; - a Java local variable is a symbol with a single declaration; - and a file is a symbol without declarations; it has only references.
To report a declaration in a PSI element, either:
- implement and register
- or implement
PsiSymbolDeclaration directly in the
The main method of
resolveReference(), which returns the collection of symbols to which the reference points,
plus additional data.
If it is not possible to resolve the reference, for example, if it points to an undefined class, an empty collection gets returned.
A counterpart to the
resolveReference() method is
which checks if the reference resolves to the specified symbol.
This method can be implemented to walk the tree only if the element's text is equal to the reference's text.
For convenience, if the reference can possibly be resolved:
- with a single result, then it might be extended from
- to a single symbol without additional data, then it might be extended from
- to multiple symbols without additional data, then
SymbolResolveResult.fromSymbol() might be used.
Own references are the references found in PSI elements, which are considered as references by the language.
PSI element representing
x * 2 Java expression has an Own reference to a local Java variable, e.g.,
var x = 42,
because this is a reference from Java language point of view, and Java language support uses it, e.g., for code analysis.
To provide Own references by the
PsiElement.getOwnReferences() in the
If the element contains a single reference,
Collections.singletonList() can be used
External references are the references which are not considered as references by the host language. The language support should not rely on their existence/absence, because they might be contributed by other plugins.
PSI element representing
new File("users.txt") Java expression is a string literal from Java language point of view,
but there is a plugin which knows that this literal references a file name, and provides such reference.
External references might be contributed to PSI elements
To allow other plugins to contribute references of
PsiExternalReferenceHost in the
To contribute an External reference to the existing
PsiExternalReferenceHost, implement and register
Implicit references are the references which should be part of the mechanism to obtain a target by a reference, without the inverse ability to search or rename such references by a target.
var keyword in
var x = new Person() Java declaration has an Implicit reference, because it doesn't make sense to obtain the reference by the target class.
At the same time it's possible:
- to navigate to the class by ctrl-clicking
- to start a refactoring (e.g., rename) from the class targeted by this reference;
- to view documentation of the class targeted by this reference.
To provide an Implicit reference, implement and register