ReplaceAssignmentWithPostfixExpressionIntention: support parentheses

This commit is contained in:
Tagir Valeev
2018-07-06 13:58:38 +07:00
parent ce75d6c612
commit 53e9baf770
7 changed files with 54 additions and 24 deletions
@@ -17,6 +17,7 @@ package com.siyeh.ipp.opassign;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.CommentTracker;
@@ -24,8 +25,7 @@ import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NotNull;
public class ReplaceAssignmentWithPostfixExpressionIntention
extends MutablyNamedIntention {
public class ReplaceAssignmentWithPostfixExpressionIntention extends MutablyNamedIntention {
@NotNull
@Override
@@ -38,7 +38,7 @@ public class ReplaceAssignmentWithPostfixExpressionIntention
final PsiAssignmentExpression assignmentExpression =
(PsiAssignmentExpression)element;
final PsiBinaryExpression rhs =
(PsiBinaryExpression)assignmentExpression.getRExpression();
(PsiBinaryExpression)PsiUtil.skipParenthesizedExprDown(assignmentExpression.getRExpression());
final PsiExpression lhs = assignmentExpression.getLExpression();
final String lhsText = lhs.getText();
final IElementType tokenType;
@@ -67,7 +67,7 @@ public class ReplaceAssignmentWithPostfixExpressionIntention
final PsiExpression lhs = assignmentExpression.getLExpression();
CommentTracker commentTracker = new CommentTracker();
final String lhsText = commentTracker.text(lhs);
final PsiExpression rhs = assignmentExpression.getRExpression();
final PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(assignmentExpression.getRExpression());
if (!(rhs instanceof PsiBinaryExpression)) {
return;
}
@@ -17,14 +17,14 @@ package com.siyeh.ipp.opassign;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ig.psiutils.VariableAccessUtils;
import com.siyeh.ipp.base.PsiElementPredicate;
class ReplaceAssignmentWithPostfixExpressionPredicate implements PsiElementPredicate {
private static final Integer ONE = Integer.valueOf(1);
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof PsiAssignmentExpression)) {
return false;
@@ -44,34 +44,21 @@ class ReplaceAssignmentWithPostfixExpressionPredicate implements PsiElementPredi
return false;
}
final PsiVariable variable = (PsiVariable)target;
final PsiExpression rhs = assignmentExpression.getRExpression();
final PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(assignmentExpression.getRExpression());
if (!(rhs instanceof PsiBinaryExpression)) {
return false;
}
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)rhs;
final PsiExpression rOperand = binaryExpression.getROperand();
if (rOperand == null) {
return false;
}
final PsiExpression lOperand = binaryExpression.getLOperand();
final PsiExpression lOperand = PsiUtil.skipParenthesizedExprDown(binaryExpression.getLOperand());
final PsiExpression rOperand = PsiUtil.skipParenthesizedExprDown(binaryExpression.getROperand());
final IElementType tokenType = binaryExpression.getOperationTokenType();
if (lOperand instanceof PsiLiteral) {
final PsiLiteral literal = (PsiLiteral)lOperand;
final Object value = literal.getValue();
if (ONE != value) {
return false;
}
if (ExpressionUtils.isLiteral(lOperand, 1)) {
if (!VariableAccessUtils.evaluatesToVariable(rOperand, variable)) {
return false;
}
return JavaTokenType.PLUS.equals(tokenType);
}
else if (rOperand instanceof PsiLiteral) {
final PsiLiteral literal = (PsiLiteral)rOperand;
final Object value = literal.getValue();
if (ONE != value) {
return false;
}
else if (ExpressionUtils.isLiteral(rOperand, 1)) {
if (!VariableAccessUtils.evaluatesToVariable(lOperand, variable)) {
return false;
}
@@ -0,0 +1,5 @@
class Test {
void test(int i) {
i = ((i)<caret>+(1));
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int i) {
i++;
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int i) {
i = i<caret>+1;
}
}
@@ -0,0 +1,5 @@
class Test {
void test(int i) {
i++<caret>;
}
}
@@ -0,0 +1,23 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ipp.opassign;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.IPPTestCase;
/**
* @see ReplaceAssignmentWithPostfixExpressionIntention
*/
public class ReplaceAssignmentWithPostfixExpressionIntentionTest extends IPPTestCase {
public void testSimple() { doTest(); }
public void testParentheses() { doTest(); }
@Override
protected String getIntentionName() {
return IntentionPowerPackBundle.message("replace.some.operator.with.other.intention.name", "=", "i++");
}
@Override
protected String getRelativePath() {
return "opassign/assignment_to_postfix";
}
}