Skip to content

Commit

Permalink
Fix trailing comment of K.FunctionType
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Nov 10, 2023
1 parent f4d68b6 commit 346ad78
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 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 @@ -159,7 +159,7 @@ public J visitFunctionType(K.FunctionType functionType, P p) {
if (f.getPadding().getParameters() != null) {
f = f.getPadding().withParameters(this.visitContainer(f.getPadding().getParameters(), KContainer.Location.FUNCTION_TYPE_PARAMETERS, p));
}
f = f.withReturnType(visitAndCast(f.getReturnType(), p));
f = f.withReturnType(visitRightPadded(f.getReturnType(), p));
return f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,9 @@ public K.FunctionType visitFunctionType(K.FunctionType functionType, P p) {
if (kf.getArrow() != null) {
kf = kf.withArrow(updateSpace(kf.getArrow(), style.getOther().getAroundArrowInFunctionTypes()));
}
kf = kf.withReturnType(spaceBefore(kf.getReturnType(), style.getOther().getAroundArrowInFunctionTypes()));

JRightPadded<TypedTree> rpTypedTree = kf.getReturnType();
kf = kf.withReturnType(rpTypedTree.withElement(spaceBefore(rpTypedTree.getElement(), style.getOther().getAroundArrowInFunctionTypes())));
return kf;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ public J visitFunctionType(K.FunctionType functionType, PrintOutputCapture<P> p)
delegate.visitContainer("(", functionType.getPadding().getParameters(), JContainer.Location.TYPE_PARAMETERS, ",", ")", p);
visitSpace(functionType.getArrow() != null ? functionType.getArrow() : Space.SINGLE_SPACE, KSpace.Location.FUNCTION_TYPE_ARROW_PREFIX, p);
p.append("->");
visit(functionType.getReturnType(), p);

visitRightPadded(functionType.getReturnType(), p);
if (nullable) {
p.append(")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,13 @@ public J visitFunctionType(KtFunctionType type, ExecutionContext data) {
return new K.FunctionType(
randomId(),
prefix(type, consumedSpaces),
Markers.EMPTY, //.addIfAbsent(new IsNullable(randomId(), Space.EMPTY)), // TODO
Markers.EMPTY,
emptyList(), // TODO
emptyList(), // TODO
type.getReceiver() != null ? padRight((NameTree) requireNonNull(type.getReceiverTypeReference()).accept(this, data), suffix(type.getReceiver())) : null,
parameters,
suffix(type.getParameterList()),
requireNonNull(type.getReturnTypeReference()).accept(this, data).withPrefix(prefix(type.getReturnTypeReference()))
padRight((TypedTree) requireNonNull(type.getReturnTypeReference()).accept(this, data), suffix(type))
);
}

Expand Down Expand Up @@ -777,9 +777,9 @@ public J visitNamedDeclaration(KtNamedDeclaration declaration, ExecutionContext

@Override
public J visitNullableType(KtNullableType nullableType, ExecutionContext data) {
J j = requireNonNull(nullableType.getInnerType()).accept(this, data);
return j.withPrefix(deepPrefix(nullableType))
.withMarkers(j.getMarkers().addIfAbsent(new IsNullable(randomId(),
TypeTree typeTree = (TypeTree) requireNonNull(nullableType.getInnerType()).accept(this, data);
return typeTree.withPrefix(deepPrefix(nullableType))
.withMarkers(typeTree.getMarkers().addIfAbsent(new IsNullable(randomId(),
prefix((PsiElement) nullableType.getQuestionMarkNode())
)));
}
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/openrewrite/kotlin/tree/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ class FunctionType implements K, TypeTree, Expression {

public FunctionType(UUID id, Space prefix, Markers markers, List<Annotation> leadingAnnotations,
List<Modifier> modifiers, @Nullable JRightPadded<NameTree> receiver,
@Nullable JContainer<TypeTree> parameters, @Nullable Space arrow, TypedTree returnType) {
@Nullable JContainer<TypeTree> parameters, @Nullable Space arrow, JRightPadded<TypedTree> returnType) {
this.id = id;
this.prefix = prefix;
this.markers = markers;
Expand All @@ -1036,9 +1036,8 @@ public FunctionType(UUID id, Space prefix, Markers markers, List<Annotation> lea
}

public Space getPrefix() {
// For backwards compatibility with older LST before there was a prefix field
//noinspection ConstantConditions
return prefix == null ? returnType.getPrefix() : prefix;
return prefix == null ? returnType.getElement().getPrefix() : prefix;
}

@With
Expand Down Expand Up @@ -1089,21 +1088,19 @@ public FunctionType withParameters(List<TypeTree> parameters) {
@Getter
Space arrow;

// backwards compatibility
@JsonAlias("typedTree")
@With
@Getter
TypedTree returnType;
JRightPadded<TypedTree> returnType;

@Override
public @Nullable JavaType getType() {
return returnType.getType();
return returnType.getElement().getType();
}

public <T extends J> T withType(@Nullable JavaType type) {
TypeTree newType = returnType.withType(type);
TypeTree newType = returnType.getElement().withType(type);
//noinspection unchecked
return (T) (newType == type ? this : withReturnType(newType));
return (T) (newType == type ? this : withReturnType(returnType.withElement(newType)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,7 @@ class KotlinParserVisitor(
receiver,
JContainer.build(before, refParams as List<JRightPadded<TypeTree>>, Markers.EMPTY),
arrow,
returnType
padRight(returnType, Space.EMPTY)
)
}

Expand Down
14 changes: 13 additions & 1 deletion src/test/java/org/openrewrite/kotlin/tree/FunctionTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,19 @@ void parenthesizedNullableType() {
rewriteRun(
kotlin(
"""
val v: ( ( Any ) -> Any) /*c1*/ ? = null
val v: ( ( Int ) -> Any) /*c1*/ ? = null
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/365")
void parenthesizedNullableTypeWithTrailingComment() {
rewriteRun(
kotlin(
"""
val v: ( ( Int ) -> Any /*C*/) ? = null
"""
)
);
Expand Down

0 comments on commit 346ad78

Please sign in to comment.