N4JS IDE provides a powerful way of automatically renaming a definition and all its references with a comparable user experience as rename refactoring of Eclipse Java Development Tool (JDT). The slogan is: I want to rename this thing, do it for me however you like but please in a safe manner so that I can move on! This will exactly be your experience with rename refactoring in N4JS IDE.
Simple rename example
Let's have look at a simple example to see how rename refactoring works in N4JS IDE in action. Assume that we have an N4JS file with the following content.Name conflicts detection
Renaming an element may cause name conflicts. The rename refactoring in N4JS IDE provides comprehensive checks for detecting name conflicts. If the new name would cause name conflicts, the rename refactoring is disallowed.In the example above, renaming class A to class C would cause a name conflict because in the script scope the name C already exists. The rename refactoring provided by N4JS IDE can recognize this conflict and shows an error message.
In a large code base, these checks are a true life saver. Imagine having to manually verify these kinds of name conflicts across hundred of files.
Additionally, N4JS IDE's rename refactoring is capable of recognizing name conflicts when renaming
- members of classifier
- formal parameters of a function or method
- field of a structural type
- enum literal
- local variable, constant
- global variable, constant
- etc.
Rename composed members
N4JS language supports composed elements. Renaming a composed element is somewhat special.
In this example, ab.foo is a composed member because ab is of the intersection type A & B which is composed of both A and B. Renaming ab.foo would rename all the definitions that contribute to the creation of ab.foo as well as all references of these definitions.
In this example, ab.foo is a composed member because ab is of the intersection type A & B which is composed of both A and B. Renaming ab.foo would rename all the definitions that contribute to the creation of ab.foo as well as all references of these definitions.
Preview of changes
When you start rename refactoring operation, you have the possibility to see the preview of changes before actually executing the operation.
Undo changes
After the rename refactoring, if you feel regret and would like to undo the operation, simply press Cmd + Z. The undo will undo all the changes in affected files previously done by the rename refactoring.
Current limitations
As the time of this writing, the rename refactoring in N4JS IDE still has several limitations:
- Renaming alias is not supported
- Checking name conflicts do not take into account shadowing
By Minh Quang Tran