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 @@ -44,7 +44,13 @@ public Xml visitTag(Xml.Tag tag, P p) {
int indexOf = contents.indexOf(content);
contents.remove(indexOf);

if (removePrecedingComment && 0 < indexOf && contents.get(indexOf - 1) instanceof Xml.Comment) {
if (indexOf < contents.size() && content.getPrefix().contains("\n") &&
!contents.get(indexOf).getPrefix().contains("\n")) {
contents.set(indexOf, (Content) contents.get(indexOf).withPrefix(content.getPrefix()));
}

if (removePrecedingComment && 0 < indexOf && contents.get(indexOf - 1) instanceof Xml.Comment &&
!isTrailingComment(contents, indexOf - 1)) {
doAfterVisit(new RemoveContentVisitor<>(contents.get(indexOf - 1), true, removePrecedingComment));
}

Expand All @@ -63,4 +69,11 @@ public Xml visitTag(Xml.Tag tag, P p) {

return t;
}

/**
* A comment that shares a line with the sibling before it documents that sibling, not the one after it.
*/
private static boolean isTrailingComment(List<Content> contents, int index) {
return 0 < index && !contents.get(index).getPrefix().contains("\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import org.openrewrite.xml.XmlIsoVisitor;
import org.openrewrite.xml.tree.Xml;

import java.util.regex.Pattern;

public class RemoveTrailingWhitespaceVisitor<P> extends XmlIsoVisitor<P> {
private static final Pattern TRAILING_WHITESPACE = Pattern.compile("[ \\t]+(?=[\\r\\n]|$)");

@Nullable
private final Tree stopAfter;

Expand All @@ -34,10 +38,8 @@ public RemoveTrailingWhitespaceVisitor(@Nullable Tree stopAfter) {

@Override
public Xml.Document visitDocument(Xml.Document doc, P p) {
String eof = doc.getEof();
eof = eof.chars().filter(c -> c == '\n' || c == '\r')
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
// `eof` holds everything after the root element, comments and processing instructions included
String eof = TRAILING_WHITESPACE.matcher(doc.getEof()).replaceAll("");

Xml.Document d = super.visitDocument(doc, p);
return d.withEof(eof);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,84 @@ public Xml visitDocument(Xml.Document x, ExecutionContext ctx) {
)
);
}

@Test
void trailingCommentKeepsItsOwnLine() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new XmlVisitor<>() {
@Override
public Xml visitDocument(Xml.Document x, ExecutionContext ctx) {
doAfterVisit(new RemoveContentVisitor<>(requireNonNull(x.getRoot().getContent()).get(1), false, false));
return super.visitDocument(x, ctx);
}
}).withMaxCycles(1)),
xml(
"""
<dependency>
<groupId>group</groupId>
<version/><!-- why version -->
</dependency>
""",
"""
<dependency>
<groupId>group</groupId>
<!-- why version -->
</dependency>
"""
)
);
}

@Test
void precedingCommentOnTheLineOfAnEarlierSiblingIsRetained() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new XmlVisitor<>() {
@Override
public Xml visitDocument(Xml.Document x, ExecutionContext ctx) {
doAfterVisit(new RemoveContentVisitor<>(requireNonNull(x.getRoot().getContent()).get(2), false, true));
return super.visitDocument(x, ctx);
}
}).withMaxCycles(1)),
xml(
"""
<dependency>
<groupId>group</groupId> <!-- why group -->
<version/>
</dependency>
""",
"""
<dependency>
<groupId>group</groupId> <!-- why group -->
</dependency>
"""
)
);
}

@Test
void precedingCommentOnItsOwnLineIsRemoved() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new XmlVisitor<>() {
@Override
public Xml visitDocument(Xml.Document x, ExecutionContext ctx) {
doAfterVisit(new RemoveContentVisitor<>(requireNonNull(x.getRoot().getContent()).get(2), false, true));
return super.visitDocument(x, ctx);
}
}).withMaxCycles(1)),
xml(
"""
<dependency>
<groupId>group</groupId>
<!-- why version -->
<version/>
</dependency>
""",
"""
<dependency>
<groupId>group</groupId>
</dependency>
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,53 @@ void misindentedTrailingCommentIsStillIndented() {
)
);
}

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

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

@Test
void trailingWhitespaceAfterRootElementIsRemoved() {
rewriteRun(
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project>
<excludes/>
</project> \s
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project>
<excludes/>
</project>
"""
)
);
}
}