about summary refs log tree commit diff stats
path: root/shell
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-04-09 20:39:53 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-04-09 20:39:53 -0700
commit5094473b2fb94d197bb80c93c47215a628c9738a (patch)
tree42893e4a4a588e5906d0da1ba503ceb87f7fcfaa /shell
parentf5defca1170bccaca5a7f3e35d59a38b8c7017f0 (diff)
downloadmu-5094473b2fb94d197bb80c93c47215a628c9738a.tar.gz
shell: highlight matching close-paren
Diffstat (limited to 'shell')
-rw-r--r--shell/gap-buffer.mu23
-rw-r--r--shell/grapheme-stack.mu93
2 files changed, 114 insertions, 2 deletions
diff --git a/shell/gap-buffer.mu b/shell/gap-buffer.mu
index a5f55096..0e023da3 100644
--- a/shell/gap-buffer.mu
+++ b/shell/gap-buffer.mu
@@ -582,6 +582,29 @@ fn test-render-gap-buffer-with-cursor-at-start {
   check-background-color-in-screen-row screen, 7/bg=cursor, 0/y, "|   ", "F - test-render-gap-buffer-with-cursor-at-start: bg"
 }
 
+fn test-render-gap-buffer-highlight-matching-close-paren {
+  var gap-storage: gap-buffer
+  var gap/esi: (addr gap-buffer) <- address gap-storage
+  initialize-gap-buffer-with gap, "(a)"
+  gap-to-start gap
+  # setup: screen
+  var screen-on-stack: screen
+  var screen/edi: (addr screen) <- address screen-on-stack
+  initialize-screen screen, 5, 4
+  #
+  var x/eax: int <- render-gap-buffer screen, gap, 0/x, 0/y, 1/show-cursor
+  check-screen-row                     screen, 0/y,                   "(a) ", "F - test-render-gap-buffer-highlight-matching-close-paren"
+  check-ints-equal x, 4, "F - test-render-gap-buffer-highlight-matching-open-paren: result"
+  check-background-color-in-screen-row screen, 7/bg=cursor,      0/y, "|   ", "F - test-render-gap-buffer-highlight-matching-open-paren: cursor"
+  check-screen-row-in-color            screen, 0xf/fg=highlight, 0/y, "  ) ", "F - test-render-gap-buffer-highlight-matching-open-paren: matching paren"
+}
+
+fn test-render-gap-buffer-highlight-matching-open-paren {
+}
+
+fn test-render-gap-buffer-highlight-matching-open-paren-of-end {
+}
+
 ## some primitives for scanning through a gap buffer
 # don't modify the gap buffer while scanning
 # this includes moving the cursor around
diff --git a/shell/grapheme-stack.mu b/shell/grapheme-stack.mu
index aa9838b4..870e32d4 100644
--- a/shell/grapheme-stack.mu
+++ b/shell/grapheme-stack.mu
@@ -128,6 +128,7 @@ fn render-stack-from-top-wrapping-right-then-down screen: (addr screen), _self:
   var x/eax: int <- copy _x
   var y/ecx: int <- copy _y
   var top-addr/ebx: (addr int) <- get self, top
+  var matching-close-paren-index/edx: int <- get-matching-close-paren-index self, render-cursor?
   var i/ebx: int <- copy *top-addr
   i <- decrement
   # if render-cursor?, peel off first iteration
@@ -144,8 +145,17 @@ fn render-stack-from-top-wrapping-right-then-down screen: (addr screen), _self:
   {
     compare i, 0
     break-if-<
+    # highlight matching paren if needed
+    var fg: int
+    copy-to fg, 3/cyan
+    compare i, matching-close-paren-index
+    {
+      break-if-!=
+      copy-to fg, 0xf/highlight
+    }
+    #
     var g/esi: (addr grapheme) <- index data, i
-    x, y <- render-grapheme screen, *g, xmin, ymin, xmax, ymax, x, y, 3/fg=cyan, 0/bg=cursor
+    x, y <- render-grapheme screen, *g, xmin, ymin, xmax, ymax, x, y, fg, 0/bg=cursor
     i <- decrement
     loop
   }
