begin tracking and adjusting textLength/lineLength for delete functions

This commit is contained in:
2025-09-14 13:12:49 +01:00
parent eebf793fbf
commit 76dd6b2b2a

View File

@@ -1246,6 +1246,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) = ) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
(rightStringsHd :: rightStringsTl, rightLinesHd :: rightLinesTl) => (rightStringsHd :: rightStringsTl, rightLinesHd :: rightLinesTl) =>
@@ -1253,7 +1255,7 @@ struct
val nextIdx = moveIdx + String.size rightStringsHd val nextIdx = moveIdx + String.size rightStringsHd
in in
if nextIdx < finish then if nextIdx < finish then
(* Keep moving right. *) (* Remove string/line head and keep moving right. *)
deleteRightFromHere deleteRightFromHere
( origIdx ( origIdx
, origLine , origLine
@@ -1263,10 +1265,15 @@ struct
, leftLines , leftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
, textLength - String.size rightStringsHd
, lineLength - Vector.length rightLinesHd
) )
else if nextIdx > finish then else if nextIdx > finish then
(* Base case: delete from the start of this string and stop moving. *) (* Base case: delete from the start of this string and stop moving. *)
let let
val oldNodeTextLength = String.size rightStringsHd
val oldNodeLineLength = Vector.length rightLinesHd
(* Delete part of string. *) (* Delete part of string. *)
val newStrStart = finish - moveIdx val newStrStart = finish - moveIdx
val newStr = String.substring val newStr = String.substring
@@ -1296,9 +1303,19 @@ struct
end end
else else
rightLinesHd (* empty vector *) rightLinesHd (* empty vector *)
val newNodeTextLength = String.size newString
val newNodeLineLength = Vector.length newLines
val textLengthDifference = oldNodeTextLength - newNodeLineLength
val textLength = textLength - textLengthDifference
val lineLengthDifference = oldNodeLineLength - newNodeLineLength
val lineLength = lineLength - lineLengthDifference
in in
{ idx = origIdx { idx = origIdx
, textLength = textLength
, line = origLine , line = origLine
, lineLength = lineLength
, leftStrings = leftStrings , leftStrings = leftStrings
, leftLines = leftLines , leftLines = leftLines
, rightStrings = newStr :: rightStringsTl , rightStrings = newStr :: rightStringsTl
@@ -1308,7 +1325,9 @@ struct
else else
(* Delete this node fully, but delete no further. *) (* Delete this node fully, but delete no further. *)
{ idx = origIdx { idx = origIdx
, textLength = textLength - String.size rightStringsHd
, line = origLine , line = origLine
, lineLength = lineLength - Vector.length rightLinesHd
, leftStrings = leftStrings , leftStrings = leftStrings
, leftLines = leftLines , leftLines = leftLines
, rightStrings = rightStringsTl , rightStrings = rightStringsTl
@@ -1317,7 +1336,9 @@ struct
end end
| (_, _) => | (_, _) =>
{ idx = 0 { idx = 0
, textLength = textLength
, line = 0 , line = 0
, lineLength = lineLength
, leftStrings = [] , leftStrings = []
, leftLines = [] , leftLines = []
, rightStrings = rightStrings , rightStrings = rightStrings
@@ -1333,6 +1354,8 @@ struct
, leftLines: int vector list , leftLines: int vector list
, rightStrings: string list , rightStrings: string list
, rightLines: int vector list , rightLines: int vector list
, textLength
, lineLength
) = ) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
(rightStringsHd :: rightStringsTl, rightLinesHd :: rightLinesTl) => (rightStringsHd :: rightStringsTl, rightLinesHd :: rightLinesTl) =>
@@ -1383,6 +1406,8 @@ struct
, newLeftLines , newLeftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
, textLength
, lineLength
) )
end end
else else
@@ -1396,6 +1421,8 @@ struct
, rightLinesHd :: leftLines , rightLinesHd :: leftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
(* Can't join heads while staying in limit, so just cons. *) (* Can't join heads while staying in limit, so just cons. *)
@@ -1408,12 +1435,17 @@ struct
, rightLinesHd :: leftLines , rightLinesHd :: leftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
, textLength
, lineLength
)) ))
else if nextIdx > start then else if nextIdx > start then
if nextIdx < finish then if nextIdx < finish then
(* Start deleting from the end of this string, (* Start deleting from the end of this string,
* and then continue deleting rightwards. *) * and then continue deleting rightwards. *)
let let
val oldNodeTextLength = String.size rightStringsHd
val oldNodeLineLength = Vector.length rightLinesHd
val length = start - curIdx val length = start - curIdx
val newString = String.substring (rightStringsHd, 0, length) val newString = String.substring (rightStringsHd, 0, length)
@@ -1429,6 +1461,11 @@ struct
in in
VectorSlice.vector slice VectorSlice.vector slice
end end
val newNodeTextLength = String.size newString
val newNodeLineLength = Vector.length newLines
val textLength = oldNodeTextLength - newNodeTextLength
val lineLength = oldNodeLineLength - newNodeLineLength
in in
(* Try joining new string with left head if possible. *) (* Try joining new string with left head if possible. *)
(case (leftStrings, leftLines) of (case (leftStrings, leftLines) of
@@ -1470,6 +1507,8 @@ struct
, newLeftLinesHd :: leftLinesTl , newLeftLinesHd :: leftLinesTl
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
, textLength
, lineLength
) )
end end
else else
@@ -1484,6 +1523,8 @@ struct
, newLines :: leftLines , newLines :: leftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
deleteRightFromHere deleteRightFromHere
@@ -1495,11 +1536,16 @@ struct
, newLines :: leftLines , newLines :: leftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
, textLength
, lineLength
)) ))
end end
else if nextIdx > finish then else if nextIdx > finish then
(* Base case: delete from the middle part of this string. *) (* Base case: delete from the middle part of this string. *)
let let
val oldNodeTextLength = String.size rightStringsHd
val oldNodeLineLength = String.size rightLinesHd
val sub1Length = start - curIdx val sub1Length = start - curIdx
val sub1 = String.substring (rightStringsHd, 0, sub1Length) val sub1 = String.substring (rightStringsHd, 0, sub1Length)
val sub1LineEnd = binSearch val sub1LineEnd = binSearch
@@ -1532,9 +1578,15 @@ struct
) )
else else
Vector.fromList [] Vector.fromList []
val newTextLength = String.size sub1 + String.size sub1
val newLineLength =
Vector.length sub1Lines + Vector.length sub2Lines
in in
{ idx = curIdx + String.size sub1 { idx = curIdx + String.size sub1
, textLength = newTextLength
, line = curLine + Vector.length sub1Lines , line = curLine + Vector.length sub1Lines
, lineLength = newLineLength
, leftStrings = sub1 :: leftStrings , leftStrings = sub1 :: leftStrings
, leftLines = sub1Lines :: leftLines , leftLines = sub1Lines :: leftLines
, rightStrings = sub2 :: rightStringsTl , rightStrings = sub2 :: rightStringsTl
@@ -1545,6 +1597,9 @@ struct
(* nextIdx = finish (* nextIdx = finish
* Base case: delete from middle to end of this string, keeping start. *) * Base case: delete from middle to end of this string, keeping start. *)
let let
val oldNodeTextLength = String.size rightStringsHd
val oldNodeLineLength = Vector.length rightLinesHd
val strLength = start - curIdx val strLength = start - curIdx
val str = String.substring (rightStringsHd, 0, strLength) val str = String.substring (rightStringsHd, 0, strLength)
val midpoint = binSearch (String.size str - 1, rightLinesHd) val midpoint = binSearch (String.size str - 1, rightLinesHd)
@@ -1558,9 +1613,16 @@ struct
in in
VectorSlice.vector slice VectorSlice.vector slice
end end
val newNodeTextLength = String.size str
val newNodeLineLength = Vector.length newLeftLines
val newTextLength = oldNodeTextLength - newNodeTextLength
val newLineLength = oldNodeLineLength - newNodeLineLength
in in
{ idx = curIdx + String.size str { idx = curIdx + String.size str
, textLength = newTextLength
, line = curLine + Vector.length newLeftLines , line = curLine + Vector.length newLeftLines
, lineLength = newLineLength
, leftStrings = str :: leftStrings , leftStrings = str :: leftStrings
, leftLines = newLeftLines :: leftLines , leftLines = newLeftLines :: leftLines
, rightStrings = rightStringsTl , rightStrings = rightStringsTl
@@ -1583,11 +1645,15 @@ struct
, rightLinesHd :: leftLines , rightLinesHd :: leftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
, textLength
, lineLength
) )
end end
| (_, _) => | (_, _) =>
{ idx = curIdx { idx = curIdx
, textLength = textLength
, line = curLine , line = curLine
, lineLength = lineLength
, leftStrings = leftStrings , leftStrings = leftStrings
, leftLines = leftLines , leftLines = leftLines
, rightStrings = rightStrings , rightStrings = rightStrings