]> git.mdlowis.com Git - projs/tide.git/commitdiff
fixed bug in context selection
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 9 Oct 2019 03:03:53 +0000 (23:03 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 9 Oct 2019 03:03:53 +0000 (23:03 -0400)
Rsconscript
TODO.md
alcc
src/lib/buf.c
tests/lib/buf.c

index 5896af31ea6af294754c47a12753652c75e13eec..4e8ba3f3b83d5ccabd5e63c2995aa8242e8f43c1 100644 (file)
@@ -11,12 +11,18 @@ build do
   env = Environment.new
   env["prefix"] = ENV["PREFIX"] || env["prefix"]
   env["CC"] = "./alcc"
-#  env["CFLAGS"] += ["-g", "-fsanitize=undefined,address"]
-#  env["LDFLAGS"] += ["-g", "-fsanitize=undefined,address"]
   env["CPPPATH"] += %w[. inc]
   env["LIBPATH"] += %w[.]
   env["CFLAGS"] << "-DNDEBUG"
 
+#  #  Enable Sanitizers
+#  env["CFLAGS"] += ["-g", "-fsanitize=undefined,address"]
+#  env["LDFLAGS"] += ["-g", "-fsanitize=undefined,address"]
+
+#  #  Enable Coverage
+#  env["CFLAGS"] += ["-g", "-O0", "--coverage"]
+#  env["LDFLAGS"] += ["-g", "-O0", "--coverage"]
+
   # Build library and binaries
   env.Library("libtide.a", glob("src/lib/**/*.c"))
   (glob("src/*.c") - ["src/term.c", "src/tsed.c"]).each do |src|
diff --git a/TODO.md b/TODO.md
index 1446e0fa6d76d987aa030977df0a5b7a1ef9217c..8cf88a2bfacd2926a8376aa9ec75869318cf31b1 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -4,10 +4,11 @@
 
 ## STAGING
 
+* tide: column tracking should be hidden in buf.c
 * tide: buf_findstr has an infinite loop condition
 * tide: refactor byword, byline, etc api
 * tide: refactor undo/redo api in view.c
-* all: eliminate multiple return statements
+* all: eliminate multiple return statements and other lint
 
 ## BACKLOG
 
diff --git a/alcc b/alcc
index c226c0b0b1aad1310453dc0fe968497eaa7b88c0..e3b061967f39c8b9fdf8c77013032f7ae89afccd 100755 (executable)
--- a/alcc
+++ b/alcc
@@ -264,7 +264,7 @@ script=$(cat <<EOS
         {
             error("reached EOF with open block");
         }
-        exit code
+#        exit code
     }
 EOS
 )
index cf418a43b60b005c6bc7bf3f62a72e4e00323456..292919f8cc9cb0a9a679572b49d94969b4a6112e 100644 (file)
@@ -661,7 +661,7 @@ static void selline(Buf* buf)
 static void selblock(Buf* buf, Rune first, Rune last)
 {
     Sel sel = selget(buf);
-    int balance = 0, dir;
+    int balance = 0, dir = 0;
     size_t beg, end = sel.end;
 
     /* figure out which end of the block we're starting at */
@@ -675,44 +675,47 @@ static void selblock(Buf* buf, Rune first, Rune last)
     }
     else
     {
-        return;
+        /* do nothing */
     }
 
-    /* scan for a blanced set of braces */
-    while (true)
+    if (dir != 0)
     {
-        if (buf_getrat(buf, end) == first)
+        /* scan for a blanced set of braces */
+        while (true)
         {
-            balance++;
-        }
-        else if (buf_getrat(buf, end) == last)
-        {
-            balance--;
-        }
+            if (buf_getrat(buf, end) == first)
+            {
+                balance++;
+            }
+            else if (buf_getrat(buf, end) == last)
+            {
+                balance--;
+            }
 
-        if (balance == 0 || end >= buf_end(buf) || end == 0)
-        {
-            break;
-        }
-        else
-        {
-            end += dir;
+            if (balance == 0 || end >= buf_end(buf) || end == 0)
+            {
+                break;
+            }
+            else
+            {
+                end += dir;
+            }
         }
-    }
 
-    /* update the passed in selection if we found a block */
-    if (balance)
-    {
-        if (end > beg)
+        /* update the selection if we found a block */
+        if (balance == 0)
         {
-            beg++;
-        }
-        else
-        {
-            end++;
+            if (end > beg)
+            {
+                beg++;
+            }
+            else
+            {
+                end++;
+            }
+            buf->selection.beg = beg;
+            buf->selection.end = end;
         }
-        buf->selection.beg = beg;
-        buf->selection.end = end;
     }
 }
 
@@ -842,7 +845,7 @@ void buf_selctx(Buf* buf, bool (*isword)(Rune))
     {
         ; /* condition performs selection */
     }
-    else if (risword(curr))
+    else if (isword(curr))
     {
         buf_selword(buf, isword);
     }
@@ -1105,12 +1108,8 @@ char* buf_fetch(Buf* buf, bool (*isword)(Rune), size_t off)
     {
         buf->selection = (Sel){ .beg = off, .end = off };
         buf_selword(buf, isword);
-        str = buf_gets(buf);
-    }
-    else
-    {
-        str = buf_gets(buf);
     }
