Undo programmatically

Can i undo few CommandProcessor's command in one action? Can i group several actions in transaction? Otherwise user should use Ctrl+Z multiple times and it looks ugly.

P.S.I know count of actions, so if i could undo programatically i will do it serially in loop.

14 comments
Comment actions Permalink

If your commands specify the same groupId, they will be undone in one step.

1
Comment actions Permalink

I use 

FileTemplateUtil.createFromTemplate(...)

which incapsulate his own CommandProcessor.getInstance().executeCommand(..)

Is it possible to add this action or wrap it in other action for undo in one step?

 

0
Comment actions Permalink

You can try to wrap it in your own command - only top-level command should matter.

0
Comment actions Permalink

I set groupId, but Ctrl+z still undone each action separatelly. (It's not wrapping, i just testing groupId with my actions)

0
Comment actions Permalink

This usually works for simple text editing actions. For actions or commands marked as 'global' (com.intellij.openapi.command.undo.UndoableAction#isGlobal, com.intellij.openapi.command.CommandProcessor#markCurrentCommandAsGlobal) this doesn't seem to be the case though. I'll try to find out whether it's possible to achieve what you want for such actions/commands.

1
Comment actions Permalink

Arsen, 

which actions exactly do you want to compose into one undoable action?

0
Comment actions Permalink

 Anton Makeev, 

  • FileTemplateUtil.createFromTemplate(...)
  • Creating directories(maybe files too)
  • Editing existing files (insert\replace text)
0
Comment actions Permalink

Dmitry Batrak, Anton Makeev, any ideas?

0
Comment actions Permalink

Is is possible to take a look at the code?

0
Comment actions Permalink

Looks like each command executed separately and deferred in invokeLater.

Please try doing all tasks under one CommandProcessor.getInstance().executeCommand.

0
Comment actions Permalink

It works with dirs

but don't work with

FileTemplateUtil.createFromTemplate

which incapsulate:

CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {

So 2 dirs + template reverts in 2 step, both dirs in one action, template in another.

0
Comment actions Permalink

Command might be broken by dialogs, template steps etc. but I'm afraid you'll need to debug the specific code to make sense of it.

Start with com.intellij.openapi.command.impl.UndoManagerImpl#commandStarted/commandFinished and check what exactly breaks the commands apart.

 

0
Comment actions Permalink

After spending several days in debugging i figure out that the problem isn't in Idea, actually error in my code.So "dir" with "CreateFromTemplate" wrapped in one Command undone fine. 

0

Please sign in to leave a comment.