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

This commit is contained in:
2025-09-19 05:23:20 +01:00
parent 4c1a76ae67
commit 9ec8891ce5
2 changed files with 7 additions and 18 deletions

View File

@@ -126,19 +126,24 @@ struct
val startOfLine = Cursor.vi0 (buffer, cursorIdx) val startOfLine = Cursor.vi0 (buffer, cursorIdx)
val column = cursorIdx - startOfLine 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 newCursorLineNumber = Int.max (cursorLineNumber - count, 0)
val buffer = LineGap.goToLine (newCursorLineNumber, buffer) val buffer = LineGap.goToLine (newCursorLineNumber, buffer)
val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer) val lineIdx = LineGap.lineNumberToIdx (newCursorLineNumber, buffer)
val buffer = LineGap.goToIdx (lineIdx, buffer) val buffer = LineGap.goToIdx (lineIdx, buffer)
val lineIdx = val lineIdx =
if Cursor.isNextChrEndOfLine (buffer, lineIdx) then cursorIdx if Cursor.isNextChrEndOfLine (buffer, lineIdx) then lineIdx
else lineIdx + 1 else lineIdx + 1
val buffer = LineGap.goToIdx (lineIdx, buffer) val buffer = LineGap.goToIdx (lineIdx, buffer)
val endOfLineIdx = Cursor.viDlr (buffer, lineIdx, 1) 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 cursorIdx = Int.min (endOfLineIdx, lineIdx + column)
val buffer = LineGap.goToIdx (cursorIdx, buffer) val buffer = LineGap.goToIdx (cursorIdx, buffer)

View File

@@ -303,22 +303,6 @@ struct
in in
Expect.isTrue isSkipped Expect.isTrue isSkipped
end) 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 _ => , test "leaves cursor at same idx when already at start of buffer" (fn _ =>
let let
(* arrange *) (* arrange *)