LoopDirection (@NotNull PsiVariable counter, @Nullable PsiStatement updateStatement) { PsiExpressionStatement expressionStatement = tryCast(updateStatement, PsiExpressionStatement.class); if (expressionStatement == null) return null; PsiExpression expression = PsiUtil.skipParenthesizedExprDown(expressionStatement.getExpression()); if (expression instanceof PsiUnaryExpression unaryExpression) { IElementType tokenType = unaryExpression.getOperationTokenType(); if (tokenType != JavaTokenType.PLUSPLUS && tokenType != JavaTokenType.MINUSMINUS) return null; PsiExpression operand = unaryExpression.getOperand(); if (!ExpressionUtils.isReferenceTo(operand, counter)) return null; return tokenType == JavaTokenType.PLUSPLUS ? ASCENDING : DESCENDING; } if (expression instanceof PsiAssignmentExpression assignmentExpression) { PsiExpression lhs = assignmentExpression.getLExpression(); if (!ExpressionUtils.isReferenceTo(lhs, counter)) return null; PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(assignmentExpression.getRExpression()); IElementType tokenType = assignmentExpression.getOperationTokenType(); if (tokenType == JavaTokenType.EQ) { PsiBinaryExpression binaryExpression = tryCast(rhs, PsiBinaryExpression.class); if (binaryExpression == null) return null; IElementType binaryTokenType = binaryExpression.getOperationTokenType(); if (binaryTokenType != JavaTokenType.PLUS && binaryTokenType != JavaTokenType.MINUS) return null; PsiExpression lOperand = binaryExpression.getLOperand(); PsiExpression rOperand = binaryExpression.getROperand(); if (ExpressionUtils.isOne(lOperand)) { if (!ExpressionUtils.isReferenceTo(rOperand, counter)) return null; return binaryTokenType == JavaTokenType.PLUS ? ASCENDING : DESCENDING; } if (ExpressionUtils.isOne(rOperand)) { if (!ExpressionUtils.isReferenceTo(lOperand, counter)) return null; return binaryTokenType == JavaTokenType.PLUS ? ASCENDING : DESCENDING; } } else if (tokenType == JavaTokenType.PLUSEQ || tokenType == JavaTokenType.MINUSEQ) { if (ExpressionUtils.isOne(rhs)) { return tokenType == JavaTokenType.PLUSEQ ? ASCENDING : DESCENDING; } } } return null; }