int (int offset) { int offsetInLeftFragment = injectedToHost(offset, true, false); int offsetInRightFragment = injectedToHost(offset, false, false); if (offsetInLeftFragment == offsetInRightFragment) return offsetInLeftFragment; // heuristics: return offset closest to the caret Editor editor = EditorFactory.getInstance().editors(getDelegate()).findFirst().orElse(null); if (editor != null) { if (editor instanceof EditorWindow) editor = ((EditorWindow)editor).getDelegate(); int caret = editor.getCaretModel().getOffset(); return Math.abs(caret - offsetInLeftFragment) < Math.abs(caret - offsetInRightFragment) ? offsetInLeftFragment : offsetInRightFragment; } return offsetInLeftFragment; }