Limit undo to active selection


I have a plugin that mainly offers text editing commands. A nice feature that other editors have is the possibility to restrict undo actions to a specific part of a file. 

More specifically, the functionality I would like to implement is that if there is an active selection in the current editor, pressing my undo action will only undo changes that were made within the selection, even if other changes have been made in other parts of the file.

Is this possible? I have browsed through the code and extension points related to undo, but I haven't found any obvious way of jacking into the undo functionality.


Undo/Redo is much more than just modifying files. Some actions can also undo more complex changes, e.g. project configuration.

See com.intellij.openapi.command.impl.FileUndoProvider as a reference on how the platform tracks such changes. But I think it will be quite hacky/tricky to “patch” the default behaviour for changes in the editor. You might want to introduce a dedicated Undo action in your plugin with the desired behaviour tracking the changes yourself.