InjectedCaret (@NotNull EditorWindow editor, @NotNull Caret hostCaret) { if (hostCaret instanceof InjectedCaret) { return (InjectedCaret)hostCaret; } for (Caret caret : editor.getCaretModel().getAllCarets()) { if (((InjectedCaret)caret).getDelegate() == hostCaret) { return (InjectedCaret)caret; } } throw new IllegalArgumentException("Cannot find injected caret corresponding to " + hostCaret); }