IDEA-141963 (Intention. Switch prefix/postfix operator.)

This commit is contained in:
Bas Leijdekkers
2015-06-29 14:38:52 +02:00
parent fd9e165adb
commit 76fcd98a1e
10 changed files with 147 additions and 0 deletions
@@ -622,5 +622,10 @@
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
<categoryKey>intention.category.other</categoryKey>
</intentionAction>
<intentionAction>
<className>com.siyeh.ipp.opassign.PostfixPrefixIntention</className>
<bundleName>com.siyeh.IntentionPowerPackBundle</bundleName>
<categoryKey>intention.category.other</categoryKey>
</intentionAction>
</extensions>
</idea-plugin>
@@ -186,6 +186,7 @@ negate.conditional.intention.name=Negate '?:'
negate.conditional.intention.family.name=Negate Conditional
objects.require.non.null.intention.name=Use 'java.util.Objects.requireNonNull()' expression
objects.require.non.null.intention.family.name=Use 'java.util.Objects.requireNonNull()' Expression
postfix.prefix.intention.family.name=Flip prefix/postfix expression
#hand made
demorgans.intention.name1=Replace '\\&\\&' with '||'
@@ -220,6 +221,7 @@ adapter.to.listener.intention.name=Replace extension of ''{0}'' with ''Listener'
obscure.thrown.exceptions.intention.name=Replace with ''throws {0}''
add.array.creation.expression.intention.name=Add ''new {0}''
change.variable.type.to.rhs.type.intention.name=Declare ''{0}'' with type ''{1}''
postfix.prefix.intention.name=Replace with ''{0}''
#categories
@@ -0,0 +1,71 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.opassign;
import com.intellij.psi.*;
import com.siyeh.IntentionPowerPackBundle;import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ipp.base.MutablyNamedIntention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class PostfixPrefixIntention extends MutablyNamedIntention {
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (element instanceof PsiPrefixExpression) {
final PsiPrefixExpression prefixExpression = (PsiPrefixExpression)element;
return prefixExpression.getOperand() != null;
}
return element instanceof PsiPostfixExpression;
}
};
}
@Override
protected String getTextForElement(PsiElement element) {
return IntentionPowerPackBundle.message("postfix.prefix.intention.name", getReplacementText(element));
}
@NotNull
private static String getReplacementText(PsiElement element) {
if (element instanceof PsiPrefixExpression) {
final PsiPrefixExpression prefixExpression = (PsiPrefixExpression)element;
final PsiExpression operand = prefixExpression.getOperand();
assert operand != null;
final PsiJavaToken sign = prefixExpression.getOperationSign();
return operand.getText() + sign.getText();
}
else {
final PsiPostfixExpression postfixExpression = (PsiPostfixExpression)element;
final PsiExpression operand = postfixExpression.getOperand();
final PsiJavaToken sign = postfixExpression.getOperationSign();
return sign.getText() + operand.getText();
}
}
@Override
protected void processIntention(@NotNull PsiElement element) {
final PsiExpression expression = (PsiExpression)element;
PsiReplacementUtil.replaceExpression(expression, getReplacementText(element));
}
}
@@ -0,0 +1,5 @@
public class X {
void f(int i) {
++i;
}
}
@@ -0,0 +1,5 @@
public class X {
void f(int i) {
<spot>i++</spot>;
}
}
@@ -0,0 +1,5 @@
<html>
<body>
Replaces a postfix expression with a prefix expression and vice versa.
</body>
</html>
@@ -0,0 +1,5 @@
class Incomplete {
void m(int i) {
++<caret>
}
}
@@ -0,0 +1,6 @@
class Simple {
void m(int i) {
<caret>i++;
}
}
@@ -0,0 +1,6 @@
class Simple {
void m(int i) {
++i;
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ipp.opassign;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.IPPTestCase;
/**
* @see PostfixPrefixIntention
*/
public class PostfixPrefixIntentionTest extends IPPTestCase {
public void testSimple() { doTest(); }
public void testIncomplete() { assertIntentionNotAvailable(); }
@Override
protected String getIntentionName() {
return IntentionPowerPackBundle.message("postfix.prefix.intention.name", "++i");
}
@Override
protected String getRelativePath() {
return "opassign/postfix_prefix";
}
}