update all functions in line_gap.sml to track textLength and lineLength, except for test code and delete functions

This commit is contained in:
2025-09-14 12:41:50 +01:00
parent 52f0da2751
commit 92a98bfb56

View File

@@ -2283,7 +2283,15 @@ struct
end end
fun helpGoToStart fun helpGoToStart
(idx, line, leftStrings, leftLines, rightStrings, rightLines) = ( idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
) =
case (leftStrings, leftLines) of case (leftStrings, leftLines) of
(lStrHd :: lStrTl, lLnHd :: lLnTl) => (lStrHd :: lStrTl, lLnHd :: lLnTl) =>
(case (rightStrings, rightLines) of (case (rightStrings, rightLines) of
@@ -2310,6 +2318,8 @@ struct
, lLnTl , lLnTl
, newRstrHd :: rStrTl , newRstrHd :: rStrTl
, newRlnHd :: rLnTl , newRlnHd :: rLnTl
, textLength
, lineLength
) )
end end
else else
@@ -2320,6 +2330,8 @@ struct
, lLnTl , lLnTl
, lStrHd :: rightStrings , lStrHd :: rightStrings
, lLnHd :: rightLines , lLnHd :: rightLines
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
(* rightStrings and rightLines are both empty *) (* rightStrings and rightLines are both empty *)
@@ -2330,22 +2342,56 @@ struct
, lLnTl , lLnTl
, [lStrHd] , [lStrHd]
, [lLnHd] , [lLnHd]
, textLength
, lineLength
)) ))
| (_, _) => | (_, _) =>
(* left strings are empty, meaning we are at start and can return *) (* left strings are empty, meaning we are at start and can return *)
{ idx = idx { idx = idx
, textLength = textLength
, line = line , line = line
, lineLength = lineLength
, leftStrings = [] , leftStrings = []
, leftLines = [] , leftLines = []
, rightStrings = rightStrings , rightStrings = rightStrings
, rightLines = rightLines , rightLines = rightLines
} }
fun goToStart fun goToStart (buffer: t) =
({idx, line, leftStrings, leftLines, rightStrings, rightLines}: t) = let
helpGoToStart (idx, line, leftStrings, leftLines, rightStrings, rightLines) val
{ idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
} = buffer
in
helpGoToStart
( idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
)
end
fun helpGoToEnd (idx, line, leftStrings, leftLines, rightStrings, rightLines) = fun helpGoToEnd
( idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
(rStrHd :: rStrTl, rLnHd :: rLnTl) => (rStrHd :: rStrTl, rLnHd :: rLnTl) =>
(case (leftStrings, leftLines) of (case (leftStrings, leftLines) of
@@ -2372,6 +2418,8 @@ struct
, newLlnHd :: lLnTl , newLlnHd :: lLnTl
, rStrTl , rStrTl
, rLnTl , rLnTl
, textLength
, lineLength
) )
end end
else else
@@ -2382,6 +2430,8 @@ struct
, rLnHd :: leftLines , rLnHd :: leftLines
, rStrTl , rStrTl
, rLnTl , rLnTl
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
(* rightStrings and rightLines are both empty *) (* rightStrings and rightLines are both empty *)
@@ -2392,19 +2442,45 @@ struct
, rLnHd :: leftLines , rLnHd :: leftLines
, rStrTl , rStrTl
, rLnTl , rLnTl
, textLength
, lineLength
)) ))
| (_, _) => | (_, _) =>
(* rightStrings strings are empty, meaning we are at end and can return *) (* rightStrings strings are empty, meaning we are at end and can return *)
{ idx = idx { idx = idx
, textLength = textLength
, line = line , line = line
, lineLength = lineLength
, leftStrings = leftStrings , leftStrings = leftStrings
, leftLines = leftLines , leftLines = leftLines
, rightStrings = [] , rightStrings = []
, rightLines = [] , rightLines = []
} }
fun goToEnd ({idx, line, leftStrings, leftLines, rightStrings, rightLines}: t) = fun goToEnd (buffer: t) =
helpGoToEnd (idx, line, leftStrings, leftLines, rightStrings, rightLines) let
val
{ idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
} = buffer
in
helpGoToEnd
( idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
)
end
(* function to abstract leftwards movement. (* function to abstract leftwards movement.
* if the left hd and the right hd can be joined in one node * if the left hd and the right hd can be joined in one node
@@ -2432,6 +2508,8 @@ struct
, lStrTl , lStrTl
, lLnHd , lLnHd
, lLnTl , lLnTl
, textLength
, lineLength
, fGoLeft , fGoLeft
) = ) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
@@ -2459,6 +2537,8 @@ struct
, lLnTl , lLnTl
, newRstrHd :: rStrTl , newRstrHd :: rStrTl
, newRlnHd :: rLnTl , newRlnHd :: rLnTl
, textLength
, lineLength
) )
end end
else else
@@ -2471,6 +2551,8 @@ struct
, lLnTl , lLnTl
, lStrHd :: rightStrings , lStrHd :: rightStrings
, lLnHd :: rightLines , lLnHd :: rightLines
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
(* right side is empty, so just move left without joining *) (* right side is empty, so just move left without joining *)
@@ -2482,6 +2564,8 @@ struct
, lLnTl , lLnTl
, [lStrHd] , [lStrHd]
, [lLnHd] , [lLnHd]
, textLength
, lineLength
) )
(* same as moveLeft function, except it move rightwards instead *) (* same as moveLeft function, except it move rightwards instead *)
@@ -2497,6 +2581,8 @@ struct
, rStrTl , rStrTl
, rLnHd , rLnHd
, rLnTl , rLnTl
, textLength
, lineLength
, fGoRight , fGoRight
) = ) =
case (leftStrings, leftLines) of case (leftStrings, leftLines) of
@@ -2524,6 +2610,8 @@ struct
, newLlnHd :: lLnTl , newLlnHd :: lLnTl
, rStrTl , rStrTl
, rLnTl , rLnTl
, textLength
, lineLength
) )
end end
else else
@@ -2536,6 +2624,8 @@ struct
, rLnHd :: leftLines , rLnHd :: leftLines
, rStrTl , rStrTl
, rLnTl , rLnTl
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
(* left side is empty, so just move rightwards without joining *) (* left side is empty, so just move rightwards without joining *)
@@ -2547,16 +2637,29 @@ struct
, [rLnHd] , [rLnHd]
, rStrTl , rStrTl
, rLnTl , rLnTl
, textLength
, lineLength
) )
fun helpGoToLineLeft fun helpGoToLineLeft
(idx, line, searchLine, leftStrings, leftLines, rightStrings, rightLines) = ( idx
, line
, searchLine
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
) =
case (leftStrings, leftLines) of case (leftStrings, leftLines) of
(lStrHd :: lStrTl, lLnHd :: lLnTl) => (lStrHd :: lStrTl, lLnHd :: lLnTl) =>
if searchLine >= line - Vector.length lLnHd then if searchLine >= line - Vector.length lLnHd then
(* line is at left head, so place it to the right and return *) (* line is at left head, so place it to the right and return *)
{ idx = idx - String.size lStrHd { idx = idx - String.size lStrHd
, textLength = textLength
, line = line - Vector.length lLnHd , line = line - Vector.length lLnHd
, lineLength = lineLength
, leftStrings = lStrTl , leftStrings = lStrTl
, leftLines = lLnTl , leftLines = lLnTl
, rightStrings = lStrHd :: rightStrings , rightStrings = lStrHd :: rightStrings
@@ -2576,12 +2679,16 @@ struct
, lStrTl , lStrTl
, lLnHd , lLnHd
, lLnTl , lLnTl
, textLength
, lineLength
, helpGoToLineLeft , helpGoToLineLeft
) )
| (_, _) => | (_, _) =>
(* left side is empty, so just return *) (* left side is empty, so just return *)
{ idx = idx { idx = idx
, textLength = textLength
, line = line , line = line
, lineLength = lineLength
, leftStrings = [] , leftStrings = []
, leftLines = [] , leftLines = []
, rightStrings = rightStrings , rightStrings = rightStrings
@@ -2589,13 +2696,24 @@ struct
} }
fun helpGoToLineRight fun helpGoToLineRight
(idx, line, searchLine, leftStrings, leftLines, rightStrings, rightLines) = ( idx
, line
, searchLine
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
(rStrHd :: rStrTl, rLnHd :: rLnTl) => (rStrHd :: rStrTl, rLnHd :: rLnTl) =>
if line + Vector.length rLnHd >= searchLine then if line + Vector.length rLnHd >= searchLine then
(* searchLine is in rStrHd/rLnHd, so return *) (* searchLine is in rStrHd/rLnHd, so return *)
{ idx = idx { idx = idx
, textLength = textLength
, line = line , line = line
, lineLength = lineLength
, leftStrings = leftStrings , leftStrings = leftStrings
, leftLines = leftLines , leftLines = leftLines
, rightStrings = rightStrings , rightStrings = rightStrings
@@ -2615,12 +2733,16 @@ struct
, rStrTl , rStrTl
, rLnHd , rLnHd
, rLnTl , rLnTl
, textLength
, lineLength
, helpGoToLineRight , helpGoToLineRight
) )
| (_, _) => | (_, _) =>
(* right side is empty, so just return *) (* right side is empty, so just return *)
{ idx = idx { idx = idx
, textLength = textLength
, line = line , line = line
, lineLength = lineLength
, leftStrings = leftStrings , leftStrings = leftStrings
, leftLines = leftLines , leftLines = leftLines
, rightStrings = [] , rightStrings = []
@@ -2629,7 +2751,16 @@ struct
fun goToLine (searchLine, buffer: t) = fun goToLine (searchLine, buffer: t) =
let let
val {idx, line, leftStrings, leftLines, rightStrings, rightLines} = buffer val
{ idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
} = buffer
in in
(* we compare current line with searchLine - 1 (* we compare current line with searchLine - 1
* because if searchLine - 1 is here, * because if searchLine - 1 is here,
@@ -2644,6 +2775,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
else if searchLine - 1 > line then else if searchLine - 1 > line then
helpGoToLineRight helpGoToLineRight
@@ -2654,13 +2787,24 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
else else
buffer buffer
end end
fun helpGoToIdxLeft fun helpGoToIdxLeft
(idx, line, searchIdx, leftStrings, leftLines, rightStrings, rightLines) = ( idx
, line
, searchIdx
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
) =
case (leftStrings, leftLines) of case (leftStrings, leftLines) of
(lStrHd :: lStrTl, lLnHd :: lLnTl) => (lStrHd :: lStrTl, lLnHd :: lLnTl) =>
if searchIdx < idx - String.size lStrHd then if searchIdx < idx - String.size lStrHd then
@@ -2677,12 +2821,16 @@ struct
, lStrTl , lStrTl
, lLnHd , lLnHd
, lLnTl , lLnTl
, textLength
, lineLength
, helpGoToIdxLeft , helpGoToIdxLeft
) )
else else
(* line is at left head, so place it to the right and return *) (* line is at left head, so place it to the right and return *)
{ idx = idx - String.size lStrHd { idx = idx - String.size lStrHd
, textLength = textLength
, line = line - Vector.length lLnHd , line = line - Vector.length lLnHd
, lineLength = lineLength
, leftStrings = lStrTl , leftStrings = lStrTl
, leftLines = lLnTl , leftLines = lLnTl
, rightStrings = lStrHd :: rightStrings , rightStrings = lStrHd :: rightStrings
@@ -2691,7 +2839,9 @@ struct
| (_, _) => | (_, _) =>
(* left side is empty, so just return *) (* left side is empty, so just return *)
{ idx = idx { idx = idx
, textLength = textLength
, line = line , line = line
, lineLength = lineLength
, leftStrings = [] , leftStrings = []
, leftLines = [] , leftLines = []
, rightStrings = rightStrings , rightStrings = rightStrings
@@ -2699,7 +2849,16 @@ struct
} }
fun helpGoToIdxRight fun helpGoToIdxRight
(idx, line, searchIdx, leftStrings, leftLines, rightStrings, rightLines) = ( idx
, line
, searchIdx
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
(rStrHd :: rStrTl, rLnHd :: rLnTl) => (rStrHd :: rStrTl, rLnHd :: rLnTl) =>
if searchIdx > idx + String.size rStrHd then if searchIdx > idx + String.size rStrHd then
@@ -2716,12 +2875,16 @@ struct
, rStrTl , rStrTl
, rLnHd , rLnHd
, rLnTl , rLnTl
, textLength
, lineLength
, helpGoToIdxRight , helpGoToIdxRight
) )
else else
(* searchLine is in rStrHd/rLnHd, so return *) (* searchLine is in rStrHd/rLnHd, so return *)
{ idx = idx { idx = idx
, textLength = textLength
, line = line , line = line
, lineLength = lineLength
, leftStrings = leftStrings , leftStrings = leftStrings
, leftLines = leftLines , leftLines = leftLines
, rightStrings = rightStrings , rightStrings = rightStrings
@@ -2730,7 +2893,9 @@ struct
| (_, _) => | (_, _) =>
(* right side is empty, so just return *) (* right side is empty, so just return *)
{ idx = idx { idx = idx
, textLength = textLength
, line = line , line = line
, lineLength = lineLength
, leftStrings = leftStrings , leftStrings = leftStrings
, leftLines = leftLines , leftLines = leftLines
, rightStrings = [] , rightStrings = []
@@ -2739,7 +2904,16 @@ struct
fun goToIdx (searchIdx, buffer: t) = fun goToIdx (searchIdx, buffer: t) =
let let
val {idx, line, leftStrings, leftLines, rightStrings, rightLines} = buffer val
{ idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
} = buffer
in in
if searchIdx < idx then if searchIdx < idx then
helpGoToIdxLeft helpGoToIdxLeft
@@ -2750,6 +2924,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
else if searchIdx > idx then else if searchIdx > idx then
helpGoToIdxRight helpGoToIdxRight
@@ -2760,6 +2936,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
else else
buffer buffer
@@ -2826,6 +3004,7 @@ struct
, rightStrings , rightStrings
, rightLines , rightLines
, line = curLine , line = curLine
, ...
} = buffer } = buffer
in in
if findIdx < curIdx then if findIdx < curIdx then