int (@NotNull final Editor editor) { final int caret = editor.getCaretModel().getOffset(); if (editor.getSelectionModel().hasSelection()) { if (caret == editor.getSelectionModel().getSelectionEnd()) { return Math.max(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd() - 1); } } return caret; }