Skip to content

Commit

Permalink
Support trailing commas in method invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Oct 6, 2023
1 parent 1585652 commit 390d891
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.marker.ImplicitReturn;
import org.openrewrite.java.marker.TrailingComma;
import org.openrewrite.java.tree.*;
import org.openrewrite.kotlin.KotlinTypeMapping;
import org.openrewrite.kotlin.marker.*;
Expand Down Expand Up @@ -1018,11 +1019,19 @@ public J visitCallExpression(KtCallExpression expression, ExecutionContext data)
List<JRightPadded<Expression>> expressions = new ArrayList<>(arguments.size());
Markers markers = Markers.EMPTY;

for (KtValueArgument arg : arguments) {
expressions.add(padRight(convertToExpression(arg.accept(this, data)).withPrefix(prefix(arg)), suffix(arg)));
}

if (expressions.isEmpty()) {
if (!arguments.isEmpty()) {
for (int i = 0; i < arguments.size(); i++) {
KtValueArgument arg = arguments.get(i);
JRightPadded<Expression> padded = padRight(convertToExpression(arg.accept(this, data)).withPrefix(prefix(arg)), suffix(arg));
if (i == arguments.size() - 1) {
PsiElement maybeComma = PsiTreeUtil.findSiblingForward(arg, KtTokens.COMMA, null);
if (maybeComma != null && maybeComma.getNode().getElementType() == KtTokens.COMMA) {
padded = padded.withMarkers(padded.getMarkers().addIfAbsent(new TrailingComma(randomId(), suffix(maybeComma))));
}
}
expressions.add(padded);
}
} else {
expressions.add(padRight(new J.Empty(randomId(), prefix(expression.getValueArgumentList().getRightParenthesis()), Markers.EMPTY), Space.EMPTY));
}

Expand Down

0 comments on commit 390d891

Please sign in to comment.