You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/12/12 20:43:27 UTC

[isis] branch master updated: ISIS-2473: suppress empty continuation blocks from being written

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new b19bb5f  ISIS-2473: suppress empty continuation blocks from being written
b19bb5f is described below

commit b19bb5f0d97f92bed6ae85a76870b3520f57dd0f
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sat Dec 12 21:43:12 2020 +0100

    ISIS-2473: suppress empty continuation blocks from being written
---
 .../apache/isis/tooling/model4adoc/NodeWriter.java | 42 +++++++++++++++++++---
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/tooling/model4adoc/src/main/java/org/apache/isis/tooling/model4adoc/NodeWriter.java b/tooling/model4adoc/src/main/java/org/apache/isis/tooling/model4adoc/NodeWriter.java
index 3bb34f8..e877a27 100644
--- a/tooling/model4adoc/src/main/java/org/apache/isis/tooling/model4adoc/NodeWriter.java
+++ b/tooling/model4adoc/src/main/java/org/apache/isis/tooling/model4adoc/NodeWriter.java
@@ -18,6 +18,7 @@
  */
 package org.apache.isis.tooling.model4adoc;
 
+import java.io.StringWriter;
 import java.io.Writer;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -123,6 +124,7 @@ final class NodeWriter implements StructuralNodeVisitor {
         val style = Style.parse(block);        
 
         if(style.isOpenBlock()){
+            pushNewWriter(); // write the open block to a StringWriter, such that can handle empty blocks
             println("+");
             println("--");
             isContinuation = true; // set continuation flag, so other blocks don't add newlines
@@ -157,6 +159,7 @@ final class NodeWriter implements StructuralNodeVisitor {
 
         if(style.isOpenBlock()){
             println("--");
+            popWriter();
             bulletCount = bulletCountStack.pop();
         } else if(style.isAdmonition()){
             if(block.getBlocks().size()>0) {
@@ -335,6 +338,37 @@ final class NodeWriter implements StructuralNodeVisitor {
     private boolean isContinuation = false;
     private Stack<Integer> bulletCountStack = new Stack<>();
 
+    
+    // -- EMPTY CONTINUATION BLOCK HANDLING
+
+    private final static int EMPTY_CONTINUATION_BLOCK_SIZE = 8;
+    
+    private Stack<StringWriter> stringWriterStack = new Stack<>();
+    
+    private Writer currentWriter;
+    private Writer currentWriter() {
+        if(currentWriter == null) {
+            this.currentWriter = writer;
+        }
+        return currentWriter;
+    }
+    private void pushNewWriter() {
+        val sw = new StringWriter();
+        currentWriter = sw;
+        stringWriterStack.push(sw);
+    }
+    @SneakyThrows
+    private void popWriter() {
+        val sw = stringWriterStack.pop();
+        currentWriter = stringWriterStack.isEmpty()
+                ? writer
+                : stringWriterStack.peek();
+        val continuationBlockAsString = sw.toString();
+        if(continuationBlockAsString.length()>EMPTY_CONTINUATION_BLOCK_SIZE) {
+            writer.append(continuationBlockAsString); // write directly to the master writer, no side-effects wanted    
+        }
+    }
+
     // -- PRINTING
 
     @SneakyThrows
@@ -342,7 +376,7 @@ final class NodeWriter implements StructuralNodeVisitor {
         if(!hasWrittenAnythingYet) {
             return;
         }
-        writer.append("\n");
+        currentWriter().append("\n");
         newLineCount++;
     }
 
@@ -353,21 +387,21 @@ final class NodeWriter implements StructuralNodeVisitor {
             val lineIter = _Text.normalize(_Text.getLines(line)).iterator();
             while(lineIter.hasNext()) {
                 val nextLine = lineIter.next(); 
-                writer.append(nextLine);
+                currentWriter().append(nextLine);
                 if(!nextLine.isEmpty()) {
                     hasWrittenAnythingYet = true;
                     isContinuation = false; // clear continuation flag
                     newLineCount = 0;
                 }
                 if(lineIter.hasNext()) {
-                    writer.append("\n");
+                    currentWriter().append("\n");
                     newLineCount++;
                 }
             }
             return;
         }
         if(!line.isEmpty()) {
-            writer.append(line);
+            currentWriter().append(line);
             hasWrittenAnythingYet = true;
             isContinuation = false; // clear continuation flag
             newLineCount = 0;