How to capture the old and new name of renamed identifiers?

Answered

I am new to plugin development so would really appreciate some help with my question

My requirement is to hook into IntelliJ's rename refactoring for all identifiers in the source code. Specifically, when a developer uses the IntelliJ's rename feature I want to capture the old and new name of the identifier.

I created a class that implements RefactoringElementListenerProvider and override elementRenamedOrMoved. Debugging the code, I can see that elementRenamedOrMoved is called when a method or class is renamed. However, it is not called when a variable or parameter is renamed. Additionally, I cannot obtain the old name of the method or class that was renamed.

Can someone please guide me on the right approach to capturing the old and new name of renamed identifiers?

 

Thanks!

3 comments
Comment actions Permalink

Hi Anthony,

inlace rename for local variables/parameters indeed doesn't fire listener events. If you would call the refactoring second time and dialog would be shown, then listener would work though (looks like a bug to me). Listener is fired in all cases after rename is done and initial name is not available anymore. 

Could you please elaborate what kind of action are you going to perform based on this information? You can e.g. listen to psi or document changes, etc but in order to advise I need to know more about a use case

Thanks,

Anna

0
Comment actions Permalink

Hi Anna,

The purpose of the plugin is to capture all renames made by a developer in a project. The plugin needs to capture the old and new name of the identifier.

Since you mentioned that there is a bug with the refactoring API, can you let me know how I can go about listening to the psi changes?

0
Comment actions Permalink

Hi,

appropriate API would be probably `com.intellij.refactoring.listeners.RefactoringEventListener` but local variables and parameters are renamed without real refactoring

Psi changes may be checked in `com.intellij.psi.PsiTreeChangeListener` though it's complicated to use in your case as you would probably need to check the surrounding command name to ensure that it's a `rename`.

Sorry for the inconvenience

Anna 

0

Please sign in to leave a comment.