Skip to content

Commit

Permalink
Fix space issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Oct 31, 2023
1 parent 34417a7 commit 93a64c1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/openrewrite/kotlin/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static void customizeExecutionContext(ExecutionContext ctx) {
}

// A helper method to adjust white spaces in the input kotlin source to help us detect parse-to-print idempotent issues
// Just change from `before` to `adjustSpaces(before)` below to test locally
// Just change from `before` to `adjustSpaces(before)` below in the `kotlin()` method to test locally
@Nullable
private static String adjustSpaces(@Nullable String input) {
if (input == null) {
Expand Down Expand Up @@ -85,6 +85,7 @@ private static String adjustSpaces(@Nullable String input) {
}

public static SourceSpecs kotlin(@Language("kotlin") @Nullable String before) {
// Change `before` to `adjustSpaces(before)` to test spaces locally here
return kotlin(before, s -> {
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ public J visitFinallySection(KtFinallySection finallySection, ExecutionContext d
@Override
public J visitFunctionType(KtFunctionType type, ExecutionContext data) {
List<JRightPadded<TypeTree>> params;
Set<PsiElement> consumedSpaces = new HashSet<>();

if (type.getParameters().isEmpty()) {
params = singletonList(JRightPadded
.<TypeTree>build(new J.Empty(randomId(), prefix(requireNonNull(requireNonNull(type.getParameterList()).getNode().findChildByType(KtTokens.RPAR)).getPsi()), Markers.EMPTY))
Expand Down Expand Up @@ -601,11 +603,12 @@ public J visitFunctionType(KtFunctionType type, ExecutionContext data) {
JContainer<TypeTree> parameters = JContainer.build(prefix(type.getParameterList()), params, Markers.EMPTY);
if (type.getFirstChild() == type.getParameterList()) {
parameters = parameters.withBefore(prefix(type));
consumedSpaces.add(findFirstPrefixSpace(type));
}

return new K.FunctionType(
randomId(),
prefix(type),
prefix(type, consumedSpaces),
Markers.EMPTY, //.addIfAbsent(new IsNullable(randomId(), Space.EMPTY)), // TODO
emptyList(), // TODO
emptyList(), // TODO
Expand Down Expand Up @@ -814,7 +817,7 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) {

if (parameter.getTypeReference() != null) {
markers = markers.addIfAbsent(new TypeReferencePrefix(randomId(), prefix(parameter.getColon())));
typeExpression = parameter.getTypeReference().accept(this, data).withPrefix(prefix(parameter.getTypeReference()));
typeExpression = (TypeTree) parameter.getTypeReference().accept(this, data);
// TODO: get type from IR of KtProperty.
}

Expand Down Expand Up @@ -2739,7 +2742,6 @@ public J visitProperty(KtProperty property, ExecutionContext data) {
if (property.getColon() != null) {
markers = markers.addIfAbsent(new TypeReferencePrefix(randomId(), prefix(property.getColon())));
typeExpression = (TypeTree) requireNonNull(property.getTypeReference()).accept(this, data);
typeExpression = typeExpression.withPrefix(suffix(property.getColon()));
}

if (property.getGetter() != null) {
Expand Down Expand Up @@ -2931,31 +2933,37 @@ public J visitSuperTypeCallEntry(KtSuperTypeCallEntry call, ExecutionContext dat
public J visitTypeReference(KtTypeReference typeReference, ExecutionContext data) {
List<J.Annotation> leadingAnnotations = new ArrayList<>();
List<J.Annotation> lastAnnotations = new ArrayList<>();
boolean prefixConsumed = false;
Set<PsiElement> consumedSpaces = new HashSet<>();

mapModifiers(typeReference.getModifierList(), leadingAnnotations, lastAnnotations, data);
List<J.Modifier> modifiers = mapModifiers(typeReference.getModifierList(), leadingAnnotations, lastAnnotations, data);
if (!leadingAnnotations.isEmpty()) {
leadingAnnotations = ListUtils.mapFirst(leadingAnnotations, anno -> anno.withPrefix(prefix(typeReference)));
prefixConsumed = true;
consumedSpaces.add(findFirstPrefixSpace(typeReference));
} else if (!modifiers.isEmpty()) {
modifiers = ListUtils.mapFirst(modifiers, mod -> mod.withPrefix(prefix(typeReference)));
consumedSpaces.add(findFirstPrefixSpace(typeReference));
}

J j = requireNonNull(typeReference.getTypeElement()).accept(this, data)
.withPrefix(prefixConsumed ? prefix(typeReference.getTypeElement()) : merge(prefix(typeReference), prefix(typeReference.getTypeElement())));
J j = requireNonNull(typeReference.getTypeElement()).accept(this, data);
j = j.withPrefix(merge(j.getPrefix(), prefix(typeReference, consumedSpaces)));

consumedSpaces.add(findFirstPrefixSpace(typeReference.getTypeElement()));

if (j instanceof K.FunctionType &&
typeReference.getModifierList() != null &&
typeReference.getModifierList().hasModifier(KtTokens.SUSPEND_KEYWORD)) {
j = ((K.FunctionType) j).withModifiers(singletonList(mapModifier(typeReference.getModifierList().getModifier(KtTokens.SUSPEND_KEYWORD), emptyList())))
.withLeadingAnnotations(leadingAnnotations);
if (((K.FunctionType) j).getReceiver() != null) {
// TODO check if we can simplify this
j = ((K.FunctionType) j).withReceiver(
((K.FunctionType) j).getReceiver().withElement(
((K.FunctionType) j).getReceiver().getElement().withPrefix(
prefix(typeReference.getTypeElement())
)
)
K.FunctionType functionType = (K.FunctionType) j;
functionType = functionType.withModifiers(modifiers).withLeadingAnnotations(leadingAnnotations);

if (functionType.getReceiver() != null) {
functionType = functionType.withReceiver(
functionType.getReceiver().withElement(functionType.getReceiver().getElement().withPrefix(functionType.getPrefix()))
);

functionType = functionType.withPrefix(Space.EMPTY);
}

j = functionType;
} else if (j instanceof J.Identifier) {
j = ((J.Identifier) j).withAnnotations(leadingAnnotations);
}
Expand All @@ -2966,6 +2974,11 @@ public J visitTypeReference(KtTypeReference typeReference, ExecutionContext data
public J visitUserType(KtUserType type, ExecutionContext data) {
// FIXME: must be mapped through parent element. I.E. functionType.
J.Identifier name = (J.Identifier) requireNonNull(type.getReferenceExpression()).accept(this, data);

if (type.getFirstChild() == type.getReferenceExpression()) {
name = name.withPrefix(merge(prefix(type), name.getPrefix()));
}

NameTree nameTree = name;

if (type.getQualifier() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void trailingComma() {
rewriteRun(
kotlin(
"""
annotation class Test ( val values : Array < String > )
annotation class Test ( val values : Array < String > )
@Test( values = [ "a" , "b" , /* trailing comma */ ] )
val a = 42
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ void receiverWithModifier() {
kotlin(
"""
class SomeReceiver
suspend inline fun SomeReceiver.method(
crossinline body: suspend SomeReceiver.() -> Unit
suspend inline fun SomeReceiver . method(
crossinline body : suspend SomeReceiver . () -> Unit
) {}
"""
)
Expand Down

0 comments on commit 93a64c1

Please sign in to comment.