void (@NotNull Editor editor) { final CaretModel caretModel = editor.getCaretModel(); final VisualPosition pos = caretModel.getVisualPosition(); VisualPosition prevPos = editor.getUserData(PREV_POS); if (prevPos != null) { int columnShift = pos.line == prevPos.line ? pos.column - prevPos.column : 0; int caret = caretModel.getOffset(); final FoldRegion collapsedUnderCaret = editor.getFoldingModel().getCollapsedRegionAtOffset(caret); if (collapsedUnderCaret != null && collapsedUnderCaret.shouldNeverExpand() && Boolean.TRUE.equals(collapsedUnderCaret.getUserData(FoldingModelImpl.SELECT_REGION_ON_CARET_NEARBY))) { if (caret > collapsedUnderCaret.getStartOffset() && columnShift > 0) { caretModel.moveToOffset(collapsedUnderCaret.getEndOffset()); } else if (caret + 1 < collapsedUnderCaret.getEndOffset() && columnShift < 0) { caretModel.moveToOffset(collapsedUnderCaret.getStartOffset()); } editor.getSelectionModel().setSelection(collapsedUnderCaret.getStartOffset(), collapsedUnderCaret.getEndOffset()); } } editor.putUserData(PREV_POS, pos); }