|
23 | 23 | import com.palantir.javaformat.doc.Doc; |
24 | 24 | import com.palantir.javaformat.doc.Level; |
25 | 25 | import java.io.IOException; |
26 | | -import java.io.UncheckedIOException; |
27 | | -import org.derive4j.ArgOption; |
28 | | -import org.derive4j.Data; |
29 | 26 |
|
30 | | -@Data(arguments = ArgOption.checkedNotNull) |
| 27 | +/** |
| 28 | + * How a {@link Level} is broken when it does not fit on one line. Each case's {@code toString()} is what |
| 29 | + * {@link com.palantir.javaformat.doc.LevelDelimitedFlatValueDocVisitor} prints for it. |
| 30 | + */ |
31 | 31 | @Immutable |
32 | 32 | @JsonSerialize(using = BreakBehaviour.Json.class) |
33 | | -public abstract class BreakBehaviour { |
34 | | - public interface Cases<R> { |
| 33 | +public sealed interface BreakBehaviour { |
35 | 34 |
|
36 | | - R breakThisLevel(); |
| 35 | + static BreakBehaviour breakThisLevel() { |
| 36 | + return new BreakThisLevel(); |
| 37 | + } |
37 | 38 |
|
38 | | - /** |
39 | | - * If the last level is breakable, prefer breaking it if it will keep the rest of this level on line line. |
40 | | - * |
41 | | - * @param keepIndentWhenInlined whether to keep this level's indent when inlined as a recursive level (when |
42 | | - * reached via a previous `preferBreakingLastInnerLevel` whose breakability was |
43 | | - * {@link LastLevelBreakability#CHECK_INNER}) |
44 | | - */ |
45 | | - R preferBreakingLastInnerLevel(boolean keepIndentWhenInlined); |
| 39 | + static BreakBehaviour preferBreakingLastInnerLevel(boolean keepIndentWhenInlined) { |
| 40 | + return new PreferBreakingLastInnerLevel(keepIndentWhenInlined); |
| 41 | + } |
46 | 42 |
|
47 | | - /** |
48 | | - * Attempt to inline the suffix of this level (which must be a {@link Level} and the last doc), recursing into |
49 | | - * the {@link Level} just before the last {@link Level} (if there is such a level) to see if that can be broken |
50 | | - * instead. |
51 | | - * |
52 | | - * <p>This behaves like {@link #breakThisLevel()} if we couldn't recurse into such an inner level, or if the |
53 | | - * suffix level doesn't fit on the last line. |
54 | | - */ |
55 | | - R inlineSuffix(); |
| 43 | + static BreakBehaviour inlineSuffix() { |
| 44 | + return new InlineSuffix(); |
| 45 | + } |
56 | 46 |
|
57 | | - /** |
58 | | - * Break if by doing so all inner levels then fit on a single line. However, don't break if we can fit in the |
59 | | - * {@link Doc docs} up to the first break (which might be nested inside the next doc if it's a {@link Level}), |
60 | | - * in order to prevent exceeding the maxLength accidentally. |
61 | | - */ |
62 | | - R breakOnlyIfInnerLevelsThenFitOnOneLine(boolean keepIndentWhenInlined); |
| 47 | + static BreakBehaviour breakOnlyIfInnerLevelsThenFitOnOneLine(boolean keepIndentWhenInlined) { |
| 48 | + return new BreakOnlyIfInnerLevelsThenFitOnOneLine(keepIndentWhenInlined); |
63 | 49 | } |
64 | 50 |
|
65 | | - public abstract <R> R match(Cases<R> cases); |
| 51 | + /** Break this level. */ |
| 52 | + record BreakThisLevel() implements BreakBehaviour { |
| 53 | + @Override |
| 54 | + public String toString() { |
| 55 | + return "breakThisLevel()"; |
| 56 | + } |
| 57 | + } |
66 | 58 |
|
67 | | - /** For {@link com.palantir.javaformat.doc.LevelDelimitedFlatValueDocVisitor}. */ |
68 | | - @Override |
69 | | - public abstract String toString(); |
| 59 | + /** |
| 60 | + * If the last level is breakable, prefer breaking it if it will keep the rest of this level on line line. |
| 61 | + * |
| 62 | + * @param keepIndentWhenInlined whether to keep this level's indent when inlined as a recursive level (when |
| 63 | + * reached via a previous `preferBreakingLastInnerLevel` whose breakability was |
| 64 | + * {@link LastLevelBreakability#CHECK_INNER}) |
| 65 | + */ |
| 66 | + record PreferBreakingLastInnerLevel(boolean keepIndentWhenInlined) implements BreakBehaviour { |
| 67 | + @Override |
| 68 | + public String toString() { |
| 69 | + return "preferBreakingLastInnerLevel(" + keepIndentWhenInlined + ")"; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Attempt to inline the suffix of this level (which must be a {@link Level} and the last doc), recursing into the |
| 75 | + * {@link Level} just before the last {@link Level} (if there is such a level) to see if that can be broken instead. |
| 76 | + * |
| 77 | + * <p>This behaves like {@link BreakThisLevel} if we couldn't recurse into such an inner level, or if the suffix |
| 78 | + * level doesn't fit on the last line. |
| 79 | + */ |
| 80 | + record InlineSuffix() implements BreakBehaviour { |
| 81 | + @Override |
| 82 | + public String toString() { |
| 83 | + return "inlineSuffix()"; |
| 84 | + } |
| 85 | + } |
70 | 86 |
|
71 | 87 | /** |
72 | | - * This is gross but just wanted to get something working. See https://github.com/derive4j/derive4j/issues/51 for a |
73 | | - * potential better implementation. |
| 88 | + * Break if by doing so all inner levels then fit on a single line. However, don't break if we can fit in the |
| 89 | + * {@link Doc docs} up to the first break (which might be nested inside the next doc if it's a {@link Level}), in |
| 90 | + * order to prevent exceeding the maxLength accidentally. |
| 91 | + * |
| 92 | + * @param keepIndentWhenInlined whether to keep this level's indent when inlined as a recursive level |
74 | 93 | */ |
75 | | - static class Json extends JsonSerializer<BreakBehaviour> { |
| 94 | + record BreakOnlyIfInnerLevelsThenFitOnOneLine(boolean keepIndentWhenInlined) implements BreakBehaviour { |
| 95 | + @Override |
| 96 | + public String toString() { |
| 97 | + return "breakOnlyIfInnerLevelsThenFitOnOneLine(" + keepIndentWhenInlined + ")"; |
| 98 | + } |
| 99 | + } |
76 | 100 |
|
| 101 | + /** Writes the case as {@code type} and its flag, if it has one. */ |
| 102 | + final class Json extends JsonSerializer<BreakBehaviour> { |
77 | 103 | @Override |
78 | 104 | public void serialize(BreakBehaviour value, JsonGenerator gen, SerializerProvider serializers) |
79 | 105 | throws IOException { |
80 | 106 | gen.writeStartObject(); |
81 | | - BreakBehaviours.caseOf(value) |
82 | | - .breakThisLevel(() -> { |
83 | | - try { |
84 | | - gen.writeObjectField("type", "breakThisLevel"); |
85 | | - } catch (IOException e) { |
86 | | - throw new UncheckedIOException(e); |
87 | | - } |
88 | | - return null; |
89 | | - }) |
90 | | - .preferBreakingLastInnerLevel(keepIndentWhenInlined -> { |
91 | | - try { |
92 | | - gen.writeObjectField("type", "preferBreakingLastInnerLevel"); |
93 | | - gen.writeObjectField("keepIndentWhenInlined", keepIndentWhenInlined); |
94 | | - } catch (IOException e) { |
95 | | - throw new UncheckedIOException(e); |
96 | | - } |
97 | | - return null; |
98 | | - }) |
99 | | - .inlineSuffix(() -> { |
100 | | - try { |
101 | | - gen.writeObjectField("type", "inlineSuffix"); |
102 | | - } catch (IOException e) { |
103 | | - throw new UncheckedIOException(e); |
104 | | - } |
105 | | - return null; |
106 | | - }) |
107 | | - .breakOnlyIfInnerLevelsThenFitOnOneLine(keepIndentWhenInlined -> { |
108 | | - try { |
109 | | - gen.writeObjectField("type", "breakOnlyIfInnerLevelsThenFitOnOneLine"); |
110 | | - gen.writeObjectField("keepIndentWhenInlined", keepIndentWhenInlined); |
111 | | - } catch (IOException e) { |
112 | | - throw new UncheckedIOException(e); |
113 | | - } |
114 | | - return null; |
115 | | - }); |
| 107 | + switch (value) { |
| 108 | + case BreakThisLevel() -> gen.writeObjectField("type", "breakThisLevel"); |
| 109 | + case PreferBreakingLastInnerLevel(boolean keepIndentWhenInlined) -> { |
| 110 | + gen.writeObjectField("type", "preferBreakingLastInnerLevel"); |
| 111 | + gen.writeObjectField("keepIndentWhenInlined", keepIndentWhenInlined); |
| 112 | + } |
| 113 | + case InlineSuffix() -> gen.writeObjectField("type", "inlineSuffix"); |
| 114 | + case BreakOnlyIfInnerLevelsThenFitOnOneLine(boolean keepIndentWhenInlined) -> { |
| 115 | + gen.writeObjectField("type", "breakOnlyIfInnerLevelsThenFitOnOneLine"); |
| 116 | + gen.writeObjectField("keepIndentWhenInlined", keepIndentWhenInlined); |
| 117 | + } |
| 118 | + } |
116 | 119 | gen.writeEndObject(); |
117 | 120 | } |
118 | 121 | } |
|
0 commit comments