Skip to content

Commit

Permalink
Minor improvements for generated enums and records (#40)
Browse files Browse the repository at this point in the history
Improves formatting of generate record and enum sources
  • Loading branch information
altro3 authored Jan 30, 2024
1 parent fbb6244 commit 4fb1df2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void emit(CodeWriter codeWriter, String enclosingName, Set<Modifier> implicitMod
codeWriter.emit("$T $L", returnType, name);
}

emitParameters(codeWriter, parameters, varargs);
emitParameters(codeWriter, parameters, varargs, false);
}

if (defaultValue != null && !defaultValue.isEmpty()) {
Expand Down Expand Up @@ -157,18 +157,28 @@ static CodeBlock makeJavadocWithParameters(CodeBlock javadoc,
}

static void emitParameters(CodeWriter codeWriter, Iterable<ParameterSpec> parameters,
boolean varargs) throws IOException {
boolean varargs, boolean isRecord) throws IOException {
codeWriter.emit(CodeBlock.of("($Z"));

boolean hasParameters = false;
boolean firstParameter = true;
for (Iterator<ParameterSpec> i = parameters.iterator(); i.hasNext(); ) {
hasParameters = true;
ParameterSpec parameter = i.next();
if (!firstParameter)
codeWriter.emit(",").emitWrappingSpace();
if (!firstParameter) {
codeWriter.emit(",");
if (!isRecord)
codeWriter.emitWrappingSpace();
}
if (isRecord)
codeWriter.emit("\n ");
parameter.emit(codeWriter, !i.hasNext() && varargs);
firstParameter = false;
}

if (isRecord && hasParameters) {
codeWriter.emit("\n");
}
codeWriter.emit(")");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private boolean emitHeader(CodeWriter codeWriter, String enumName,

// Record components.
if (kind == Kind.RECORD) {
MethodSpec.emitParameters(codeWriter, recordComponents, varargs);
MethodSpec.emitParameters(codeWriter, recordComponents, varargs, true);
}

List<TypeName> extendsTypes;
Expand Down Expand Up @@ -313,7 +313,7 @@ void emit(CodeWriter codeWriter, String enumName,
for (Iterator<Map.Entry<String, TypeSpec>> i = enumConstants.entrySet().iterator();
i.hasNext(); ) {
Map.Entry<String, TypeSpec> enumConstant = i.next();
if (!firstMember) codeWriter.emit("\n");
if (firstMember) codeWriter.emit("\n");
enumConstant.getValue().emit(codeWriter, enumConstant.getKey(), Collections.emptySet());
firstMember = false;
if (i.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ public void basicRecord() throws Exception {
+ "import java.lang.IllegalArgumentException;\n"
+ "import java.lang.String;\n"
+ "\n"
+ "public record KeyValue(String key, String value) implements Cloneable {\n"
+ "public record KeyValue(\n"
+ " String key,\n"
+ " String value\n"
+ ") implements Cloneable {\n"
+ " public KeyValue {\n"
+ " if (key.indexOf(':') != -1) {\n"
+ " throw new IllegalArgumentException();\n"
Expand Down Expand Up @@ -417,7 +420,10 @@ public void varargsRecord() throws Exception {
+ "\n"
+ "import java.lang.String;\n"
+ "\n"
+ "public record Vararg(int val, String... keys) {\n"
+ "public record Vararg(\n"
+ " int val,\n"
+ " String... keys\n"
+ ") {\n"
+ "}\n");
}

Expand Down Expand Up @@ -473,18 +479,17 @@ public void basicSealedClass() {
+ "import java.lang.String;\n"
+ "\n"
+ "public enum Roshambo {\n"
+ "\n"
+ " /**\n"
+ " * Avalanche!\n"
+ " */\n"
+ " ROCK,\n"
+ "\n"
+ " PAPER(\"flat\") {\n"
+ " @Override\n"
+ " public String toString() {\n"
+ " return \"paper airplane!\";\n"
+ " }\n"
+ " },\n"
+ "\n"
+ " SCISSORS(\"peace sign\");\n"
+ "\n"
+ " private final String handPosition;\n"
Expand Down Expand Up @@ -518,6 +523,7 @@ public void basicSealedClass() {
+ "import java.lang.Override;\n"
+ "\n"
+ "public enum Tortilla {\n"
+ "\n"
+ " CORN {\n"
+ " @Override\n"
+ " public void fold() {\n"
Expand Down Expand Up @@ -569,6 +575,7 @@ public void basicSealedClass() {
+ "import java.lang.String;\n"
+ "\n"
+ "enum Roshambo {\n"
+ "\n"
+ " SPOCK {\n"
+ " @Override\n"
+ " public String toString() {\n"
Expand All @@ -593,11 +600,10 @@ public void basicSealedClass() {
+ "import java.lang.Deprecated;\n"
+ "\n"
+ "public enum Roshambo {\n"
+ "\n"
+ " @Deprecated\n"
+ " ROCK,\n"
+ "\n"
+ " PAPER,\n"
+ "\n"
+ " SCISSORS\n"
+ "}\n");
}
Expand Down Expand Up @@ -769,8 +775,8 @@ public void basicSealedClass() {
+ "import java.lang.Cloneable;\n"
+ "\n"
+ "enum Food implements Serializable, Cloneable {\n"
+ " LEAN_GROUND_BEEF,\n"
+ "\n"
+ " LEAN_GROUND_BEEF,\n"
+ " SHREDDED_CHEESE\n"
+ "}\n");
}
Expand Down Expand Up @@ -836,8 +842,8 @@ public void basicSealedClass() {
+ " Sauce sauce;\n"
+ "\n"
+ " enum Topping {\n"
+ " SHREDDED_CHEESE,\n"
+ "\n"
+ " SHREDDED_CHEESE,\n"
+ " LEAN_GROUND_BEEF\n"
+ " }\n"
+ " }\n"
Expand All @@ -849,14 +855,11 @@ public void basicSealedClass() {
+ " }\n"
+ "\n"
+ " enum Sauce {\n"
+ " SOUR_CREAM,\n"
+ "\n"
+ " SOUR_CREAM,\n"
+ " SALSA,\n"
+ "\n"
+ " QUESO,\n"
+ "\n"
+ " MILD,\n"
+ "\n"
+ " FIRE\n"
+ " }\n"
+ "}\n");
Expand Down Expand Up @@ -1323,7 +1326,10 @@ public void recordComponentJavadoc() throws Exception {
+ " * @param shell the outer level of tortilla of the taco\n"
+ " * @param soft true for a soft flour tortilla, or false for a crunchy corn tortilla\n"
+ " */\n"
+ "record Taco(String shell, boolean soft) {\n"
+ "record Taco(\n"
+ " String shell,\n"
+ " boolean soft\n"
+ ") {\n"
+ " /**\n"
+ " * Makes a taco without a cat :(.\n"
+ " *\n"
Expand Down Expand Up @@ -1597,10 +1603,13 @@ public void recordComponentJavadoc() throws Exception {
+ " }\n"
+ "\n"
+ " enum Topping {\n"
+ "\n"
+ " SALSA\n"
+ " }\n"
+ "\n"
+ " record Veggie(int name) {\n"
+ " record Veggie(\n"
+ " int name\n"
+ " ) {\n"
+ " }\n"
+ "}\n");
}
Expand Down

0 comments on commit 4fb1df2

Please sign in to comment.