diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-30 08:50:17 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-30 08:50:17 +0100 |
| commit | 065958bb0bbe55a40a1c3df0748bb0b1625f679f (patch) | |
| tree | 67210219fe2fdf364dec35a5f2e34d21d99d183b /src/main/java | |
| parent | b6976dcf7696daf01d4a5b698617fc190dfc8d65 (diff) | |
Write doc comments for structs
Diffstat (limited to 'src/main/java')
4 files changed, 43 insertions, 10 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/DocComment.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/DocComment.java index f7a8ac2..5b9090f 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/DocComment.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/DocComment.java @@ -6,4 +6,8 @@ public record DocComment(String value) { public Iterable<String> lines() { return () -> value.lines().iterator(); } + + public boolean isEmpty() { + return value.isEmpty(); + } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java index a6263bf..878824a 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/typescripttypes/TypescriptTypesGenerator.java @@ -106,7 +106,8 @@ public class TypescriptTypesGenerator implements Generator { private TypescriptStatement generateStructDefinition(TypedStructDefinitionNode structDefinition) { return new TypescriptClassDeclaration( this.typescriptGenerator.generateTypeName(structDefinition.name()), - List.of() + List.of(), + structDefinition.docComment() ); } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java index c553b2b..851311e 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriter.java @@ -1,5 +1,6 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript; +import org.zwobble.hobgoblin.compiler.ast.DocComment; import org.zwobble.hobgoblin.compiler.output.CodeWriter; import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.*; @@ -74,6 +75,7 @@ public class TypescriptWriter implements AutoCloseable { } private void writeClassDeclaration(TypescriptClassDeclaration classDeclaration) throws IOException { + this.writeDocComment(classDeclaration.docComment()); this.writer.write("class "); this.writeIdentifier(classDeclaration.name()); this.writer.write(" {"); @@ -133,6 +135,26 @@ public class TypescriptWriter implements AutoCloseable { this.writer.write(Double.toString(numberLiteral.value())); } + // == Doc comments == + + private void writeDocComment(DocComment docComment) throws IOException { + if (docComment.isEmpty()) { + return; + } + + this.writer.write("/**"); + this.writer.newLine(); + + for (var line : docComment.lines()) { + this.writer.write(" * "); + this.writer.write(line); + this.writer.newLine(); + } + + this.writer.write(" */"); + this.writer.newLine(); + } + // == Identifiers == private void writeIdentifier(TypescriptIdentifier name) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java index 2b5a951..d509828 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassDeclaration.java @@ -7,42 +7,48 @@ package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; public record TypescriptClassDeclaration( org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, - java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> body + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> body, + org.zwobble.hobgoblin.compiler.ast.DocComment docComment ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement { public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), java.util.List.of()); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.arbitrary().build(), java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.HobgoblinNativeAst.arbitraryDocComment()); } public record Builder( org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name, - java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> body + java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> body, + org.zwobble.hobgoblin.compiler.ast.DocComment docComment ) implements org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStatement.Builder { public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration build() { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration(name, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier name) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier.Builder name) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name.build(), body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name.build(), body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withBody(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement> body) { - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement bodyElement) { var body = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement>(this.body); body.add(bodyElement); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); } public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder addBodyElement(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement.Builder bodyElement) { var body = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassElement>(this.body); body.add(bodyElement.build()); - return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder(name, body, docComment); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassDeclaration.Builder body |
