From 961ce63023007bc94c2017c80a12ea5a891b8619 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Tue, 23 Sep 2025 10:50:10 +0100 Subject: [PATCH] begin refactoring to let cursor move to very last char of file (including unix line ending) --- fcore/move.sml | 2 +- fcore/normal-mode/normal-move.sml | 10 +++++----- fcore/text-builder/text-builder-with-cursor.sml | 10 ---------- fcore/text-builder/text-builder-with-highlight.sml | 5 ----- test/normal-move.sml | 13 +++++++------ 5 files changed, 13 insertions(+), 27 deletions(-) diff --git a/fcore/move.sml b/fcore/move.sml index d18751a..98407b7 100644 --- a/fcore/move.sml +++ b/fcore/move.sml @@ -64,7 +64,7 @@ struct val textLength = #textLength buffer val cursorIdx = - if cursorIdx >= textLength - 2 then Int.max (textLength - 2, 0) + if cursorIdx >= textLength - 1 then Int.max (textLength - 1, 0) else cursorIdx in NormalFinish.buildTextAndClear diff --git a/fcore/normal-mode/normal-move.sml b/fcore/normal-mode/normal-move.sml index dfbc01a..2b551ac 100644 --- a/fcore/normal-mode/normal-move.sml +++ b/fcore/normal-mode/normal-move.sml @@ -65,7 +65,7 @@ struct val buffer = LineGap.goToEnd buffer val {line = bufferLine, textLength, ...} = buffer - val bufferIdx = Int.max (0, textLength - 2) + val bufferIdx = Int.max (0, textLength - 1) val bufferLine = bufferLine - 1 val buffer = LineGap.goToIdx (bufferIdx, buffer) @@ -124,8 +124,8 @@ struct val buffer = LineGap.goToIdx (lineIdx, buffer) val endOfLineIdx = Cursor.viDlr (buffer, lineIdx, 1) val endOfLineIdx = - if endOfLineIdx >= #textLength buffer - 2 then - Int.max (0, #textLength buffer - 2) + if endOfLineIdx >= #textLength buffer - 1 then + Int.max (0, #textLength buffer - 1) else endOfLineIdx @@ -256,8 +256,8 @@ struct val lineIdx = Cursor.vi0 (buffer, lineIdx) val lineIdx = - if lineIdx >= #textLength buffer - 2 then - Int.max (0, #textLength buffer - 2) + if lineIdx >= #textLength buffer - 1 then + Int.max (0, #textLength buffer - 1) else lineIdx in diff --git a/fcore/text-builder/text-builder-with-cursor.sml b/fcore/text-builder/text-builder-with-cursor.sml index cc9b3a6..48c5863 100644 --- a/fcore/text-builder/text-builder-with-cursor.sml +++ b/fcore/text-builder/text-builder-with-cursor.sml @@ -3,11 +3,6 @@ struct structure TC = TextConstants structure Utils = TextBuilderUtils - fun isSecondLastChr (pos, str, tl) = - case tl of - [] => pos = String.size str - 2 - | _ => false - fun goToFirstLineAfter (stl, ltl, posY, lineNumber, absIdx, cursorIdx, env, acc) = case (stl, ltl) of @@ -140,11 +135,6 @@ struct #"\n" => if lineNumber + 1 > #lastLineNumber env then acc - else if isSecondLastChr (pos, str, stl) then - if absIdx = cursorIdx then - Utils.makeCursor (#startX env, posY + TC.ySpace, env) :: acc - else - acc else let val acc = diff --git a/fcore/text-builder/text-builder-with-highlight.sml b/fcore/text-builder/text-builder-with-highlight.sml index b038b0c..7f6593a 100644 --- a/fcore/text-builder/text-builder-with-highlight.sml +++ b/fcore/text-builder/text-builder-with-highlight.sml @@ -237,11 +237,6 @@ struct | #"\n" => if lineNumber + 1 > #lastLineNumber env then acc - else if isSecondLastChr (pos, str, stl) then - if absIdx = cursorIdx then - Utils.makeCursor (#startX env, posY + TC.ySpace, env) :: acc - else - acc else let val acc = diff --git a/test/normal-move.sml b/test/normal-move.sml index 3e245ab..d7237af 100644 --- a/test/normal-move.sml +++ b/test/normal-move.sml @@ -119,17 +119,17 @@ struct (* assert *) Expect.isTrue (oldCursorIdx = 0 andalso cursorIdx = 1) end) - , test "does not move cursor when cursorIdx is at end of line" (fn _ => + , test "does not move cursor when cursorIdx is at end of buffer" (fn _ => let (* arrange *) - val app = TestUtils.init "hello world" - val app = AppWith.idx (app, 10) + val app = TestUtils.init "hello world\n" + val app = AppWith.idx (app, 11) (* act *) val {cursorIdx, ...} = TestUtils.update (app, CHAR_EVENT #"l") in (* assert *) - Expect.isTrue (cursorIdx = 10) + Expect.isTrue (cursorIdx = 11) end) , test "moves cursor to char past newline when newline is preceded by char" (fn _ => @@ -1362,13 +1362,14 @@ struct * *) let (* arrange *) - val app = TestUtils.init "01234\n56789\n" + val str = "01234\n56789\n" + val app = TestUtils.init str (* act *) val app = TestUtils.update (app, CHAR_EVENT #"G") in (* assert *) - Expect.isTrue (getChr app = #"9") + Expect.isTrue (#cursorIdx app = String.size str - 1) end)] val percentMove = describe "move motion '%'"