Question

I am working on a plugin for IntelliJ Idea 13. I do some changes in beforeDocumentSaving and I use document.setText:

public class AppendAction implements ApplicationComponent
{
    @Override public void initComponent()
    {
        MessageBus bus = ApplicationManager.getApplication().getMessageBus();
        MessageBusConnection connection = bus.connect();

        connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter()
            {
                @Override public void beforeDocumentSaving(Document document)
                {
                    document.setText(appendSomething(document.getText()));                        
                }
            });
    }
}

This works great, my only problem is that when this plugin is run, and I want to undo the changes, I get to following error message:

Cannot Undo
Following files have changes that cannot be undone:

Any Idea? :-)

Was it helpful?

Solution 2

You should wrap the change through the CommandProcessor API.

From IntelliJ IDEA Architectural Overview:

Any operations which modify the contents of the document must be wrapped in a command (CommandProcessor.getInstance().executeCommand()). executeCommand() calls can be nested, and the outermost executeCommand call is added to the undo stack. If multiple documents are modified within a command, undoing this command will by default show a confirmation dialog to the user.

OTHER TIPS

The answer is wrapping the document.setText into ApplicationManager.getApplication().runWriteAction and CommandProcessor.getInstance().runUndoTransparentAction.

I found an example TrailingSpacesStripper among intellij-community sources on githib: https://github.com/JetBrains/intellij-community/blob/master/platform/platform-impl/src/com/intellij/openapi/editor/impl/TrailingSpacesStripper.java

public class AppendAction implements ApplicationComponent
{
    @Override public void initComponent()
    {
        MessageBus bus = ApplicationManager.getApplication().getMessageBus();
        MessageBusConnection connection = bus.connect();

        connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter()
            {
                @Override public void beforeDocumentSaving(final Document document)
                {
                    ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null)
                        {
                            @Override public void run()
                            {
                                CommandProcessor.getInstance().runUndoTransparentAction(new Runnable()
                                    {
                                        @Override public void run()
                                        {
                                            document.setText(appendSomething(document.getText()));
                                        }
                                    });
                            }
                        });
                }
            });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top