Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for loops with destructuring declaration #595

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public J visitForExpression(KtForExpression expression, ExecutionContext data) {
randomId(),
prefix(expression.getLeftParenthesis()),
Markers.EMPTY,
padRight((J.VariableDeclarations) requireNonNull(expression.getLoopParameter()).accept(this, data), suffix(expression.getLoopParameter())),
padRight((TypedTree) requireNonNull(expression.getLoopParameter()).accept(this, data), suffix(expression.getLoopParameter())),
padRight(requireNonNull(expression.getLoopRange()).accept(this, data)
.withPrefix(prefix(expression.getLoopRange().getParent())), suffix(expression.getLoopRange().getParent()))
),
Expand Down Expand Up @@ -886,6 +886,10 @@ public J visitNullableType(KtNullableType nullableType, ExecutionContext data) {

@Override
public J visitParameter(KtParameter parameter, ExecutionContext data) {
if (parameter.getDestructuringDeclaration() != null) {
return parameter.getDestructuringDeclaration().accept(this, data).withPrefix(prefix(parameter));
}

Markers markers = Markers.EMPTY;
List<J.Annotation> leadingAnnotations = new ArrayList<>();
List<J.Annotation> lastAnnotations = new ArrayList<>();
Expand Down Expand Up @@ -916,11 +920,6 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) {
}
}

if (parameter.getDestructuringDeclaration() != null) {
return mapDestructuringDeclaration(parameter.getDestructuringDeclaration(), data)
.withPrefix(prefix(parameter));
}

JavaType.Variable vt = variableType(parameter, owner(parameter));
J.Identifier name = createIdentifier(requireNonNull(parameter.getNameIdentifier()), vt, consumedSpaces);

Expand Down Expand Up @@ -2338,21 +2337,21 @@ public J visitClassBody(KtClassBody classBody, ExecutionContext data) {

@Override
public J visitDestructuringDeclaration(KtDestructuringDeclaration multiDeclaration, ExecutionContext data) {
List<J.Modifier> modifiers = new ArrayList<>();
List<J.Annotation> leadingAnnotations = new ArrayList<>();
List<JRightPadded<Statement>> destructVars = new ArrayList<>();

JLeftPadded<Expression> paddedInitializer = null;

J.Modifier modifier = new J.Modifier(
Tree.randomId(),
prefix(multiDeclaration.getValOrVarKeyword(), preConsumedInfix(multiDeclaration)),
Markers.EMPTY,
multiDeclaration.isVar() ? "var" : null,
multiDeclaration.isVar() ? J.Modifier.Type.LanguageExtension : J.Modifier.Type.Final,
Collections.emptyList()
);
modifiers.add(modifier);
List<J.Modifier> modifiers = multiDeclaration.getValOrVarKeyword() != null ?
singletonList(
new J.Modifier(
Tree.randomId(),
prefix(multiDeclaration.getValOrVarKeyword(), preConsumedInfix(multiDeclaration)),
Markers.EMPTY,
multiDeclaration.isVar() ? "var" : null,
multiDeclaration.isVar() ? J.Modifier.Type.LanguageExtension : J.Modifier.Type.Final,
Collections.emptyList()
)) : emptyList();

if (multiDeclaration.getInitializer() != null) {
paddedInitializer = padLeft(suffix(multiDeclaration.getRPar()),
Expand Down Expand Up @@ -2445,7 +2444,8 @@ public J visitDestructuringDeclaration(KtDestructuringDeclaration multiDeclarati
Markers.EMPTY,
variableDeclarations,
null,
JContainer.build(prefix(multiDeclaration.getLPar()), destructVars, Markers.EMPTY)
JContainer.build(prefix(multiDeclaration.getLPar()), destructVars, Markers.EMPTY),
type(multiDeclaration)
);
}

Expand Down Expand Up @@ -3980,49 +3980,6 @@ private List<J.Annotation> mapAnnotations(List<KtAnnotationEntry> ktAnnotationEn
.collect(Collectors.toList());
}

private J mapDestructuringDeclaration(KtDestructuringDeclaration ktDestructuringDeclaration, ExecutionContext data) {
List<KtDestructuringDeclarationEntry> entries = ktDestructuringDeclaration.getEntries();
List<JRightPadded<J.VariableDeclarations.NamedVariable>> variables = new ArrayList<>(entries.size());

for (KtDestructuringDeclarationEntry ktDestructuringDeclarationEntry : entries) {
J.Identifier name = (J.Identifier) ktDestructuringDeclarationEntry.accept(this, data);

J.VariableDeclarations.NamedVariable namedVariable = new J.VariableDeclarations.NamedVariable(
randomId(),
Space.EMPTY,
Markers.EMPTY,
name,
emptyList(),
null,
variableType(ktDestructuringDeclarationEntry, owner(ktDestructuringDeclarationEntry))
);
variables.add(padRight(namedVariable, suffix(ktDestructuringDeclarationEntry)));
}

J j = new J.VariableDeclarations(
randomId(),
prefix(ktDestructuringDeclaration),
Markers.EMPTY.addIfAbsent(new OmitEquals(randomId())),
emptyList(),
emptyList(),
null,
null,
emptyList(),
variables
);

if (entries.size() == 1) {
// Handle potential redundant parentheses
List<PsiElement> allChildren = getAllChildren(ktDestructuringDeclaration);
int l = findFirstLPAR(allChildren, 0);
int r = findLastRPAR(allChildren, allChildren.size() - 1);
if (l >= 0 && l < r) {
j = new J.Parentheses<>(randomId(), Space.EMPTY, Markers.EMPTY, padRight(j, Space.EMPTY));
}
}
return j;
}

private J.Modifier mapModifier(PsiElement modifier, List<J.Annotation> annotations, @Nullable Set<PsiElement> consumedSpaces) {
Space prefix = prefix(modifier, consumedSpaces);
J.Modifier.Type type;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/openrewrite/kotlin/tree/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ public String toString() {
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
final class DestructuringDeclaration implements K, Statement {
final class DestructuringDeclaration implements K, Statement, TypedTree {

@Nullable
@NonFinal
Expand Down Expand Up @@ -939,6 +939,11 @@ final class DestructuringDeclaration implements K, Statement {

JContainer<Statement> destructAssignments;

@With
@Getter
@Nullable
JavaType type;

@Deprecated
@Nullable
public List<J.VariableDeclarations.NamedVariable> getAssignments() {
Expand Down Expand Up @@ -1004,7 +1009,7 @@ public JContainer<Statement> getDestructAssignments() {
}

public DestructuringDeclaration withDestructAssignments(JContainer<Statement> assignments) {
return t.destructAssignments == assignments ? t : new DestructuringDeclaration(t.id, t.prefix, t.markers, t.initializer, null, assignments);
return t.destructAssignments == assignments ? t : new DestructuringDeclaration(t.id, t.prefix, t.markers, t.initializer, null, assignments, t.type);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/openrewrite/kotlin/tree/ForLoopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ fun method ( array : Array < Int > ) {
);
}

@Test
void destructuring() {
rewriteRun(
kotlin(
"""
fun method (array: Array<Array<String>>) {
for ( (s:String) in array ) {
}
}
"""
)
);
}

@Test
void downToWithStep() {
rewriteRun(
Expand Down
Loading