Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.Tree;
import org.openrewrite.xml.XmlIsoVisitor;
import org.openrewrite.xml.tree.Content;
import org.openrewrite.xml.tree.Misc;
import org.openrewrite.xml.tree.Xml;

import java.util.List;

public class LineBreaksVisitor<P> extends XmlIsoVisitor<P> {
@Nullable
private final Tree stopAfter;
Expand All @@ -35,10 +38,25 @@ public LineBreaksVisitor(@Nullable Tree stopAfter) {

@Override
public Xml.Comment visitComment(Xml.Comment comment, P p) {
if (isTrailingComment(comment)) {
return super.visitComment(comment, p);
}
return keepMaximumLines(minimumLines(super.visitComment(comment, p),
isFirstMisc(comment) ? 0 : 1), 2);
}

private boolean isTrailingComment(Xml.Comment comment) {
if (comment.getPrefix().contains("\n")) {
return false;
}
Object parent = getCursor().getParentTreeCursor().getValue();
if (!(parent instanceof Xml.Tag)) {
return false;
}
List<? extends Content> content = ((Xml.Tag) parent).getContent();
return content != null && !content.isEmpty() && content.get(0) != comment;
}

@Override
public Xml.DocTypeDecl visitDocTypeDecl(Xml.DocTypeDecl docTypeDecl, P p) {
return keepMaximumLines(minimumLines(super.visitDocTypeDecl(docTypeDecl, p),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,63 @@ void tagContentIndentation() {
)
);
}

@Test
void trailingCommentStaysOnSameLine() {
rewriteRun(
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project>
<excludes>
<exclude>com.example.profit.ProfitFactory.processProfit.1</exclude> <!--tmp logic, tested elsewhere-->
<exclude>com.example.profit.io.ProfitUtils</exclude><!--tmp logic, tested elsewhere-->
</excludes>
</project>
"""
)
);
}

@Test
void trailingCommentAfterTagContentStaysOnSameLine() {
rewriteRun(
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project>
<excludes>
<exclude>a</exclude> <!--why a-->
</excludes> <!--why excludes-->
</project>
"""
)
);
}

@Test
void misindentedTrailingCommentIsStillIndented() {
rewriteRun(
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project>
<excludes>
<exclude>a</exclude>
<!--why a-->
</excludes>
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project>
<excludes>
<exclude>a</exclude>
<!--why a-->
</excludes>
</project>
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ void comments() {
);
}

@Test
void trailingCommentsStayOnSameLine() {
rewriteRun(
xml(
"""
<project>
<excludes>
<exclude>a</exclude> <!--comment-->
<exclude>b</exclude><!--comment-->
</excludes>
</project>
"""
)
);
}

@SuppressWarnings("CheckTagEmptyBody")
@Test
void docTypeDecl() {
Expand Down