void (final @NotNull CaretAction action, final boolean reverseOrder) { if (myCurrentCaret.get() != null) { throw new IllegalStateException("Recursive runForEachCaret invocations are not allowed"); } Runnable iteration = () -> { try { List<Caret> sortedCarets = getAllCarets(); Iterable<Caret> caretIterable = reverseOrder ? ContainerUtil.iterateBackward(sortedCarets) : sortedCarets; for (Caret caret : caretIterable) { myCurrentCaret.set((CaretImpl)caret); action.perform(caret); } } finally { myCurrentCaret.set(null); } }; if (ApplicationManager.getApplication().isDispatchThread()) { myCaretActionListeners.getMulticaster().beforeAllCaretsAction(); doWithCaretMerging(iteration); myCaretActionListeners.getMulticaster().afterAllCaretsAction(); } else { iteration.run(); } }