-
Notifications
You must be signed in to change notification settings - Fork 435
/
FenceStep.java
258 lines (221 loc) · 7.46 KB
/
FenceStep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright 2020-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.generic;
import java.io.File;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
public class FenceStep {
/** Declares the name of the step. */
public static FenceStep named(String name) {
return new FenceStep(name);
}
public static String defaultToggleName() {
return "toggle";
}
public static String defaultToggleOff() {
return "spotless:off";
}
public static String defaultToggleOn() {
return "spotless:on";
}
String name;
Pattern regex;
private FenceStep(String name) {
this.name = Objects.requireNonNull(name);
}
/** Defines the opening and closing markers. */
public FenceStep openClose(String open, String close) {
return regex(Pattern.quote(open) + "([\\s\\S]*?)" + Pattern.quote(close));
}
/** Defines the pipe via regex. Must have *exactly one* capturing group. */
public FenceStep regex(String regex) {
return regex(Pattern.compile(regex));
}
/** Defines the pipe via regex. Must have *exactly one* capturing group. */
public FenceStep regex(Pattern regex) {
this.regex = Objects.requireNonNull(regex);
return this;
}
private void assertRegexSet() {
Objects.requireNonNull(regex, "must call regex() or openClose()");
}
/** Returns a step which will apply the given steps but preserve the content selected by the regex / openClose pair. */
public FormatterStep preserveWithin(List<FormatterStep> steps) {
assertRegexSet();
return new PreserveWithin(name, regex, steps);
}
/**
* Returns a step which will apply the given steps only within the blocks selected by the regex / openClose pair.
* Linting within the substeps is not supported.
*/
public FormatterStep applyWithin(List<FormatterStep> steps) {
assertRegexSet();
return new ApplyWithin(name, regex, steps);
}
static class ApplyWithin extends BaseStep {
private static final long serialVersionUID = 17061466531957339L;
ApplyWithin(String name, Pattern regex, List<FormatterStep> steps) {
super(name, regex, steps);
}
@Override
public String apply(Formatter formatter, String unix, File file) throws Exception {
List<String> groups = groupsZeroed();
Matcher matcher = regex.matcher(unix);
while (matcher.find()) {
// apply the formatter to each group
groups.add(formatter.compute(matcher.group(1), file));
}
// and then assemble the result right away
return assembleGroups(unix);
}
}
static class PreserveWithin extends BaseStep {
private static final long serialVersionUID = -8676786492305178343L;
PreserveWithin(String name, Pattern regex, List<FormatterStep> steps) {
super(name, regex, steps);
}
private void storeGroups(String unix) {
List<String> groups = groupsZeroed();
Matcher matcher = regex.matcher(unix);
while (matcher.find()) {
// store whatever is within the open/close tags
groups.add(matcher.group(1));
}
}
@Override
public String apply(Formatter formatter, String unix, File file) throws Exception {
storeGroups(unix);
String formatted = formatter.compute(unix, file);
return assembleGroups(formatted);
}
}
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
public static abstract class BaseStep implements Serializable, FormatterStep, FormatterFunc.Closeable.ResourceFuncNeedsFile<Formatter> {
final String name;
private static final long serialVersionUID = -2301848328356559915L;
final Pattern regex;
final List<FormatterStep> steps;
transient ArrayList<String> groups = new ArrayList<>();
transient StringBuilder builderInternal;
public BaseStep(String name, Pattern regex, List<FormatterStep> steps) {
this.name = name;
this.regex = regex;
this.steps = steps;
}
protected ArrayList<String> groupsZeroed() {
if (groups == null) {
groups = new ArrayList<>();
} else {
groups.clear();
}
return groups;
}
private StringBuilder builderZeroed() {
if (builderInternal == null) {
builderInternal = new StringBuilder();
} else {
builderInternal.setLength(0);
}
return builderInternal;
}
protected Formatter buildFormatter() {
return Formatter.builder()
.encoding(StandardCharsets.UTF_8) // can be any UTF, doesn't matter
.lineEndingsPolicy(LineEnding.UNIX.createPolicy()) // just internal, won't conflict with user
.steps(steps)
.rootDir(Path.of("")) // TODO: error messages will be suboptimal for now, but it will get fixed when we ship linting
.build();
}
protected String assembleGroups(String unix) {
if (groups.isEmpty()) {
return unix;
}
StringBuilder builder = builderZeroed();
Matcher matcher = regex.matcher(unix);
int lastEnd = 0;
int groupIdx = 0;
while (matcher.find()) {
builder.append(unix, lastEnd, matcher.start(1));
builder.append(groups.get(groupIdx));
lastEnd = matcher.end(1);
++groupIdx;
}
if (groupIdx == groups.size()) {
builder.append(unix, lastEnd, unix.length());
return builder.toString();
} else {
// these will be needed to generate Lints later on
// int startLine = 1 + (int) builder.toString().codePoints().filter(c -> c == '\n').count();
// int endLine = 1 + (int) unix.codePoints().filter(c -> c == '\n').count();
// throw an error with either the full regex, or the nicer open/close pair
Matcher openClose = Pattern.compile("\\\\Q([\\s\\S]*?)\\\\E" + "\\Q([\\s\\S]*?)\\E" + "\\\\Q([\\s\\S]*?)\\\\E")
.matcher(regex.pattern());
String pattern;
if (openClose.matches()) {
pattern = openClose.group(1) + " " + openClose.group(2);
} else {
pattern = regex.pattern();
}
throw new Error("An intermediate step removed a match of " + pattern);
}
}
@Override
public String getName() {
return name;
}
private transient Formatter formatter;
@Nullable
@Override
public String format(String rawUnix, File file) throws Exception {
if (formatter == null) {
formatter = buildFormatter();
}
return this.apply(formatter, rawUnix, file);
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
BaseStep step = (BaseStep) o;
return name.equals(step.name) && regex.pattern().equals(step.regex.pattern()) && regex.flags() == step.regex.flags() && steps.equals(step.steps);
}
@Override
public int hashCode() {
return Objects.hash(name, regex.pattern(), regex.flags(), steps);
}
@Override
public void close() {
if (formatter != null) {
formatter.close();
formatter = null;
}
}
}
}