From 0de7a9278aae0d446e9b399c0c3ca51b4d28374c Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 8 Oct 2025 10:27:19 +0100 Subject: [PATCH] progress implementing help-prev-match for vector --- fcore/normal-mode/make-normal-delete.sml | 3 +- fcore/normal-mode/normal-move.sml | 6 +- fcore/normal-mode/normal-yank.sml | 2 +- fcore/persistent-vector.sml | 86 +++++++++++++++++++++--- fcore/search-list/search-list.sml | 2 - 5 files changed, 82 insertions(+), 17 deletions(-) diff --git a/fcore/normal-mode/make-normal-delete.sml b/fcore/normal-mode/make-normal-delete.sml index f3a1f60..1b1e4ce 100644 --- a/fcore/normal-mode/make-normal-delete.sml +++ b/fcore/normal-mode/make-normal-delete.sml @@ -778,7 +778,8 @@ struct fun deleteToPrevMatch (app: app_type, count, time) = let val {cursorIdx, searchList, ...} = app - val newCursorIdx = SearchList.prevMatch (cursorIdx, searchList, count) + val newCursorIdx = + PersistentVector.prevMatch (cursorIdx, searchList, count) in if newCursorIdx = ~1 orelse newCursorIdx >= cursorIdx then NormalFinish.clearMode app diff --git a/fcore/normal-mode/normal-move.sml b/fcore/normal-mode/normal-move.sml index 48385fd..1b9da80 100644 --- a/fcore/normal-mode/normal-move.sml +++ b/fcore/normal-mode/normal-move.sml @@ -494,7 +494,8 @@ struct , visualScrollColumn , ... } = app - val newCursorIdx = PersistentVector.nextMatch (cursorIdx, searchList, count) + val newCursorIdx = + PersistentVector.nextMatch (cursorIdx, searchList, count) in if newCursorIdx = ~1 then NormalFinish.clearMode app @@ -506,7 +507,8 @@ struct fun moveToPrevMatch (app: app_type, count) = let val {cursorIdx, searchList, buffer, bufferModifyTime, ...} = app - val newCursorIdx = SearchList.prevMatch (cursorIdx, searchList, count) + val newCursorIdx = + PersistentVector.prevMatch (cursorIdx, searchList, count) in if newCursorIdx = ~1 then NormalFinish.clearMode app diff --git a/fcore/normal-mode/normal-yank.sml b/fcore/normal-mode/normal-yank.sml index 117e946..a0f9a4f 100644 --- a/fcore/normal-mode/normal-yank.sml +++ b/fcore/normal-mode/normal-yank.sml @@ -188,7 +188,7 @@ struct fun yankToPrevMatch (app: app_type, count) = let val {cursorIdx, searchList, buffer, ...} = app - val low = SearchList.prevMatch (cursorIdx, searchList, count) + val low = PersistentVector.prevMatch (cursorIdx, searchList, count) in if low = ~1 orelse low >= cursorIdx then NormalFinish.clearMode app diff --git a/fcore/persistent-vector.sml b/fcore/persistent-vector.sml index db9941d..e762951 100644 --- a/fcore/persistent-vector.sml +++ b/fcore/persistent-vector.sml @@ -110,41 +110,41 @@ struct LEAF (values, _) => Vector.sub (values, 0) | BRANCH (nodes, _) => getStart (Vector.sub (nodes, 0)) - fun helpNextMatch (cusorIdx, tree) = + fun helpNextMatch (cursorIdx, tree) = case tree of LEAF (values, sizes) => let - val idx = BinSearch.equalOrMore (cusorIdx, sizes) + val idx = BinSearch.equalOrMore (cursorIdx, sizes) in if idx = ~1 then {start = ~1, finish = ~1} else Vector.sub (values, idx) end | BRANCH (nodes, sizes) => let - val idx = BinSearch.equalOrMore (cusorIdx, sizes) + val idx = BinSearch.equalOrMore (cursorIdx, sizes) in if idx = ~1 then {start = ~1, finish = ~1} - else helpNextMatch (cusorIdx, Vector.sub (nodes, idx)) + else helpNextMatch (cursorIdx, Vector.sub (nodes, idx)) end - fun startNextMatch (cusorIdx, tree) = + fun startNextMatch (cursorIdx, tree) = case tree of LEAF (values, sizes) => if Vector.length sizes = 0 then {start = ~1, finish = ~1} else let - val idx = BinSearch.equalOrMore (cusorIdx, sizes) + val idx = BinSearch.equalOrMore (cursorIdx, sizes) val idx = if idx = ~1 then 0 else idx in Vector.sub (values, idx) end | BRANCH (nodes, sizes) => let - val idx = BinSearch.equalOrMore (cusorIdx, sizes) + val idx = BinSearch.equalOrMore (cursorIdx, sizes) in if idx = ~1 then {start = ~1, finish = ~1} - else helpNextMatch (cusorIdx, Vector.sub (nodes, idx)) + else helpNextMatch (cursorIdx, Vector.sub (nodes, idx)) end fun loopNextMatch (prevStart, prevFinish, tree, count) = @@ -162,18 +162,18 @@ struct loopNextMatch (start, finish, tree, count - 1) end - fun nextMatch (cusorIdx, tree, count) = + fun nextMatch (cursorIdx, tree, count) = if isEmpty tree then ~1 else let - val {start, finish} = startNextMatch (cusorIdx, tree) + val {start, finish} = startNextMatch (cursorIdx, tree) in if start = ~1 then let val {start, finish} = getStart tree in loopNextMatch (start, finish, tree, count - 1) end - else if cusorIdx >= start andalso cusorIdx <= finish then + else if cursorIdx >= start andalso cursorIdx <= finish then loopNextMatch (start, finish, tree, count) else loopNextMatch (start, finish, tree, count - 1) @@ -185,4 +185,68 @@ struct Vector.sub (values, Vector.length values - 1) | BRANCH (nodes, _) => getLast (Vector.sub (nodes, Vector.length nodes - 1)) + + fun helpPrevMatch (cursorIdx, tree) = + case tree of + LEAF (values, sizes) => + let + val idx = BinSearch.equalOrMore (cursorIdx, sizes) + in + if idx < 0 then {start = ~1, finish = ~1} + else if idx = 0 then + let + val current = Vector.sub (values, 0) + in + current + end + else + let + val current = Vector.sub (values, idx) + val prev = Vector.sub (values, idx - 1) + in + if cursorIdx > #start current then + current + else + prev + end + end + | BRANCH (nodes, sizes) => + let + val idx = BinSearch.equalOrMore (cursorIdx, sizes) + in + if idx < 0 then {start = ~1, finish = ~1} + else helpPrevMatch (cursorIdx, Vector.sub (nodes, idx)) + end + + fun loopPrevMatch (prevStart, tree, count) = + if count = 0 then + prevStart + else + let + val {start, finish} = helpPrevMatch (prevStart - 1, tree) + in + if start = ~1 then + let val {start, finish} = getLast tree + in loopPrevMatch (start, tree, count - 1) + end + else + loopPrevMatch (start, tree, count - 1) + end + + fun prevMatch (cursorIdx, tree, count) = + if isEmpty tree then + ~1 + else + let + val {start, finish} = helpPrevMatch (cursorIdx, tree) + in + if start = ~1 then + let val {start, finish} = getLast tree + in loopPrevMatch (start, tree, count - 1) + end + else if cursorIdx >= start andalso cursorIdx <= finish then + loopPrevMatch (start, tree, count) + else + loopPrevMatch (start, tree, count - 1) + end end diff --git a/fcore/search-list/search-list.sml b/fcore/search-list/search-list.sml index a143687..6828be6 100644 --- a/fcore/search-list/search-list.sml +++ b/fcore/search-list/search-list.sml @@ -121,6 +121,4 @@ struct ) else (buffer, PersistentVector.empty) - - fun prevMatch (cursorIdx, searchList, count) = raise Fail "todo: reimplement" end