Skip to content

Commit

Permalink
Ensure SourceSpec text blcks start on a new line (#3735)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Nov 28, 2023
1 parent a02bfed commit 8d45a33
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import static org.openrewrite.java.Assertions.java;

public class SourceSpecTextBlockIndentationTest implements RewriteTest {
class SourceSpecTextBlockIndentationTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
Expand Down Expand Up @@ -83,4 +83,56 @@ void test() {
)
);
}

@Test
void startsOnNewline() {
rewriteRun(
java(
"""
import org.openrewrite.test.RewriteTest;
import static org.openrewrite.test.SourceSpecs.text;
class MyRecipeTest implements RewriteTest {
void test() {
rewriteRun(
text(\"""
class Test {
\s
\s
void test() {
System.out.println("Hello, world!");
}
}
\"""
)
);
}
}
""",

"""
import org.openrewrite.test.RewriteTest;
import static org.openrewrite.test.SourceSpecs.text;
class MyRecipeTest implements RewriteTest {
void test() {
rewriteRun(
text(
\"""
class Test {
\s
\s
void test() {
System.out.println("Hello, world!");
}
}
\"""
)
);
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeUtils;

import java.util.Arrays;
Expand Down Expand Up @@ -77,7 +78,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
nonSpaceCharacter[0] &&
nonSpaceCharacter[indentations.length - 2] &&
indentations[0] == indentations[indentations.length - 2] &&
indentations[0] > expectedIndent) {
indentations[0] >= expectedIndent) {

for (int i = 0; i < indentations.length - 1; i++) {
if (nonSpaceCharacter[i] && indentations[i] < indentations[0]) {
Expand All @@ -91,14 +92,19 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
StringJoiner fixedSource = new StringJoiner("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
if(i == 0 || i == lines.length - 1 || indentations[i - 1] < expectedIndent) {
if (i == 0 || i == lines.length - 1 || indentations[i - 1] < expectedIndent) {
fixedSource.add(line);
} else {
fixedSource.add(line.substring(marginTrim));
}
}

return source.withValueSource(fixedSource.toString());
J.Literal withFixedSource = source.withValueSource(fixedSource.toString());
if (withFixedSource.getPrefix().getComments().isEmpty()
&& withFixedSource.getPrefix().getWhitespace().isEmpty()) {
return maybeAutoFormat(withFixedSource, withFixedSource.withPrefix(Space.format("\n")), ctx);
}
return withFixedSource;
}

}
Expand Down

0 comments on commit 8d45a33

Please sign in to comment.