Commit 7ce28dd6cc75d9294d3ef645c2f99d98f003ccbb

Thomas de Grivel 2024-08-09T15:17:45

buf: base_column for block indentation

diff --git a/libkc3/buf.c b/libkc3/buf.c
index fae88cc..031fc32 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -1131,14 +1131,14 @@ sw buf_write (s_buf *buf, const void *data, uw len)
 {
   s_str str;
   str_init(&str, NULL, len, data);
-  return buf_write_str(buf, &str);
+  return buf_write_str_memcpy(buf, &str);
 }
 
 sw buf_write_1 (s_buf *buf, const char *p)
 {
-  s_str stra;
-  str_init_1(&stra, NULL, p);
-  return buf_write_str(buf, &stra);
+  s_str str;
+  str_init_1(&str, NULL, p);
+  return buf_write_str(buf, &str);
 }
 
 sw buf_write_character_utf8 (s_buf *buf, character c)
@@ -1248,6 +1248,34 @@ sw buf_write_s64 (s_buf *buf, s64 v)
 
 sw buf_write_str (s_buf *buf, const s_str *src)
 {
+  character c;
+  uw i;
+  sw r;
+  sw result = 0;
+  s_str s;
+  assert(buf);
+  assert(src);
+  s = *src;
+  while ((r = str_read_character_utf8(&s, &c)) > 0) {
+    if ((r = buf_write_character_utf8(buf, c)) < 0)
+      return r;
+    result += r;
+    if (c == '\n') {
+      i = 0;
+      while (i < buf->base_column) {
+        if ((r = buf_write_u8(buf, ' ')) < 0)
+          return r;
+        result += r;
+        i++;
+      }
+    }
+  }
+  buf_flush(buf);
+  return result;
+}
+
+sw buf_write_str_memcpy (s_buf *buf, const s_str *src)
+{
   sw r;
   assert(buf);
   assert(src);
diff --git a/libkc3/buf.h b/libkc3/buf.h
index d02980d..baaa363 100644
--- a/libkc3/buf.h
+++ b/libkc3/buf.h
@@ -109,6 +109,7 @@ sw      buf_write_s8 (s_buf *buf, s8 i);
 sw      buf_write_s16 (s_buf *buf, s16 i);
 sw      buf_write_s32 (s_buf *buf, s32 i);
 sw      buf_write_s64 (s_buf *buf, s64 i);
+sw      buf_write_str_memcpy (s_buf *buf, const s_str *src);
 sw      buf_write_str (s_buf *buf, const s_str *src);
 sw      buf_write_u8 (s_buf *buf, u8 i);
 sw      buf_write_u16 (s_buf *buf, u16 i);
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index 6047c4f..512abc7 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -244,6 +244,7 @@ sw buf_inspect_array_size (const s_array *array)
 
 sw buf_inspect_block (s_buf *buf, const s_block *block)
 {
+  uw base_column;
   u64 i = 0;
   sw r;
   sw result = 0;
@@ -253,12 +254,14 @@ sw buf_inspect_block (s_buf *buf, const s_block *block)
     else
       return buf_write_1(buf, "do end");
   }
+  base_column = buf->base_column;
+  buf->base_column += 2;
   if (block->short_form) {    
     if ((r = buf_write_1(buf, "{ ")) < 0)
       return r;
   }
   else {
-    if ((r = buf_write_1(buf, "do\n  ")) < 0)
+    if ((r = buf_write_1(buf, "do\n")) < 0)
       return r;
   }
   result += r;
@@ -271,7 +274,7 @@ sw buf_inspect_block (s_buf *buf, const s_block *block)
         return r;
     }
     else {
-      if ((r = buf_write_1(buf, "\n  ")) < 0)
+      if ((r = buf_write_1(buf, "\n")) < 0)
         return r;
     }
     result += r;
@@ -280,6 +283,7 @@ sw buf_inspect_block (s_buf *buf, const s_block *block)
   if ((r = buf_inspect_tag(buf, block->tag + i)) < 0)
     return r;
   result += r;
+  buf->base_column = base_column;
   if (block->short_form) {
     if ((r = buf_write_1(buf, " }")) < 0)
       return r;
diff --git a/libkc3/types.h b/libkc3/types.h
index b3f87f6..0131386 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -349,6 +349,7 @@ struct var {
 /* 2 */
 
 struct buf {
+  uw          base_column;
   sw          column;
   sw        (*flush) (s_buf *buf);
   bool        free;
diff --git a/test/ikc3/def.out.expected b/test/ikc3/def.out.expected
index 6997c75..79d477b 100644
--- a/test/ikc3/def.out.expected
+++ b/test/ikc3/def.out.expected
@@ -16,17 +16,17 @@ double(zero)
 0
 def double_tuple = macro (x) do
   quote do
-  x = ^ unquote(x)
-  {x, x}
-end
+    x = ^ unquote(x)
+    {x, x}
+  end
 end
 double_tuple
 double_tuple
 macro (x) do
   quote do
-  x = ^ unquote(x)
-  {x, x}
-end
+    x = ^ unquote(x)
+    {x, x}
+  end
 end
 double_tuple(200)
 {200, 200}