RunWriteCommandAction and suspend functions


Some methods from the IntelliJ Platform require wrapping them in runWriteCommandAction.

This also means that an undo/redo action will be created for them. I need to create several such operations in one "compound" undo/redo scope to allow the user to undo all of them at once, rather than one by one. This compels me to use runWriteCommandAction for the entire code snippet.

This snippet may contain suspend functions. However, it is impossible to call them inside runWriteCommandAction because I can't wrap suspend calls with runBlocking (long EDT thread freeze), and I can't launch a coroutine to use a "fire and forget" approach because I need to complete all my compound operations before proceeding to the next step of the processing algorithm.

I am okay with showing the user a modal progress bar while the code inside the long runWriteCommandAction is being executed.

What should I do then? 

1 comment

There is token-based way, which can be used from a coroutine: com.intellij.openapi.command.CommandProcessorEx#startCommand

launch {
  val token = startCommand(…)
  try {
    writeAction {} // suspend variant
    writeAction {} 
  finally {
    finishCommand(token, …)

Please sign in to leave a comment.