From 130a96efa74f90c9d2f8634e2ddc6d6c705f146d Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Thu, 27 Aug 2020 11:22:40 +0200
Subject: [PATCH] Printing without deleting newlines.

---
 Mustache.relast |  2 +-
 Navigation.jrag |  6 ++++++
 Printing.jrag   | 27 +++++++++++++++++----------
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/Mustache.relast b/Mustache.relast
index 98b475b..4ffd34a 100644
--- a/Mustache.relast
+++ b/Mustache.relast
@@ -2,7 +2,7 @@ Document ::= <FileName> [RootElement:ComplexElement] ;
 abstract Element ;
 abstract ComplexElement : Element ;
 MappingElement : ComplexElement ::= KeyValuePair* ;
-KeyValuePair : ComplexElement ::= <Key> Value:Element ;
+KeyValuePair ::= <Key> Value:Element ;
 ListElement : ComplexElement ::= Element* ;
 abstract SimpleElement : Element ;
 ValueElement : SimpleElement ::= <Value> ;
diff --git a/Navigation.jrag b/Navigation.jrag
index 5ad8611..4d9f923 100644
--- a/Navigation.jrag
+++ b/Navigation.jrag
@@ -5,4 +5,10 @@ aspect Navigation {
   syn boolean Element.isEmpty() = false;
   eq MappingElement.isEmpty() = getNumKeyValuePair() == 0;
   eq ListElement.isEmpty() = getNumElement() == 0;
+
+  inh ListElement Element.containingListElement();
+  eq ListElement.getElement().containingListElement() = this;
+  eq KeyValuePair.getValue().containingListElement() = null;
+  eq Document.getRootElement().containingListElement() = null;
 }
+
diff --git a/Printing.jrag b/Printing.jrag
index 8da958d..856e42a 100644
--- a/Printing.jrag
+++ b/Printing.jrag
@@ -1,6 +1,14 @@
 aspect Printing {
   String ASTNode.PRINT_INDENT = "  ";
 
+  inh boolean KeyValuePair.isLast();
+  inh boolean Element.isLast();
+  eq MappingElement.getKeyValuePair(int i).isLast() = i == getNumKeyValuePair() - 1;
+  eq ListElement.getElement(int i).isLast() = i == getNumElement() - 1;
+  eq Document.getRootElement().isLast() = true;
+  syn boolean KeyValuePair.needTrailingNewLine() = !this.isLast();
+  syn boolean Element.needTrailingNewLine() = !this.isLast() || containingListElement() == null;
+
   public String Document.prettyPrint() {
     return prettyPrint(true);
   }
@@ -8,7 +16,7 @@ aspect Printing {
   public String Document.prettyPrint(boolean prependCreationComment) {
     StringBuilder sb = new StringBuilder();
     if (prependCreationComment) {
-      sb.append("# RagConnect created at ").append(java.time.Instant.now()).append("\n");
+      sb.append("# Created at ").append(java.time.Instant.now()).append("\n");
     }
     if (hasRootElement()) {
       getRootElement().prettyPrint(sb, false, "");
@@ -41,21 +49,20 @@ aspect Printing {
       for (Element element : getElementList()) {
         sb.append(indent).append("- ");
         element.prettyPrint(sb, false, indent + PRINT_INDENT);
-        sb.append("\n");
+        if (element.needTrailingNewLine()) {
+          sb.append("\n");
+        }
       }
-      // delete last newline
-      sb.deleteCharAt(sb.length() - 1);
     }
     return sb;
   }
 
-  @Override
   protected StringBuilder KeyValuePair.prettyPrint(StringBuilder sb, boolean printIndent, String indent) {
     if (printIndent) sb.append(indent);
     sb.append(getKey()).append(":");
     if (getValue().isComplex() && !getValue().isEmpty()) {
       sb.append("\n");
-      getValue().prettyPrint(sb, true, indent + PRINT_INDENT);  //);
+      getValue().prettyPrint(sb, true, indent + PRINT_INDENT);
     } else {
       sb.append(" ");
       getValue().prettyPrint(sb, false, indent);
@@ -72,11 +79,11 @@ aspect Printing {
       for (KeyValuePair pair : getKeyValuePairList()) {
         if (!first || printIndent) sb.append(indent);
         first = false;
-        pair.prettyPrint(sb, false, indent);  // + PRINT_INDENT
-        sb.append("\n");
+        pair.prettyPrint(sb, false, indent);
+        if (pair.needTrailingNewLine()) {
+          sb.append("\n");
+        }
       }
-      // delete last newline
-      sb.deleteCharAt(sb.length() - 1);
     }
     return sb;
   }
-- 
GitLab