add additional test for 'dn' motion after finding bug, fixed bug (rewrote high-level delete fundtion in persistent-vector.sml to address it), and begin adding tests for 'dN' motion

This commit is contained in:
2026-03-28 00:45:08 +00:00
parent 3f6eaa730a
commit 9d46ec9f34
4 changed files with 118 additions and 67 deletions

View File

@@ -483,7 +483,7 @@ struct
else else
if splitIdx > Vector.sub (sizes, Vector.length sizes - 1) then if splitIdx > Vector.sub (sizes, Vector.length sizes - 1) then
empty empty
else if splitIdx < Vector.sub (sizes, 0) then else if splitIdx < #start (Vector.sub (items, 0)) then
tree tree
else else
let let
@@ -743,13 +743,6 @@ struct
let let
val finish = start + length val finish = start + length
val matchBeforeStart = prevMatch (start, tree, 1)
val matchBeforeStart =
if matchBeforeStart >= start then
~1
else
matchBeforeStart
val matchAfterFinish = nextMatch (finish, tree, 1) val matchAfterFinish = nextMatch (finish, tree, 1)
val matchAfterFinish = val matchAfterFinish =
if matchAfterFinish < finish then if matchAfterFinish < finish then
@@ -757,45 +750,29 @@ struct
else else
matchAfterFinish matchAfterFinish
in in
if matchBeforeStart = ~1 andalso matchAfterFinish = ~1 then
empty
else if matchAfterFinish = ~1 then
(* there is no match after 'finish', so just split left.
* No need to decrement or split right,
* because the right vector would be empty. *)
splitLeft (start, tree)
else if matchBeforeStart = ~1 then
(* We don't want to use left split
* because there are no elements
* before the deletion range's 'start'.
* So we make a right split and then decrement it. *)
let
val right = splitRight (finish, tree)
in
if isEmpty right then
empty
else
let
val startIdx = getStartIdx right
val shouldBeStartIdx = matchAfterFinish - length
val difference = startIdx - shouldBeStartIdx
in
if difference = 0 then
right
else if isEmpty right then
empty
else
decrementBy (difference, right)
end
end
else
let let
val left = splitLeft (start, tree) val left = splitLeft (start, tree)
val right = splitRight (finish, tree) val right = splitRight (finish, tree)
in in
if isEmpty right then if isEmpty left andalso isEmpty right then
empty
else if isEmpty left then
(* just decrement right and return it *)
let
val rightStart = getStartIdx right
val shouldBeStartIdx = matchAfterFinish - length
val difference = rightStart - shouldBeStartIdx
in
if difference = 0 then
right
else
decrementBy (difference, right)
end
else if isEmpty right then
(* return left half without doing anything *)
left left
else else
(* decrement right, and then merge both together *)
let let
val leftSize = getFinishIdx left val leftSize = getFinishIdx left
val rightStartRelative = getStartIdx right val rightStartRelative = getStartIdx right

View File

@@ -144,13 +144,13 @@ struct
in in
(buffer, searchList) (buffer, searchList)
end end
else if PersistentVector.isInRange (idx, searchList) then
(buffer, searchList)
else if Dfa.isDead curState then else if Dfa.isDead curState then
if prevFinalPos = ~1 then if prevFinalPos = ~1 then
(* no match found: restart search from `startPos + 1` *) (* no match found: restart search from `startPos + 1` *)
insertUntilMatch insertUntilMatch
(startPos + 1, buffer, searchList, dfa, 0, startPos + 1, ~1) (startPos + 1, buffer, searchList, dfa, 0, startPos + 1, ~1)
else if PersistentVector.isInRange (prevFinalPos, searchList) then
(buffer, searchList)
else else
(* new match. Insert and continue *) (* new match. Insert and continue *)
let let

View File

@@ -1 +1,2 @@
hellohello hello hello hello
world world world

View File

@@ -5824,7 +5824,6 @@ struct
(* assert *) (* assert *)
Expect.isTrue assertion Expect.isTrue assertion
end) end)
, test "extends match when match should extend after deletion" (fn _ => , test "extends match when match should extend after deletion" (fn _ =>
let let
(* arrange *) (* arrange *)
@@ -5850,6 +5849,41 @@ struct
[{start = 0, finish = 4}, {start = 6, finish = 10}] [{start = 0, finish = 4}, {start = 6, finish = 10}]
val expectedNewSearchList = [{start = 0, finish = 9}] val expectedNewSearchList = [{start = 0, finish = 9}]
val assertion =
oldSearchList = expectedOldSearchList
andalso newSearchList = expectedNewSearchList
in
Expect.isTrue assertion
end)
, test "detects match when we delete in middle of it" (fn _ =>
let
(* arrange *)
val originalIdx = 1
val originalString = "hello hello hello\n"
val app = TestUtils.init originalString
val app = AppWith.idx (app, originalIdx)
val app = TestUtils.updateMany (app, "/hello")
val app = AppUpdate.update (app, InputMsg.KEY_ENTER, Time.now ())
(* act *)
val newApp = TestUtils.updateMany (app, "dn")
(* assert *)
val oldSearchList = #searchList app
val oldSearchList = PersistentVector.toList oldSearchList
val newSearchList = #searchList newApp
val newSearchList = PersistentVector.toList newSearchList
val expectedOldSearchList =
[ {start = 0, finish = 4}
, {start = 6, finish = 10}
, {start = 12, finish = 16}
]
val expectedNewSearchList =
[{start = 1, finish = 5}, {start = 7, finish = 11}]
val assertion = val assertion =
oldSearchList = expectedOldSearchList oldSearchList = expectedOldSearchList
andalso newSearchList = expectedNewSearchList andalso newSearchList = expectedNewSearchList
@@ -5952,6 +5986,45 @@ struct
(actualString = expectedString (actualString = expectedString
andalso cursorIdx = expectedCursorIdx) andalso cursorIdx = expectedCursorIdx)
end) end)
, test
"decrements subsequent matches in search list \
\when we delete prior to last match"
(fn _ =>
let
(* arrange *)
val originalIdx = 1
val originalString = "hello hello hello\n"
val app = TestUtils.init originalString
val app = AppWith.idx (app, originalIdx)
val app = TestUtils.updateMany (app, "/hello")
val app = AppUpdate.update (app, InputMsg.KEY_ENTER, Time.now ())
(* act *)
val newApp = TestUtils.updateMany (app, "dN")
(* assert *)
val oldSearchList = #searchList app
val oldSearchList = PersistentVector.toList oldSearchList
val newSearchList = #searchList newApp
val newSearchList = PersistentVector.toList newSearchList
val expectedOldSearchList =
[ {start = 0, finish = 4}
, {start = 6, finish = 10}
, {start = 12, finish = 16}
]
val expectedNewSearchList =
[{start = 5, finish = 9}, {start = 11, finish = 15}]
val assertion =
oldSearchList = expectedOldSearchList
andalso newSearchList = expectedNewSearchList
in
Expect.isTrue assertion
end)
] ]
val dfDelete = describe "delete motion 'df<char>'" val dfDelete = describe "delete motion 'df<char>'"