PsiPolyadicExpression (PsiElement element, boolean acceptAnd, boolean acceptOr) { if (!(element instanceof PsiJavaToken token)) { return null; } if (!(token.getParent() instanceof PsiPolyadicExpression expression)) return null; boolean isAndExpression = acceptAnd && expression.getOperationTokenType() == JavaTokenType.ANDAND; boolean isOrExpression = acceptOr && expression.getOperationTokenType() == JavaTokenType.OROR; if (!isAndExpression && !isOrExpression) return null; if (ErrorUtil.containsError(expression)) { // Incomplete expression like "something &&" return null; } while (expression.getParent() instanceof PsiPolyadicExpression) { expression = (PsiPolyadicExpression)expression.getParent(); if (isAndExpression && expression.getOperationTokenType() != JavaTokenType.ANDAND) return null; if (isOrExpression && expression.getOperationTokenType() != JavaTokenType.OROR) return null; } return expression; }