handle edge case when deleting from buffer: if the previous match is extended into a new match, then replace the old match in the search list with the extended match

This commit is contained in:
2026-02-07 02:25:45 +00:00
parent 68be55342d
commit 340e52019f
2 changed files with 21 additions and 36 deletions

View File

@@ -175,15 +175,19 @@ struct
end end
fun tryExtendingPrevMatch fun tryExtendingPrevMatch
(idx, buffer, searchList, dfa, prevMatchFinish, finalPos, curState) = (idx, buffer, searchList, dfa, finalPos, curState, start) =
if idx = #textLength buffer then if idx = #textLength buffer then
if finalPos = prevMatchFinish then (* reached end of buffer without finding anything
(* call insertUntilMatch *) * so return current buffer and searchList *)
0 (buffer, searchList)
else else if Dfa.isDead curState then
(* update searchList, replacing prevMatchFinish with finalPos, let
* and then call insertUntilMatch with new searchList *) val searchList =
0 PersistentVector.extendExistingMatch (start, finalPos, searchList)
in
insertUntilMatch
(finalPos + 1, buffer, searchList, dfa, 0, finalPos + 1, ~1)
end
else else
let let
val buffer = LineGap.goToIdx (idx, buffer) val buffer = LineGap.goToIdx (idx, buffer)
@@ -191,40 +195,24 @@ struct
val newState = Dfa.nextState (dfa, curState, chr) val newState = Dfa.nextState (dfa, curState, chr)
val finalPos = if Dfa.isFinal (dfa, newState) then idx else finalPos val finalPos = if Dfa.isFinal (dfa, newState) then idx else finalPos
in in
if Dfa.isDead newState then
if finalPos = prevMatchFinish then
(* call insertUntilMatch *)
0
else
(* update searchList, replacing prevMatchFinish with finalPos,
* and then call insertUntilMatch with new searchList *)
0
else
(* continue *) (* continue *)
tryExtendingPrevMatch tryExtendingPrevMatch
( idx + 1 (idx + 1, buffer, searchList, dfa, finalPos, newState, start)
, buffer
, searchList
, dfa
, prevMatchFinish
, finalPos
, newState
)
end end
fun deleteBufferAndSearchList (start, length, buffer, searchList, dfa) = fun deleteBufferAndSearchList (start, length, buffer, searchList, dfa) =
let let
val buffer = LineGap.delete (start, length, buffer) val buffer = LineGap.delete (start, length, buffer)
val searchList = PersistentVector.delete (start, length, searchList) val searchList = PersistentVector.delete (start, length, searchList)
val oldStart = PersistentVector.prevMatch (start, searchList, 1)
val {finish = searchStart, ...} =
PersistentVector.helpPrevMatch (start, searchList, 0)
val searchStart = searchStart + 1
in in
if Vector.length dfa = 0 then if Vector.length dfa = 0 then
(buffer, searchList) (buffer, searchList)
else if oldStart >= start 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 else
insertUntilMatch tryExtendingPrevMatch
(searchStart, buffer, searchList, dfa, 0, searchStart, ~1) (oldStart, buffer, searchList, dfa, ~1, 0, oldStart)
end end
end end

View File

@@ -1,8 +1,5 @@
# To-do list # To-do list
- Make sure that all delete function in make-normal-delete.sml also delete from searchList - Make sure that all delete function in make-normal-delete.sml also delete from searchList
- Handle edge cases regarding deletion from searchList.
- Edge case 1: deletion causes a match to be extended
- Any other edge cases possible?
- Add normal-delete tests for each motion, checking that searchList is as expected - Add normal-delete tests for each motion, checking that searchList is as expected
- Add tests for other yank motoins - Add tests for other yank motoins
- Tests should be based on existing tests for delete-motions, and in the same order. - Tests should be based on existing tests for delete-motions, and in the same order.