From 7fde3851e31c94b6b73d039f7d62a51c81c5e459 Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Thu, 19 Dec 2024 09:10:55 +0100 Subject: [PATCH 1/3] Add `MarkerMode` to select a `MarkerPrinter` 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. --- .../org/openrewrite/PrintOutputCapture.java | 44 +++++++++++++++++++ .../src/main/java/org/openrewrite/Result.java | 10 +++++ 2 files changed, 54 insertions(+) diff --git a/rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java b/rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java index fc69045e4e1..bd609351654 100644 --- a/rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java +++ b/rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java @@ -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; @@ -67,6 +71,22 @@ public PrintOutputCapture

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 commentWrapper) { @@ -81,6 +101,30 @@ public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator c } }; + MarkerPrinter FENCED = new MarkerPrinter() { + @Override + public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator commentWrapper) { + return marker instanceof SearchResult || marker instanceof Markup ? "{{" + marker.getId() + "}}" : ""; + } + + @Override + public String afterSyntax(Marker marker, Cursor cursor, UnaryOperator commentWrapper) { + return marker instanceof SearchResult || marker instanceof Markup ? "{{" + marker.getId() + "}}" : ""; + } + }; + + MarkerPrinter SEARCH_ONLY = new MarkerPrinter() { + @Override + public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator commentWrapper) { + return marker instanceof SearchResult ? marker.print(cursor, commentWrapper, false) : ""; + } + + @Override + public String afterSyntax(Marker marker, Cursor cursor, UnaryOperator commentWrapper) { + return marker instanceof SearchResult ? marker.print(cursor, commentWrapper, false) : ""; + } + }; + default String beforePrefix(Marker marker, Cursor cursor, UnaryOperator commentWrapper) { return ""; } diff --git a/rewrite-core/src/main/java/org/openrewrite/Result.java b/rewrite-core/src/main/java/org/openrewrite/Result.java index ecd0b458807..d284f37c2a9 100755 --- a/rewrite-core/src/main/java/org/openrewrite/Result.java +++ b/rewrite-core/src/main/java/org/openrewrite/Result.java @@ -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(); From 66dc881a0d7e45992dfc0b4ef06bd492c77f0cda Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Thu, 19 Dec 2024 09:49:22 +0100 Subject: [PATCH 2/3] Update names and add comments --- .../org/openrewrite/PrintOutputCapture.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java b/rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java index bd609351654..3e2d5da5041 100644 --- a/rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java +++ b/rewrite-core/src/main/java/org/openrewrite/PrintOutputCapture.java @@ -75,11 +75,31 @@ public interface MarkerPrinter { @Incubating(since = "8.41.4") @RequiredArgsConstructor enum MarkerMode { + /** + * Does not print any markers. + */ NONE(MarkerPrinter.NONE), + /** + * Prints a squiggly line arrow in front of the marked element wrapped in a comment. + * /*~~>* /Thing thing + */ DEFAULT(MarkerPrinter.DEFAULT), + /** + * Prints a squiggly line arrow in front of the marked element wrapped in a comment. + * Possibly adding verbose information, depending on the marker + * /*~~>(this is some verbose information)* /Thing thing + */ VERBOSE(MarkerPrinter.VERBOSE), - FENCED(MarkerPrinter.FENCED), - SEARCH_ONLY(MarkerPrinter.SEARCH_ONLY); + /** + * Prints a squiggly arrow in front and after the marked element. It only includes {@link SearchResult} + * /*~~>* /Thing thing /**~~>* / + */ + SEARCH_ONLY(MarkerPrinter.SEARCH_ONLY), + /** + * Prints a fenced marker ID in front and after the marked element. It only includes {@link Markup} and {@link SearchResult} + * /*{{3e2a36bb-7c16-4b03-bdde-bffda08838e7}}* /Thing thing /*{{3e2a36bb-7c16-4b03-bdde-bffda08838e7}}* / + */ + FENCED_MARKUP_AND_SEARCH(MarkerPrinter.FENCED_MARKUP_AND_SEARCH); @Getter private final MarkerPrinter printer; } @@ -101,7 +121,7 @@ public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator c } }; - MarkerPrinter FENCED = new MarkerPrinter() { + MarkerPrinter FENCED_MARKUP_AND_SEARCH = new MarkerPrinter() { @Override public String beforeSyntax(Marker marker, Cursor cursor, UnaryOperator commentWrapper) { return marker instanceof SearchResult || marker instanceof Markup ? "{{" + marker.getId() + "}}" : ""; From 7c16d38dc04b53b88ad86b5d7eb0bc52e8856d40 Mon Sep 17 00:00:00 2001 From: Peter Streef Date: Thu, 19 Dec 2024 10:14:28 +0100 Subject: [PATCH 3/3] fix compilation --- rewrite-core/src/main/java/org/openrewrite/Result.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/Result.java b/rewrite-core/src/main/java/org/openrewrite/Result.java index d284f37c2a9..8d5c764e3a8 100755 --- a/rewrite-core/src/main/java/org/openrewrite/Result.java +++ b/rewrite-core/src/main/java/org/openrewrite/Result.java @@ -185,7 +185,7 @@ public String diff() { * @return Git-style patch diff representing the changes to this compilation unit. */ public String diff(@Nullable Path relativeTo) { - return diff(relativeTo, null); + return diff(relativeTo, (PrintOutputCapture.MarkerPrinter) null); } public String diff(@Nullable Path relativeTo, PrintOutputCapture.@Nullable MarkerPrinter markerPrinter) {