From 1cc30d7d45377b98ee754e248149bf2f0cc485fb Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 23 May 2026 11:33:47 +0100 Subject: Write custom area content --- .../compiler/output/lang/java/JavaWriter.java | 6 +++ .../compiler/output/lang/java/JavaWriterTests.java | 62 ++++++++++++++++++++++ 2 files changed, 68 insertions(+) (limited to 'src') 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 ff8171a..9f87e0f 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 @@ -106,6 +106,12 @@ public class JavaWriter implements AutoCloseable { this.writer.write("// Custom area start: "); this.writer.write(name); this.writer.newLine(); + + for (var line : javaCustomArea.lines()) { + this.writer.write(line); + this.writer.newLine(); + } + this.writer.write("// Custom area end: "); this.writer.write(name); this.writer.newLine(); 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 5595b62..e13c3dd 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 @@ -72,6 +72,26 @@ public class JavaWriterTests { )); } + @Test + public void classDeclarationWithCustomArea() throws IOException { + var java = new JavaClassDeclaration( + "Point", + List.of(), + new JavaCustomArea(List.of("// ")), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public class Point { + // Custom area start: body + // + // Custom area end: body + }""" + )); + } + @Test public void classDeclarationWithDocComment() throws IOException { var java = new JavaClassDeclaration( @@ -133,6 +153,26 @@ public class JavaWriterTests { )); } + @Test + public void interfaceDeclarationWithCustomArea() throws IOException { + var java = new JavaInterfaceDeclaration( + "Shape", + List.of(), + new JavaCustomArea(List.of("// ")), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public sealed interface Shape { + // Custom area start: body + // + // Custom area end: body + }""" + )); + } + @Test public void interfaceDeclarationWithDocComment() throws IOException { var java = new JavaInterfaceDeclaration( @@ -174,6 +214,28 @@ public class JavaWriterTests { )); } + @Test + public void recordDeclarationWithCustomArea() throws IOException { + var java = new JavaRecordDeclaration( + "Point", + List.of(), + List.of(), + List.of(), + new JavaCustomArea(List.of("// ")), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public record Point() { + // Custom area start: body + // + // Custom area end: body + }""" + )); + } + @Test public void recordDeclarationWithDocComment() throws IOException { var java = new JavaRecordDeclaration( -- cgit v1.2.3