ConditionalExpressionGenerator (PsiExpression condition, PsiExpression expression) { boolean equal = true; PsiExpression left, right; if (BoolUtils.isNegation(condition)) { equal = false; left = Objects.requireNonNull(BoolUtils.getNegated(condition)); } else { left = condition; } if (BoolUtils.isNegation(expression)) { equal = !equal; right = Objects.requireNonNull(BoolUtils.getNegated(expression)); } else { right = expression; } String token = equal ? "==" : "!="; // RELATIONAL_PRECEDENCE is technically enough here, but it may produces quite confusing code like "a == b > c" // so we add (formally redundant) parentheses in this case: "a == (b > c)" return new ConditionalExpressionGenerator(token, ct -> ct.text(left, PsiPrecedenceUtil.SHIFT_PRECEDENCE) + " " + token + " " + ct.text(right, PsiPrecedenceUtil.SHIFT_PRECEDENCE)); }