found bug in '0' motion, added test for it, and fixed it as well

This commit is contained in:
2025-09-23 16:05:23 +01:00
parent 9a15b1715a
commit 51a7c358ca
2 changed files with 28 additions and 5 deletions

View File

@@ -28,8 +28,8 @@ struct
val textLength = #textLength buffer val textLength = #textLength buffer
val newCursorIdx = Fn.fMove (buffer, cursorIdx) val newCursorIdx = Fn.fMove (buffer, cursorIdx)
in in
if newCursorIdx >= textLength - 2 then if newCursorIdx >= textLength - 1 then
let val newCursorIdx = Int.max (textLength - 2, 0) let val newCursorIdx = Int.max (textLength - 1, 0)
in finish (app, buffer, newCursorIdx) in finish (app, buffer, newCursorIdx)
end end
else else

View File

@@ -1278,7 +1278,7 @@ struct
val zeroMove = describe "move motion '0'" val zeroMove = describe "move motion '0'"
[ test "moves cursor to 0 in contiguous string when on first line" (fn _ => [ test "moves cursor to 0 when on first line" (fn _ =>
let let
(* arrange *) (* arrange *)
val app = TestUtils.init "hello w7rld\n" val app = TestUtils.init "hello w7rld\n"
@@ -1317,8 +1317,7 @@ struct
Expect.isTrue (oldIdx = newIdx) Expect.isTrue (oldIdx = newIdx)
end) end)
, test , test
"moves cursor to first char after '\\n' in contiguous string\ "moves cursor to first char after newline when cursor is after first line"
\when cursor is after first line"
(fn _ => (fn _ =>
let let
(* arrange *) (* arrange *)
@@ -1334,6 +1333,30 @@ struct
(* assert *) (* assert *)
Expect.isTrue (chr = #"#") Expect.isTrue (chr = #"#")
end) end)
, test "leaves cursor at same idx when on last newline" (fn _ =>
let
(* arrange *)
val str =
"hello\n\
\\n\
\\n\
\world\n\
\\n\
\\n"
val initialCursorIdx = String.size str - 1
val app = TestUtils.init str
val app = AppWith.idx (app, initialCursorIdx)
(* act *)
val {cursorIdx, ...} = TestUtils.update (app, CHAR_EVENT #"0")
(* assert *)
val expectedIdx = initialCursorIdx
in
(* assert *)
Expect.isTrue (cursorIdx = expectedIdx)
end)
] ]
val dlrMove = describe "move motion '$'" val dlrMove = describe "move motion '$'"