mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ReplaceShiftWithMultiplyIntention: support parentheses; tests
This commit is contained in:
+3
-2
@@ -17,6 +17,7 @@ package com.siyeh.ipp.shift;
|
||||
|
||||
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;
|
||||
@@ -69,7 +70,7 @@ public class ReplaceShiftWithMultiplyIntention extends MutablyNamedIntention {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processIntention(PsiElement element) {
|
||||
public void processIntention(@NotNull PsiElement element) {
|
||||
if (element instanceof PsiBinaryExpression) {
|
||||
replaceShiftWithMultiplyOrDivide(element);
|
||||
}
|
||||
@@ -100,7 +101,7 @@ public class ReplaceShiftWithMultiplyIntention extends MutablyNamedIntention {
|
||||
private static void replaceShiftWithMultiplyOrDivide(PsiElement element) {
|
||||
final PsiBinaryExpression exp = (PsiBinaryExpression)element;
|
||||
final PsiExpression lhs = exp.getLOperand();
|
||||
final PsiExpression rhs = exp.getROperand();
|
||||
final PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(exp.getROperand());
|
||||
final IElementType tokenType = exp.getOperationTokenType();
|
||||
final String operatorString;
|
||||
if (tokenType.equals(JavaTokenType.LTLT)) {
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.siyeh.ipp.shift;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
|
||||
class ShiftByLiteralPredicate implements PsiElementPredicate {
|
||||
@@ -68,7 +69,7 @@ class ShiftByLiteralPredicate implements PsiElementPredicate {
|
||||
if (!ShiftUtils.isIntegral(lhsType)) {
|
||||
return false;
|
||||
}
|
||||
final PsiExpression rhs = expression.getROperand();
|
||||
final PsiExpression rhs = PsiUtil.skipParenthesizedExprDown(expression.getROperand());
|
||||
return ShiftUtils.isIntLiteral(rhs);
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void test(int foo) {
|
||||
int x = foo << 1<caret>2;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void test(int foo) {
|
||||
foo <<= 1<caret>2;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void test(int foo) {
|
||||
foo *= 4096<caret>;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void test(int foo) {
|
||||
int x = foo * 4096<caret>;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void test(int foo) {
|
||||
int x = foo << (2<caret>4);
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void test(int foo) {
|
||||
int x = foo * 16777<caret>216;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void test(int foo) {
|
||||
int x = foo >> 1<caret>2;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
void test(int foo) {
|
||||
int x = foo / 4096<caret>;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// 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.shift;
|
||||
|
||||
import com.siyeh.ipp.IPPTestCase;
|
||||
|
||||
public class ReplaceShiftWithMultiplyIntentionTest extends IPPTestCase {
|
||||
|
||||
public void testLeftShift() { doTest("Replace '<<' with '*'"); }
|
||||
public void testLeftShiftAssign() { doTest("Replace '<<=' with '*='"); }
|
||||
public void testParentheses() { doTest("Replace '<<' with '*'"); }
|
||||
public void testRightShift() { doTest("Replace '>>' with '/'"); }
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
return "shift/replace_shift_with_multiply";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user