UndoRefactoringElementListener.undoElementMovedOrRenamed is not called on undo of a file move

Answered

I implement RefactoringElementListenerProvider to listen to rename and move of a file or a directory within the opened project.

The steps:

  • Select a file and move it to a folder by dragging it with mouse
  • RefactoringElementAdapter.elementRenamedOrMoved is called
  • Press Ctrl+Z to undo the move and confirm in the Undo dialog
  • UndoRefactoringElementListener.undoElementMovedOrRenamed is not called

Why RefactoringElementAdapter.elementRenamedOrMoved work on a file move, but UndoRefactoringElementListener.undoElementMovedOrRenamed doesn't work on undo the file move?

0
3 comments

Hi Maksim,

It's hard to help without more details. I suggest finding usages of UndoRefactoringElementListener.undoElementMovedOrRenamed in the platform code and setting breakpoints around to find out why it is not called in your context. If this approach doesn't work for you, please provide a minimum reproducible plugin example, test project and detailed steps to reproduce.

0

Hi Karol,

I did try to debug the code before posting the question here. Unfortunately, the code is pretty complicated, I thought maybe here I get an answer earlier, because the cause may be the design.

Here is a repo with the minimal plugin implementing the whole thing, see https://github.com/KMax/undoElementMovedOrRenamed-not-called-on-undo-file-move.

The steps to reproduce the issue:

  • Run the plugin locally
  • Open an existing or create a new project
  • Create a folder and a file on the same level of the hierarchy
  • Rename the file, you should see in the logs:

elementRenamedOrMoved: PsiFile(plain text):<file name>

  • Press Ctrl+Z to undo the rename, you should see in the logs:

undoElementMovedOrRenamed: PsiFile(plain text):<file name>, …

  • At this point, you can observe that the rename of a file and its undo works as expected. Now let's move the file and undo the move.
  • Select the file in the project sidebar and drag it into the folder, you should see in the logs:

elementRenamedOrMoved: PsiFile(plain text):<file name>

  • Press Ctrl+Z to undo the move and confirm the action, you won't see anything in the logs, because undoElementMovedOrRenamed is not called, although I expect it to be called as in the case of rename.

I guess, the cause is somewhere in MoveFilesOrDirectoriesProcessor. RenameProcess, which is also a subclass of BaseRefactoringProcessor, calls RenameUtil.registerUndoableRename, but MoveFilesOrDirectoriesProcessor doesn't seem to have a similar call.

 

0

Okay, after more debugging, my conclusion is that MoveFilesOrDirectoriesProcessor (or any other processor which would handle the move refactoring) does not create an UndoableAction which would call undoElementMovedOrRenamed method.

I opened a bug report, see https://youtrack.jetbrains.com/issue/IDEA-333312/MoveFilesOrDirectoriesProcessor-does-not-call-undoElementMovedOrRenamed.

0

Please sign in to leave a comment.