Any way to automate "create local variable" action?
I am working on a Java test framework project that has a lot of bad patterns in it, including the use of global variables all over the place. I need to refactor this so that individual tests use their own locally declared variable instead of using the global variable. IntelliJ has a feature where if a line of code includes a variable name and assignment where the variable has not been declared yet, you can click on the popup red light bulb icon, click on the option "create local variable X", and it will insert the variable type. For example, it would change...
d = new Date()
... to ...
Date d = new Date()
Is there anyway to automate this action across an entire project or collection of files? What I want to do is simply delete the global variable and then apply the above action, but for the entire project. In some cases, a global variable that I want to convert to local may be referenced in hundreds of places and manually going through each one-by-one is something I'd like to avoid.
Please sign in to leave a comment.
If usages all share the same declaration, you can just try to use "Replace in Files" (cmd+shift+R, it supports regex).
If situation is more complicated, you can try to use "Structural Search and Replace" for that, see reference here https://www.jetbrains.com/help/idea/structural-search-and-replace.html#structural_replace
This thread can be helpful as an example https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006874219-Bulk-refactor-rename-using-structural-search-and-replace