refactor LineGap.append function to track textLength and lineLength

This commit is contained in:
2025-09-14 12:27:11 +01:00
parent 9927e6bf1c
commit 52f0da2751

View File

@@ -1092,6 +1092,8 @@ struct
, line , line
, leftLines , leftLines
, rightLines , rightLines
, textLength
, lineLength
) = ) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
(rStrHd :: rStrTl, rLnHd :: rLnTl) => (rStrHd :: rStrTl, rLnHd :: rLnTl) =>
@@ -1122,6 +1124,8 @@ struct
, line + Vector.length rLnHd , line + Vector.length rLnHd
, newLlnHd :: lLnTl , newLlnHd :: lLnTl
, rLnTl , rLnTl
, textLength
, lineLength
) )
end end
else else
@@ -1134,6 +1138,8 @@ struct
, line + Vector.length rLnHd , line + Vector.length rLnHd
, rLnHd :: leftLines , rLnHd :: leftLines
, rLnTl , rLnTl
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
(* left side is empty; we are at start *) (* left side is empty; we are at start *)
@@ -1146,6 +1152,8 @@ struct
, Vector.length rLnHd , Vector.length rLnHd
, [rLnHd] , [rLnHd]
, rLnTl , rLnTl
, textLength
, lineLength
)) ))
| (_, _) => | (_, _) =>
(* we have reached the end, and right side is empty *) (* we have reached the end, and right side is empty *)
@@ -1167,7 +1175,9 @@ struct
) )
in in
{ idx = idx + String.size newString { idx = idx + String.size newString
, textLength = textLength
, line = line + Vector.length newLines , line = line + Vector.length newLines
, lineLength = lineLength
, leftStrings = newLstrHd :: lStrTl , leftStrings = newLstrHd :: lStrTl
, leftLines = newLlnHd :: lLnTl , leftLines = newLlnHd :: lLnTl
, rightStrings = [] , rightStrings = []
@@ -1176,7 +1186,9 @@ struct
end end
else else
{ idx = idx + String.size newString { idx = idx + String.size newString
, textLength = textLength
, line = line + Vector.length newLines , line = line + Vector.length newLines
, lineLength = lineLength
, leftStrings = newString :: leftStrings , leftStrings = newString :: leftStrings
, leftLines = newLines :: leftLines , leftLines = newLines :: leftLines
, rightStrings = [] , rightStrings = []
@@ -1184,19 +1196,30 @@ struct
} }
| (_, _) => | (_, _) =>
{ idx = idx + String.size newString { idx = idx + String.size newString
, textLength = textLength
, line = line + Vector.length newLines , line = line + Vector.length newLines
, lineLength = lineLength
, leftStrings = newString :: leftStrings , leftStrings = newString :: leftStrings
, leftLines = newLines :: leftLines , leftLines = newLines :: leftLines
, rightStrings = [] , rightStrings = []
, rightLines = [] , rightLines = []
}) })
fun append fun append (newString, buffer) =
( newString
, {idx, line, leftStrings, leftLines, rightStrings, rightLines}: t
) =
let let
val
{ idx
, line
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
} = buffer
val newTextLength = textLength + String.size newString
val newLines = countLineBreaks newString val newLines = countLineBreaks newString
val newLineLength = lineLength + Vector.length newLines
in in
helpGoToEndAndAppend helpGoToEndAndAppend
( newString ( newString
@@ -1207,6 +1230,8 @@ struct
, line , line
, leftLines , leftLines
, rightLines , rightLines
, textLength
, lineLength
) )
end end