begin refactoring to let cursor move to very last char of file (including unix line ending)

This commit is contained in:
2025-09-23 10:50:10 +01:00
parent 8931d4d071
commit 961ce63023
5 changed files with 13 additions and 27 deletions

View File

@@ -64,7 +64,7 @@ struct
val textLength = #textLength buffer val textLength = #textLength buffer
val cursorIdx = val cursorIdx =
if cursorIdx >= textLength - 2 then Int.max (textLength - 2, 0) if cursorIdx >= textLength - 1 then Int.max (textLength - 1, 0)
else cursorIdx else cursorIdx
in in
NormalFinish.buildTextAndClear NormalFinish.buildTextAndClear

View File

@@ -65,7 +65,7 @@ struct
val buffer = LineGap.goToEnd buffer val buffer = LineGap.goToEnd buffer
val {line = bufferLine, textLength, ...} = 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 bufferLine = bufferLine - 1
val buffer = LineGap.goToIdx (bufferIdx, buffer) val buffer = LineGap.goToIdx (bufferIdx, buffer)
@@ -124,8 +124,8 @@ struct
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 endOfLineIdx = val endOfLineIdx =
if endOfLineIdx >= #textLength buffer - 2 then if endOfLineIdx >= #textLength buffer - 1 then
Int.max (0, #textLength buffer - 2) Int.max (0, #textLength buffer - 1)
else else
endOfLineIdx endOfLineIdx
@@ -256,8 +256,8 @@ struct
val lineIdx = Cursor.vi0 (buffer, lineIdx) val lineIdx = Cursor.vi0 (buffer, lineIdx)
val lineIdx = val lineIdx =
if lineIdx >= #textLength buffer - 2 then if lineIdx >= #textLength buffer - 1 then
Int.max (0, #textLength buffer - 2) Int.max (0, #textLength buffer - 1)
else else
lineIdx lineIdx
in in

View File

@@ -3,11 +3,6 @@ struct
structure TC = TextConstants structure TC = TextConstants
structure Utils = TextBuilderUtils structure Utils = TextBuilderUtils
fun isSecondLastChr (pos, str, tl) =
case tl of
[] => pos = String.size str - 2
| _ => false
fun goToFirstLineAfter fun goToFirstLineAfter
(stl, ltl, posY, lineNumber, absIdx, cursorIdx, env, acc) = (stl, ltl, posY, lineNumber, absIdx, cursorIdx, env, acc) =
case (stl, ltl) of case (stl, ltl) of
@@ -140,11 +135,6 @@ struct
#"\n" => #"\n" =>
if lineNumber + 1 > #lastLineNumber env then if lineNumber + 1 > #lastLineNumber env then
acc acc
else if isSecondLastChr (pos, str, stl) then
if absIdx = cursorIdx then
Utils.makeCursor (#startX env, posY + TC.ySpace, env) :: acc
else
acc
else else
let let
val acc = val acc =

View File

@@ -237,11 +237,6 @@ struct
| #"\n" => | #"\n" =>
if lineNumber + 1 > #lastLineNumber env then if lineNumber + 1 > #lastLineNumber env then
acc acc
else if isSecondLastChr (pos, str, stl) then
if absIdx = cursorIdx then
Utils.makeCursor (#startX env, posY + TC.ySpace, env) :: acc
else
acc
else else
let let
val acc = val acc =

View File

@@ -119,17 +119,17 @@ struct
(* assert *) (* assert *)
Expect.isTrue (oldCursorIdx = 0 andalso cursorIdx = 1) Expect.isTrue (oldCursorIdx = 0 andalso cursorIdx = 1)
end) 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 let
(* arrange *) (* arrange *)
val app = TestUtils.init "hello world" val app = TestUtils.init "hello world\n"
val app = AppWith.idx (app, 10) val app = AppWith.idx (app, 11)
(* act *) (* act *)
val {cursorIdx, ...} = TestUtils.update (app, CHAR_EVENT #"l") val {cursorIdx, ...} = TestUtils.update (app, CHAR_EVENT #"l")
in in
(* assert *) (* assert *)
Expect.isTrue (cursorIdx = 10) Expect.isTrue (cursorIdx = 11)
end) end)
, test "moves cursor to char past newline when newline is preceded by char" , test "moves cursor to char past newline when newline is preceded by char"
(fn _ => (fn _ =>
@@ -1362,13 +1362,14 @@ struct
* *) * *)
let let
(* arrange *) (* arrange *)
val app = TestUtils.init "01234\n56789\n" val str = "01234\n56789\n"
val app = TestUtils.init str
(* act *) (* act *)
val app = TestUtils.update (app, CHAR_EVENT #"G") val app = TestUtils.update (app, CHAR_EVENT #"G")
in in
(* assert *) (* assert *)
Expect.isTrue (getChr app = #"9") Expect.isTrue (#cursorIdx app = String.size str - 1)
end)] end)]
val percentMove = describe "move motion '%'" val percentMove = describe "move motion '%'"