From 9ec8891ce5a5c9e2030bf3942aeef85f6bb8b291 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Fri, 19 Sep 2025 05:23:20 +0100 Subject: [PATCH] fix bugs in new function for moving cursor upwards, taking care to calculate column different if on first line (where there is no preceding newline) vs any line other than the first --- fcore/normal-mode/normal-move.sml | 9 +++++++-- test/normal-move.sml | 16 ---------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/fcore/normal-mode/normal-move.sml b/fcore/normal-mode/normal-move.sml index 20a8187..b5fdb25 100644 --- a/fcore/normal-mode/normal-move.sml +++ b/fcore/normal-mode/normal-move.sml @@ -126,19 +126,24 @@ struct val startOfLine = Cursor.vi0 (buffer, cursorIdx) val column = cursorIdx - startOfLine - val cursorLineNumber = LineGap.idxToLineNumber (cursorIdx, buffer) + val cursorLineNumber = + if Cursor.isNextChrEndOfLine (buffer, cursorIdx) then + LineGap.idxToLineNumber (cursorIdx + 1, buffer) + else + LineGap.idxToLineNumber (cursorIdx, buffer) val newCursorLineNumber = Int.max (cursorLineNumber - count, 0) val buffer = LineGap.goToLine (newCursorLineNumber, buffer) val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer) val buffer = LineGap.goToIdx (lineIdx, buffer) val lineIdx = - if Cursor.isNextChrEndOfLine (buffer, lineIdx) then cursorIdx + if Cursor.isNextChrEndOfLine (buffer, lineIdx) then lineIdx else lineIdx + 1 val buffer = LineGap.goToIdx (lineIdx, buffer) val endOfLineIdx = Cursor.viDlr (buffer, lineIdx, 1) + val column = if newCursorLineNumber = 0 then column - 1 else column val cursorIdx = Int.min (endOfLineIdx, lineIdx + column) val buffer = LineGap.goToIdx (cursorIdx, buffer) diff --git a/test/normal-move.sml b/test/normal-move.sml index be9d427..45b604e 100644 --- a/test/normal-move.sml +++ b/test/normal-move.sml @@ -303,22 +303,6 @@ struct in Expect.isTrue isSkipped end) - , test "moves to 0 of buffer when on first line" (fn _ => - let - (* arrange *) - val str = "hello \nworld \ntime to go\n" - val buffer = LineGap.fromString str - val app = TestUtils.init buffer - val app = AppWith.idx (app, 5) - - (* act *) - val {cursorIdx, ...} = TestUtils.update (app, CHAR_EVENT #"k") - - (* assert *) - val isAtStart = cursorIdx = 0 - in - Expect.isTrue isAtStart - end) , test "leaves cursor at same idx when already at start of buffer" (fn _ => let (* arrange *)