How to intercept Undo/Redo operations?
Is there a way for me to intercept undo/redo actions performed by user and perform my own custom task after the undo is finished?
As an example, when the user undo a text insert, I want to intercept that and filter it by command name or group Id, and then perform my own custom task like writing to a log file.
Thanks
请先登录再写评论。
No direct way to intercept undo, but any undo/redo is a command, that can be listened via com.intellij.openapi.command.CommandListener
Hi Anton,
By using CommandListener, is there a way to distinguish an undo command from an actual command? As an example, a user might insert some text in the editor (which is a valid command). The same operation, i.e. inserting text in the editor window can happen due to an undo operation as well (when user press cmd + z) I want to distinguish these two scenarios.
It should be possible to check UndoManager.isUndo/RedoInProgress inside a command
I have tried that. But both UndoManager.isUndo/RedoInProgress returns false. But I was able to identify the the action with its name. All the undo/redo actions are prefixed with the name Undo/Redo respectively.
But the problem is, in my case, there are so many actions, and using the command name to filter out those is inefficient. Further, I have tries to filter out the commands via Group ID, but unfortunately, the group ID is null, since the command is initiated by UndoManagerImpl.
Apart from the command name, there is no contextual information to identify the command. Even the document is null, hence, there is no way to find out if the undo operation is related to the Editor view or for another area (e.g: project view) in the IDE.
Is there any way to obtain contextual information about the Undo operation?
I'm afraid there is no special API for that. Could you please elaborate, what kind on info do you need and what for?
In my plugin, for a particular file extension, there are two editor views. One is text view and the other one is a graphical view which I have implemented.
When the user perform undo/redo operation, the text view is updated and I have to manually do the necessary changes in the graphical view to reflect the undo/redo changes. For that I want to intercept the undo/redo operations.
What I have done so far is create a CommandListener for each editor and add that to the CommandProcessor(within constructor) and remove the listener from CommandProcessor when the editor is disposed.
In the CommandListener implementation, the commandFinished() method, the CommandEvent parameter can be used to get details about the command executed. But as I have mentioned earlier, only the command name is present for undo/redo operations.
I would like to know which document which is modified and the command operation group as well. Is there any way to obtain these information? At least the information of the document which is being modified.
Hi Anton,
Is there a way to accomplish what I need in IDEA 2017.1?
Sajith,
in case the graphical view might have changes that are not reflected in the document, you might need a custom UndoProvider for them. If you simply need to keep the document and the view in sync, you need to watch for corresponding document modifications and update the view accordingly.