+    str = buf_gets(buf);
     buf->selection = prev;
     return str;
 }
index c88917360b832d1d0bf69f5978b4aaf82f678d06..b36836f3ed7a985900900898d84b14d3b9d92bed 100644 (file)
@@ -257,9 +257,23 @@ TEST_SUITE(BufferTests)
         CHECK(TestBuf.status != MODIFIED);
     }
 
+    /* Basic Operation
+     *************************************************************************/
+    TEST(buffer should grow to fit data)
+    {
+        size_t datasz = 2*1024*1024;
+        char* data = malloc(datasz);
+        memset(data, '.', datasz);
+        data[datasz - 1] = '\0';
+        buf_init(&TestBuf);
+        buf_puts(&TestBuf, data);
+        int result = buffer_equals(data);
+        free(data);
+        CHECK(result);
+    }
+
     /* Movements
      *************************************************************************/
-    // Start of Line
     TEST(buf_bol should return 0 if column 1 of first line)
     {
         set_buffer_text("ab\ncd");
@@ -345,4 +359,201 @@ TEST_SUITE(BufferTests)
 //        printf("%lu\n", TestBuf.selection.end);
 //        CHECK(8 == TestBuf.selection.end);
 //    }
+
+    /* Context-Sensitive Selection
+     *************************************************************************/
+    TEST(buf_selctx should select from ( to ))
+    {
+        set_buffer_text("(...)");
+        TestBuf.selection = (Sel){ .beg = 0, .end = 0 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select from < to >)
+    {
+        set_buffer_text("<...>");
+        TestBuf.selection = (Sel){ .beg = 0, .end = 0 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select from [ to ])
+    {
+        set_buffer_text("[...]");
+        TestBuf.selection = (Sel){ .beg = 0, .end = 0 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select from { to })
+    {
+        set_buffer_text("{...}");
+        TestBuf.selection = (Sel){ .beg = 0, .end = 0 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should do nothing for unbalanced block)
+    {
+        set_buffer_text("...}{...");
+        TestBuf.selection = (Sel){ .beg = 3, .end = 3 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(3 == buf_selbeg(&TestBuf));
+        CHECK(3 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(4 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select '"' quotes)
+    {
+        set_buffer_text(" \"...\" ");
+        TestBuf.selection = (Sel){ .beg = 1, .end = 1 }; // opening quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 2, .end = 2 }; // first char of quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 }; // last char of quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select '`' quotes)
+    {
+        set_buffer_text(" `...` ");
+        TestBuf.selection = (Sel){ .beg = 1, .end = 1 }; // opening quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 2, .end = 2 }; // first char of quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 }; // last char of quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select '\'' quotes)
+    {
+        set_buffer_text(" '...' ");
+        TestBuf.selection = (Sel){ .beg = 1, .end = 1 }; // opening quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 2, .end = 2 }; // first char of quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 }; // last char of quote
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select word using risword)
+    {
+        set_buffer_text(" abc/def ");
+        TestBuf.selection = (Sel){ .beg = 1, .end = 1 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(4 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 5, .end = 5 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(5 == buf_selbeg(&TestBuf));
+        CHECK(8 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select word using risbigword)
+    {
+        set_buffer_text(" abc/def ");
+        TestBuf.selection = (Sel){ .beg = 1, .end = 1 };
+        buf_selctx(&TestBuf, risbigword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(8 == buf_selend(&TestBuf));
+        TestBuf.selection = (Sel){ .beg = 5, .end = 5 };
+        buf_selctx(&TestBuf, risbigword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(8 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select big word on non word character)
+    {
+        set_buffer_text(" abc/def ");
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(1 == buf_selbeg(&TestBuf));
+        CHECK(8 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select line on bol)
+    {
+        set_buffer_text("\n...\n");
+        TestBuf.selection = (Sel){ .beg = 2, .end = 2 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(7 == buf_selend(&TestBuf));
+    }
+
+    TEST(buf_selctx should select line on eol)
+    {
+        set_buffer_text("\n...\n");
+        TestBuf.selection = (Sel){ .beg = 4, .end = 4 };
+        buf_selctx(&TestBuf, risword);
+        CHECK(2 == buf_selbeg(&TestBuf));
+        CHECK(5 == buf_selend(&TestBuf));
+    }
+
+    /* String Retrieval
+     *************************************************************************/
+    TEST(buf_getsat should get string defined by range)
+    {
+        set_buffer_text(" abc/def ");
+        char* str = buf_getsat(&TestBuf, 1, 8);
+        int result = !strcmp(str, "abc/def");
+        free(str);
+        CHECK(result);
+    }
+
+    TEST(buf_fetch should get string defined by function)
+    {
+        set_buffer_text(" abc/def ");
+        TestBuf.selection = (Sel){ .beg = 0, .end = 1 };
+        char* str = buf_fetch(&TestBuf, risbigword, 4);
+        int result = !strcmp(str, "abc/def");
+        free(str);
+        CHECK(result);
+        TestBuf.selection = (Sel){ .beg = 1, .end = 8 };
+        str = buf_fetch(&TestBuf, risbigword, 4);
+        result = !strcmp(str, "abc/def");
+        free(str);
+        CHECK(result);
+    }
 }