Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction()

I only try following:

 
def type(String str) {
    EditorModificationUtil.insertStringAtCaret(ed, str)
}


and I get this error, how to fix it

7 comments
Comment actions Permalink

I use a helper function if I have many write actions, which may or may not be called from the dispatch thread, in this class the document is always delegateDocumentEx field:

    public void runWriteAction(@NotNull final Runnable writeAction) {         final Application application = ApplicationManager.getApplication();         if (application.isDispatchThread()) {             application.runWriteAction(new Runnable() {                 @Override                 public void run() {                     CommandProcessor.getInstance().executeCommand(project, writeAction, null, null, UndoConfirmationPolicy.DEFAULT, delegateDocumentEx);                 }             });         }         else {             application.invokeLater(new Runnable() {                 @Override                 public void run() {                     application.runWriteAction(new Runnable() {                         @Override                         public void run() {                             CommandProcessor.getInstance().executeCommand(project, writeAction, null, null, UndoConfirmationPolicy.DEFAULT, delegateDocumentEx);                         }                     });                 }             });         }     }


then to perform some write action somewhere, call the function passing it a Runnable instance:

            runWriteAction(new Runnable() {                 @Override                 public void run() {                     delegateDocumentEx.replaceString(0, delegateDocumentEx.getTextLength(), text == null ? "" : text);                     final CaretModel caretModel = scriptShellPanel.getEditor().getCaretModel();                     if (caretModel.getOffset() >= getTextLength()) {                         caretModel.moveToOffset(getTextLength());                     }                     setMark();                 }             });

1
Comment actions Permalink

Hi,

I usually use a similar code:

Runnable runnable = new Runnable() {

@Override
public void run() {
// your code here
}
};
Application application = ApplicationManager.getApplication();

if(application.isDispatchThread()) {

application.runWriteAction(runnable);

} else {
application.invokeLater(()-> application.runWriteAction(runnable));
}
1
Comment actions Permalink

try this:

Runnable r = ()-> EditorModificationUtil.insertStringAtCaret(ed, str);

Application app = ApplicationManager.getApplication();
 

app.executeOnPooledThread(() ->  app.runWriteAction(r));
0
Comment actions Permalink

executeOnPooledThread() is entirely wrong here. Write actions may only be executed in the event dispatch thread. See http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/general_threading_rules.html for more information.

0
Comment actions Permalink

Item 1.5 in the tutorial that I sent you as an answer to your previous question shows the correct way to perform this operation.

0
Comment actions Permalink

ok how about this:

Runnable r = ()-> EditorModificationUtil.insertStringAtCaret(editor, string); WriteCommandAction.runWriteCommandAction(project, r);

0
Comment actions Permalink

That's correct, yes.


0

Please sign in to leave a comment.