void (final @NotNull CaretListener listener) { CaretListener wrapper = new CaretListener() { @Override public void caretPositionChanged(@NotNull CaretEvent e) { if (!myEditorWindow.getDocument().isValid()) return; // injected document can be destroyed by now Caret caret = e.getCaret(); assert caret != null; CaretEvent event = new CaretEvent(createInjectedCaret(caret), myEditorWindow.hostToInjected(e.getOldPosition()), myEditorWindow.hostToInjected(e.getNewPosition())); listener.caretPositionChanged(event); } }; myCaretListeners.registerWrapper(listener, wrapper); myDelegate.addCaretListener(wrapper); }