Relax transformation assertions
This commit is contained in:
@@ -255,8 +255,8 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JetBinaryExpression createBinaryExpression(Project project, @NotNull JetExpression lhs, @NotNull String op, @NotNull JetExpression rhs) {
|
public static JetBinaryExpression createBinaryExpression(Project project, @Nullable JetExpression lhs, @NotNull String op, @Nullable JetExpression rhs) {
|
||||||
return createBinaryExpression(project, lhs.getText(), op, rhs.getText());
|
return createBinaryExpression(project, lhs != null ? lhs.getText() : "", op, rhs != null ? rhs.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JetTypeCodeFragment createTypeCodeFragment(Project project, String text, PsiElement context) {
|
public static JetTypeCodeFragment createTypeCodeFragment(Project project, String text, PsiElement context) {
|
||||||
@@ -267,29 +267,19 @@ public class JetPsiFactory {
|
|||||||
return new JetExpressionCodeFragmentImpl(project, "fragment.kt", text, context);
|
return new JetExpressionCodeFragmentImpl(project, "fragment.kt", text, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static JetIsExpression createIsExpression(Project project, @NotNull String lhs, @NotNull String rhs, boolean negated) {
|
|
||||||
return (JetIsExpression) createExpression(project, lhs + " " + (negated ? "!is" : "is") + " " + rhs);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static JetIsExpression createIsExpression(Project project, @NotNull JetExpression lhs, @NotNull JetTypeReference rhs, boolean negated) {
|
|
||||||
return createIsExpression(project, lhs.getText(), rhs.getText(), negated);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JetReturnExpression createReturn(Project project, @NotNull String text) {
|
public static JetReturnExpression createReturn(Project project, @NotNull String text) {
|
||||||
return (JetReturnExpression) createExpression(project, "return " + text);
|
return (JetReturnExpression) createExpression(project, "return " + text);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JetReturnExpression createReturn(Project project, @NotNull JetExpression expression) {
|
public static JetReturnExpression createReturn(Project project, @Nullable JetExpression expression) {
|
||||||
return createReturn(project, expression.getText());
|
return createReturn(project, expression != null ? expression.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JetIfExpression createIf(Project project,
|
public static JetIfExpression createIf(Project project,
|
||||||
@NotNull JetExpression condition, @NotNull JetExpression thenExpr, @Nullable JetExpression elseExpr) {
|
@Nullable JetExpression condition, @Nullable JetExpression thenExpr, @Nullable JetExpression elseExpr) {
|
||||||
return (JetIfExpression) createExpression(project, JetPsiUnparsingUtils.toIf(condition, thenExpr, elseExpr));
|
return (JetIfExpression) createExpression(project, JetPsiUnparsingUtils.toIf(condition, thenExpr, elseExpr));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,8 +315,8 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public IfChainBuilder elseBranch(@NotNull JetExpression expression) {
|
public IfChainBuilder elseBranch(@Nullable JetExpression expression) {
|
||||||
return elseBranch(expression.getText());
|
return elseBranch(expression != null ? expression.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -373,8 +363,8 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public WhenBuilder condition(@NotNull JetExpression expression) {
|
public WhenBuilder condition(@Nullable JetExpression expression) {
|
||||||
return condition(expression.getText());
|
return condition(expression != null ? expression.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -383,8 +373,8 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public WhenBuilder pattern(@NotNull JetTypeReference typeReference, boolean negated) {
|
public WhenBuilder pattern(@Nullable JetTypeReference typeReference, boolean negated) {
|
||||||
return pattern(typeReference.getText(), negated);
|
return pattern(typeReference != null ? typeReference.getText() : "", negated);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -393,8 +383,8 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public WhenBuilder range(@NotNull JetExpression argument, boolean negated) {
|
public WhenBuilder range(@Nullable JetExpression argument, boolean negated) {
|
||||||
return range(argument.getText(), negated);
|
return range(argument != null ? argument.getText() : "", negated);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -409,8 +399,8 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public WhenBuilder branchExpression(@NotNull JetExpression expression) {
|
public WhenBuilder branchExpression(@Nullable JetExpression expression) {
|
||||||
return branchExpression(expression.getText());
|
return branchExpression(expression != null ? expression.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -424,8 +414,8 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public WhenBuilder entry(@NotNull JetWhenEntry whenEntry) {
|
public WhenBuilder entry(@Nullable JetWhenEntry whenEntry) {
|
||||||
return entry(whenEntry.getText());
|
return entry(whenEntry != null ? whenEntry.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -434,8 +424,8 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public WhenBuilder elseEntry(@NotNull JetExpression expression) {
|
public WhenBuilder elseEntry(@Nullable JetExpression expression) {
|
||||||
return elseEntry(expression.getText());
|
return elseEntry(expression != null ? expression.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -447,20 +437,4 @@ public class JetPsiFactory {
|
|||||||
return (JetWhenExpression) createExpression(project, sb.toString());
|
return (JetWhenExpression) createExpression(project, sb.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static JetParenthesizedExpression createParenthesizedExpression(Project project, @NotNull String text) {
|
|
||||||
return (JetParenthesizedExpression) createExpression(project, "(" + text + ")");
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static JetParenthesizedExpression createParenthesizedExpression(Project project, @NotNull JetExpression expression) {
|
|
||||||
return createParenthesizedExpression(project, expression.getText());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static JetParenthesizedExpression createParenthesizedExpressionIfNeeded(Project project, @NotNull JetExpression expression) {
|
|
||||||
return (expression instanceof JetParenthesizedExpression) ?
|
|
||||||
(JetParenthesizedExpression) expression : createParenthesizedExpression(project, expression);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,12 @@ public class JetPsiUnparsingUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static String toIf(@NotNull JetExpression condition, @NotNull JetExpression thenExpression, @Nullable JetExpression elseExpression) {
|
public static String toIf(@Nullable JetExpression condition, @Nullable JetExpression thenExpression, @Nullable JetExpression elseExpression) {
|
||||||
return toIf(condition.getText(), thenExpression.getText(), elseExpression != null ? elseExpression.getText() : null);
|
return toIf(
|
||||||
|
condition != null ? condition.getText() : "",
|
||||||
|
thenExpression != null ? thenExpression.getText() : "",
|
||||||
|
elseExpression != null ? elseExpression.getText() : null
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -34,8 +38,8 @@ public class JetPsiUnparsingUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static String toBinaryExpression(@NotNull JetExpression left, @NotNull String op, @NotNull JetElement right) {
|
public static String toBinaryExpression(@Nullable JetExpression left, @NotNull String op, @Nullable JetElement right) {
|
||||||
return toBinaryExpression(left.getText(), op, right.getText());
|
return toBinaryExpression(left != null ? left.getText() : "", op, right != null ? right.getText() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -44,7 +48,9 @@ public class JetPsiUnparsingUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static String parenthesizeIfNeeded(@NotNull JetExpression expression) {
|
public static String parenthesizeIfNeeded(@Nullable JetExpression expression) {
|
||||||
|
if (expression == null) return "";
|
||||||
|
|
||||||
String text = expression.getText();
|
String text = expression.getText();
|
||||||
return (expression instanceof JetParenthesizedExpression ||
|
return (expression instanceof JetParenthesizedExpression ||
|
||||||
expression instanceof JetConstantExpression ||
|
expression instanceof JetConstantExpression ||
|
||||||
|
|||||||
+15
-13
@@ -1,10 +1,12 @@
|
|||||||
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
|
package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
import org.jetbrains.jet.lexer.JetTokens;
|
import org.jetbrains.jet.lexer.JetTokens;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.*;
|
import static org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils.*;
|
||||||
@@ -18,7 +20,7 @@ public class IfWhenUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean checkIfToWhen(@NotNull JetIfExpression ifExpression) {
|
public static boolean checkIfToWhen(@NotNull JetIfExpression ifExpression) {
|
||||||
return ifExpression.getCondition() != null && ifExpression.getThen() != null && ifExpression.getElse() != null;
|
return ifExpression.getThen() != null && ifExpression.getElse() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean checkWhenToIf(@NotNull JetWhenExpression whenExpression) {
|
public static boolean checkWhenToIf(@NotNull JetWhenExpression whenExpression) {
|
||||||
@@ -29,7 +31,9 @@ public class IfWhenUtils {
|
|||||||
assert expression != null : TRANSFORM_WITHOUT_CHECK;
|
assert expression != null : TRANSFORM_WITHOUT_CHECK;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<JetExpression> splitExpressionToOrBranches(JetExpression expression) {
|
private static List<JetExpression> splitExpressionToOrBranches(@Nullable JetExpression expression) {
|
||||||
|
if (expression == null) return Collections.emptyList();
|
||||||
|
|
||||||
final List<JetExpression> branches = new ArrayList<JetExpression>();
|
final List<JetExpression> branches = new ArrayList<JetExpression>();
|
||||||
|
|
||||||
expression.accept(
|
expression.accept(
|
||||||
@@ -80,14 +84,19 @@ public class IfWhenUtils {
|
|||||||
JetExpression thenBranch = currIfExpression.getThen();
|
JetExpression thenBranch = currIfExpression.getThen();
|
||||||
JetExpression elseBranch = currIfExpression.getElse();
|
JetExpression elseBranch = currIfExpression.getElse();
|
||||||
|
|
||||||
assertNotNull(condition);
|
|
||||||
assertNotNull(thenBranch);
|
assertNotNull(thenBranch);
|
||||||
assertNotNull(elseBranch);
|
assertNotNull(elseBranch);
|
||||||
|
|
||||||
List<JetExpression> orBranches = splitExpressionToOrBranches(condition);
|
List<JetExpression> orBranches = splitExpressionToOrBranches(condition);
|
||||||
for (JetExpression orBranch : orBranches) {
|
|
||||||
builder.condition(orBranch);
|
if (orBranches.isEmpty()) {
|
||||||
|
builder.condition("");
|
||||||
|
} else {
|
||||||
|
for (JetExpression orBranch : orBranches) {
|
||||||
|
builder.condition(orBranch);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
builder.branchExpression(thenBranch);
|
builder.branchExpression(thenBranch);
|
||||||
|
|
||||||
@@ -135,19 +144,12 @@ public class IfWhenUtils {
|
|||||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||||
for (JetWhenEntry entry : entries) {
|
for (JetWhenEntry entry : entries) {
|
||||||
JetExpression branch = entry.getExpression();
|
JetExpression branch = entry.getExpression();
|
||||||
assertNotNull(branch);
|
|
||||||
|
|
||||||
if (entry.isElse()) {
|
if (entry.isElse()) {
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.elseBranch(branch);
|
builder.elseBranch(branch);
|
||||||
} else {
|
} else {
|
||||||
String branchConditionText = combineWhenConditions(entry.getConditions(), whenExpression.getSubjectExpression());
|
String branchConditionText = combineWhenConditions(entry.getConditions(), whenExpression.getSubjectExpression());
|
||||||
|
builder.ifBranch(branchConditionText, branch != null ? branch.getText() : "");
|
||||||
JetExpression branchExpression = entry.getExpression();
|
|
||||||
assertNotNull(branchExpression);
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.ifBranch(branchConditionText, branchExpression.getText());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-37
@@ -120,10 +120,8 @@ public class WhenUtils {
|
|||||||
|
|
||||||
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
||||||
JetExpression branchExpression = entry.getExpression();
|
JetExpression branchExpression = entry.getExpression();
|
||||||
assertNotNull(branchExpression);
|
|
||||||
|
|
||||||
if (entry.isElse()) {
|
if (entry.isElse()) {
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.elseEntry(branchExpression);
|
builder.elseEntry(branchExpression);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -135,30 +133,21 @@ public class WhenUtils {
|
|||||||
|
|
||||||
if (conditionExpression instanceof JetIsExpression) {
|
if (conditionExpression instanceof JetIsExpression) {
|
||||||
JetIsExpression isExpression = (JetIsExpression) conditionExpression;
|
JetIsExpression isExpression = (JetIsExpression) conditionExpression;
|
||||||
|
builder.pattern(isExpression.getTypeRef(), isExpression.isNegated());
|
||||||
JetTypeReference typeReference = isExpression.getTypeRef();
|
|
||||||
assertNotNull(typeReference);
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.pattern(typeReference, isExpression.isNegated());
|
|
||||||
}
|
}
|
||||||
else if (conditionExpression instanceof JetBinaryExpression) {
|
else if (conditionExpression instanceof JetBinaryExpression) {
|
||||||
JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression;
|
JetBinaryExpression binaryExpression = (JetBinaryExpression) conditionExpression;
|
||||||
|
|
||||||
JetExpression rhs = binaryExpression.getRight();
|
JetExpression rhs = binaryExpression.getRight();
|
||||||
assertNotNull(rhs);
|
|
||||||
|
|
||||||
IElementType op = binaryExpression.getOperationToken();
|
IElementType op = binaryExpression.getOperationToken();
|
||||||
if (op == JetTokens.IN_KEYWORD) {
|
if (op == JetTokens.IN_KEYWORD) {
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.range(rhs, false);
|
builder.range(rhs, false);
|
||||||
}
|
}
|
||||||
else if (op == JetTokens.NOT_IN) {
|
else if (op == JetTokens.NOT_IN) {
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.range(rhs, true);
|
builder.range(rhs, true);
|
||||||
}
|
}
|
||||||
else if (op == JetTokens.EQEQ) {
|
else if (op == JetTokens.EQEQ) {
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.condition(rhs);
|
builder.condition(rhs);
|
||||||
}
|
}
|
||||||
else assert false : TRANSFORM_WITHOUT_CHECK;
|
else assert false : TRANSFORM_WITHOUT_CHECK;
|
||||||
@@ -166,7 +155,6 @@ public class WhenUtils {
|
|||||||
else assert false : TRANSFORM_WITHOUT_CHECK;
|
else assert false : TRANSFORM_WITHOUT_CHECK;
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.branchExpression(branchExpression);
|
builder.branchExpression(branchExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,37 +164,22 @@ public class WhenUtils {
|
|||||||
static String whenConditionToExpressionText(@NotNull JetWhenCondition condition, JetExpression subject) {
|
static String whenConditionToExpressionText(@NotNull JetWhenCondition condition, JetExpression subject) {
|
||||||
if (condition instanceof JetWhenConditionIsPattern) {
|
if (condition instanceof JetWhenConditionIsPattern) {
|
||||||
JetWhenConditionIsPattern patternCondition = (JetWhenConditionIsPattern) condition;
|
JetWhenConditionIsPattern patternCondition = (JetWhenConditionIsPattern) condition;
|
||||||
|
return toBinaryExpression(subject, (patternCondition.isNegated() ? "!is" : "is"), patternCondition.getTypeRef());
|
||||||
JetTypeReference typeReference = patternCondition.getTypeRef();
|
|
||||||
assertNotNull(typeReference);
|
|
||||||
|
|
||||||
assertNotNull(subject);
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
return toBinaryExpression(subject, (patternCondition.isNegated() ? "!is" : "is"), typeReference);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (condition instanceof JetWhenConditionInRange) {
|
if (condition instanceof JetWhenConditionInRange) {
|
||||||
JetWhenConditionInRange rangeCondition = (JetWhenConditionInRange) condition;
|
JetWhenConditionInRange rangeCondition = (JetWhenConditionInRange) condition;
|
||||||
|
return toBinaryExpression(subject, rangeCondition.getOperationReference().getText(), rangeCondition.getRangeExpression());
|
||||||
JetExpression rangeExpression = rangeCondition.getRangeExpression();
|
|
||||||
assertNotNull(rangeExpression);
|
|
||||||
|
|
||||||
assertNotNull(subject);
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
return toBinaryExpression(subject, rangeCondition.getOperationReference().getText(), rangeExpression);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert condition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK;
|
assert condition instanceof JetWhenConditionWithExpression : TRANSFORM_WITHOUT_CHECK;
|
||||||
|
|
||||||
JetExpression conditionExpression = ((JetWhenConditionWithExpression) condition).getExpression();
|
JetExpression conditionExpression = ((JetWhenConditionWithExpression) condition).getExpression();
|
||||||
assertNotNull(conditionExpression);
|
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
if (subject != null) {
|
||||||
return subject != null ?
|
return toBinaryExpression(parenthesizeIfNeeded(subject), "==", parenthesizeIfNeeded(conditionExpression));
|
||||||
toBinaryExpression(parenthesizeIfNeeded(subject), "==", parenthesizeIfNeeded(conditionExpression))
|
}
|
||||||
: conditionExpression.getText();
|
return conditionExpression != null ? conditionExpression.getText() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void eliminateWhenSubject(@NotNull JetWhenExpression whenExpression) {
|
public static void eliminateWhenSubject(@NotNull JetWhenExpression whenExpression) {
|
||||||
@@ -217,10 +190,8 @@ public class WhenUtils {
|
|||||||
|
|
||||||
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
||||||
JetExpression branchExpression = entry.getExpression();
|
JetExpression branchExpression = entry.getExpression();
|
||||||
assertNotNull(branchExpression);
|
|
||||||
|
|
||||||
if (entry.isElse()) {
|
if (entry.isElse()) {
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.elseEntry(branchExpression);
|
builder.elseEntry(branchExpression);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
@@ -230,7 +201,6 @@ public class WhenUtils {
|
|||||||
builder.condition(whenConditionToExpressionText(condition, subject));
|
builder.condition(whenConditionToExpressionText(condition, subject));
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection ConstantConditions
|
|
||||||
builder.branchExpression(branchExpression);
|
builder.branchExpression(branchExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user