diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java index 92af4a42247..156445850a1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUtil.java @@ -37,6 +37,7 @@ import org.jetbrains.jet.lang.types.expressions.OperatorConventions; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lexer.JetToken; import org.jetbrains.jet.lexer.JetTokens; +import org.jetbrains.jet.util.QualifiedNamesUtil; import java.util.Collection; import java.util.HashSet; @@ -621,4 +622,9 @@ public class JetPsiUtil { int parentPrecedence = getPrecedenceOfOperation(parentExpression, parentOperation); return innerPrecedence < parentPrecedence; } + + public static boolean isAssignment(@NotNull JetElement element) { + return element instanceof JetBinaryExpression && + JetTokens.ALL_ASSIGNMENTS.contains(((JetBinaryExpression) element).getOperationToken()); + } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lexer/JetTokens.java b/compiler/frontend/src/org/jetbrains/jet/lexer/JetTokens.java index c7aeb44da88..8bd4eb4594d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lexer/JetTokens.java +++ b/compiler/frontend/src/org/jetbrains/jet/lexer/JetTokens.java @@ -195,4 +195,5 @@ public interface JetTokens { IDENTIFIER, LABEL_IDENTIFIER, ATAT, AT); TokenSet AUGMENTED_ASSIGNMENTS = TokenSet.create(PLUSEQ, MINUSEQ, MULTEQ, PERCEQ, DIVEQ); + TokenSet ALL_ASSIGNMENTS = TokenSet.create(EQ, PLUSEQ, MINUSEQ, MULTEQ, PERCEQ, DIVEQ); } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AssignmentWithIfExpressionToStatementIntention.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AssignmentWithIfExpressionToStatementIntention.java index 1192e20b1cb..fc5722b207c 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AssignmentWithIfExpressionToStatementIntention.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AssignmentWithIfExpressionToStatementIntention.java @@ -26,6 +26,7 @@ import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.psi.JetBinaryExpression; +import org.jetbrains.jet.lang.psi.JetElement; import org.jetbrains.jet.plugin.JetBundle; public class AssignmentWithIfExpressionToStatementIntention extends BaseIntentionAction { @@ -45,7 +46,9 @@ public class AssignmentWithIfExpressionToStatementIntention extends BaseIntentio PsiElement element = file.findElementAt(offset); while (element != null) { - if (CodeTransformationUtils.checkAssignmentWithIfExpression(element)) return (JetBinaryExpression)element; + if (!(element instanceof JetElement)) return null; + + if (CodeTransformationUtils.checkAssignmentWithIfExpression((JetElement) element)) return (JetBinaryExpression)element; PsiElement parent = PsiTreeUtil.getParentOfType(element, JetBinaryExpression.class, false); element = (element != parent) ? parent : null; } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationUtils.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationUtils.java index d49198f2480..d20b67c5c5a 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationUtils.java @@ -17,10 +17,8 @@ package org.jetbrains.jet.plugin.codeInsight.codeTransformations; import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.psi.*; -import org.jetbrains.jet.lexer.JetTokens; import java.util.ArrayList; import java.util.List; @@ -94,17 +92,10 @@ public class CodeTransformationUtils { ); } - public static boolean isAssignment(@NotNull PsiElement element) { - if (!(element instanceof JetBinaryExpression)) return false; - JetBinaryExpression binaryExpression = (JetBinaryExpression)element; - if (binaryExpression.getOperationReference().getReferencedNameElementType() != JetTokens.EQ) return false; - return true; - } - private static boolean checkAllOutcomesAreCompatibleAssignments(@NotNull List outcomes) { JetExpression lastLhs = null; for (JetExpression outcome : outcomes) { - if (!isAssignment(outcome)) return false; + if (!JetPsiUtil.isAssignment(outcome)) return false; JetExpression currLhs = ((JetBinaryExpression)outcome).getLeft(); if (!(currLhs instanceof JetSimpleNameExpression)) return false; @@ -124,8 +115,8 @@ public class CodeTransformationUtils { return !outcomes.isEmpty() && checkAllIfExpressionsAreComplete(ifExpression) && checkAllOutcomesAreCompatibleAssignments(outcomes); } - static boolean checkAssignmentWithIfExpression(@NotNull PsiElement element) { - if (!isAssignment(element)) return false; + static boolean checkAssignmentWithIfExpression(@NotNull JetElement element) { + if (!JetPsiUtil.isAssignment(element)) return false; JetBinaryExpression assignment = (JetBinaryExpression)element; return (assignment.getLeft() instanceof JetSimpleNameExpression) && (assignment.getRight() instanceof JetIfExpression);