Assertion failed: Write access is allowed inside write-action only

Error message: Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())
java.lang.Throwable

at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:64)
at com.intellij.openapi.e.a.c.assertWriteAccessAllowed(c.java)
at com.intellij.openapi.editor.b.m.a(m.java:31)
at com.intellij.openapi.editor.b.m.replaceString(m.java:259)
...and more lines

This exception obtain always when I trying replace code from my plugin. I'm calling this code:

FileEditorManager.getInstance(myproject).getSelectedTextEditor().getDocument().replaceString(0, 0, mystring);

Text is replaced, but exception is generated.

In tracker I have found only this:

http://www.intellij.net/tracker/idea/viewSCR?publicId=32464

I don't know what it is. Can somebody help me?

-- likeapear


2 comments
Comment actions Permalink

I'm sorry for these posts. I'm posting from Outlook Express and I omit my messages are posted to this thread...

-- likeapear


"likeapear" <likeapear@centrum.cz> pí¹e v diskusním pøíspìvku news:c8kcbm$aje$1@is.intellij.net...

Error message: Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())
java.lang.Throwable

at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:64)
at com.intellij.openapi.e.a.c.assertWriteAccessAllowed(c.java)
at com.intellij.openapi.editor.b.m.a(m.java:31)
at com.intellij.openapi.editor.b.m.replaceString(m.java:259)
...and more lines

This exception obtain always when I trying replace code from my plugin. I'm calling this code:

FileEditorManager.getInstance(myproject).getSelectedTextEditor().getDocument().replaceString(0, 0, mystring);

Text is replaced, but exception is generated.

In tracker I have found only this:

http://www.intellij.net/tracker/idea/viewSCR?publicId=32464

I don't know what it is. Can somebody help me?

-- likeapear


0
Comment actions Permalink

The exception is telling you that you are modifying the document but you didn't execute the code in a write action.

I have a helper class for this sort of thing (see below). If you use this class, call 'runWriteCommand' passing in the project and a Runnable whose 'run' method contains whatever code you want that modifies a file.

Enjoy.

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.project.Project;

public class RunnableHelper
{
public static void runReadCommand(Project project, Runnable cmd)
{
CommandProcessor.getInstance().executeCommand(project, new ReadAction(cmd), "Foo", "Bar");
}

public static void runWriteCommand(Project project, Runnable cmd)
{
CommandProcessor.getInstance().executeCommand(project, new WriteAction(cmd), "Foo", "Bar");
}

static class ReadAction implements Runnable
{
ReadAction(Runnable cmd)
{
this.cmd = cmd;
}

public void run()
{
ApplicationManager.getApplication().runReadAction(cmd);
}

Runnable cmd;
}

static class WriteAction implements Runnable
{
WriteAction(Runnable cmd)
{
this.cmd = cmd;
}

public void run()
{
ApplicationManager.getApplication().runWriteAction(cmd);
}

Runnable cmd;
}

private RunnableHelper() {}
}

0

Please sign in to leave a comment.