Rename Refactoring
The operation of the Rename refactoring 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 locating the files which 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.
Both of 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 a fully correct AST node from scratch is quite difficult.
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 necessary node from it.
Example:
setName()
implementation for a
Properties language plugin
Name Validation
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.
Implementations of
NamesValidator
are registered in the com.intellij.lang.namesValidator
extension point.
Example:
NamesValidator
for
Properties language plugin
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.
Example:
RenameHandler
for renaming a resource bundle in the
Properties language plugin
If you’re fine with the standard UI but need to extend the default logic of renaming, you can provide an implementation of the
RenamePsiElementProcessor
interface.
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 search for code references or text references is performed
-
etc.
Example:
RenamePsiElementProcessor
for renaming a property in
Properties plugin language