From fdf837b4b3baffc65e429c2e6ecc2e764bfed0ac Mon Sep 17 00:00:00 2001
From: David Robillard <d@drobilla.net>
Date: Thu, 12 Aug 2021 23:38:52 -0400
Subject: Fix TriG graph indentation

---
 src/writer.c                 | 15 ++++++++++-----
 test/pretty/manifest.ttl     | 12 ++++++++++++
 test/pretty/named-graph.trig |  8 ++++++++
 test/run_pretty_suite.py     | 11 +++++++----
 4 files changed, 37 insertions(+), 9 deletions(-)
 create mode 100644 test/pretty/named-graph.trig

diff --git a/src/writer.c b/src/writer.c
index 3c7a510a..30e09a4f 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -114,7 +114,7 @@ static const SepRule rules[] = {
   {"(", 1, +1, SEP_NONE, SEP_NONE, SEP_NONE},
   {"", 0, +0, SEP_ALL, SEP_NONE, SEP_NONE},
   {")", 1, -1, SEP_NONE, SEP_NONE, SEP_NONE},
-  {"{", 1, +1, SEP_ALL, SEP_NONE, SEP_NONE},
+  {"{", 1, +1, SEP_ALL, SEP_NONE, SEP_ALL},
   {"}", 1, -1, SEP_NONE, SEP_NONE, SEP_ALL},
 };
 
@@ -707,14 +707,14 @@ write_sep(SerdWriter* writer, const SerdStatementFlags flags, Sep sep)
 }
 
 static void
-reset_context(SerdWriter* writer, bool graph)
+reset_context(SerdWriter* writer, const bool including_graph)
 {
   // Free any lingering contexts in case there was an error
   while (writer->anon_stack_size > 0) {
     pop_context(writer);
   }
 
-  if (graph && writer->context.graph) {
+  if (including_graph && writer->context.graph) {
     memset(writer->context.graph, 0, sizeof(SerdNode));
   }
 
@@ -728,8 +728,11 @@ reset_context(SerdWriter* writer, bool graph)
 
   writer->anon_stack_size         = 0;
   writer->context.indented_object = false;
-  writer->indent                  = 0;
   writer->empty                   = false;
+
+  if (including_graph) {
+    writer->indent = 0;
+  }
 }
 
 static bool
@@ -1123,7 +1126,9 @@ write_turtle_trig_statement(SerdWriter* const        writer,
     }
 
     // Write new subject
-    TRY(st, write_top_level_sep(writer));
+    if (!ctx(writer, SERD_GRAPH)) {
+      TRY(st, write_top_level_sep(writer));
+    }
     reset_context(writer, false);
     serd_node_set(&writer->context.subject, subject);
     TRY(st, write_node(writer, subject, SERD_SUBJECT, flags));
diff --git a/test/pretty/manifest.ttl b/test/pretty/manifest.ttl
index faae5857..b28f4e68 100644
--- a/test/pretty/manifest.ttl
+++ b/test/pretty/manifest.ttl
@@ -21,6 +21,7 @@
 		<#list-subject-with-list-extras>
 		<#list-subject>
 		<#many-objects>
+		<#named-graph>
 		<#nested-list-object-with-empty-lists>
 		<#nested-list-object>
 		<#nested-list-subject>
@@ -96,6 +97,11 @@
 	mf:action <many-objects.ttl> ;
 	mf:name "many-objects" .
 
+<#named-graph>
+	a serd:TestTrigPrint ;
+	mf:action <named-graph.trig> ;
+	mf:name "named-graph" .
+
 <#nested-list-object>
 	a serd:TestTurtlePrint ;
 	mf:action <nested-list-object.ttl> ;
@@ -111,6 +117,12 @@
 	mf:action <nested-list-subject.ttl> ;
 	mf:name "nested-list-subject" .
 
+serd:TestTrigPrint
+	a rdfs:Class ;
+	rdfs:comment "Tests that a TriG document pretty-prints exactly as written." ;
+	rdfs:label "TriG Pretty-Printing" ;
+	rdfs:subClassOf rdft:Test .
+
 serd:TestTurtlePrint
 	a rdfs:Class ;
 	rdfs:comment "Tests that a Turtle document pretty-prints exactly as written." ;
diff --git a/test/pretty/named-graph.trig b/test/pretty/named-graph.trig
new file mode 100644
index 00000000..5cd12f3b
--- /dev/null
+++ b/test/pretty/named-graph.trig
@@ -0,0 +1,8 @@
+@prefix eg: <http://example.org/> .
+
+eg:g {
+	eg:s
+		eg:p [
+			a eg:Object
+		] .
+}
diff --git a/test/run_pretty_suite.py b/test/run_pretty_suite.py
index a17dd0f8..52686555 100755
--- a/test/run_pretty_suite.py
+++ b/test/run_pretty_suite.py
@@ -53,7 +53,7 @@ def test_suite(
             self.n_tests = 0
             self.n_failures = 0
 
-    def run_test(entry, results):
+    def run_test(entry, syntax, results):
         """Run a single test entry from the manifest."""
 
         input_uri = model[entry][mf + "action"][0]
@@ -65,7 +65,7 @@ def test_suite(
             "-B",
             base_uri,
             "-O",
-            "turtle",
+            syntax,
             "-o",
             output_path,
             input_path,
@@ -92,9 +92,12 @@ def test_suite(
     # Run all test types in the test suite
     results = Results()
     for klass, instances in instances.items():
-        if klass == "http://drobilla.net/ns/serd#TestTurtlePrint":
+        if klass == "http://drobilla.net/ns/serd#TestTrigPrint":
             for entry in instances:
-                run_test(entry, results)
+                run_test(entry, "TriG", results)
+        elif klass == "http://drobilla.net/ns/serd#TestTurtlePrint":
+            for entry in instances:
+                run_test(entry, "Turtle", results)
 
     # Print result summary
     if results.n_failures > 0:
-- 
cgit v1.2.1