summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-05-23 11:31:02 +0100
committerMichael Williamson <mike@zwobble.org>2026-05-23 11:31:02 +0100
commit4575cd222aa78442d41373b710be0640b24871c5 (patch)
treeeb9bce51dd0b3ec617228104dd72dff4ff4851aa /src
parentc7fbd6c9fdc3fe140b0f1ff713855ab3a3ad599e (diff)
Write custom area delimiters
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java46
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java20
2 files changed, 48 insertions, 18 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
index 89c3aa0..ff8171a 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
@@ -77,17 +77,16 @@ public class JavaWriter implements AutoCloseable {
this.writer.write(" {");
this.writer.indent();
- writeWithSeparator(
- classDeclaration.body(),
- bodyDeclaration -> {
- writer.newLine();
- writeClassBodyDeclaration(bodyDeclaration);
- },
- () -> writer.newLine()
- );
+ for (var bodyDeclaration: classDeclaration.body()) {
+ writer.newLine();
+ writeClassBodyDeclaration(bodyDeclaration);
+ writer.newLine();
+ }
- this.writer.dedent();
this.writer.newLine();
+ this.writeCustomArea("body", classDeclaration.customArea());
+
+ this.writer.dedent();
this.writer.write("}");
}
@@ -103,6 +102,15 @@ public class JavaWriter implements AutoCloseable {
}
}
+ private void writeCustomArea(String name, JavaCustomArea javaCustomArea) throws IOException {
+ this.writer.write("// Custom area start: ");
+ this.writer.write(name);
+ this.writer.newLine();
+ this.writer.write("// Custom area end: ");
+ this.writer.write(name);
+ this.writer.newLine();
+ }
+
void writeExpression(JavaExpression expression) throws IOException {
switch (expression) {
case JavaIntegerLiteral integerLiteral -> {
@@ -149,7 +157,10 @@ public class JavaWriter implements AutoCloseable {
}
this.writer.write("{");
+ this.writer.indent();
this.writer.newLine();
+ this.writeCustomArea("body", interfaceDeclaration.customArea());
+ this.writer.dedent();
this.writer.write("}");
}
@@ -247,17 +258,16 @@ public class JavaWriter implements AutoCloseable {
this.writer.write("{");
this.writer.indent();
- writeWithSeparator(
- recordDeclaration.body(),
- bodyDeclaration -> {
- writer.newLine();
- writeClassBodyDeclaration(bodyDeclaration);
- },
- () -> writer.newLine()
- );
+ for (var bodyDeclaration : recordDeclaration.body()) {
+ writer.newLine();
+ writeClassBodyDeclaration(bodyDeclaration);
+ writer.newLine();
+ }
- this.writer.dedent();
this.writer.newLine();
+ this.writeCustomArea("body", recordDeclaration.customArea());
+
+ this.writer.dedent();
this.writer.write("}");
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
index 280dc21..5595b62 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
@@ -26,6 +26,8 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
public class Rectangle {
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}
@@ -63,6 +65,9 @@ public class JavaWriterTests {
public void b() {
}
+
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}
@@ -81,6 +86,8 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
/// A 2D point.
public class Point {
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}
@@ -98,6 +105,8 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
public sealed interface Shape {
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}
@@ -118,6 +127,8 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
public sealed interface Shape permits abc.def.One, abc.def.Two {
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}
@@ -136,6 +147,8 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
/// A 2D shape.
public sealed interface Shape {
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}
@@ -155,6 +168,8 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
public record Point() {
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}
@@ -175,6 +190,8 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
/// A 2D point.
public record Point() {
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}
@@ -214,6 +231,9 @@ public class JavaWriterTests {
public void b() {
}
+
+ // Custom area start: body
+ // Custom area end: body
}"""
));
}