@@ -8,6 +8,7 @@ public class TextContentWriter {
88 private final Appendable buffer ;
99 private final LineBreakRendering lineBreakRendering ;
1010
11+ private final LinkedList <String > prefixes = new LinkedList <>();
1112 private final LinkedList <Boolean > tight = new LinkedList <>();
1213
1314 private String blockSeparator = null ;
@@ -36,6 +37,7 @@ public void colon() {
3637
3738 public void line () {
3839 append ('\n' );
40+ writePrefixes ();
3941 }
4042
4143 public void block () {
@@ -61,6 +63,32 @@ public void write(char c) {
6163 append (c );
6264 }
6365
66+ /**
67+ * Push a prefix onto the top of the stack. All prefixes are written at the beginning of each line, until the
68+ * prefix is popped again.
69+ *
70+ * @param prefix the raw prefix string
71+ */
72+ public void pushPrefix (String prefix ) {
73+ prefixes .addLast (prefix );
74+ }
75+
76+ /**
77+ * Write a prefix.
78+ *
79+ * @param prefix the raw prefix string to write
80+ */
81+ public void writePrefix (String prefix ) {
82+ write (prefix );
83+ }
84+
85+ /**
86+ * Remove the last prefix from the top of the stack.
87+ */
88+ public void popPrefix () {
89+ prefixes .removeLast ();
90+ }
91+
6492 /**
6593 * Change whether blocks are tight or loose. Loose is the default where blocks are separated by a blank line. Tight
6694 * is where blocks are not separated by a blank line. Tight blocks are used in lists, if there are no blank lines
@@ -84,12 +112,26 @@ private boolean isTight() {
84112 return !tight .isEmpty () && tight .getLast ();
85113 }
86114
115+ private void writePrefixes () {
116+ for (String prefix : prefixes ) {
117+ append (prefix );
118+ }
119+ }
120+
87121 /**
88122 * If a block separator has been enqueued with {@link #block()} but not yet written, write it now.
89123 */
90124 private void flushBlockSeparator () {
91125 if (blockSeparator != null ) {
92- append (blockSeparator );
126+ if (blockSeparator .equals ("\n " ) || blockSeparator .equals ("\n \n " )) {
127+ for (int i = 0 ; i < blockSeparator .length (); i ++) {
128+ var sep = blockSeparator .charAt (i );
129+ append (sep );
130+ writePrefixes ();
131+ }
132+ } else {
133+ append (blockSeparator );
134+ }
93135 blockSeparator = null ;
94136 }
95137 }
0 commit comments