IntelliJ Platform Plugin SDK Help

Rename Refactoring

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.

Examples:

If a renamed reference extends PsiReferenceBase, renaming is performed by invoking the ElementManipulator.handleContentChange(), responsible for handling the content change and calculating the text range of reference inside the element.

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.

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: PropertiesNamesValidator for Properties language plugin

Another way to check is RenameInputValidator, unlike NamesValidator it allows you to more flexibly check the entered name for correctness based on the rule defined in the isInputValid() method.

To determine which elements this validator will apply to, override the getPattern() method returning the pattern of the element to validate.

Example: YAMLAnchorRenameInputValidator validating YAML language anchor names

RenameInputValidator can be extended to RenameInputValidatorEx to override the default error message. The getErrorMessage() method should return a custom error message in case of an invalid name, or null otherwise.

Note that getErrorMessage() only works if all RenameInputValidator accept the new name in isInputValid() and the name is a valid identifier for the language of the element.

Example: YamlKeyValueRenameInputValidator validating YAML language keys

Implementations of RenameInputValidator or RenameInputValidatorEx are registered in the com.intellij.renameInputValidator 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.

Example: RenameHandler for renaming a resource bundle in the Properties language plugin

If you're okay 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 a search for code references or text references is performed

  • etc.

Example: RenamePsiElementProcessor for renaming a property in Properties plugin language

Last modified: 29 九月 2022