From dbb60a3cb0c1ada39692426e1b5a45245b3b791d Mon Sep 17 00:00:00 2001 From: bptato Date: Sat, 18 Nov 2023 19:52:35 +0100 Subject: 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. --- src/server/buffer.nim | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/server') 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] -- cgit