Result (@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { // The algorithm is as follows: // 1. Check if the editor has selection. Do nothing then as Emacs behaves so; // 2. Indent current line one level right if possible; // 3. Indent current line to the left-most position if possible; SelectionModel selectionModel = editor.getSelectionModel(); // Emacs Tab doesn't adjust indent in case of active selection. So do we. if (selectionModel.hasSelection()) { return Result.CONTINUE; } // Check if current line is empty. Return eagerly then. Document document = editor.getDocument(); int caretOffset = editor.getCaretModel().getOffset(); int caretLine = document.getLineNumber(caretOffset); if (DocumentUtil.isLineEmpty(document, caretLine)) { return Result.CONTINUE; } ChangeIndentContext context = new ChangeIndentContext(project, file, editor, document, caretLine); int targetLineIndent = getLineIndent(context, context.targetLine); int soleLineIndent = getSoleIndent(context); int lineStart = context.document.getLineStartOffset(context.targetLine); if (caretOffset - lineStart < targetLineIndent) { changeIndent(context, soleLineIndent); return Result.STOP; } switch (tryToIndentToRight(context)) { case STOP_SUCCESSFUL -> { return Result.STOP; } case STOP_UNSUCCESSFUL -> { return Result.CONTINUE; } case CONTINUE -> { } } if (tryToIndentToLeft(context)) { return Result.STOP; } return Result.CONTINUE; }