Skip to content

Commit

Permalink
Simplify & make DocXmlWriter more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
Swimburger committed Oct 4, 2024
1 parent 669164a commit 8f8e20c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
4 changes: 3 additions & 1 deletion generators/csharp/codegen/src/ast/Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClassInstantiation } from "./ClassInstantiation";
import { ClassReference } from "./ClassReference";
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
import { DocXmlWriter } from "./core/DocXmlWriter";
import { Writer } from "./core/Writer";
import { Field } from "./Field";
import { Interface } from "./Interface";
Expand Down Expand Up @@ -168,7 +169,8 @@ export class Class extends AstNode {
}

if (this.summary != null) {
writer.writeDocXml().writeNodeWithEscaping("summary", this.summary);
const docXmlWriter = new DocXmlWriter(writer);
docXmlWriter.writeNodeWithEscaping("summary", this.summary);
}
if (this.annotations.length > 0) {
writer.write("[");
Expand Down
4 changes: 3 additions & 1 deletion generators/csharp/codegen/src/ast/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Annotation } from "./Annotation";
import { ClassReference } from "./ClassReference";
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
import { DocXmlWriter } from "./core/DocXmlWriter";
import { Writer } from "./core/Writer";
import { Type } from "./Type";

Expand Down Expand Up @@ -108,7 +109,8 @@ export class Field extends AstNode {

public write(writer: Writer): void {
if (this.summary != null) {
writer.writeDocXml().writeNodeWithEscaping("summary", this.summary);
const docXmlWriter = new DocXmlWriter(writer);
docXmlWriter.writeNodeWithEscaping("summary", this.summary);
}

if (this.annotations.length > 0) {
Expand Down
16 changes: 8 additions & 8 deletions generators/csharp/codegen/src/ast/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Annotation } from "./Annotation";
import { ClassReference } from "./ClassReference";
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
import { DocXmlWriter } from "./core/DocXmlWriter";
import { Writer } from "./core/Writer";
import { Parameter } from "./Parameter";
import { Type } from "./Type";
Expand Down Expand Up @@ -89,17 +90,16 @@ export class Method extends AstNode {
}

public write(writer: Writer): void {
const docXmlWriter = new DocXmlWriter(writer);
if (this.summary != null) {
writer.writeDocXml().writeNodeWithEscaping("summary", this.summary);
docXmlWriter.writeNodeWithEscaping("summary", this.summary);
}
if (this.codeExample != null) {
writer
.writeDocXml()
.writeOpenNode("example")
.writeOpenNode("code")
.writeMultilineWithEscaping(this.codeExample)
.writeCloseNode("code")
.writeCloseNode("example");
docXmlWriter.writeOpenNode("example");
docXmlWriter.writeOpenNode("code");
docXmlWriter.writeMultilineWithEscaping(this.codeExample);
docXmlWriter.writeCloseNode("code");
docXmlWriter.writeCloseNode("example");
}

if (this.annotations.length > 0) {
Expand Down
21 changes: 7 additions & 14 deletions generators/csharp/codegen/src/ast/core/DocXmlWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,46 @@ export class DocXmlWriter {
this.writer = writer;
}

public writeLine(text?: string): this {
public writeLine(text?: string): void {
this.writer.writeLine(`/// ${text}`);
return this;
}

public writePrefix(): this {
this.writer.write("/// ");
return this;
}

public writeOpenNode(nodeName: string): this {
public writeOpenNode(nodeName: string): void {
this.writeLine(`<${nodeName}>`);
return this;
}

public writeCloseNode(nodeName: string): this {
public writeCloseNode(nodeName: string): void {
this.writeLine(`</${nodeName}>`);
return this;
}

public writeNode(nodeName: string, text: string): this {
public writeNode(nodeName: string, text: string): void {
this.writeOpenNode(nodeName);
this.writeMultiline(text);
this.writeCloseNode(nodeName);
return this;
}

public writeNodeWithEscaping(nodeName: string, text: string): this {
public writeNodeWithEscaping(nodeName: string, text: string): void {
this.writeOpenNode(nodeName);
this.writeMultilineWithEscaping(text);
this.writeCloseNode(nodeName);
return this;
}

public writeMultiline(text: string): this {
public writeMultiline(text: string): void {
text.trim()
.split("\n")
.forEach((line) => {
this.writeLine(line);
});
return this;
}

public writeMultilineWithEscaping(text: string): this {
public writeMultilineWithEscaping(text: string): void {
text = this.escapeXmlDocContent(text);
this.writeMultiline(text);
return this;
}

private escapeXmlDocContent(text: string): string {
Expand Down
8 changes: 0 additions & 8 deletions generators/csharp/codegen/src/ast/core/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ export class Writer extends AbstractWriter {
return this.customConfig["simplify-object-dictionaries"] ?? true;
}

public writeDocXml(write?: (writer: DocXmlWriter) => void): DocXmlWriter {
const writer = new DocXmlWriter(this);
if (write) {
write(writer);
}
return writer;
}

public toString(skipImports = false): string {
if (!skipImports) {
const imports = this.stringifyImports();
Expand Down

0 comments on commit 8f8e20c

Please sign in to comment.