10. Reference Contributor
The references functionality is one of the most important parts in the implementation of custom language support. Resolving references means the ability to go from the usage of an element to its declaration, completion, rename refactoring, find usages, etc.
Define a Named Element Class
The classes below show how the Simple Language fulfills the need to implement PsiNamedElement
.
The SimpleNamedElement
interface is subclassed from PsiNameIdentifierOwner
.
The SimpleNamedElementImpl
class implements the SimpleNamedElement
interface and extends ASTWrapperPsiElement
.
Define Helper Methods for Generated PSI Elements
Modify SimplePsiImplUtil
to support new methods that get added to the PSI class for Simple Language. Note that SimpleElementFactory
isn't defined until the next step, so for now it shows as an error.
Define an Element Factory
The SimpleElementFactory
provides methods for creating SimpleFile
.
Update Grammar and Regenerate the Parser
Now make corresponding changes to the Simple.bnf grammar file by replacing the property
definition with the lines below. Don't forget to regenerate the parser after updating the file! Right-click on the Simple.bnf file and select Generate Parser Code.
Define a Reference
Now define a reference class SimpleReference.java
to resolve a property from its usage. This requires extending PsiReferenceBase
and implementing PsiPolyVariantReference
. The latter enables the reference to resolve to more than one element or to resolve result(s) for a superset of valid resolve cases.
Define a Reference Contributor
A reference contributor allows the simple_language_plugin
to provide references to Simple Language from elements in other languages such as Java. Create SimpleReferenceContributor
by subclassing PsiReferenceContributor
. Contribute a reference to each usage of a property:
Register the Reference Contributor
The SimpleReferenceContributor
implementation is registered using the com.intellij.psi.referenceContributor
extension point and specifying language="JAVA"
.
Run the Project with the Reference Contributor
Run the project by using the Gradle runIde
task.
The IDE now resolves the property and provides completion suggestions:
The Rename refactoring functionality is now available from definition and usages.
Define a Refactoring Support Provider
Support for in-place refactoring is specified explicitly in a refactoring support provider. Create SimpleRefactoringSupportProvider
by subclassing RefactoringSupportProvider
As long as an element is a SimpleProperty
it is allowed to be refactored:
Register the Refactoring Support Provider
The SimpleRefactoringSupportProvider
implementation is registered with the IntelliJ Platform in the plugin configuration file using the com.intellij.lang.refactoringSupport
extension point.
Run the Project
Run the project by using the Gradle runIde
task.
The IDE now supports refactoring suggestions: