Skip to content

Commit fe9de5e

Browse files
Move depth tracking to DocWriter
1 parent 7fa4974 commit fe9de5e

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/writers/DocWriter.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package software.amazon.smithy.docgen.core.writers;
77

8+
import software.amazon.smithy.codegen.core.CodegenException;
89
import software.amazon.smithy.codegen.core.SymbolWriter;
910
import software.amazon.smithy.model.shapes.Shape;
1011
import software.amazon.smithy.utils.SmithyUnstableApi;
@@ -16,6 +17,10 @@
1617
*/
1718
@SmithyUnstableApi
1819
public abstract class DocWriter extends SymbolWriter<DocWriter, DocImportContainer> {
20+
private static final int MAX_HEADING_DEPTH = 6;
21+
22+
private int headingDepth = 0;
23+
1924
public DocWriter(DocImportContainer importContainer) {
2025
super(importContainer);
2126
}
@@ -47,12 +52,44 @@ public DocWriter(DocImportContainer importContainer) {
4752
* @param content A string to use as the heading content.
4853
* @return returns the writer.
4954
*/
50-
public abstract DocWriter openHeading(String content);
55+
public DocWriter openHeading(String content) {
56+
headingDepth++;
57+
if (headingDepth > MAX_HEADING_DEPTH) {
58+
throw new CodegenException(String.format(
59+
"Tried opening a heading nested more deeply than the max depth of %d.",
60+
MAX_HEADING_DEPTH
61+
));
62+
}
63+
return openHeading(content, headingDepth);
64+
}
5165

5266
/**
53-
* Closes the current heading, cleaning any context created for the current level.
67+
* Writes a heading of a given level with the given content.
5468
*
69+
* <p>{@link #closeHeading} will be called to enable cleaning up any resources or
70+
* context this method creates.
71+
*
72+
* @param content A string to use as the heading content.
73+
* @param level The level of the heading to open. This corresponds to HTML heading
74+
* levels, and will only have values between 1 and 6.
5575
* @return returns the writer.
5676
*/
57-
public abstract DocWriter closeHeading();
77+
abstract DocWriter openHeading(String content, int level);
78+
79+
/**
80+
* Closes the current heading, cleaning any context created for the current level,
81+
* then writes a blank line.
82+
*
83+
* @return returns the writer.
84+
*/
85+
public DocWriter closeHeading() {
86+
headingDepth--;
87+
if (headingDepth < 0) {
88+
throw new CodegenException(
89+
"Attempted to close a heading when at the base heading level."
90+
);
91+
}
92+
write("");
93+
return this;
94+
}
5895
}

smithy-docgen-core/src/main/java/software/amazon/smithy/docgen/core/writers/MarkdownWriter.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55

66
package software.amazon.smithy.docgen.core.writers;
77

8-
import software.amazon.smithy.codegen.core.CodegenException;
98
import software.amazon.smithy.codegen.core.SymbolWriter;
109
import software.amazon.smithy.model.shapes.Shape;
1110
import software.amazon.smithy.model.traits.DocumentationTrait;
1211
import software.amazon.smithy.utils.StringUtils;
1312

1413
public final class MarkdownWriter extends DocWriter {
15-
private static int MAX_HEADING_DEPTH = 5;
16-
17-
private int headerLevel = 0;
1814

1915
public MarkdownWriter() {
2016
super(new DocImportContainer());
@@ -42,28 +38,8 @@ private DocWriter writeWithNewline(Object content, Object... args) {
4238
}
4339

4440
@Override
45-
public DocWriter openHeading(String content) {
46-
headerLevel++;
47-
if (headerLevel > MAX_HEADING_DEPTH) {
48-
throw new CodegenException(String.format(
49-
"Tried opening a heading nested more deeply than the max depth of %d.",
50-
MAX_HEADING_DEPTH
51-
));
52-
}
53-
54-
writeWithNewline(StringUtils.repeat("#", headerLevel) + " " + content);
55-
56-
return this;
57-
}
58-
59-
@Override
60-
public DocWriter closeHeading() {
61-
if (headerLevel <= 0) {
62-
throw new CodegenException("Closed a header that was never opened.");
63-
}
64-
write("");
65-
66-
headerLevel--;
41+
public DocWriter openHeading(String content, int level) {
42+
writeWithNewline(StringUtils.repeat("#", level) + " " + content);
6743
return this;
6844
}
6945
}

0 commit comments

Comments
 (0)