Skip to content

Commit 10052ef

Browse files
authored
Fix Semi-colon on inline methods after property declaration disappears (#585)
1 parent 301e371 commit 10052ef

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

src/main/java/org/openrewrite/kotlin/KotlinVisitor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public J visitProperty(K.Property property, P p) {
289289
pr = (K.Property) temp;
290290
}
291291

292-
pr = pr.withVariableDeclarations(visitAndCast(pr.getVariableDeclarations(), p));
292+
pr = pr.getPadding().withVariableDeclarations(visitRightPadded(pr.getPadding().getVariableDeclarations(), p));
293293
pr = pr.getPadding().withReceiver(visitRightPadded(pr.getPadding().getReceiver(), p));
294294
pr = pr.withAccessors(visitContainer(pr.getAccessors(), p));
295295
return pr;

src/main/java/org/openrewrite/kotlin/format/SpacesVisitor.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@ public K.Property visitProperty(K.Property property, P p) {
352352
if (prop.getPadding().getReceiver() != null) {
353353
prop = prop.getPadding().withReceiver(prop.getPadding().getReceiver().withAfter(updateSpace(prop.getPadding().getReceiver().getAfter(), false)));
354354
}
355+
355356
if (prop.getVariableDeclarations() != null && !prop.getVariableDeclarations().getVariables().isEmpty()) {
356-
prop = prop.withVariableDeclarations(
357-
prop.getVariableDeclarations().withVariables(
358-
ListUtils.mapFirst(prop.getVariableDeclarations().getVariables(),
359-
v -> spaceBefore(v, false))
360-
)
361-
);
357+
List<J.VariableDeclarations.NamedVariable> variables = ListUtils.mapFirst(prop.getVariableDeclarations().getVariables(),
358+
v -> spaceBefore(v, false));
359+
JRightPadded<J.VariableDeclarations> rp = prop.getPadding().getVariableDeclarations();
360+
rp = rp.withElement(rp.getElement().withVariables(variables));
361+
prop = prop.getPadding().withVariableDeclarations(rp);
362362
}
363363
return prop;
364364
}

src/main/java/org/openrewrite/kotlin/internal/KotlinPrinter.java

+5
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ public J visitProperty(K.Property property, PrintOutputCapture<P> p) {
388388
delegate.visitContainer("where", property.getTypeConstraints().getPadding().getConstraints(), JContainer.Location.TYPE_PARAMETERS, ",", "", p);
389389
}
390390

391+
visitSpace(property.getPadding().getVariableDeclarations().getAfter(), Space.Location.VARIABLE_INITIALIZER, p);
392+
if (property.getPadding().getVariableDeclarations().getMarkers().findFirst(Semicolon.class).isPresent()) {
393+
p.append(";");
394+
}
395+
391396
visitContainer(property.getAccessors(), p);
392397
afterSyntax(property, p);
393398
return property;

src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -2959,7 +2959,19 @@ public J visitProperty(KtProperty property, ExecutionContext data) {
29592959
if (!ktPropertyAccessors.isEmpty() || receiver != null || typeConstraints != null) {
29602960
List<JRightPadded<J.MethodDeclaration>> accessors = new ArrayList<>(ktPropertyAccessors.size());
29612961

2962-
for (KtPropertyAccessor ktPropertyAccessor : ktPropertyAccessors) {
2962+
Space beforeSemiColon = Space.EMPTY;
2963+
Markers rpMarkers = Markers.EMPTY;
2964+
for (int i = 0; i < ktPropertyAccessors.size(); i++) {
2965+
KtPropertyAccessor ktPropertyAccessor = ktPropertyAccessors.get(i);
2966+
2967+
if (i == 0) {
2968+
PsiElement maybeSemiColon = PsiTreeUtil.findSiblingBackward(ktPropertyAccessor, KtTokens.SEMICOLON, null);
2969+
if (maybeSemiColon != null) {
2970+
beforeSemiColon = prefix(maybeSemiColon);
2971+
rpMarkers = rpMarkers.addIfAbsent(new Semicolon(randomId()));
2972+
}
2973+
}
2974+
29632975
J.MethodDeclaration accessor = (J.MethodDeclaration) ktPropertyAccessor.accept(this, data);
29642976
accessors.add(maybeTrailingSemicolonInternal(accessor, ktPropertyAccessor));
29652977
}
@@ -2969,7 +2981,7 @@ public J visitProperty(KtProperty property, ExecutionContext data) {
29692981
deepPrefix(property),
29702982
markers,
29712983
typeParameters,
2972-
variableDeclarations.withPrefix(Space.EMPTY),
2984+
padRight(variableDeclarations.withPrefix(Space.EMPTY), beforeSemiColon, rpMarkers),
29732985
typeConstraints,
29742986
JContainer.build(accessors),
29752987
receiver

src/main/java/org/openrewrite/kotlin/tree/K.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ public List<TypeParameter> getTypeParameters() {
15041504
return typeParameters == null ? null : typeParameters.getElements();
15051505
}
15061506

1507-
J.VariableDeclarations variableDeclarations;
1507+
JRightPadded<J.VariableDeclarations> paddedVariableDeclarations;
15081508

15091509
@Nullable
15101510
TypeConstraints typeConstraints;
@@ -1523,7 +1523,8 @@ public Property(UUID id,
15231523
Space prefix,
15241524
Markers markers,
15251525
JContainer<TypeParameter> typeParameters,
1526-
VariableDeclarations variableDeclarations,
1526+
@Nullable JRightPadded<J.VariableDeclarations> paddedVariableDeclarations,
1527+
@Nullable VariableDeclarations variableDeclarations,
15271528
@Nullable K.TypeConstraints typeConstraints,
15281529
@Nullable @JsonProperty("getter") J.MethodDeclaration getter,
15291530
@Nullable @JsonProperty("setter") J.MethodDeclaration setter,
@@ -1535,7 +1536,14 @@ public Property(UUID id,
15351536
this.prefix = prefix;
15361537
this.markers = markers;
15371538
this.typeParameters = typeParameters;
1538-
this.variableDeclarations = variableDeclarations;
1539+
1540+
if (variableDeclarations != null) {
1541+
// from old LST
1542+
this.paddedVariableDeclarations = new JRightPadded<>(variableDeclarations, Space.EMPTY, Markers.EMPTY);
1543+
} else {
1544+
this.paddedVariableDeclarations = requireNonNull(paddedVariableDeclarations);
1545+
}
1546+
15391547
this.typeConstraints = typeConstraints;
15401548

15411549
if (getter != null || setter != null || isSetterFirst != null) {
@@ -1561,6 +1569,10 @@ public Property(UUID id,
15611569
this.receiver = receiver;
15621570
}
15631571

1572+
public J.VariableDeclarations getVariableDeclarations() {
1573+
return paddedVariableDeclarations.getElement();
1574+
}
1575+
15641576
@Nullable
15651577
public Expression getReceiver() {
15661578
return receiver == null ? null : receiver.getElement();
@@ -1602,14 +1614,23 @@ public String toString() {
16021614
public static class Padding {
16031615
private final Property t;
16041616

1617+
public JRightPadded<J.VariableDeclarations> getVariableDeclarations() {
1618+
return t.paddedVariableDeclarations;
1619+
}
1620+
1621+
public Property withVariableDeclarations(JRightPadded<J.VariableDeclarations> variableDeclarations) {
1622+
return t.paddedVariableDeclarations == variableDeclarations ? t : new Property(t.id, t.prefix, t.markers, t.typeParameters,
1623+
variableDeclarations, t.typeConstraints, t.accessors, t.receiver);
1624+
}
1625+
16051626
@Nullable
16061627
public JContainer<TypeParameter> getTypeParameters() {
16071628
return t.typeParameters;
16081629
}
16091630

16101631
public Property withTypeParameters(@Nullable JContainer<TypeParameter> typeParameters) {
16111632
return t.typeParameters == typeParameters ? t : new Property(t.id, t.prefix, t.markers, typeParameters,
1612-
t.variableDeclarations, t.typeConstraints, t.accessors, t.receiver);
1633+
t.paddedVariableDeclarations, t.typeConstraints, t.accessors, t.receiver);
16131634
}
16141635

16151636
@Nullable
@@ -1620,7 +1641,7 @@ public JRightPadded<Expression> getReceiver() {
16201641
@Nullable
16211642
public Property withReceiver(@Nullable JRightPadded<Expression> receiver) {
16221643
return t.receiver == receiver ? t : new Property(t.id, t.prefix, t.markers, t.typeParameters,
1623-
t.variableDeclarations, t.typeConstraints, t.accessors, receiver);
1644+
t.paddedVariableDeclarations, t.typeConstraints, t.accessors, receiver);
16241645
}
16251646
}
16261647
}

src/test/java/org/openrewrite/kotlin/tree/VariableDeclarationTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,20 @@ class Test {
520520
);
521521
}
522522

523+
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/560")
524+
@Test
525+
void accessorAfterTrailingSemiColon() {
526+
rewriteRun(
527+
kotlin(
528+
"""
529+
class Test {
530+
var n: Int = 0 ; protected set
531+
}
532+
"""
533+
)
534+
);
535+
}
536+
523537
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/135")
524538
@Test
525539
void checkNonNull() {

0 commit comments

Comments
 (0)