Skip to content

Commit 299fac3

Browse files
committed
tweak preset and omit extra newline for case-arrow with a single line of body
1 parent 21557a7 commit 299fac3

File tree

6 files changed

+92
-78
lines changed

6 files changed

+92
-78
lines changed

src/main/java/io/papermc/typewriter/preset/EnumCloneRewriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.papermc.typewriter.preset;
22

3-
import io.papermc.typewriter.preset.model.EnumValue;
3+
import io.papermc.typewriter.preset.model.EnumConstant;
44

55
import java.util.EnumSet;
66

@@ -19,7 +19,7 @@ protected Iterable<T> getValues() {
1919
}
2020

2121
@Override
22-
protected EnumValue.Builder rewriteEnumValue(final T item) {
23-
return EnumValue.builder(item.name());
22+
protected EnumConstant.Builder constantPrototype(T value) {
23+
return EnumConstant.builder(value.name());
2424
}
2525
}

src/main/java/io/papermc/typewriter/preset/EnumRewriter.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.papermc.typewriter.preset;
22

33
import com.google.common.base.Preconditions;
4-
import io.papermc.typewriter.preset.model.EnumValue;
4+
import io.papermc.typewriter.preset.model.EnumConstant;
55
import io.papermc.typewriter.replace.SearchMetadata;
66
import io.papermc.typewriter.replace.SearchReplaceRewriter;
77
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@@ -24,10 +24,15 @@ public EnumRewriter<T> reachEnd(boolean reachEnd) {
2424

2525
protected abstract Iterable<T> getValues();
2626

27-
protected abstract EnumValue.Builder rewriteEnumValue(T item);
27+
protected abstract EnumConstant.Builder constantPrototype(T value);
2828

29-
protected void appendEnumValue(T item, StringBuilder builder, String indent, boolean reachEnd) {
30-
this.rewriteEnumValue(item).build().emitCode(indent, this.indentUnit(), builder);
29+
protected void rewriteConstant(EnumConstant.Builder builder, T value) {
30+
}
31+
32+
protected void appendConstant(T value, StringBuilder builder, String indent, boolean reachEnd) {
33+
EnumConstant.Builder prototype = this.constantPrototype(value);
34+
this.rewriteConstant(prototype, value);
35+
prototype.build().emitCode(indent, this.indentUnit(), builder);
3136
if (reachEnd && !this.values.hasNext()) {
3237
builder.append(';');
3338
} else {
@@ -51,7 +56,7 @@ private void populateValues() {
5156
protected void replaceLine(SearchMetadata metadata, StringBuilder builder) {
5257
this.populateValues();
5358
Preconditions.checkState(this.values.hasNext(), "Enum size doesn't match between generated values and replaced values.");
54-
appendEnumValue(this.values.next(), builder, metadata.indent(), this.canReachEnd(metadata));
59+
appendConstant(this.values.next(), builder, metadata.indent(), this.canReachEnd(metadata));
5560
}
5661

5762
@Override
@@ -60,7 +65,7 @@ protected void insert(SearchMetadata metadata, StringBuilder builder) {
6065
boolean reachEnd = this.canReachEnd(metadata);
6166

6267
while (this.values.hasNext()) {
63-
appendEnumValue(this.values.next(), builder, metadata.indent(), reachEnd);
68+
appendConstant(this.values.next(), builder, metadata.indent(), reachEnd);
6469
}
6570
}
6671
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package io.papermc.typewriter.preset;
22

3-
import io.papermc.typewriter.preset.model.SwitchContent;
3+
import io.papermc.typewriter.preset.model.SwitchBody;
44
import io.papermc.typewriter.replace.SearchMetadata;
55
import io.papermc.typewriter.replace.SearchReplaceRewriter;
66

77
public abstract class SwitchRewriter extends SearchReplaceRewriter {
88

9-
protected abstract SwitchContent getContent();
9+
protected abstract SwitchBody getBody();
1010

1111
@Override
1212
protected void insert(SearchMetadata metadata, StringBuilder builder) {
13-
this.getContent().emitCode(metadata.indent(), this.indentUnit(), builder);
13+
this.getBody().emitCode(metadata.indent(), this.indentUnit(), builder);
1414
}
1515
}

src/main/java/io/papermc/typewriter/preset/model/EnumValue.java renamed to src/main/java/io/papermc/typewriter/preset/model/EnumConstant.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
import java.util.List;
1010
import java.util.function.UnaryOperator;
1111

12-
public record EnumValue(String name, List<String> arguments, @Nullable CodeBlock body) implements CodeEmitter {
12+
public record EnumConstant(String name, List<String> arguments, @Nullable CodeBlock body) implements CodeEmitter {
1313

14-
public EnumValue(String name, List<String> arguments, @Nullable CodeBlock body) {
14+
public EnumConstant(String name, List<String> arguments, @Nullable CodeBlock body) {
1515
this.name = name;
1616
this.arguments = List.copyOf(arguments);
1717
this.body = body;
1818
}
1919

2020
@Contract(value = "_ -> new", pure = true)
21-
public static EnumValue value(String name) {
22-
return new EnumValue(name, Collections.emptyList(), null);
21+
public static EnumConstant simple(String name) {
22+
return new EnumConstant(name, Collections.emptyList(), null);
2323
}
2424

2525
@Contract(value = "_ -> new", pure = true)
@@ -46,13 +46,17 @@ public static final class Builder {
4646

4747
private final String name;
4848
private List<String> arguments = Collections.emptyList();
49-
private UnaryOperator<String> nameTransformer = name -> name;
49+
private UnaryOperator<String> nameTransformer = UnaryOperator.identity();
5050
private @Nullable CodeBlock body;
5151

5252
private Builder(String name) {
5353
this.name = name;
5454
}
5555

56+
public String initialName() {
57+
return this.name;
58+
}
59+
5660
@Contract(value = "_ -> this", mutates = "this")
5761
public Builder argument(String argument) {
5862
return arguments(Collections.singletonList(argument));
@@ -82,8 +86,8 @@ public Builder body(CodeBlock body) {
8286
}
8387

8488
@Contract(value = "-> new", pure = true)
85-
public EnumValue build() {
86-
return new EnumValue(this.nameTransformer.apply(this.name), this.arguments, this.body);
89+
public EnumConstant build() {
90+
return new EnumConstant(this.nameTransformer.apply(this.name), this.arguments, this.body);
8791
}
8892
}
8993
}

src/main/java/io/papermc/typewriter/preset/model/SwitchContent.java renamed to src/main/java/io/papermc/typewriter/preset/model/SwitchBody.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,73 @@
1414
import java.util.Map;
1515
import java.util.Objects;
1616

17-
public class SwitchContent implements CodeEmitter {
17+
public class SwitchBody implements CodeEmitter {
1818

1919
private List<SwitchCases> cases = new ArrayList<>();
2020
private @Nullable SwitchCases defaultCase;
2121

22-
private SwitchContent(Collection<SwitchCases> branches) {
22+
private SwitchBody(Collection<SwitchCases> branches) {
2323
this.then(branches);
2424
}
2525

2626
@Contract(value = "_ -> new", pure = true)
27-
public static SwitchContent of(SwitchCases... branches) {
27+
public static SwitchBody of(SwitchCases... branches) {
2828
return of(Arrays.asList(branches));
2929
}
3030

3131
@Contract(value = "_ -> new", pure = true)
32-
public static SwitchContent of(Collection<SwitchCases> branches) {
33-
return new SwitchContent(branches);
32+
public static SwitchBody of(Collection<SwitchCases> branches) {
33+
return new SwitchBody(branches);
3434
}
3535

3636
@Contract(value = "_ -> this", mutates = "this")
37-
public SwitchContent then(SwitchCases... branches) {
37+
public SwitchBody then(SwitchCases... branches) {
3838
return then(Arrays.asList(branches));
3939
}
4040

4141
@Contract(value = "_ -> this", mutates = "this")
42-
public SwitchContent then(Collection<SwitchCases> branches) {
42+
public SwitchBody then(Collection<SwitchCases> branches) {
4343
Preconditions.checkArgument(branches.stream().noneMatch(SwitchCases::isDefault), "These switch cases cannot contains default one!");
4444
this.cases.addAll(branches);
4545
return this;
4646
}
4747

4848
@Contract(value = "_ -> this", mutates = "this")
49-
public SwitchContent withDefault(SwitchCases defaultCase) {
49+
public SwitchBody withDefault(SwitchCases defaultCase) {
5050
Preconditions.checkArgument(defaultCase.isDefault(), "This switch case is not a default one!");
5151
this.defaultCase = defaultCase;
5252
return this;
5353
}
5454

5555
@Contract(value = "_ -> this", mutates = "this")
56-
public SwitchContent withDefault(CodeBlock content) {
57-
return withDefault(SwitchCases.ofDefault(content, false));
56+
public SwitchBody withDefault(CodeBlock body) {
57+
return withDefault(SwitchCases.ofDefault(body, false));
5858
}
5959

6060
public void mergeSimilarBranches(Comparator<String> keySort) {
6161
List<SwitchCases> cases = new ArrayList<>();
6262

6363
List<SwitchCases> nonDefaultCases = new ArrayList<>();
64-
Map<CodeBlock, SwitchCases.SwitchCasesChain> handleObjects = new HashMap<>();
65-
@Nullable CodeBlock defaultValue = this.defaultCase == null ? null : this.defaultCase.content();
64+
Map<CodeBlock, SwitchCases.SwitchCasesChain> stats = new HashMap<>();
65+
@Nullable CodeBlock defaultValue = this.defaultCase == null ? null : this.defaultCase.body();
6666

6767
// merge similar cases, and omit cases similar to the default
6868
for (SwitchCases branch : this.cases) {
69-
CodeBlock content = branch.content();
69+
CodeBlock body = branch.body();
7070

71-
if (defaultValue == null || !Objects.equals(branch.content(), defaultValue)) {
71+
if (defaultValue == null || !Objects.equals(branch.body(), defaultValue)) {
7272
nonDefaultCases.add(branch);
7373

7474
final SwitchCases.SwitchCasesChain newCases;
75-
if (handleObjects.containsKey(content)) {
76-
newCases = handleObjects.get(content);
75+
if (stats.containsKey(body)) {
76+
newCases = stats.get(body);
7777
} else {
7878
newCases = SwitchCases.chain();
79-
newCases.sortValues(keySort);
79+
newCases.sortLabels(keySort);
8080
}
81-
newCases.addAll(Objects.requireNonNull(branch.values()));
81+
newCases.addAll(Objects.requireNonNull(branch.labels()));
8282

83-
handleObjects.put(content, newCases);
83+
stats.put(body, newCases);
8484
}
8585
}
8686

@@ -98,13 +98,13 @@ public void mergeSimilarBranches(Comparator<String> keySort) {
9898
}
9999

100100
// complete builders with collected infos
101-
for (Map.Entry<CodeBlock, SwitchCases.SwitchCasesChain> entry : handleObjects.entrySet()) {
102-
SwitchCases.SwitchCasesChain branch = entry.getValue();
101+
for (Map.Entry<CodeBlock, SwitchCases.SwitchCasesChain> stat : stats.entrySet()) {
102+
SwitchCases.SwitchCasesChain branch = stat.getValue();
103103

104104
if (inlined) {
105105
branch.inlined(arrow);
106106
}
107-
cases.add(branch.enclosingContent(entry.getKey()).build());
107+
cases.add(branch.body(stat.getKey()).build());
108108
}
109109
this.cases = cases;
110110
}

0 commit comments

Comments
 (0)