add EditorUtil.runBatchFoldingOperationOutsideOfBulkUpdate to simplify adding folding...
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Thu, 27 Oct 2016 16:29:54 +0000 (19:29 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Thu, 27 Oct 2016 16:29:54 +0000 (19:29 +0300)
platform/platform-impl/src/com/intellij/openapi/editor/ex/util/EditorUtil.java

index f4261e87558a66958ab247cdfe0ce618194ead33..c19576888362cbd02fdaa164b657c2eda304c97d 100644 (file)
@@ -29,6 +29,8 @@ import com.intellij.openapi.editor.colors.EditorColorsManager;
 import com.intellij.openapi.editor.colors.EditorColorsScheme;
 import com.intellij.openapi.editor.event.EditorFactoryAdapter;
 import com.intellij.openapi.editor.event.EditorFactoryEvent;
+import com.intellij.openapi.editor.ex.DocumentBulkUpdateListener;
+import com.intellij.openapi.editor.ex.DocumentEx;
 import com.intellij.openapi.editor.ex.EditorEx;
 import com.intellij.openapi.editor.impl.ComplementaryFontsRegistry;
 import com.intellij.openapi.editor.impl.EditorImpl;
@@ -42,6 +44,8 @@ import com.intellij.openapi.util.*;
 import com.intellij.openapi.util.registry.Registry;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.util.DocumentUtil;
+import com.intellij.util.ObjectUtils;
+import com.intellij.util.messages.MessageBusConnection;
 import org.intellij.lang.annotations.JdkConstants;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -923,6 +927,29 @@ public final class EditorUtil {
       }
     }, disposable);
   }
-}
-
 
+  public static void runBatchFoldingOperationOutsideOfBulkUpdate(@NotNull Editor editor, @NotNull Runnable operation) {
+    DocumentEx document = ObjectUtils.tryCast(editor.getDocument(), DocumentEx.class);
+    if (document != null && document.isInBulkUpdate()) {
+      MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect();
+      disposeWithEditor(editor, new Disposable() {
+        @Override
+        public void dispose() {
+          connection.disconnect();
+        }
+      });
+      connection.subscribe(DocumentBulkUpdateListener.TOPIC, new DocumentBulkUpdateListener.Adapter() {
+        @Override
+        public void updateFinished(@NotNull Document doc) {
+          if (doc == editor.getDocument()) {
+            editor.getFoldingModel().runBatchFoldingOperation(operation);
+            connection.disconnect();
+          }
+        }
+      });
+    }
+    else {
+      editor.getFoldingModel().runBatchFoldingOperation(operation);
+    }
+  }
+}