Change contents of file when a directory is renamed

Answered

Suppose we have a directory named "dir" in a project, and a language file in the same project (but not necessarily under dir), that has the following text:

dir/some_other_text

This line directly references something in that directory. Therefore, if we change dir's name to something else like "bir" the text of the file should become

bir/some_other_text

How can I implement a feature in my plugin that applies that change to the file (or files) when the relevant directory is renamed?

0
3 comments
Official comment

Ideally you would use PsiReference to resolve to that directory, which would also then handle renaming http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/psi_references.html

 

see com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet

First, its good to know that references are the right way when I need to reference directories.

However, although the above strategy would work just fine, I have an intermediate PSI object that:

  • is not within any file
  • is already referenced by all files that include the directory name
  • it has references to all the directories it is linked to.

The issue I encountered is that when I rename the directory, the reference from the intermediate object to the directory is not created, and thus that intermediate object does not process the refactoring and does not pass the action to the files which reference it.

Do I have to create a reference directly from the file to the directories, or is it somehow possible to use references of references?

0

Please consider using com.intellij.pom.PomRenameableTarget for your intermediate object.

0

Please sign in to leave a comment.