Skip to content

Commit

Permalink
Fix Semi-colon on inline methods after property declaration disappears (
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 authored Jan 9, 2024
1 parent 301e371 commit 10052ef
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/kotlin/KotlinVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public J visitProperty(K.Property property, P p) {
pr = (K.Property) temp;
}

pr = pr.withVariableDeclarations(visitAndCast(pr.getVariableDeclarations(), p));
pr = pr.getPadding().withVariableDeclarations(visitRightPadded(pr.getPadding().getVariableDeclarations(), p));
pr = pr.getPadding().withReceiver(visitRightPadded(pr.getPadding().getReceiver(), p));
pr = pr.withAccessors(visitContainer(pr.getAccessors(), p));
return pr;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/openrewrite/kotlin/format/SpacesVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,13 @@ public K.Property visitProperty(K.Property property, P p) {
if (prop.getPadding().getReceiver() != null) {
prop = prop.getPadding().withReceiver(prop.getPadding().getReceiver().withAfter(updateSpace(prop.getPadding().getReceiver().getAfter(), false)));
}

if (prop.getVariableDeclarations() != null && !prop.getVariableDeclarations().getVariables().isEmpty()) {
prop = prop.withVariableDeclarations(
prop.getVariableDeclarations().withVariables(
ListUtils.mapFirst(prop.getVariableDeclarations().getVariables(),
v -> spaceBefore(v, false))
)
);
List<J.VariableDeclarations.NamedVariable> variables = ListUtils.mapFirst(prop.getVariableDeclarations().getVariables(),
v -> spaceBefore(v, false));
JRightPadded<J.VariableDeclarations> rp = prop.getPadding().getVariableDeclarations();
rp = rp.withElement(rp.getElement().withVariables(variables));
prop = prop.getPadding().withVariableDeclarations(rp);
}
return prop;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ public J visitProperty(K.Property property, PrintOutputCapture<P> p) {
delegate.visitContainer("where", property.getTypeConstraints().getPadding().getConstraints(), JContainer.Location.TYPE_PARAMETERS, ",", "", p);
}

visitSpace(property.getPadding().getVariableDeclarations().getAfter(), Space.Location.VARIABLE_INITIALIZER, p);
if (property.getPadding().getVariableDeclarations().getMarkers().findFirst(Semicolon.class).isPresent()) {
p.append(";");
}

visitContainer(property.getAccessors(), p);
afterSyntax(property, p);
return property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2959,7 +2959,19 @@ public J visitProperty(KtProperty property, ExecutionContext data) {
if (!ktPropertyAccessors.isEmpty() || receiver != null || typeConstraints != null) {
List<JRightPadded<J.MethodDeclaration>> accessors = new ArrayList<>(ktPropertyAccessors.size());

for (KtPropertyAccessor ktPropertyAccessor : ktPropertyAccessors) {
Space beforeSemiColon = Space.EMPTY;
Markers rpMarkers = Markers.EMPTY;
for (int i = 0; i < ktPropertyAccessors.size(); i++) {
KtPropertyAccessor ktPropertyAccessor = ktPropertyAccessors.get(i);

if (i == 0) {
PsiElement maybeSemiColon = PsiTreeUtil.findSiblingBackward(ktPropertyAccessor, KtTokens.SEMICOLON, null);
if (maybeSemiColon != null) {
beforeSemiColon = prefix(maybeSemiColon);
rpMarkers = rpMarkers.addIfAbsent(new Semicolon(randomId()));
}
}

J.MethodDeclaration accessor = (J.MethodDeclaration) ktPropertyAccessor.accept(this, data);
accessors.add(maybeTrailingSemicolonInternal(accessor, ktPropertyAccessor));
}
Expand All @@ -2969,7 +2981,7 @@ public J visitProperty(KtProperty property, ExecutionContext data) {
deepPrefix(property),
markers,
typeParameters,
variableDeclarations.withPrefix(Space.EMPTY),
padRight(variableDeclarations.withPrefix(Space.EMPTY), beforeSemiColon, rpMarkers),
typeConstraints,
JContainer.build(accessors),
receiver
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/org/openrewrite/kotlin/tree/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ public List<TypeParameter> getTypeParameters() {
return typeParameters == null ? null : typeParameters.getElements();
}

J.VariableDeclarations variableDeclarations;
JRightPadded<J.VariableDeclarations> paddedVariableDeclarations;

@Nullable
TypeConstraints typeConstraints;
Expand All @@ -1523,7 +1523,8 @@ public Property(UUID id,
Space prefix,
Markers markers,
JContainer<TypeParameter> typeParameters,
VariableDeclarations variableDeclarations,
@Nullable JRightPadded<J.VariableDeclarations> paddedVariableDeclarations,
@Nullable VariableDeclarations variableDeclarations,
@Nullable K.TypeConstraints typeConstraints,
@Nullable @JsonProperty("getter") J.MethodDeclaration getter,
@Nullable @JsonProperty("setter") J.MethodDeclaration setter,
Expand All @@ -1535,7 +1536,14 @@ public Property(UUID id,
this.prefix = prefix;
this.markers = markers;
this.typeParameters = typeParameters;
this.variableDeclarations = variableDeclarations;

if (variableDeclarations != null) {
// from old LST
this.paddedVariableDeclarations = new JRightPadded<>(variableDeclarations, Space.EMPTY, Markers.EMPTY);
} else {
this.paddedVariableDeclarations = requireNonNull(paddedVariableDeclarations);
}

this.typeConstraints = typeConstraints;

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

public J.VariableDeclarations getVariableDeclarations() {
return paddedVariableDeclarations.getElement();
}

@Nullable
public Expression getReceiver() {
return receiver == null ? null : receiver.getElement();
Expand Down Expand Up @@ -1602,14 +1614,23 @@ public String toString() {
public static class Padding {
private final Property t;

public JRightPadded<J.VariableDeclarations> getVariableDeclarations() {
return t.paddedVariableDeclarations;
}

public Property withVariableDeclarations(JRightPadded<J.VariableDeclarations> variableDeclarations) {
return t.paddedVariableDeclarations == variableDeclarations ? t : new Property(t.id, t.prefix, t.markers, t.typeParameters,
variableDeclarations, t.typeConstraints, t.accessors, t.receiver);
}

@Nullable
public JContainer<TypeParameter> getTypeParameters() {
return t.typeParameters;
}

public Property withTypeParameters(@Nullable JContainer<TypeParameter> typeParameters) {
return t.typeParameters == typeParameters ? t : new Property(t.id, t.prefix, t.markers, typeParameters,
t.variableDeclarations, t.typeConstraints, t.accessors, t.receiver);
t.paddedVariableDeclarations, t.typeConstraints, t.accessors, t.receiver);
}

@Nullable
Expand All @@ -1620,7 +1641,7 @@ public JRightPadded<Expression> getReceiver() {
@Nullable
public Property withReceiver(@Nullable JRightPadded<Expression> receiver) {
return t.receiver == receiver ? t : new Property(t.id, t.prefix, t.markers, t.typeParameters,
t.variableDeclarations, t.typeConstraints, t.accessors, receiver);
t.paddedVariableDeclarations, t.typeConstraints, t.accessors, receiver);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,20 @@ class Test {
);
}

@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/560")
@Test
void accessorAfterTrailingSemiColon() {
rewriteRun(
kotlin(
"""
class Test {
var n: Int = 0 ; protected set
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/135")
@Test
void checkNonNull() {
Expand Down

0 comments on commit 10052ef

Please sign in to comment.