RmoveUnnecessaryParenthesesIntention

This commit is contained in:
Wojciech Lopata
2013-04-04 13:36:33 +02:00
parent 3e76fc2902
commit cd0e1b7508
32 changed files with 299 additions and 5 deletions
@@ -124,7 +124,7 @@ public class JetExpressionParsing extends AbstractJetParsing {
);
@SuppressWarnings({"UnusedDeclaration"})
private enum Precedence {
public enum Precedence {
POSTFIX(PLUSPLUS, MINUSMINUS, EXCLEXCL,
// HASH,
DOT, SAFE_ACCESS), // typeArguments? valueArguments : typeArguments : arrayAccess
@@ -22,7 +22,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lexer.JetTokens;
public class JetIsExpression extends JetExpressionImpl {
public class JetIsExpression extends JetExpressionImpl implements JetOperationExpression {
public JetIsExpression(@NotNull ASTNode node) {
super(node);
}
@@ -47,6 +47,7 @@ public class JetIsExpression extends JetExpressionImpl {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
}
@Override
@NotNull
public JetSimpleNameExpression getOperationReference() {
return (JetSimpleNameExpression) findChildByType(JetNodeTypes.OPERATION_REFERENCE);
@@ -29,6 +29,7 @@ import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lang.parsing.JetExpressionParsing;
import org.jetbrains.jet.lang.resolve.ImportPath;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -562,4 +563,62 @@ public class JetPsiUtil {
current = (JetClassOrObject) parent.getParent();
}
}
private static IElementType getOperation(@NotNull JetExpression expression) {
if (expression instanceof JetQualifiedExpression) {
return ((JetQualifiedExpression) expression).getOperationSign();
}
else if (expression instanceof JetOperationExpression) {
return ((JetOperationExpression) expression).getOperationReference().getReferencedNameElementType();
}
return null;
}
private static int getPrecedenceOfOperation(@NotNull JetExpression expression, @NotNull IElementType operation) {
if (expression instanceof JetPostfixExpression) return 0;
if (expression instanceof JetQualifiedExpression) return 0;
if (expression instanceof JetPrefixExpression) return 1;
for (JetExpressionParsing.Precedence precedence : JetExpressionParsing.Precedence.values()) {
if (precedence != JetExpressionParsing.Precedence.PREFIX && precedence != JetExpressionParsing.Precedence.POSTFIX &&
precedence.getOperations().contains(operation)) {
return precedence.ordinal();
}
}
throw new IllegalStateException("Unknown operation");
}
public static boolean areParenthesesUseless(@NotNull JetParenthesizedExpression expression) {
JetExpression innerExpression = expression.getExpression();
JetExpression parentExpression = PsiTreeUtil.getParentOfType(expression, JetExpression.class, true);
if (innerExpression == null || parentExpression == null) return true;
IElementType innerOperation = getOperation(innerExpression);
IElementType parentOperation = getOperation(parentExpression);
// 'return (@label{...})' case
if (parentExpression instanceof JetReturnExpression && innerOperation == JetTokens.LABEL_IDENTIFIER) {
return false;
}
// '(x: Int) < y' case
if (innerExpression instanceof JetBinaryExpressionWithTypeRHS && parentOperation == JetTokens.LT) {
return false;
}
// associative operations
if (innerOperation == parentOperation && (innerOperation == JetTokens.OROR || innerOperation == JetTokens.ANDAND)) {
return true;
}
if (innerOperation == null) return true;
if (parentExpression instanceof JetArrayAccessExpression) {
return ((JetArrayAccessExpression) parentExpression).getArrayExpression() != expression;
}
if (parentOperation == null) return true;
int innerPrecedence = getPrecedenceOfOperation(innerExpression, innerOperation);
int parentPrecedence = getPrecedenceOfOperation(parentExpression, parentOperation);
return innerPrecedence < parentPrecedence;
}
}
@@ -298,7 +298,8 @@ public class GenerateTests {
"CodeTransformationsTestGenerated",
AbstractCodeTransformationTest.class,
testModel("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression", "doTestIfStatementWithAssignmentsToExpression"),
testModel("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement", "doTestAssignmentWithIfExpressionToStatement")
testModel("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement", "doTestAssignmentWithIfExpressionToStatement"),
testModel("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses", "doTestRemoveUnnecessaryParentheses")
);
generateTest(
@@ -0,0 +1,3 @@
fun foo(x: Int) {
x * x + x
}
@@ -0,0 +1,3 @@
fun foo(x: Int) {
(x * x) + x
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention removes unnecessary parentheses.
</body>
</html>
+5
View File
@@ -294,6 +294,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.RemoveUnnecessaryParenthesesIntention</className>
<category>Kotlin</category>
</intentionAction>
<project.converterProvider implementation="org.jetbrains.jet.plugin.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
</extensions>
@@ -146,3 +146,5 @@ transform.if.statement.with.assignments.to.expression=Transform 'if' statement w
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
transform.if.statement.with.assignments.to.expression.family=Transform 'if' Statement with Assignments to Expression
transform.assignment.with.if.expression.to.statement.family=Transform Assignment with 'if' Expression to Statement
remove.unnecessary.parentheses=Remove unnecessary parentheses
remove.unnecessary.parentheses.family=Remove Unnecessary Parentheses
@@ -27,7 +27,9 @@ import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.DeferredType;
import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.util.LinkedList;
@@ -0,0 +1,60 @@
/*
* Copyright 2010-2013 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 org.jetbrains.jet.plugin.codeInsight.codeTransformations;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetParenthesizedExpression;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.plugin.JetBundle;
public class RemoveUnnecessaryParenthesesIntention extends BaseIntentionAction {
private JetParenthesizedExpression expression;
public RemoveUnnecessaryParenthesesIntention() {
setText(JetBundle.message("remove.unnecessary.parentheses"));
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("remove.unnecessary.parentheses.family");
}
@Override
public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
expression = PsiTreeUtil.getParentOfType(element, JetParenthesizedExpression.class);
return expression != null && JetPsiUtil.areParenthesesUseless(expression);
}
@Override
public void invoke(
@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file
) throws IncorrectOperationException {
JetExpression innerExpression = expression.getExpression();
assert innerExpression != null : "parenthesizedExpression.getExpression() == null despite @IfNotParsed annotation";
expression.replace(innerExpression);
}
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun foo(x: Int, y: Int) {
x + <caret>(x + y)
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun foo() : Any {
return <caret>(@a{})
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun foo(x: Int) : Any {
return <caret>(x as Int) < 42
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun foo(x: Int) {
+<caret>(+x)
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
trait Foo {
fun get(x : Any) : Foo
fun plus(x : Any) : Foo
}
fun foo(x: Foo) {
<caret>(x + x)[x]
}
@@ -0,0 +1,3 @@
fun foo(x: Int) {
<caret>(x * x / x) + x
}
@@ -0,0 +1,3 @@
fun foo(x: Int) {
x * x / x + x
}
@@ -0,0 +1,3 @@
fun foo(x: Int) : Int {
return <caret>(x)
}
@@ -0,0 +1,3 @@
fun foo(x: Int) : Int {
return <caret>x
}
@@ -0,0 +1,6 @@
trait Foo {
fun get(x : Any) : Foo
}
fun foo(x: Foo) {
<caret>(x[x])[x]
}
@@ -0,0 +1,6 @@
trait Foo {
fun get(x : Any) : Foo
}
fun foo(x: Foo) {
x[x][x]
}
@@ -0,0 +1,7 @@
trait Foo {
fun inc() : Foo
fun not() : Foo
}
fun foo(x: Foo) {
!(<caret>x.inc())
}
@@ -0,0 +1,7 @@
trait Foo {
fun inc() : Foo
fun not() : Foo
}
fun foo(x: Foo) {
!x.inc()
}
@@ -0,0 +1,3 @@
fun foo(x: Boolean) : Boolean {
return x || (x || x<caret>)
}
@@ -0,0 +1,3 @@
fun foo(x: Boolean) : Boolean {
return x || x || x
}
@@ -0,0 +1,3 @@
fun foo(x: Boolean) : Boolean {
return x && (x && x<caret>)
}
@@ -0,0 +1,3 @@
fun foo(x: Boolean) : Boolean {
return x && x && x
}
@@ -0,0 +1,3 @@
fun foo(i: Int) {
array(42)[(i + i<caret>)]
}
@@ -0,0 +1,3 @@
fun foo(i: Int) {
array(42)[i + i]
}
@@ -33,6 +33,10 @@ public abstract class AbstractCodeTransformationTest extends LightCodeInsightTes
doTest(path, new AssignmentWithIfExpressionToStatementIntention());
}
public void doTestRemoveUnnecessaryParentheses(@NotNull String path) throws Exception {
doTest(path, new RemoveUnnecessaryParenthesesIntention());
}
private void doTest(@NotNull String path, @NotNull IntentionAction intentionAction) throws Exception {
configureByFile(path);
@@ -30,7 +30,7 @@ import org.jetbrains.jet.plugin.codeInsight.codeTransformations.AbstractCodeTran
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@InnerTestClasses({CodeTransformationsTestGenerated.IfStatementWithAssignmentsToExpression.class, CodeTransformationsTestGenerated.AssignmentWithIfExpressionToStatement.class})
@InnerTestClasses({CodeTransformationsTestGenerated.IfStatementWithAssignmentsToExpression.class, CodeTransformationsTestGenerated.AssignmentWithIfExpressionToStatement.class, CodeTransformationsTestGenerated.RemoveUnnecessaryParentheses.class})
public class CodeTransformationsTestGenerated extends AbstractCodeTransformationTest {
@TestMetadata("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression")
public static class IfStatementWithAssignmentsToExpression extends AbstractCodeTransformationTest {
@@ -93,10 +93,79 @@ public class CodeTransformationsTestGenerated extends AbstractCodeTransformation
}
@TestMetadata("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses")
public static class RemoveUnnecessaryParentheses extends AbstractCodeTransformationTest {
public void testAllFilesPresentInRemoveUnnecessaryParentheses() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("necessaryParentheses1.kt")
public void testNecessaryParentheses1() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses1.kt");
}
@TestMetadata("necessaryParentheses2.kt")
public void testNecessaryParentheses2() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses2.kt");
}
@TestMetadata("necessaryParentheses3.kt")
public void testNecessaryParentheses3() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses3.kt");
}
@TestMetadata("necessaryParentheses4.kt")
public void testNecessaryParentheses4() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses4.kt");
}
@TestMetadata("necessaryParentheses5.kt")
public void testNecessaryParentheses5() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses5.kt");
}
@TestMetadata("unnecessaryParentheses1.kt")
public void testUnnecessaryParentheses1() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses1.kt");
}
@TestMetadata("unnecessaryParentheses2.kt")
public void testUnnecessaryParentheses2() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses2.kt");
}
@TestMetadata("unnecessaryParentheses3.kt")
public void testUnnecessaryParentheses3() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses3.kt");
}
@TestMetadata("unnecessaryParentheses4.kt")
public void testUnnecessaryParentheses4() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses4.kt");
}
@TestMetadata("unnecessaryParentheses5.kt")
public void testUnnecessaryParentheses5() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses5.kt");
}
@TestMetadata("unnecessaryParentheses6.kt")
public void testUnnecessaryParentheses6() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses6.kt");
}
@TestMetadata("unnecessaryParentheses7.kt")
public void testUnnecessaryParentheses7() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses7.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("CodeTransformationsTestGenerated");
suite.addTestSuite(IfStatementWithAssignmentsToExpression.class);
suite.addTestSuite(AssignmentWithIfExpressionToStatement.class);
suite.addTestSuite(RemoveUnnecessaryParentheses.class);
return suite;
}
}