Skip to content

Commit

Permalink
casid#285 default Content parameters work with binary templates
Browse files Browse the repository at this point in the history
The default Content parameters are also generated in the renderMap() method as default parameters, so we need to wait with writing the static class fields until that part is complete.
  • Loading branch information
casid committed Oct 14, 2023
1 parent 478f494 commit 302ca59
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,6 @@ public void onLineFinished() {

@Override
public void onComplete() {
// Line information must be updated before insert, otherwise the line info field is not up-to-date
int lineCount = 2;
if (!binaryTextParts.isEmpty()) {
lineCount += binaryTextParts.size() + 1;
}
kotlinCode.fillLines(fieldsMarker, lineCount);

StringBuilder fields = new StringBuilder();
addNameField(fields, classInfo.name);
addLineInfoField(fields);
writeBinaryTextParts(fields);

kotlinCode.insert(fieldsMarker, fields, false);

kotlinCode.append("\t}\n");

kotlinCode.append("\t@JvmStatic fun renderMap(");
Expand Down Expand Up @@ -172,6 +158,20 @@ public void onComplete() {
kotlinCode.append("}\n");
kotlinCode.append("}\n");

// Line information must be updated before insert, otherwise the line info field is not up-to-date
int lineCount = 2;
if (!binaryTextParts.isEmpty()) {
lineCount += binaryTextParts.size() + 1;
}
kotlinCode.fillLines(fieldsMarker, lineCount);

StringBuilder fields = new StringBuilder();
addNameField(fields, classInfo.name);
addLineInfoField(fields);
writeBinaryTextParts(fields);

kotlinCode.insert(fieldsMarker, fields, false);

this.classInfo.lineInfo = kotlinCode.getLineInfo();
}

Expand Down
10 changes: 10 additions & 0 deletions jte-kotlin/src/test/java/gg/jte/kotlin/TemplateEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,16 @@ void tagWithDefaultParam_secondSet() {
thenOutputIs("1, 5");
}

@Test
void templateWithDefaultParam_content() {
givenTag("named", "@param one:gg.jte.Content = @`This is`\n" +
"@param two:Int = 2\n" +
"${one}: ${two}");
givenTemplate("@template.tag.named()");

thenOutputIs("This is: 2");
}

@Test
void tagWithVarArgs1() {
givenTag("varargs",
Expand Down
28 changes: 14 additions & 14 deletions jte/src/main/java/gg/jte/compiler/java/JavaCodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,6 @@ public void onLineFinished() {

@Override
public void onComplete() {
// Line information must be updated before insert, otherwise the line info field is not up-to-date
int lineCount = 2;
if (!binaryTextParts.isEmpty()) {
lineCount += binaryTextParts.size() + 1;
}
javaCode.fillLines(fieldsMarker, lineCount);

StringBuilder fields = new StringBuilder();
addNameField(fields, classInfo.name);
addLineInfoField(fields);
writeBinaryTextParts(fields);

javaCode.insert(fieldsMarker, fields, false);

javaCode.append("\t}\n");

javaCode.append("\tpublic static void renderMap(");
Expand Down Expand Up @@ -166,6 +152,20 @@ public void onComplete() {

javaCode.append("}\n");

// Line information must be updated before insert, otherwise the line info field is not up-to-date
int lineCount = 2;
if (!binaryTextParts.isEmpty()) {
lineCount += binaryTextParts.size() + 1;
}
javaCode.fillLines(fieldsMarker, lineCount);

StringBuilder fields = new StringBuilder();
addNameField(fields, classInfo.name);
addLineInfoField(fields);
writeBinaryTextParts(fields);

javaCode.insert(fieldsMarker, fields, false);

this.classInfo.lineInfo = javaCode.getLineInfo();
}

Expand Down
10 changes: 10 additions & 0 deletions jte/src/test/java/gg/jte/TemplateEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,16 @@ void templateWithDefaultParam_secondSet() {
thenOutputIs("1, 5");
}

@Test
void templateWithDefaultParam_content() {
givenTemplate("named.jte", "@param gg.jte.Content one = @`This is`\n" +
"@param int two = 2\n" +
"${one}: ${two}");
givenTemplate("@template.named()");

thenOutputIs("This is: 2");
}

@Test
void templateWithVarArgs1() {
givenTemplate("varargs.jte",
Expand Down

0 comments on commit 302ca59

Please sign in to comment.