about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-11-18 19:52:35 +0100
committerbptato <nincsnevem662@gmail.com>2023-11-20 15:47:31 +0100
commitdbb60a3cb0c1ada39692426e1b5a45245b3b791d (patch)
tree2350a39a39342e8a4d7bb201fb556d83169c6537
parent3b09d016a262abef593fe748392d4c3a655438d1 (diff)
downloadchawan-dbb60a3cb0c1ada39692426e1b5a45245b3b791d.tar.gz
buffer: optimize findPrevLink
It's better to not do it perfectly in 100% of all cases than to
loop through the entire document in all cases.
-rw-r--r--src/server/buffer.nim8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 3a458cc4..97acc8e2 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -412,6 +412,8 @@ proc findPrevLink*(buffer: Buffer, cursorx, cursory: int): tuple[x, y: int] {.pr
     for iy in countdown(ly - 1, 0):
       let line = buffer.lines[iy]
       i = line.formats.len - 1
+      let oly = iy
+      let olx = lx
       while i >= 0:
         let format = line.formats[i]
         let nl = format.node.getClickable()
@@ -419,6 +421,12 @@ proc findPrevLink*(buffer: Buffer, cursorx, cursory: int): tuple[x, y: int] {.pr
           ly = iy
           lx = format.pos
         dec i
+      if iy == oly and olx == lx:
+        # Assume multiline anchors are always placed on consecutive lines.
+        # This is not true, but otherwise we would have to loop through
+        # the entire document, which would be rather inefficient. TODO: find
+        # an efficient and correct way to do this.
+        break
 
   while i >= 0:
     let format = line.formats[i]