@@ -193,10 +203,89 @@ fn test-render-grapheme-stack {
   #
   var x/eax: int <- render-stack-from-top screen, gs, 0/x, 2/y, 1/cursor=true
   check-screen-row screen, 2/y, "cba ", "F - test-render-grapheme-stack from top with cursor"
-  check-ints-equal x, 3, "F - test-render-grapheme-stack from top without cursor: result"
+  check-ints-equal x, 3, "F - test-render-grapheme-stack from top with cursor: result"
   check-background-color-in-screen-row screen, 7/bg=cursor, 2/y, "|   ", "F - test-render-grapheme-stack from top with cursor: bg"
 }
 
+fn test-render-grapheme-stack-while-highlighting-matching-close-paren {
+  # setup: gs = "abc"
+  var gs-storage: grapheme-stack
+  var gs/edi: (addr grapheme-stack) <- address gs-storage
+  initialize-grapheme-stack gs, 5
+  var g/eax: grapheme <- copy 0x29/close-paren
+  push-grapheme-stack gs, g
+  g <- copy 0x62/b
+  push-grapheme-stack gs, g
+  g <- copy 0x28/open-paren
+  push-grapheme-stack gs, g
+  # setup: screen
+  var screen-on-stack: screen
+  var screen/esi: (addr screen) <- address screen-on-stack
+  initialize-screen screen, 5, 4
+  #
+  var x/eax: int <- render-stack-from-top screen, gs, 0/x, 2/y, 1/cursor=true
+  check-screen-row screen, 2/y, "(b) ", "F - test-render-grapheme-stack-while-highlighting-matching-close-paren"
+  check-ints-equal x, 3, "F - test-render-grapheme-stack-while-highlighting-matching-close-paren: result"
+  check-background-color-in-screen-row screen, 7/bg=cursor,  2/y, "|   ", "F - test-render-grapheme-stack-while-highlighting-matching-close-paren: cursor"
+  check-screen-row-in-color            screen, 0xf/fg=white, 2/y, "  ) ", "F - test-render-grapheme-stack-while-highlighting-matching-close-paren: matching paren"
+}
+
+# return the index of the matching close-paren of the grapheme at cursor (top of stack)
+# or top index if there's no matching close-paren
+fn get-matching-close-paren-index _self: (addr grapheme-stack), render-cursor?: boolean -> _/edx: int {
+  var self/esi: (addr grapheme-stack) <- copy _self
+  var top-addr/edx: (addr int) <- get self, top
+  # if not rendering cursor, return
+  compare render-cursor?, 0/false
+  {
+    break-if-!=
+    return *top-addr
+  }
+  var data-ah/eax: (addr handle array grapheme) <- get self, data
+  var data/eax: (addr array grapheme) <- lookup *data-ah
+  var i/ecx: int <- copy *top-addr
+  # if stack is empty, return
+  compare i, 0
+  {
+    break-if->
+    return *top-addr
+  }
+  # if cursor is not '(' return
+  i <- decrement
+  var g/esi: (addr grapheme) <- index data, i
+  compare *g, 0x28/open-paren
+  {
+    break-if-=
+    return *top-addr
+  }
+  # otherwise scan to matching paren
+  var paren-count/ebx: int <- copy 1
+  i <- decrement
+  {
+    compare i, 0
+    break-if-<
+    var g/esi: (addr grapheme) <- index data, i
+    compare *g, 0x28/open-paren
+    {
+      break-if-!=
+      paren-count <- increment
+    }
+    compare *g, 0x29/close-paren
+    {
+      break-if-!=
+      compare paren-count, 1
+      {
+        break-if-!=
+        return i
+      }
+      paren-count <- decrement
+    }
+    i <- decrement
+    loop
+  }
+  return *top-addr
+}
+
 # compare from bottom
 # beware: modifies 'stream', which must be disposed of after a false result
 fn prefix-match? _self: (addr grapheme-stack), s: (addr stream byte) -> _/eax: boolean {