mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-30 18:27:49 +08:00
Grammar and wording tweaks
This commit is contained in:
parent
5a9dfa89fa
commit
b0bae80759
@ -2,9 +2,9 @@
|
||||
title: References and Resolve
|
||||
---
|
||||
|
||||
One of the most important and tricky parts in the implementation of a custom language PSI is resolving references.
|
||||
Resolving references means the ability to go from the usage of an element (access of a variable, call of a method and so on) to the declaration of the element (the variable definition, the method declaration and so on).
|
||||
This is obviously needed in order to support `Go to Declaration` action invoked by **Ctrl-B** and **Ctrl-Click**, and it is also a prerequisite for the `Find Usages` action, the `Rename` refactoring and the code completion.
|
||||
One of the most important and tricky parts in implementing a custom language PSI is resolving references.
|
||||
Resolving references gives users the ability to navigate from a PSI element usage (accessing a variable, calling a method and so on) to the declaration of that element (the variable's definition, a method declaration and so on).
|
||||
This feature is needed in order to support the `Go to Declaration` action invoked by **Ctrl-B** and **Ctrl-Click**, and it is a prerequisite for implementing the `Find Usages` action, the `Rename` refactoring and code completion.
|
||||
|
||||
All PSI elements which work as references (for which the `Go to Declaration` action applies) need to implement the
|
||||
[PsiElement.getReference()](https://upsource.jetbrains.com/idea-community/file/1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/psi/PsiElement.java)
|
||||
@ -21,9 +21,9 @@ and return the references as an array.
|
||||
|
||||
The main method of the
|
||||
[PsiReference](https://upsource.jetbrains.com/idea-community/file/1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/psi/PsiReference.java)
|
||||
interface is `resolve()`, which 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, it points to an undefined class).
|
||||
interface is `resolve()`, which 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).
|
||||
A counterpart to this 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, but additional optimizations (for example, performing the tree walk only if the text of the element is equal to the text of the reference) are possible.
|
||||
The latter method can be implemented by calling `resolve()` and comparing the result with the passed PSI element, but additional optimizations are possible (for example, performing the tree walk only if the element text is equal to the text of the reference).
|
||||
|
||||
**Example**:
|
||||
[Reference](https://upsource.jetbrains.com/idea-community/file/1731d054af4ca27aa827c03929e27eeb0e6a8366/plugins/properties/src/com/intellij/lang/properties/ResourceBundleReference.java)
|
||||
@ -71,9 +71,9 @@ interface, which allows a reference to resolve to multiple targets, is the
|
||||
[PsiPolyVariantReference](https://upsource.jetbrains.com/idea-community/file/1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/psi/PsiPolyVariantReference.java)
|
||||
interface.
|
||||
The targets to which the reference resolves are returned from the `multiResolve()` method.
|
||||
The `Go to Declaration` action for such references allows the user to choose the target to navigate to.
|
||||
The `Go to Declaration` action for such references allows the user to choose a navigation target.
|
||||
The implementation of `multiResolve()` can be also based on
|
||||
[PsiScopeProcessor](https://upsource.jetbrains.com/idea-community/file/1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/psi/scope/PsiScopeProcessor.java),
|
||||
and can collect all valid targets for the reference instead of stopping when the first valid target is found.
|
||||
|
||||
The `Quick Definition Lookup` action is based on the same mechanism as `Go to Declaration`, so it becomes automatically available for all references which can be resolved by the language plugin.
|
||||
The `Quick Definition Lookup` action is based on the same mechanism as `Go to Declaration`, so it becomes automatically available for all references that can be resolved by the language plugin.
|
||||
|
Loading…
x
Reference in New Issue
Block a user