|
1 | 1 | /* |
2 | | - * Copyright 2020-2024 DiffPlug |
| 2 | + * Copyright 2020-2025 DiffPlug |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
24 | 24 | import java.util.regex.Matcher; |
25 | 25 | import java.util.regex.Pattern; |
26 | 26 |
|
27 | | -import javax.annotation.Nullable; |
28 | | - |
| 27 | +import com.diffplug.spotless.ConfigurationCacheHackList; |
29 | 28 | import com.diffplug.spotless.Formatter; |
| 29 | +import com.diffplug.spotless.FormatterFunc; |
30 | 30 | import com.diffplug.spotless.FormatterStep; |
31 | 31 | import com.diffplug.spotless.LineEnding; |
32 | 32 | import com.diffplug.spotless.Lint; |
@@ -80,94 +80,83 @@ private void assertRegexSet() { |
80 | 80 |
|
81 | 81 | /** Returns a step which will apply the given steps but preserve the content selected by the regex / openClose pair. */ |
82 | 82 | public FormatterStep preserveWithin(List<FormatterStep> steps) { |
83 | | - assertRegexSet(); |
84 | | - return new PreserveWithin(name, regex, steps); |
| 83 | + return createStep(Kind.PRESERVE, steps); |
85 | 84 | } |
86 | 85 |
|
87 | 86 | /** |
88 | 87 | * Returns a step which will apply the given steps only within the blocks selected by the regex / openClose pair. |
89 | 88 | * Linting within the substeps is not supported. |
90 | 89 | */ |
91 | 90 | public FormatterStep applyWithin(List<FormatterStep> steps) { |
| 91 | + return createStep(Kind.APPLY, steps); |
| 92 | + } |
| 93 | + |
| 94 | + private FormatterStep createStep(Kind kind, List<FormatterStep> steps) { |
92 | 95 | assertRegexSet(); |
93 | | - return new ApplyWithin(name, regex, steps); |
| 96 | + return FormatterStep.createLazy(name, () -> new RoundtripAndEqualityState(kind, regex, steps, false), |
| 97 | + RoundtripAndEqualityState::toEqualityState, |
| 98 | + RoundtripAndEqualityState::toFormatterFunc); |
94 | 99 | } |
95 | 100 |
|
96 | | - static class ApplyWithin extends BaseStep { |
97 | | - private static final long serialVersionUID = 17061466531957339L; |
| 101 | + private enum Kind { |
| 102 | + APPLY, PRESERVE |
| 103 | + } |
98 | 104 |
|
99 | | - ApplyWithin(String name, Pattern regex, List<FormatterStep> steps) { |
100 | | - super(name, regex, steps); |
101 | | - } |
| 105 | + private static class RoundtripAndEqualityState implements Serializable { |
| 106 | + private static final long serialVersionUID = 272603249547598947L; |
| 107 | + final String regexPattern; |
| 108 | + final int regexFlags; |
| 109 | + final Kind kind; |
| 110 | + final ConfigurationCacheHackList steps; |
102 | 111 |
|
103 | | - @Override |
104 | | - protected String applySubclass(Formatter formatter, String unix, File file) { |
105 | | - List<String> groups = groupsZeroed(); |
106 | | - Matcher matcher = regex.matcher(unix); |
107 | | - while (matcher.find()) { |
108 | | - // apply the formatter to each group |
109 | | - groups.add(formatter.compute(matcher.group(1), file)); |
110 | | - } |
111 | | - // and then assemble the result right away |
112 | | - return assembleGroups(unix); |
| 112 | + /** Roundtrip state. */ |
| 113 | + private RoundtripAndEqualityState(Kind kind, Pattern regex, List<FormatterStep> steps, boolean optimizeForEquality) { |
| 114 | + this.kind = kind; |
| 115 | + this.regexPattern = regex.pattern(); |
| 116 | + this.regexFlags = regex.flags(); |
| 117 | + this.steps = optimizeForEquality ? ConfigurationCacheHackList.forEquality() : ConfigurationCacheHackList.forRoundtrip(); |
| 118 | + this.steps.addAll(steps); |
113 | 119 | } |
114 | | - } |
115 | 120 |
|
116 | | - static class PreserveWithin extends BaseStep { |
117 | | - private static final long serialVersionUID = -8676786492305178343L; |
| 121 | + private Pattern regex() { |
| 122 | + return Pattern.compile(regexPattern, regexFlags); |
| 123 | + } |
118 | 124 |
|
119 | | - PreserveWithin(String name, Pattern regex, List<FormatterStep> steps) { |
120 | | - super(name, regex, steps); |
| 125 | + private List<FormatterStep> steps() { |
| 126 | + return steps.getSteps(); |
121 | 127 | } |
122 | 128 |
|
123 | | - private void storeGroups(String unix) { |
124 | | - List<String> groups = groupsZeroed(); |
125 | | - Matcher matcher = regex.matcher(unix); |
126 | | - while (matcher.find()) { |
127 | | - // store whatever is within the open/close tags |
128 | | - groups.add(matcher.group(1)); |
129 | | - } |
| 129 | + public RoundtripAndEqualityState toEqualityState() { |
| 130 | + return new RoundtripAndEqualityState(kind, regex(), steps(), true); |
130 | 131 | } |
131 | 132 |
|
132 | | - @Override |
133 | | - protected String applySubclass(Formatter formatter, String unix, File file) { |
134 | | - storeGroups(unix); |
135 | | - String formatted = formatter.compute(unix, file); |
136 | | - return assembleGroups(formatted); |
| 133 | + public BaseFormatter toFormatterFunc() { |
| 134 | + return new BaseFormatter(kind, this); |
137 | 135 | } |
138 | 136 | } |
139 | 137 |
|
140 | 138 | @SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED") |
141 | | - private static abstract class BaseStep implements Serializable, FormatterStep { |
142 | | - final String name; |
143 | | - private static final long serialVersionUID = -2301848328356559915L; |
| 139 | + private static class BaseFormatter implements FormatterFunc.NeedsFile, FormatterFunc.Closeable { |
| 140 | + final Kind kind; |
144 | 141 | final Pattern regex; |
145 | 142 | final List<FormatterStep> steps; |
146 | 143 |
|
147 | | - transient ArrayList<String> groups = new ArrayList<>(); |
148 | | - transient StringBuilder builderInternal; |
| 144 | + final ArrayList<String> groups = new ArrayList<>(); |
| 145 | + final StringBuilder builderInternal = new StringBuilder(); |
149 | 146 |
|
150 | | - public BaseStep(String name, Pattern regex, List<FormatterStep> steps) { |
151 | | - this.name = name; |
152 | | - this.regex = regex; |
153 | | - this.steps = steps; |
| 147 | + public BaseFormatter(Kind kind, RoundtripAndEqualityState state) { |
| 148 | + this.kind = kind; |
| 149 | + this.regex = state.regex(); |
| 150 | + this.steps = state.steps(); |
154 | 151 | } |
155 | 152 |
|
156 | 153 | protected ArrayList<String> groupsZeroed() { |
157 | | - if (groups == null) { |
158 | | - groups = new ArrayList<>(); |
159 | | - } else { |
160 | | - groups.clear(); |
161 | | - } |
| 154 | + groups.clear(); |
162 | 155 | return groups; |
163 | 156 | } |
164 | 157 |
|
165 | 158 | private StringBuilder builderZeroed() { |
166 | | - if (builderInternal == null) { |
167 | | - builderInternal = new StringBuilder(); |
168 | | - } else { |
169 | | - builderInternal.setLength(0); |
170 | | - } |
| 159 | + builderInternal.setLength(0); |
171 | 160 | return builderInternal; |
172 | 161 | } |
173 | 162 |
|
@@ -215,41 +204,33 @@ protected String assembleGroups(String unix) { |
215 | 204 | } |
216 | 205 | } |
217 | 206 |
|
218 | | - @Override |
219 | | - public String getName() { |
220 | | - return name; |
221 | | - } |
| 207 | + private Formatter formatter; |
222 | 208 |
|
223 | | - private transient Formatter formatter; |
224 | | - |
225 | | - private String apply(String rawUnix, File file) throws Exception { |
| 209 | + @Override |
| 210 | + public String applyWithFile(String unix, File file) throws Exception { |
226 | 211 | if (formatter == null) { |
227 | 212 | formatter = buildFormatter(); |
228 | 213 | } |
229 | | - return applySubclass(formatter, rawUnix, file); |
230 | | - } |
231 | | - |
232 | | - @Nullable |
233 | | - @Override |
234 | | - public String format(String rawUnix, File file) throws Exception { |
235 | | - return apply(rawUnix, file); |
236 | | - } |
237 | | - |
238 | | - protected abstract String applySubclass(Formatter formatter, String unix, File file) throws Exception; |
239 | | - |
240 | | - @Override |
241 | | - public boolean equals(Object o) { |
242 | | - if (this == o) |
243 | | - return true; |
244 | | - if (o == null || getClass() != o.getClass()) |
245 | | - return false; |
246 | | - BaseStep step = (BaseStep) o; |
247 | | - return name.equals(step.name) && regex.pattern().equals(step.regex.pattern()) && regex.flags() == step.regex.flags() && steps.equals(step.steps); |
248 | | - } |
249 | | - |
250 | | - @Override |
251 | | - public int hashCode() { |
252 | | - return Objects.hash(name, regex.pattern(), regex.flags(), steps); |
| 214 | + List<String> groups = groupsZeroed(); |
| 215 | + Matcher matcher = regex.matcher(unix); |
| 216 | + switch (kind) { |
| 217 | + case APPLY: |
| 218 | + while (matcher.find()) { |
| 219 | + // apply the formatter to each group |
| 220 | + groups.add(formatter.compute(matcher.group(1), file)); |
| 221 | + } |
| 222 | + // and then assemble the result right away |
| 223 | + return assembleGroups(unix); |
| 224 | + case PRESERVE: |
| 225 | + while (matcher.find()) { |
| 226 | + // store whatever is within the open/close tags |
| 227 | + groups.add(matcher.group(1)); |
| 228 | + } |
| 229 | + String formatted = formatter.compute(unix, file); |
| 230 | + return assembleGroups(formatted); |
| 231 | + default: |
| 232 | + throw new Error(); |
| 233 | + } |
253 | 234 | } |
254 | 235 |
|
255 | 236 | @Override |
|
0 commit comments