Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 7, 2023
1 parent b3275c5 commit 492a94e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/java/org/openrewrite/kotlin/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openrewrite.marker.Markers;
import org.openrewrite.test.SourceSpec;
import org.openrewrite.test.SourceSpecs;
import org.openrewrite.test.UncheckedConsumer;
import org.opentest4j.AssertionFailedError;

import java.util.Collections;
Expand Down Expand Up @@ -181,14 +182,20 @@ public Space visitSpace(Space space, Space.Location loc, Integer integer) {
};
}

public static UncheckedConsumer<SourceSpec<?>> spaceConscious() {
return source -> {
if (source.getSourceFileType() == K.CompilationUnit.class) {
SourceSpec<K.CompilationUnit> kotlinSourceSpec = (SourceSpec<K.CompilationUnit>) source;
kotlinSourceSpec.afterRecipe(spaceConscious(kotlinSourceSpec));
}
};
}

public static ThrowingConsumer<K.CompilationUnit> spaceConscious(SourceSpec<K.CompilationUnit> spec) {
return cu -> {
K.CompilationUnit visited = (K.CompilationUnit) new KotlinIsoVisitor<Integer>() {
final int max = 20;
final String buf = "\n ";
int id = 0;


@Override
public Space visitSpace(Space space, KSpace.Location loc, Integer integer) {
return next(space);
Expand All @@ -200,9 +207,6 @@ private Space next(Space space) {
return space;
}
return space.withComments(Collections.singletonList(new TextComment(true, Integer.toString(id++), "", Markers.EMPTY)));
// boolean nl = space.getWhitespace().indexOf('\n') >= 0;
// String ws = buf.substring(nl ? 0 : 1, (id++ % max) + 2);
// return space.withWhitespace(ws);
}

@Override
Expand All @@ -223,7 +227,6 @@ public Space visitSpace(Space space, Space.Location loc, Integer integer) {
} else if (getCursor().firstEnclosing(J.Package.class) != null) {
return space;
}
// return space.withComments(Collections.singletonList(new TextComment(true, Integer.toString(id++), "", Markers.EMPTY)));
return next(space);
}
}.visit(cu, 0);
Expand All @@ -233,7 +236,7 @@ public Space visitSpace(Space space, Space.Location loc, Integer integer) {
ctx.putMessage(ExecutionContext.REQUIRE_PRINT_EQUALS_INPUT, false);
SourceFile cu2 = spec.getParser().build().parse(ctx, s).findFirst().get();
String s1 = cu2.printAll();
assertEquals(s, s1, "Parser is not space print idempotent");
assertEquals(s, s1, "Parser is not whitespace print idempotent");
} catch (Exception e) {
fail(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

class AnonymousFunctionTest implements RewriteTest {

// @Override
// public void defaults(RecipeSpec spec) {
// spec
// .recipe(Recipe.noop())
// .allSources(spaceConscious());
// }

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

0 comments on commit 492a94e

Please sign in to comment.