Skip to content

Commit

Permalink
Remove preceding comments by default, except in AddProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Oct 6, 2024
1 parent 2e313f0 commit d3c231d
Show file tree
Hide file tree
Showing 15 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (maybeProfile.isPresent()) {
Xml.Tag profile = maybeProfile.get();

t = (Xml.Tag) new RemoveContentVisitor(profile, false).visitNonNull(t, ctx, getCursor().getParentOrThrow());
t = (Xml.Tag) new RemoveContentVisitor(profile, false, false).visitNonNull(t, ctx, getCursor().getParentOrThrow());

}
Xml.Tag profileTag = Xml.Tag.build("<profile>\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
repositories = (Xml.Tag) new AddToTagVisitor<>(repo, Xml.Tag.build(assembleReleases()))
.visitNonNull(repositories, ctx, getCursor().getParentOrThrow());
} else {
repositories = (Xml.Tag) new RemoveContentVisitor<>(releases, true)
repositories = (Xml.Tag) new RemoveContentVisitor<>(releases, true, true)
.visitNonNull(repositories, ctx, getCursor().getParentOrThrow());
if (!isNoSnapshots()) {
repositories = (Xml.Tag) new AddToTagVisitor<>(repo, Xml.Tag.build(assembleReleases()))
Expand All @@ -184,7 +184,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (snapshots == null) {
repositories = (Xml.Tag) new AddToTagVisitor<>(repo, Xml.Tag.build(assembleSnapshots())).visitNonNull(repositories, ctx, getCursor().getParentOrThrow());
} else {
repositories = (Xml.Tag) new RemoveContentVisitor<>(snapshots, true).visitNonNull(repositories, ctx, getCursor().getParentOrThrow());
repositories = (Xml.Tag) new RemoveContentVisitor<>(snapshots, true, true).visitNonNull(repositories, ctx, getCursor().getParentOrThrow());
if (!isNoSnapshots()) {
repositories = (Xml.Tag) new AddToTagVisitor<>(repo, Xml.Tag.build(assembleSnapshots())).visitNonNull(repositories, ctx, getCursor().getParentOrThrow());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
Optional<Xml.Tag> classifier = tag.getChild("classifier");
if (classifier.isPresent()) {
if (newClassifier == null) {
doAfterVisit(new RemoveContentVisitor<>(classifier.get(), false));
doAfterVisit(new RemoveContentVisitor<>(classifier.get(), false, true));
} else if (!newClassifier.equals(classifier.get().getValue().orElse(null))) {
doAfterVisit(new ChangeTagValueVisitor<>(classifier.get(), newClassifier));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (versionTagPresent) {
// If the previous dependency had a version but the new artifact is managed, removed the version tag.
if (!configuredToOverrideManageVersion && newDependencyManaged || (oldDependencyManaged && configuredToChangeManagedDependency)) {
t = (Xml.Tag) new RemoveContentVisitor<>(versionTag.get(), false).visit(t, ctx);
t = (Xml.Tag) new RemoveContentVisitor<>(versionTag.get(), false, true).visit(t, ctx);
} else {
// Otherwise, change the version to the new value.
t = changeChildTagValue(t, "version", resolvedNewVersion, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
Optional<Xml.Tag> scope = tag.getChild("scope");
if (scope.isPresent()) {
if (newScope == null) {
doAfterVisit(new RemoveContentVisitor<>(scope.get(), false));
doAfterVisit(new RemoveContentVisitor<>(scope.get(), false, true));
} else if (!newScope.equals(scope.get().getValue().orElse(null))) {
doAfterVisit(new ChangeTagValueVisitor<>(scope.get(), newScope));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public RemoveVersionTagVisitor(String groupPattern, String artifactPattern) {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (isDependencyTag() && isDependencyTag(groupPattern, artifactPattern)) {
tag.getChild("version").ifPresent(versionTag -> doAfterVisit(new RemoveContentVisitor<>(versionTag, false)));
tag.getChild("version").ifPresent(versionTag -> doAfterVisit(new RemoveContentVisitor<>(versionTag, false, true)));
return tag;
}
return super.visitTag(tag, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Scope checkScope = scope != null ? Scope.fromName(scope) : null;
ResolvedDependency dependency = findDependency(tag, checkScope);
if (dependency != null) {
doAfterVisit(new RemoveContentVisitor<>(tag, true));
doAfterVisit(new RemoveContentVisitor<>(tag, true, true));
maybeUpdateModel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Scope checkScope = scope != null ? Scope.fromName(scope) : null;
boolean isBomImport = tag.getChildValue("scope").map("import"::equalsIgnoreCase).orElse(false);
if (isBomImport || findManagedDependency(tag, checkScope) != null) {
doAfterVisit(new RemoveContentVisitor<>(tag, true));
doAfterVisit(new RemoveContentVisitor<>(tag, true, true));
maybeUpdateModel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
for (Xml.Tag plugin : FindPlugin.find(document, groupId, artifactId)) {
doAfterVisit(new RemoveContentVisitor<>(plugin, true));
doAfterVisit(new RemoveContentVisitor<>(plugin, true, true));
}
return super.visitDocument(document, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (isManagedDependencyTag() && tag.getChildValue("version").orElse(null) == null) {
doAfterVisit(new RemoveContentVisitor<>(tag, true));
doAfterVisit(new RemoveContentVisitor<>(tag, true, true));
}
return super.visitTag(tag, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void preExistingMatchingProfile() {
<artifactId>artifact</artifactId>
<version>1</version>
<profiles>
<!-- retained comment -->
<profile>
<id>myprofile</id>
<activation>
Expand All @@ -146,6 +147,7 @@ void preExistingMatchingProfile() {
<artifactId>artifact</artifactId>
<version>1</version>
<profiles>
<!-- retained comment -->
<profile>
<id>myprofile</id>
<activation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public class RemoveContentVisitor<P> extends XmlVisitor<P> {
private final boolean removeEmptyAncestors;
private final boolean removePrecedingComment;

@Deprecated
public RemoveContentVisitor(Content tag, boolean removeEmptyAncestors) {
this(tag, removeEmptyAncestors, false);
}

public RemoveContentVisitor(Content tag, boolean removeEmptyAncestors, boolean removePrecedingComment) {
this.scope = tag;
this.removeEmptyAncestors = removeEmptyAncestors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag t = super.visitTag(tag, ctx);
//noinspection ConstantValue
if (t != null && (t.getContent() == null || t.getContent().isEmpty()) && t.getAttributes().isEmpty()) {
doAfterVisit(new RemoveContentVisitor<>(t, true));
doAfterVisit(new RemoveContentVisitor<>(t, true, true));
}
return t;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (xPathMatcher.matches(getCursor())) {
doAfterVisit(new RemoveContentVisitor<>(tag, true));
doAfterVisit(new RemoveContentVisitor<>(tag, true, true));
}
return super.visitTag(tag, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void removeContent() {
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));
doAfterVisit(new RemoveContentVisitor<>(requireNonNull(x.getRoot().getContent()).get(1), false, true));
return super.visitDocument(x, ctx);
}
}).withMaxCycles(1)),
Expand All @@ -61,7 +61,7 @@ void removeAncestorsThatBecomeEmpty() {
@Override
public Xml visitDocument(Xml.Document x, ExecutionContext ctx) {
doAfterVisit(new RemoveContentVisitor<>(requireNonNull(x.getRoot().getChildren()).get(1)
.getChildren().get(0).getChildren().get(0), true));
.getChildren().get(0).getChildren().get(0), true, true));
return super.visitDocument(x, ctx);
}
}).withMaxCycles(1)),
Expand Down Expand Up @@ -92,7 +92,7 @@ void rootChangedToEmptyTagIfLastRemainingTag() {
@Override
public Xml visitDocument(Xml.Document x, ExecutionContext ctx) {
doAfterVisit(new RemoveContentVisitor<>(requireNonNull(x.getRoot().getChildren()).get(0)
.getChildren().get(0).getChildren().get(0), true));
.getChildren().get(0).getChildren().get(0), true, true));
return super.visitDocument(x, ctx);
}
}).withMaxCycles(1)),
Expand Down

0 comments on commit d3c231d

Please sign in to comment.