From b31d7650a8a9607c63263e93e05ac99eaf47463c Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Thu, 25 Sep 2025 14:59:29 +0100 Subject: [PATCH] change the way we calculate the newCursorIdx when we delete using the 'dk' motion while on the last line. We go to the buffer's last line and find the first column. This more directly expresses what we want and now passes the failing unit test for 'dk'. --- fcore/normal-mode/make-normal-delete.sml | 21 +++++++++++++-------- temp.txt | 2 ++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/fcore/normal-mode/make-normal-delete.sml b/fcore/normal-mode/make-normal-delete.sml index 4f604c2..8bb7ebd 100644 --- a/fcore/normal-mode/make-normal-delete.sml +++ b/fcore/normal-mode/make-normal-delete.sml @@ -486,14 +486,6 @@ struct (* deleting from last line *) let (* go to first column of previous line *) - val buffer = LineGap.goToIdx (lineIdx, buffer) - val newCursorIdx = Cursor.viH (buffer, lineIdx, 1) - val buffer = LineGap.goToIdx (newCursorIdx, buffer) - val newCursorIdx = Cursor.vi0 (buffer, newCursorIdx) - - (* clip endOfLine so we leave a newline at end of file *) - val endOfLine = Int.max (0, #textLength buffer - 1) - val buffer = LineGap.goToIdx (endOfLine, buffer) val initialMsg = Fn.initMsgs (lineIdx, length, buffer) val buffer = LineGap.delete (lineIdx, length, buffer) @@ -501,6 +493,19 @@ struct val buffer = if #textLength buffer = 0 then LineGap.append ("\n", buffer) else buffer + + (* since we deleted from the last line, + * we want to place the cursor at the first column + * of the now-last line in the buffer. *) + val newCursorIdx = Int.max (#textLength buffer - 1, 0) + val buffer = LineGap.goToIdx (newCursorIdx, buffer) + val newCursorIdx = + if Cursor.isOnNewlineAfterChr (buffer, newCursorIdx) then + newCursorIdx - 1 + else + newCursorIdx + val buffer = LineGap.goToIdx (newCursorIdx, buffer) + val newCursorIdx = Cursor.vi0 (buffer, newCursorIdx) in finishAfterDeletingBuffer (app, newCursorIdx, buffer, time, initialMsg) end diff --git a/temp.txt b/temp.txt index c100072..0169eed 100644 --- a/temp.txt +++ b/temp.txt @@ -1,3 +1,5 @@ hello world +world +hello