From 76fcd98a1e2835d7831473e22b62017ccf1b0e2e Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 27 Jun 2015 21:18:35 +0200 Subject: [PATCH] IDEA-141963 (Intention. Switch prefix/postfix operator.) --- .../src/META-INF/IntentionPowerPack.xml | 5 ++ .../siyeh/IntentionPowerPackBundle.properties | 2 + .../ipp/opassign/PostfixPrefixIntention.java | 71 +++++++++++++++++++ .../after.java.template | 5 ++ .../before.java.template | 5 ++ .../PostfixPrefixIntention/description.html | 5 ++ .../opassign/postfix_prefix/Incomplete.java | 5 ++ .../ipp/opassign/postfix_prefix/Simple.java | 6 ++ .../opassign/postfix_prefix/Simple_after.java | 6 ++ .../opassign/PostfixPrefixIntentionTest.java | 37 ++++++++++ 10 files changed, 147 insertions(+) create mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/PostfixPrefixIntention.java create mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/after.java.template create mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/before.java.template create mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/description.html create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Incomplete.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Simple.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Simple_after.java create mode 100644 plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/opassign/PostfixPrefixIntentionTest.java diff --git a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml index 95c585fef36c..ad885b84f380 100644 --- a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml +++ b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml @@ -622,5 +622,10 @@ com.siyeh.IntentionPowerPackBundle intention.category.other + + com.siyeh.ipp.opassign.PostfixPrefixIntention + com.siyeh.IntentionPowerPackBundle + intention.category.other + diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index 6a5d499f42da..0d5c4217eb4d 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -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 diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/PostfixPrefixIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/PostfixPrefixIntention.java new file mode 100644 index 000000000000..ceddc5bcc595 --- /dev/null +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/opassign/PostfixPrefixIntention.java @@ -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)); + } +} diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/after.java.template new file mode 100644 index 000000000000..62ff7e5742d2 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/after.java.template @@ -0,0 +1,5 @@ +public class X { + void f(int i) { + ++i; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/before.java.template new file mode 100644 index 000000000000..ce2d4d35c298 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/before.java.template @@ -0,0 +1,5 @@ +public class X { + void f(int i) { + i++; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/description.html new file mode 100644 index 000000000000..6a031b16018b --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/PostfixPrefixIntention/description.html @@ -0,0 +1,5 @@ + + +Replaces a postfix expression with a prefix expression and vice versa. + + diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Incomplete.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Incomplete.java new file mode 100644 index 000000000000..e5e2497aa54f --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Incomplete.java @@ -0,0 +1,5 @@ +class Incomplete { + void m(int i) { + ++ + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Simple.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Simple.java new file mode 100644 index 000000000000..9595d54d7030 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Simple.java @@ -0,0 +1,6 @@ +class Simple { + + void m(int i) { + i++; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Simple_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Simple_after.java new file mode 100644 index 000000000000..d23b2ee884d9 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/opassign/postfix_prefix/Simple_after.java @@ -0,0 +1,6 @@ +class Simple { + + void m(int i) { + ++i; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/opassign/PostfixPrefixIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/opassign/PostfixPrefixIntentionTest.java new file mode 100644 index 000000000000..94dcb427d56e --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/opassign/PostfixPrefixIntentionTest.java @@ -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"; + } +}