ForeachStatement added
This commit is contained in:
@@ -208,7 +208,7 @@ public class Converter {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Parameter parameterToParameter(PsiParameter parameter) {
|
||||
public static Parameter parameterToParameter(PsiParameter parameter) {
|
||||
return new Parameter(
|
||||
new IdentifierImpl(parameter.getName()), // TODO: remove
|
||||
typeToType(parameter.getType())
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class ForeachStatement extends Statement {
|
||||
private final Parameter myVariable;
|
||||
private final Expression myExpression;
|
||||
private final Statement myStatement;
|
||||
|
||||
public ForeachStatement(Parameter variable, Expression expression, Statement statement) {
|
||||
myVariable = variable;
|
||||
myExpression = expression;
|
||||
myStatement = statement;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
return "for" + SPACE + "(" + myVariable.toKotlin() + SPACE + IN + SPACE + myExpression.toKotlin() + ")" + N +
|
||||
myStatement.toKotlin();
|
||||
}
|
||||
}
|
||||
@@ -123,7 +123,7 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
|
||||
public void visitExpressionList(PsiExpressionList list) {
|
||||
super.visitExpressionList(list);
|
||||
myResult =
|
||||
new ExpressionList(expressionsToExpressionList(list.getExpressions()));
|
||||
new ExpressionList(expressionsToExpressionList(list.getExpressions()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,9 +9,7 @@ import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.j2k.Converter.expressionToExpression;
|
||||
import static org.jetbrains.jet.j2k.Converter.expressionsToExpressionList;
|
||||
import static org.jetbrains.jet.j2k.Converter.statementToStatement;
|
||||
import static org.jetbrains.jet.j2k.Converter.*;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
@@ -101,6 +99,11 @@ public class StatementVisitor extends ElementVisitor implements Visitor {
|
||||
@Override
|
||||
public void visitForeachStatement(PsiForeachStatement statement) {
|
||||
super.visitForeachStatement(statement);
|
||||
myResult = new ForeachStatement(
|
||||
parameterToParameter(statement.getIterationParameter()),
|
||||
expressionToExpression(statement.getIteratedValue()),
|
||||
statementToStatement(statement.getBody())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
package org.jetbrains.jet.j2k.visitors;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.ast.Element;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.jet.j2k.JetTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class ForeachStatementTest extends JetTestCaseBase {
|
||||
public void testEnhancedForWithEmptyBlock() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin("for (Node n : list) {}"),
|
||||
"for (n : Node? in list)\n{\n" + "}"
|
||||
);
|
||||
}
|
||||
|
||||
public void testEnhancedForWithBlock() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin("for (Node n : list) {int i = 1; i++;}"),
|
||||
"for (n : Node? in list)\n" +
|
||||
"{\n" +
|
||||
"var i : Int = 1\n" +
|
||||
"(i++)\n" +
|
||||
"}"
|
||||
);
|
||||
}
|
||||
|
||||
public void testEnhancedForWithReturn() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin("for (Node n : list) return n;"),
|
||||
"for (n : Node? in list)\nreturn n"
|
||||
);
|
||||
}
|
||||
|
||||
public void testEnhancedForWithExpression() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin("for (Node n : list) i++;"),
|
||||
"for (n : Node? in list)\n(i++)"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user