Renaming files in IDE

Hi,

I would like to know what the best way is to take an action in the code of the file when renaming a file and vice versa. I want to do something similar like in Java, where you can rename the class name when you rename the file name and vice versa. Can somebody point me to the documentation about that? I already found (and implemented) how to perform renames of elements in the files themselves, to perform renames of functions and so on, but renaming the file seems to elude me. The only documentation I found about that mentioned something like using the VirtualFileManager's event handler, but that dated from 2003. If that is really the case, where is the best place to register such an event handler in a plugin?

Kasper

4 comments
Comment actions Permalink

The correct way to implement this depends on whether you want the rename of the file to happen automatically or not. If you want it to be optional (where the user can choose whether to rename the file according to the name of the class), you need to use the AutomaticRenamerFactory API. Here's an example:

https://upsource.jetbrains.com/ideac/file/HEAD/python%2Fsrc%2Fcom%2Fjetbrains%2Fpython%2Frefactoring%2Frename%2FPyContainingFileRenamerFactory.java

If you want the rename to happen unconditionally, you can extend RenamePsiFileProcessor and add the file to the list of elements to be renamed. Here's an example for that behavior:

https://upsource.jetbrains.com/ideac/file/HEAD/plugins%2Fgroovy%2Fsrc%2Forg%2Fjetbrains%2Fplugins%2Fgroovy%2Frefactoring%2Frename%2FRenameGroovyScriptProcessor.java

1
Comment actions Permalink

Okay, this worked, I also added the class in the plugin.xml as renamePsiElementProcessor, when I now rename a file, I can also rename the 'class' name. However, for the other way around, when doing a class rename, I don't manage to rename the file. When I try this from HaskellReference I seem to get a stack trace. What's the canonical way of doing the 'inverse' rename, from 'class' name to file name?

0
Comment actions Permalink

What's the stacktrace that you get?

Normally there's one 'canonical' direction for the rename; for the opposite direction, you can implement RenamePsiElementProcessor.substituteElementToRename() to change the rename to a 'canonical' one.

1
Comment actions Permalink

My fault, I was trying to rename the file through the PsiFile setname function. My penny dropped I just have to add it to the allRenames map, together with my 'class' rename, which then gets handled through the normal PsiReferenceBase system. I added the imports and class renames to the substituteElementToRename function just liky said, substituting those renames for file renames and the tests seem to indicate it works. Running the plugin also indicates it seems to work, Thanks for the info!

0

Please sign in to leave a comment.