void (@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) { DocumentEx document = (DocumentEx)editor.getDocument(); Project project = editor.getProject(); assert project != null; PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); PsiFile file = psiDocumentManager.getPsiFile(document); assert file != null; int selectionStart = caret.getSelectionStart(); int selectionEnd = caret.getSelectionEnd(); assert selectionStart <= selectionEnd; PsiElement[] elementList = getElementList(file, selectionStart, selectionEnd); assert elementList != null; Range<Integer> elementRange = findRangeOfElementsToMove(elementList, selectionStart, selectionEnd); if (elementRange == null) return; int toMoveStart = elementList[elementRange.getFrom()].getTextRange().getStartOffset(); int toMoveEnd = elementList[elementRange.getTo()].getTextRange().getEndOffset(); int otherIndex = myIsLeft ? elementRange.getFrom() - 1 : elementRange.getTo() + 1; int otherStart = elementList[otherIndex].getTextRange().getStartOffset(); int otherEnd = elementList[otherIndex].getTextRange().getEndOffset(); selectionStart = trim(selectionStart, toMoveStart, toMoveEnd); selectionEnd = trim(selectionEnd, toMoveStart, toMoveEnd); int caretOffset = trim(caret.getOffset(), toMoveStart, toMoveEnd); int caretShift; if (toMoveStart < otherStart) { document.moveText(toMoveStart, toMoveEnd, otherStart); document.moveText(otherStart, otherEnd, toMoveStart); caretShift = otherEnd - toMoveEnd; } else { document.moveText(otherStart, otherEnd, toMoveStart); document.moveText(toMoveStart, toMoveEnd, otherStart); caretShift = otherStart - toMoveStart; } caret.moveToOffset(caretOffset + caretShift); caret.setSelection(selectionStart + caretShift, selectionEnd + caretShift); }