From c28ae4d8cdb2e733fe8fcee7efea6a226c01b302 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sun, 8 Feb 2026 02:32:32 +0000 Subject: [PATCH] code function that can insert into both searchList and buffer --- fcore/normal-mode/normal-mode.sml | 1 + fcore/persistent-vector.sml | 21 +++++++++++++++++++++ fcore/search-list/search-list.sml | 30 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/fcore/normal-mode/normal-mode.sml b/fcore/normal-mode/normal-mode.sml index 41606ab..e7a921e 100644 --- a/fcore/normal-mode/normal-mode.sml +++ b/fcore/normal-mode/normal-mode.sml @@ -46,6 +46,7 @@ struct val buffer = LineGap.goToIdx (cursorIdx, buffer) val lineStart = Cursor.vi0 (buffer, cursorIdx) + (* todo: update searchList based on insert *) val buffer = LineGap.insert (lineStart, " ", buffer) val buffer = LineGap.goToIdx (lineStart, buffer) diff --git a/fcore/persistent-vector.sml b/fcore/persistent-vector.sml index 6da0540..36bcb65 100644 --- a/fcore/persistent-vector.sml +++ b/fcore/persistent-vector.sml @@ -538,6 +538,27 @@ struct LEAF (items, sizes) end + fun incrementBy (incBy, tree) = + case tree of + BRANCH (nodes, sizes) => + let + val child = incrementBy (incBy, Vector.sub (nodes, 0)) + val nodes = Vector.update (nodes, 0, child) + val sizes = Vector.map (fn sz => sz + incBy) sizes + in + BRANCH (nodes, sizes) + end + | LEAF (items, sizes) => + let + val items = Vector.map + (fn {start, finish} => + {start = start - incBy, finish = finish - incBy} + ) items + val sizes = Vector.map #finish items + in + LEAF (items, sizes) + end + fun countDepthLoop (counter, tree) = case tree of BRANCH (nodes, _) => countDepthLoop (counter + 1, Vector.sub (nodes, 0)) diff --git a/fcore/search-list/search-list.sml b/fcore/search-list/search-list.sml index e4f396f..6a219f1 100644 --- a/fcore/search-list/search-list.sml +++ b/fcore/search-list/search-list.sml @@ -215,4 +215,34 @@ struct tryExtendingPrevMatch (oldStart, buffer, searchList, dfa, ~1, 0, oldStart) end + + (* inserts into buffer and searchList both *) + fun insert (insIdx, insString, buffer, searchList, dfa) = + let + val buffer = LineGap.insert (insIdx, insString, buffer) + + (* incremennt existing elements in the searchList after the insIdx + * by the length of the string that was just inserted *) + val searchList = + let + val searchListLeft = PersistentVector.splitLeft (insIdx, searchList) + val searchListRight = PersistentVector.splitRight (insIdx, searchList) + val searchListRight = + PersistentVector.incrementBy (String.size insString, searchList) + in + PersistentVector.merge (searchListLeft, searchListRight) + end + + (* start looking for new matches from the previous match *) + val oldStart = PersistentVector.prevMatch (insIdx, searchList, 1) + in + if Vector.length dfa = 0 then + (buffer, searchList) + else if oldStart >= insIdx orelse oldStart = ~1 then + (* no previous match, so try searching for a match from start of buffer *) + insertUntilMatch (0, buffer, searchList, dfa, 0, 0, ~1) + else + tryExtendingPrevMatch + (oldStart, buffer, searchList, dfa, ~1, 0, oldStart) + end end