From 51a7c358ca235956b41430e59538a9c266a081df Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Tue, 23 Sep 2025 16:05:23 +0100 Subject: [PATCH] found bug in '0' motion, added test for it, and fixed it as well --- fcore/move.sml | 4 ++-- test/normal-move.sml | 29 ++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/fcore/move.sml b/fcore/move.sml index 6c1c427..200dcf0 100644 --- a/fcore/move.sml +++ b/fcore/move.sml @@ -28,8 +28,8 @@ struct val textLength = #textLength buffer val newCursorIdx = Fn.fMove (buffer, cursorIdx) in - if newCursorIdx >= textLength - 2 then - let val newCursorIdx = Int.max (textLength - 2, 0) + if newCursorIdx >= textLength - 1 then + let val newCursorIdx = Int.max (textLength - 1, 0) in finish (app, buffer, newCursorIdx) end else diff --git a/test/normal-move.sml b/test/normal-move.sml index ec2055d..eacbea1 100644 --- a/test/normal-move.sml +++ b/test/normal-move.sml @@ -1278,7 +1278,7 @@ struct 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 (* arrange *) val app = TestUtils.init "hello w7rld\n" @@ -1317,8 +1317,7 @@ struct Expect.isTrue (oldIdx = newIdx) end) , test - "moves cursor to first char after '\\n' in contiguous string\ - \when cursor is after first line" + "moves cursor to first char after newline when cursor is after first line" (fn _ => let (* arrange *) @@ -1334,6 +1333,30 @@ struct (* assert *) Expect.isTrue (chr = #"#") 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 '$'"