KT-17379: Fix J2K removal of parentheses in multiline expressions

When there is multiline polyadic expression with some operators
J2K should keep surrounding parentheses, otherwise
operators will be dangling due resolved to prefix variant

 #KT-17379 fixed
This commit is contained in:
Dimach
2017-07-30 04:49:55 +03:00
committed by Simon Ogorodnik
parent 0920b2574c
commit f0035a7be0
11 changed files with 147 additions and 53 deletions
@@ -530,6 +530,10 @@ public class KtPsiUtil {
return false;
}
if (innerExpression instanceof KtBinaryExpression && isKeepBinaryExpressionParenthesized((KtBinaryExpression) innerExpression)) {
return true;
}
int innerPriority = getPriority(innerExpression);
int parentPriority = getPriority((KtExpression) parentElement);
@@ -551,6 +555,21 @@ public class KtPsiUtil {
return innerPriority < parentPriority;
}
private static boolean isKeepBinaryExpressionParenthesized(KtBinaryExpression expression) {
PsiElement expr = expression.getFirstChild();
while (expr != null) {
if (expr instanceof PsiWhiteSpace && expr.textContains('\n')) {
return true;
}
if (expr instanceof KtOperationReferenceExpression) {
break;
}
expr = expr.getNextSibling();
}
return (expression.getRight() instanceof KtBinaryExpression && isKeepBinaryExpressionParenthesized((KtBinaryExpression) expression.getRight())) ||
(expression.getLeft() instanceof KtBinaryExpression && isKeepBinaryExpressionParenthesized((KtBinaryExpression) expression.getLeft()));
}
public static boolean isAssignment(@NotNull PsiElement element) {
return element instanceof KtBinaryExpression &&
KtTokens.ALL_ASSIGNMENTS.contains(((KtBinaryExpression) element).getOperationToken());