generate chained append calls for chained string concatenations
This commit is contained in:
@@ -723,6 +723,14 @@ public class ExpressionCodegen extends JetVisitor {
|
||||
}
|
||||
|
||||
private void invokeAppend(final JetExpression expr) {
|
||||
if (expr instanceof JetBinaryExpression) {
|
||||
final JetBinaryExpression binaryExpression = (JetBinaryExpression) expr;
|
||||
if (binaryExpression.getOperationToken() == JetTokens.PLUS) {
|
||||
invokeAppend(binaryExpression.getLeft());
|
||||
invokeAppend(binaryExpression.getRight());
|
||||
return;
|
||||
}
|
||||
}
|
||||
Type exprType = expressionType(expr);
|
||||
gen(expr, exprType);
|
||||
Method appendDescriptor = new Method("append", Type.getObjectType(CLASS_STRING_BUILDER),
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.jetbrains.jet.lang.psi;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetNodeTypes;
|
||||
@@ -45,4 +46,8 @@ public class JetBinaryExpression extends JetExpression {
|
||||
return (JetSimpleNameExpression) findChildByType(JetNodeTypes.OPERATION_REFERENCE);
|
||||
}
|
||||
|
||||
public IElementType getOperationToken() {
|
||||
return getOperationReference().getReferencedNameElementType();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -568,6 +568,15 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
|
||||
assertEquals("jetLang", main.invoke(null, "jet", "Lang"));
|
||||
}
|
||||
|
||||
public void testStringPlusChained() throws Exception {
|
||||
loadText("fun foo(s1: String, s2: String, s3: String) = s1 + s2 + s3");
|
||||
final String text = generateToText();
|
||||
final int firstStringBuilderCreation = text.indexOf("NEW java/lang/StringBuilder");
|
||||
assertEquals(-1, text.indexOf("NEW java/lang/StringBuilder", firstStringBuilderCreation+1));
|
||||
final Method main = generateFunction();
|
||||
assertEquals("jet Lang", main.invoke(null, "jet", " ", "Lang"));
|
||||
}
|
||||
|
||||
private void binOpTest(final String text, final Object arg1, final Object arg2, final Object expected) throws Exception {
|
||||
loadText(text);
|
||||
System.out.println(generateToText());
|
||||
|
||||
Reference in New Issue
Block a user