Skip to content

Commit

Permalink
Add MarkerMode to select a MarkerPrinter
Browse files Browse the repository at this point in the history
Instead of having to redefine MarkerPrinter everywhere we have a few standard `MarkerPrinter`s that can be used. There is a new diff method that uses the enum to select the right marker printer.
  • Loading branch information
pstreef committed Dec 19, 2024
1 parent 65c9cf1 commit 7fde385
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
44 changes: 44 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package org.openrewrite;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.Nullable;
import org.openrewrite.marker.Marker;
import org.openrewrite.marker.Markup;
import org.openrewrite.marker.SearchResult;

import java.util.function.UnaryOperator;

Expand Down Expand Up @@ -67,6 +71,22 @@ public PrintOutputCapture<P> clone() {
}

public interface MarkerPrinter {

@Incubating(since = "8.41.4")
@RequiredArgsConstructor
enum MarkerMode {
NONE(MarkerPrinter.NONE),
DEFAULT(MarkerPrinter.DEFAULT),
VERBOSE(MarkerPrinter.VERBOSE),
FENCED(MarkerPrinter.FENCED),
SEARCH_ONLY(MarkerPrinter.SEARCH_ONLY);
@Getter
private final MarkerPrinter printer;
}

MarkerPrinter NONE = new MarkerPrinter() {
};

MarkerPrinter DEFAULT = new MarkerPrinter() {
@Override
public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
Expand All @@ -81,6 +101,30 @@ public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator<String> c
}
};

MarkerPrinter FENCED = new MarkerPrinter() {
@Override
public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
return marker instanceof SearchResult || marker instanceof Markup ? "{{" + marker.getId() + "}}" : "";
}

@Override
public String afterSyntax(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
return marker instanceof SearchResult || marker instanceof Markup ? "{{" + marker.getId() + "}}" : "";
}
};

MarkerPrinter SEARCH_ONLY = new MarkerPrinter() {
@Override
public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
return marker instanceof SearchResult ? marker.print(cursor, commentWrapper, false) : "";
}

@Override
public String afterSyntax(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
return marker instanceof SearchResult ? marker.print(cursor, commentWrapper, false) : "";
}
};

default String beforePrefix(Marker marker, Cursor cursor, UnaryOperator<String> commentWrapper) {
return "";
}
Expand Down
10 changes: 10 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public String diff(@Nullable Path relativeTo, PrintOutputCapture.@Nullable Marke
return diff(relativeTo, markerPrinter, false);
}

@Incubating(since = "8.41.4")
public String diff(@Nullable Path relativeTo, PrintOutputCapture.MarkerPrinter.MarkerMode markerMode) {
return diff(relativeTo, markerMode, false);
}

@Incubating(since = "8.41.4")
public String diff(@Nullable Path relativeTo, PrintOutputCapture.MarkerPrinter.MarkerMode markerMode, boolean ignoreAllWhitespace) {
return diff(relativeTo, markerMode.getPrinter(), ignoreAllWhitespace);
}

@Incubating(since = "7.34.0")
public String diff(@Nullable Path relativeTo, PrintOutputCapture.@Nullable MarkerPrinter markerPrinter, @Nullable Boolean ignoreAllWhitespace) {
Path beforePath = before == null ? null : before.getSourcePath();
Expand Down

0 comments on commit 7fde385

Please sign in to comment.