Skip to content

Commit

Permalink
Add SearchResults.mergingFound (openrewrite#3196)
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Leitschuh <[email protected]>
  • Loading branch information
JLLeitschuh authored May 10, 2023
1 parent 1ea8648 commit a7e98d8
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import lombok.*;
import lombok.experimental.FieldDefaults;
import org.openrewrite.Cursor;
import org.openrewrite.Incubating;
import org.openrewrite.Tree;
import org.openrewrite.internal.lang.Nullable;

import java.util.Objects;
import java.util.UUID;
import java.util.function.UnaryOperator;

Expand Down Expand Up @@ -51,6 +53,43 @@ public static <T extends Tree> T found(@Nullable T t, @Nullable String descripti
(s1, s2) -> s1 == null ? s2 : s1));
}

/**
* Merge the description of two search results into a single search result with a unified description.
*/
@Incubating(since ="8.0.0")
public static <T extends Tree> T mergingFound(@Nullable T t,String description) {
return mergingFound(t, description, ", ");
}

/**
* Merge the description of two search results into a single search result with a unified description.
* @param delimiter The delimiter to use when merging descriptions.
*/
@Incubating(since ="8.0.0")
public static <T extends Tree> T mergingFound(@Nullable T t, String description, String delimiter) {
Objects.requireNonNull(delimiter, "delimiter must not be null");
if (t == null) {
//noinspection ConstantConditions
return null;
}
return t.withMarkers(t.getMarkers().computeByType(new SearchResult(randomId(), description),
(s1, s2) -> {
if (s1 == null) {
return s2;
}
if (s2 == null) {
return s1;
}
if (s1.getDescription() == null) {
return s2;
}
if (s2.getDescription() == null) {
return s1;
}
return s1.withDescription(s1.getDescription() + delimiter + s2.getDescription());
}));
}

/**
* @param cursor The cursor at the point where the marker is being visited.
* @param commentWrapper A function that wraps arbitrary text in a multi-line comment that is language-specific.
Expand Down

0 comments on commit a7e98d8

Please sign in to comment.