The Rename refactoring operation is quite similar to that of Find Usages. It uses the same rules for locating the element to be renamed and the same index of words for finding the files that may have references to the element being renamed.
When the rename refactoring is performed, the method
PsiNamedElement.setName() is called for the renamed element, and
PsiReference.handleElementRename() is called for all references to the renamed element.
These methods perform basically the same action: replace the underlying AST node of the PSI element with the node containing the new text entered by the user.
Creating an entirely correct AST node from scratch is quite tricky.
Thus, surprisingly, the easiest way to get the replacement node is to create a dummy file in the custom language so that it would contain the necessary node in its parse tree, build the parse tree and extract the required node from it.
setName() implementation for a Properties language plugin
- Custom Language Support Tutorial: Reference Contributor
To disable renaming for specific elements, implement
com.intellij.openapi.util.Condition<T> for PsiElement of type
T and register it in
com.intellij.vetoRenameCondition extension point.
NamesValidator allows a plugin to check if the name entered by the user in the
Rename dialog is a valid identifier (and not a keyword) according to the custom language rules.
If an implementation of this interface is not provided by the plugin, Java rules for validating identifiers are used.
NamesValidator are registered in the
com.intellij.lang.namesValidator extension point.
Custom Rename UI and Workflow
Further customization of the Rename refactoring processing is possible on multiple levels.
Providing a custom implementation of the
RenameHandler interface allows you to entirely replace the UI and workflow of the rename refactoring, and also to support renaming something which is not a
PsiElement at all.
If you're okay with the standard UI but need to extend the default logic of renaming, you can provide an implementation of the
This allows you to:
- Rename an element different from the one on which the action was invoked (a super method, for example)
- Rename multiple elements at once (if their names are linked according to the logic of your language)
- Check for name conflicts (existing names, etc.)
- Customize how a search for code references or text references is performed