diff options
Diffstat (limited to 'plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils')
-rw-r--r-- | plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ConcatenationUtils.java | 20 | ||||
-rw-r--r-- | plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java | 49 |
2 files changed, 40 insertions, 29 deletions
diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ConcatenationUtils.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ConcatenationUtils.java index c2766e595967..0a62b3181b88 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ConcatenationUtils.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ConcatenationUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +20,7 @@ import com.intellij.psi.tree.IElementType; public class ConcatenationUtils { - private ConcatenationUtils() { - super(); - } + private ConcatenationUtils() {} public static boolean isConcatenation(PsiElement element) { if (!(element instanceof PsiPolyadicExpression)) { @@ -33,20 +31,24 @@ public class ConcatenationUtils { if (!tokenType.equals(JavaTokenType.PLUS)) { return false; } - PsiExpression[] operands = expression.getOperands(); - if (operands.length <= 1) return false; + final PsiExpression[] operands = expression.getOperands(); + if (operands.length <= 1) { + return false; + } final PsiType type = expression.getType(); if (type == null) { for (PsiExpression operand : operands) { - if (hasStringType(operand)) return true; + if (hasStringType(operand)) { + return true; + } } return false; } - return type.equalsToText("java.lang.String"); + return type.equalsToText(CommonClassNames.JAVA_LANG_STRING); } private static boolean hasStringType(PsiExpression expression) { final PsiType type = expression.getType(); - return type != null && type.equalsToText("java.lang.String"); + return type != null && type.equalsToText(CommonClassNames.JAVA_LANG_STRING); } } diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java index b4d97f52687d..ab2971af6b81 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/psiutils/ParenthesesUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,7 +81,7 @@ public class ParenthesesUtils { return expression; } - public static boolean isCommutativeBinaryOperator(@NotNull IElementType token) { + public static boolean isCommutativeOperator(@NotNull IElementType token) { return !(token.equals(JavaTokenType.MINUS) || token.equals(JavaTokenType.DIV) || token.equals(JavaTokenType.PERC) || @@ -185,8 +185,8 @@ public class ParenthesesUtils { removeParensFromPostfixExpression(postfixExpression, ignoreClarifyingParentheses); } if (expression instanceof PsiPolyadicExpression) { - final PsiPolyadicExpression binaryExpression = (PsiPolyadicExpression)expression; - removeParensFromPolyadicExpression(binaryExpression, ignoreClarifyingParentheses); + final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression; + removeParensFromPolyadicExpression(polyadicExpression, ignoreClarifyingParentheses); } if (expression instanceof PsiInstanceOfExpression) { final PsiInstanceOfExpression instanceofExpression = (PsiInstanceOfExpression)expression; @@ -225,12 +225,15 @@ public class ParenthesesUtils { return; } else if (parent instanceof PsiArrayAccessExpression) { - // use addAfter() + delete() instead of replace() to - // workaround automatic insertion of parentheses by psi - final PsiExpression newExpression = (PsiExpression)parent.addAfter(body, parenthesizedExpression); - parenthesizedExpression.delete(); - removeParentheses(newExpression, ignoreClarifyingParentheses); - return; + final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression)parent; + if (parenthesizedExpression == arrayAccessExpression.getIndexExpression()) { + // use addAfter() + delete() instead of replace() to + // workaround automatic insertion of parentheses by psi + final PsiExpression newExpression = (PsiExpression)parent.addAfter(body, parenthesizedExpression); + parenthesizedExpression.delete(); + removeParentheses(newExpression, ignoreClarifyingParentheses); + return; + } } final PsiExpression parentExpression = (PsiExpression)parent; final int parentPrecedence = getPrecedence(parentExpression); @@ -245,15 +248,15 @@ public class ParenthesesUtils { } else if (parentPrecedence == childPrecedence) { if (parentExpression instanceof PsiPolyadicExpression && body instanceof PsiPolyadicExpression) { - final PsiPolyadicExpression parentBinaryExpression = (PsiPolyadicExpression)parentExpression; - final IElementType parentOperator = parentBinaryExpression.getOperationTokenType(); - final PsiPolyadicExpression bodyBinaryExpression = (PsiPolyadicExpression)body; - final IElementType bodyOperator = bodyBinaryExpression.getOperationTokenType(); - final PsiType parentType = parentBinaryExpression.getType(); + final PsiPolyadicExpression parentPolyadicExpression = (PsiPolyadicExpression)parentExpression; + final IElementType parentOperator = parentPolyadicExpression.getOperationTokenType(); + final PsiPolyadicExpression bodyPolyadicExpression = (PsiPolyadicExpression)body; + final IElementType bodyOperator = bodyPolyadicExpression.getOperationTokenType(); + final PsiType parentType = parentPolyadicExpression.getType(); final PsiType bodyType = body.getType(); if (parentType != null && parentType.equals(bodyType) && parentOperator.equals(bodyOperator)) { - final PsiExpression[] parentOperands = parentBinaryExpression.getOperands(); - if (PsiTreeUtil.isAncestor(parentOperands[0], body, true) || isCommutativeBinaryOperator(bodyOperator)) { + final PsiExpression[] parentOperands = parentPolyadicExpression.getOperands(); + if (PsiTreeUtil.isAncestor(parentOperands[0], body, true) || isCommutativeOperator(bodyOperator)) { // use addAfter() + delete() instead of replace() to // workaround automatic insertion of parentheses by psi final PsiExpression newExpression = (PsiExpression)parent.addAfter(body, parenthesizedExpression); @@ -407,12 +410,18 @@ public class ParenthesesUtils { if (child == null) { return true; } + if (parent instanceof PsiArrayAccessExpression) { + final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression)parent; + final PsiExpression indexExpression = arrayAccessExpression.getIndexExpression(); + if (expression == indexExpression) { + return false; + } + } return areParenthesesNeeded(child, (PsiExpression)parent); } public static boolean areParenthesesNeeded(PsiExpression expression, PsiExpression parentExpression) { - if (parentExpression instanceof PsiParenthesizedExpression || parentExpression instanceof PsiArrayAccessExpression || - parentExpression instanceof PsiArrayInitializerExpression) { + if (parentExpression instanceof PsiParenthesizedExpression || parentExpression instanceof PsiArrayInitializerExpression) { return false; } final int parentPrecedence = getPrecedence(parentExpression); @@ -451,7 +460,7 @@ public class ParenthesesUtils { final PsiExpression[] parentOperands = parentPolyadicExpression.getOperands(); if (!PsiTreeUtil.isAncestor(parentOperands[0], expression, false)) { final IElementType parentOperator = parentPolyadicExpression.getOperationTokenType(); - if (!isCommutativeBinaryOperator(parentOperator)) { + if (!isCommutativeOperator(parentOperator)) { return true; } } |