Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honoring useCRLF in WrappingAndBracesVisitor #4946

Merged
merged 1 commit into from
Jan 27, 2025
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 @@ -19,5 +19,11 @@

@Value
public class GeneralFormatStyle implements Style{
public static final GeneralFormatStyle DEFAULT = new GeneralFormatStyle(false);

boolean useCRLFNewLines;

public String newLine() {
return useCRLFNewLines ? "\r\n" : "\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public enum LineWrapSetting {
DoNotWrap, WrapAlways;
// Eventually we would add values like WrapIfTooLong or ChopIfTooLong

public String delimiter() {
return this == DoNotWrap ? "" : "\n";
public String delimiter(GeneralFormatStyle generalFormatStyle) {
return this == DoNotWrap ? "" : generalFormatStyle.newLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public Json preVisit(Json tree, P p) {
GeneralFormatStyle generalStyle = Style.from(GeneralFormatStyle.class, doc, () -> autodetectedStyle.getStyle(GeneralFormatStyle.class));
WrappingAndBracesStyle wrappingStyle = Style.from(WrappingAndBracesStyle.class, doc, () -> autodetectedStyle.getStyle(WrappingAndBracesStyle.class));

js = new WrappingAndBracesVisitor<>(wrappingStyle, stopAfter).visitNonNull(js, p, cursor.fork());
js = new TabsAndIndentsVisitor<>(wrappingStyle, tabsIndentsStyle, stopAfter).visitNonNull(js, p, cursor.fork());
js = new WrappingAndBracesVisitor<>(wrappingStyle, generalStyle, stopAfter).visitNonNull(js, p, cursor.fork());
js = new TabsAndIndentsVisitor<>(wrappingStyle, tabsIndentsStyle, generalStyle, stopAfter).visitNonNull(js, p, cursor.fork());
js = new NormalizeLineBreaksVisitor<>(generalStyle, stopAfter).visitNonNull(js, p, cursor.fork());
return js;
}
Expand All @@ -64,8 +64,8 @@ public Json.Document visitDocument(Json.Document js, P p) {
GeneralFormatStyle generalStyle = Style.from(GeneralFormatStyle.class, js, () -> autodetectedStyle.getStyle(GeneralFormatStyle.class));
WrappingAndBracesStyle wrappingStyle = Style.from(WrappingAndBracesStyle.class, js, () -> autodetectedStyle.getStyle(WrappingAndBracesStyle.class));

js = (Json.Document) new WrappingAndBracesVisitor<>(wrappingStyle, stopAfter).visitNonNull(js, p);
js = (Json.Document) new TabsAndIndentsVisitor<>(wrappingStyle, tabsIndentsStyle, stopAfter).visitNonNull(js, p);
js = (Json.Document) new WrappingAndBracesVisitor<>(wrappingStyle, generalStyle, stopAfter).visitNonNull(js, p);
js = (Json.Document) new TabsAndIndentsVisitor<>(wrappingStyle, tabsIndentsStyle, generalStyle, stopAfter).visitNonNull(js, p);
js = (Json.Document) new NormalizeLineBreaksVisitor<>(generalStyle, stopAfter).visitNonNull(js, p);

return js;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openrewrite.json.style.TabsAndIndentsStyle;
import org.openrewrite.json.style.WrappingAndBracesStyle;
import org.openrewrite.json.tree.Json;
import org.openrewrite.style.GeneralFormatStyle;
import org.openrewrite.style.Style;

public class Indents extends Recipe {
Expand All @@ -47,7 +48,8 @@ public Json. Document visitDocument(Json.Document docs, ExecutionContext ctx) {
Autodetect autodetected = Autodetect.detector().sample(docs).build();
TabsAndIndentsStyle tabsAndIndentsStyle = Style.from(TabsAndIndentsStyle.class, docs, () -> autodetected.getStyle(TabsAndIndentsStyle.class));
WrappingAndBracesStyle wrappingAndBracesStyle = Style.from(WrappingAndBracesStyle.class, docs, () -> autodetected.getStyle(WrappingAndBracesStyle.class));
doAfterVisit(new TabsAndIndentsVisitor<>(wrappingAndBracesStyle, tabsAndIndentsStyle, null));
GeneralFormatStyle generalFormatStyle = Style.from(GeneralFormatStyle.class, docs, () -> autodetected.getStyle(GeneralFormatStyle.class));
doAfterVisit(new TabsAndIndentsVisitor<>(wrappingAndBracesStyle, tabsAndIndentsStyle, generalFormatStyle,null));
return docs;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.json.tree.Json;
import org.openrewrite.json.tree.JsonRightPadded;
import org.openrewrite.json.tree.Space;
import org.openrewrite.style.GeneralFormatStyle;

import java.util.List;
import java.util.Optional;
Expand All @@ -37,7 +38,10 @@ public class TabsAndIndentsVisitor<P> extends JsonIsoVisitor<P> {
@Nullable
private final Tree stopAfter;

public TabsAndIndentsVisitor(WrappingAndBracesStyle wrappingAndBracesStyle, TabsAndIndentsStyle tabsAndIndentsStyle, @Nullable Tree stopAfter) {
public TabsAndIndentsVisitor(WrappingAndBracesStyle wrappingAndBracesStyle,
TabsAndIndentsStyle tabsAndIndentsStyle,
GeneralFormatStyle generalFormatStyle,
@Nullable Tree stopAfter) {
this.stopAfter = stopAfter;

if (tabsAndIndentsStyle.getUseTabCharacter()) {
Expand All @@ -49,7 +53,7 @@ public TabsAndIndentsVisitor(WrappingAndBracesStyle wrappingAndBracesStyle, Tabs
}
singleIndent = sb.toString();
}
this.objectsWrappingDelimiter = wrappingAndBracesStyle.getWrapObjects().delimiter();
this.objectsWrappingDelimiter = wrappingAndBracesStyle.getWrapObjects().delimiter(generalFormatStyle);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.json.style.WrappingAndBracesStyle;
import org.openrewrite.style.GeneralFormatStyle;

public class WrappingAndBraces extends Recipe {
@Override
Expand All @@ -32,6 +33,6 @@ public String getDescription() {

@Override
public WrappingAndBracesVisitor<ExecutionContext> getVisitor() {
return new WrappingAndBracesVisitor<>(WrappingAndBracesStyle.DEFAULT, null);
return new WrappingAndBracesVisitor<>(WrappingAndBracesStyle.DEFAULT, GeneralFormatStyle.DEFAULT, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.json.tree.JsonRightPadded;
import org.openrewrite.json.tree.JsonValue;
import org.openrewrite.json.tree.Space;
import org.openrewrite.style.GeneralFormatStyle;
import org.openrewrite.style.LineWrapSetting;

import java.util.List;
Expand All @@ -34,28 +35,34 @@ public class WrappingAndBracesVisitor<P> extends JsonIsoVisitor<P> {
@Nullable
private final Tree stopAfter;

private final WrappingAndBracesStyle style;
private final WrappingAndBracesStyle wrappingAndBracesStyle;
private final GeneralFormatStyle generalFormatStyle;

public WrappingAndBracesVisitor(WrappingAndBracesStyle style, @Nullable Tree stopAfter) {
this.style = style;
public WrappingAndBracesVisitor(WrappingAndBracesStyle wrappingAndBracesStyle,
GeneralFormatStyle generalFormatStyle,
@Nullable Tree stopAfter) {
this.wrappingAndBracesStyle = wrappingAndBracesStyle;
this.generalFormatStyle = generalFormatStyle;
this.stopAfter = stopAfter;
}

@Override
public Json.JsonObject visitObject(Json.JsonObject obj, P p) {
Json.JsonObject ret = super.visitObject(obj, p);
final String possibleNewLine = this.wrappingAndBracesStyle.getWrapObjects().delimiter(generalFormatStyle);
List<JsonRightPadded<Json>> members = ret.getPadding().getMembers();
members = applyWrappingStyleToLastChildSuffix(members, this.style.getWrapObjects());
members = applyWrappingStyleToPrefixes(members, this.style.getWrapObjects());
members = applyWrappingStyleToLastChildSuffix(members, possibleNewLine);
members = applyWrappingStyleToPrefixes(members, possibleNewLine);
return ret.getPadding().withMembers(members);
}

@Override
public Json.Array visitArray(Json.Array array, P p) {
Json.Array ret = super.visitArray(array, p);
final String possibleNewLine = this.wrappingAndBracesStyle.getWrapArrays().delimiter(generalFormatStyle);
List<JsonRightPadded<JsonValue>> members = ret.getPadding().getValues();
members = applyWrappingStyleToLastChildSuffix(members, this.style.getWrapArrays());
members = applyWrappingStyleToPrefixes(members, this.style.getWrapArrays());
members = applyWrappingStyleToLastChildSuffix(members, possibleNewLine);
members = applyWrappingStyleToPrefixes(members, possibleNewLine);
return ret.getPadding().withValues(members);
}

Expand All @@ -75,8 +82,7 @@ public Json.Array visitArray(Json.Array array, P p) {
return super.visit(tree, p);
}

private static <JS extends Json> List<JsonRightPadded<JS>> applyWrappingStyleToPrefixes(List<JsonRightPadded<JS>> elements, LineWrapSetting wrap) {
final String possibleNewLine = wrap.delimiter();
private static <JS extends Json> List<JsonRightPadded<JS>> applyWrappingStyleToPrefixes(List<JsonRightPadded<JS>> elements, String possibleNewLine) {
return ListUtils.map(elements, elem -> {
String oldAfterNewLine = elem.getElement().getPrefix().getWhitespaceIndent();
String newPrefix = possibleNewLine + oldAfterNewLine;
Expand All @@ -88,8 +94,7 @@ private static <JS extends Json> List<JsonRightPadded<JS>> applyWrappingStyleToP
});
}

private static <JS extends Json> List<JsonRightPadded<JS>> applyWrappingStyleToLastChildSuffix(List<JsonRightPadded<JS>> elements, LineWrapSetting wrap) {
final String possibleNewLine = wrap.delimiter();
private static <JS extends Json> List<JsonRightPadded<JS>> applyWrappingStyleToLastChildSuffix(List<JsonRightPadded<JS>> elements, String possibleNewLine) {
return ListUtils.mapLast(elements, last -> {
String currentAfterNewLine = last.getAfter().getWhitespaceIndent();
String currentAfter = last.getAfter().getWhitespace();
Expand Down
Loading