Simplify "is assignment" check and move it to JetPsiUtil
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+4
-1
@@ -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;
|
||||
}
|
||||
|
||||
+3
-12
@@ -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<JetExpression> 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);
|
||||
|
||||
Reference in New Issue
Block a user