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;
}
}