Skip to content

Commit

Permalink
Remove excess indentation and simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
literallyfiro committed Apr 24, 2024
1 parent 35eb1e0 commit 254a96a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
16 changes: 1 addition & 15 deletions src/main/java/com/squareup/javapoet/TypeSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ void emit(CodeWriter codeWriter, String enumName, Set<Modifier> implicitModifier

// Record constructor
boolean firstParameter = true;
boolean hasAnnotations = false; // We check for annotations in case we need to unindent later
codeWriter.emit("(");
int fieldSpecsLength = fieldSpecs.size();
for (int i = 0; i < fieldSpecsLength; i++) {
Expand All @@ -256,25 +255,12 @@ void emit(CodeWriter codeWriter, String enumName, Set<Modifier> implicitModifier
if (fieldSpec.hasModifier(Modifier.STATIC))
continue;
ParameterSpec parameter = ParameterSpec.builder(fieldSpec.type, fieldSpec.name).build();
hasAnnotations = !fieldSpec.annotations.isEmpty();
if (!firstParameter)
codeWriter.emit(",").emitWrappingSpace();

if (hasAnnotations) {
if (firstParameter) {
codeWriter.emit("\n").indent(2);
} else {
codeWriter.emit("\n\n");
}
codeWriter.emitAnnotations(fieldSpec.annotations, false);
}

codeWriter.emitAnnotations(fieldSpec.annotations, true);
parameter.emit(codeWriter, !(i < fieldSpecsLength));
firstParameter = false;
}
if (hasAnnotations) {
codeWriter.unindent(2).emit("\n");
}
codeWriter.emit(")");
} else {
extendsTypes = superclass.equals(ClassName.OBJECT)
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/com/squareup/javapoet/JavaFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,11 @@ public void recordTwoAnnotatedFields() {
.build())
.skipJavaLangImports(true).build().toString();

assertThat(source).isEqualTo("" + "package com.squareup.tacos;\n" + "\n"
+ "record Taco(\n" + " @Spicy\n String name, \n\n"
+ " @Spicy\n Integer code\n" + ") {\n}\n" + "");
assertThat(source).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "record Taco(@Spicy String name, @Spicy Integer code) {\n"
+ "}\n");
}

@Test public void conflictingImports() throws Exception {
Expand Down

0 comments on commit 254a96a

Please sign in to comment.