implemented textLength and lineLength tracking in line_gap.sml: need to check if they are valid next

This commit is contained in:
2025-09-14 15:23:53 +01:00
parent 76dd6b2b2a
commit 1c59dbf943

View File

@@ -1304,11 +1304,11 @@ struct
else else
rightLinesHd (* empty vector *) rightLinesHd (* empty vector *)
val newNodeTextLength = String.size newString val newNodeTextLength = String.size newStr
val newNodeLineLength = Vector.length newLines val textLengthDifference = oldNodeTextLength - newNodeTextLength
val textLengthDifference = oldNodeTextLength - newNodeLineLength
val textLength = textLength - textLengthDifference val textLength = textLength - textLengthDifference
val newNodeLineLength = Vector.length newLines
val lineLengthDifference = oldNodeLineLength - newNodeLineLength val lineLengthDifference = oldNodeLineLength - newNodeLineLength
val lineLength = lineLength - lineLengthDifference val lineLength = lineLength - lineLengthDifference
in in
@@ -1463,9 +1463,14 @@ struct
end end
val newNodeTextLength = String.size newString val newNodeTextLength = String.size newString
val textLengthDifference =
oldNodeTextLength - newNodeTextLength
val textLength = textLength - textLengthDifference
val newNodeLineLength = Vector.length newLines val newNodeLineLength = Vector.length newLines
val textLength = oldNodeTextLength - newNodeTextLength val lineLengthDifference =
val lineLength = oldNodeLineLength - newNodeLineLength oldNodeLineLength - newNodeLineLength
val lineLength = lineLength - lineLengthDifference
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
@@ -1544,7 +1549,7 @@ struct
(* 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 oldNodeTextLength = String.size rightStringsHd
val oldNodeLineLength = String.size rightLinesHd val oldNodeLineLength = Vector.length rightLinesHd
val sub1Length = start - curIdx val sub1Length = start - curIdx
val sub1 = String.substring (rightStringsHd, 0, sub1Length) val sub1 = String.substring (rightStringsHd, 0, sub1Length)
@@ -1615,9 +1620,14 @@ struct
end end
val newNodeTextLength = String.size str val newNodeTextLength = String.size str
val textLengthDifference =
oldNodeTextLength - newNodeTextLength
val newTextLength = textLength - textLengthDifference
val newNodeLineLength = Vector.length newLeftLines val newNodeLineLength = Vector.length newLeftLines
val newTextLength = oldNodeTextLength - newNodeTextLength val lineLengthDifference =
val newLineLength = oldNodeLineLength - newNodeLineLength oldNodeLineLength - newNodeLineLength
val newLineLength = lineLength - lineLengthDifference
in in
{ idx = curIdx + String.size str { idx = curIdx + String.size str
, textLength = newTextLength , textLength = newTextLength
@@ -1661,7 +1671,16 @@ struct
} }
fun deleteLeftFromHere fun deleteLeftFromHere
(start, curIdx, curLine, leftStrings, leftLines, rightStrings, rightLines) = ( start
, curIdx
, curLine
, leftStrings
, leftLines
, rightStrings
, rightLines
, textLength
, lineLength
) =
case (leftStrings, leftLines) of case (leftStrings, leftLines) of
(leftStringsHd :: leftStringsTl, leftLinesHd :: leftLinesTl) => (leftStringsHd :: leftStringsTl, leftLinesHd :: leftLinesTl) =>
let let
@@ -1678,10 +1697,15 @@ struct
, leftLinesTl , leftLinesTl
, rightStrings , rightStrings
, rightLines , rightLines
, textLength - String.size leftStringsHd
, lineLength - Vector.length leftLinesHd
) )
else if start > prevIdx then else if start > prevIdx then
(* Base case: delete end part of this string and return. *) (* Base case: delete end part of this string and return. *)
let let
val oldNodeTextLength = String.size leftStringsHd
val oldNodeLineLength = Vector.length leftLinesHd
val length = start - prevIdx val length = start - prevIdx
val newStr = String.substring (leftStringsHd, 0, length) val newStr = String.substring (leftStringsHd, 0, length)
val newLines = val newLines =
@@ -1696,9 +1720,19 @@ struct
end end
else else
Vector.fromList [] Vector.fromList []
val newNodeTextLength = String.size newStr
val textLengthDifference = oldNodeTextLength - newNodeTextLength
val newTextLength = textLength - textLengthDifference
val newNodeLineLength = Vector.length newLines
val lineLengthDifference = oldNodeLineLength - newNodeLineLength
val newLineLength = lineLength - lineLengthDifference
in in
{ idx = prevIdx + String.size newStr { idx = prevIdx + String.size newStr
, textLength = newTextLength
, line = prevLine + Vector.length newLines , line = prevLine + Vector.length newLines
, lineLength = newLineLength
, leftStrings = newStr :: leftStringsTl , leftStrings = newStr :: leftStringsTl
, leftLines = newLines :: leftLinesTl , leftLines = newLines :: leftLinesTl
, rightStrings = rightStrings , rightStrings = rightStrings
@@ -1714,6 +1748,8 @@ struct
, leftLines = leftLinesTl , leftLines = leftLinesTl
, rightStrings = rightStrings , rightStrings = rightStrings
, rightLines = rightLines , rightLines = rightLines
, textLength = textLength - String.size leftStringsHd
, lineLength = lineLength - Vector.length leftLinesHd
} }
end end
| (_, _) => | (_, _) =>
@@ -1723,6 +1759,8 @@ struct
, leftLines = leftLines , leftLines = leftLines
, rightStrings = rightStrings , rightStrings = rightStrings
, rightLines = rightLines , rightLines = rightLines
, textLength = textLength
, lineLength = lineLength
} }
fun deleteFromLetAndRight fun deleteFromLetAndRight
@@ -1734,6 +1772,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) = ) =
let let
val val
@@ -1743,6 +1783,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
} = deleteRightFromHere } = deleteRightFromHere
( curIdx ( curIdx
, curLine , curLine
@@ -1752,6 +1794,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
in in
deleteLeftFromHere deleteLeftFromHere
@@ -1762,6 +1806,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
end end
@@ -1774,6 +1820,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) = ) =
case (leftStrings, leftLines) of case (leftStrings, leftLines) of
(leftStringsHd :: leftStringsTl, leftLinesHd :: leftLinesTl) => (leftStringsHd :: leftStringsTl, leftLinesHd :: leftLinesTl) =>
@@ -1826,6 +1874,8 @@ struct
, leftLinesTl , leftLinesTl
, newRightStrings , newRightStrings
, newRightLines , newRightLines
, textLength
, lineLength
) )
end end
else else
@@ -1839,6 +1889,8 @@ struct
, leftLinesTl , leftLinesTl
, leftStringsHd :: rightStrings , leftStringsHd :: rightStrings
, leftLinesHd :: rightLines , leftLinesHd :: rightLines
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
(* Base case: reached empty list while trying to move leftwards. (* Base case: reached empty list while trying to move leftwards.
@@ -1852,12 +1904,17 @@ struct
, leftLinesTl , leftLinesTl
, [leftStringsHd] , [leftStringsHd]
, [leftLinesHd] , [leftLinesHd]
, textLength
, lineLength
)) ))
else if prevIdx < finish then else if prevIdx < finish then
if prevIdx > start then if prevIdx > start then
(* Delete from start point of this string, (* Delete from start point of this string,
* and then call function to continue deleting leftwards. *) * and then call function to continue deleting leftwards. *)
let let
val oldNodeTextLength = String.size leftStringsHd
val oldNodeLineLength = Vector.length leftLinesHd
val stringStart = finish - prevIdx val stringStart = finish - prevIdx
val newString = String.substring val newString = String.substring
( leftStringsHd ( leftStringsHd
@@ -1881,6 +1938,16 @@ struct
val newRightStrings = newString :: rightStrings val newRightStrings = newString :: rightStrings
val newRightLines = newLines :: rightLines val newRightLines = newLines :: rightLines
val prevLine = curLine - Vector.length leftLinesHd val prevLine = curLine - Vector.length leftLinesHd
val newNodeTextLength = String.size newString
val textLengthDifference =
oldNodeTextLength - newNodeTextLength
val textLength = textLength - textLengthDifference
val newNodeLineLength = Vector.length newLines
val lineLengthDifference =
oldNodeLineLength - newNodeLineLength
val lineLength = lineLength - lineLengthDifference
in in
deleteLeftFromHere deleteLeftFromHere
( start ( start
@@ -1890,6 +1957,8 @@ struct
, leftLinesTl , leftLinesTl
, newRightStrings , newRightStrings
, newRightLines , newRightLines
, textLength
, lineLength
) )
end end
else if prevIdx < start then else if prevIdx < start then
@@ -1897,6 +1966,9 @@ struct
* We also have to delete in the middle of leftLinesHd in order to * We also have to delete in the middle of leftLinesHd in order to
* do this. *) * do this. *)
let let
val oldNodeTextLength = String.size leftStringsHd
val oldNodeLineLength = Vector.length leftLinesHd
val sub1Length = start - prevIdx val sub1Length = start - prevIdx
val sub1 = String.substring (leftStringsHd, 0, sub1Length) val sub1 = String.substring (leftStringsHd, 0, sub1Length)
val sub2Start = finish - prevIdx val sub2Start = finish - prevIdx
@@ -1939,6 +2011,17 @@ struct
else else
Vector.fromList [] Vector.fromList []
end end
val newNodeTextLength = String.size sub1 + String.size sub2
val textLengthDifference =
oldNodeTextLength - newNodeTextLength
val textLength = textLength - textLengthDifference
val newNodeLineLength =
Vector.length sub1Lines + Vector.length sub2Lines
val lineLengthDifference =
oldNodeLineLength - newNodeLineLength
val lineLength = lineLength - lineLengthDifference
in in
{ idx = prevIdx + String.size sub1 { idx = prevIdx + String.size sub1
, line = , line =
@@ -1948,12 +2031,17 @@ struct
, leftLines = sub1Lines :: leftLinesTl , leftLines = sub1Lines :: leftLinesTl
, rightStrings = sub2 :: rightStrings , rightStrings = sub2 :: rightStrings
, rightLines = sub2Lines :: rightLines , rightLines = sub2Lines :: rightLines
, textLength = textLength
, lineLength = lineLength
} }
end end
else else
(* prevIdx = start (* prevIdx = start
* We want to delete from the start of this string and stop. *) * We want to delete from the start of this string and stop. *)
let let
val oldNodeTextLength = String.size leftStringsHd
val oldNodeLineLength = Vector.length leftLinesHd
val strStart = finish - prevIdx val strStart = finish - prevIdx
val str = String.substring val str = String.substring
( leftStringsHd ( leftStringsHd
@@ -1974,10 +2062,22 @@ struct
else else
Vector.fromList [] Vector.fromList []
end end
val newNodeTextLength = String.size str
val textLengthDifference =
oldNodeTextLength - newNodeTextLength
val textLength = textLength - textLengthDifference
val newNodeLineLength = Vector.length lines
val lineLengthDifference =
oldNodeLineLength - newNodeLineLength
val lineLength = lineLength - lineLengthDifference
in in
{ idx = prevIdx + String.size str { idx = prevIdx + String.size str
, textLength = textLength
, line = , line =
(curLine - Vector.length leftLinesHd) + String.size str (curLine - Vector.length leftLinesHd) + String.size str
, lineLength = lineLength
, leftStrings = str :: leftStringsTl , leftStrings = str :: leftStringsTl
, leftLines = lines :: leftLinesTl , leftLines = lines :: leftLinesTl
, rightStrings = rightStrings , rightStrings = rightStrings
@@ -2026,6 +2126,8 @@ struct
, leftLinesTl , leftLinesTl
, newRightStringsHd :: rightStringsTl , newRightStringsHd :: rightStringsTl
, newRightLinesHd :: rightLinesTl , newRightLinesHd :: rightLinesTl
, textLength
, lineLength
) )
end end
else else
@@ -2038,6 +2140,8 @@ struct
, leftLinesTl , leftLinesTl
, leftStringsHd :: rightStrings , leftStringsHd :: rightStrings
, leftLinesHd :: rightLines , leftLinesHd :: rightLines
, textLength
, lineLength
) )
| (_, _) => | (_, _) =>
(* Right strings and lines are empty, so can't join. *) (* Right strings and lines are empty, so can't join. *)
@@ -2049,12 +2153,16 @@ struct
, leftLinesTl , leftLinesTl
, [leftStringsHd] , [leftStringsHd]
, [leftLinesHd] , [leftLinesHd]
, textLength
, lineLength
)) ))
end end
| (_, _) => | (_, _) =>
(* Can't move further leftward so just return. *) (* Can't move further leftward so just return. *)
{ idx = 0 { idx = 0
, textLength = textLength
, line = 0 , line = 0
, lineLength = lineLength
, leftStrings = [] , leftStrings = []
, leftLines = [] , leftLines = []
, rightStrings = rightStrings , rightStrings = rightStrings
@@ -2070,6 +2178,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) = ) =
if start > curIdx then if start > curIdx then
moveRightAndDelete moveRightAndDelete
@@ -2081,6 +2191,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
else if start < curIdx then else if start < curIdx then
if finish <= curIdx then if finish <= curIdx then
@@ -2093,6 +2205,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
else else
deleteFromLetAndRight deleteFromLetAndRight
@@ -2104,6 +2218,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
else else
deleteRightFromHere deleteRightFromHere
@@ -2115,6 +2231,8 @@ struct
, leftLines , leftLines
, rightStrings , rightStrings
, rightLines , rightLines
, textLength
, lineLength
) )
in in
fun delete (start, length, buffer: t) = fun delete (start, length, buffer: t) =
@@ -2128,6 +2246,8 @@ struct
, #leftLines buffer , #leftLines buffer
, #rightStrings buffer , #rightStrings buffer
, #rightLines buffer , #rightLines buffer
, #textLength buffer
, #lineLength buffer
) )
else else
buffer buffer
@@ -3158,8 +3278,15 @@ struct
val correctIdx = calcIndexStart (#leftStrings buffer) val correctIdx = calcIndexStart (#leftStrings buffer)
val idxIsCorrect = bufferIdx = correctIdx val idxIsCorrect = bufferIdx = correctIdx
val {rightLines, rightStrings, ...} = goToStart buffer
val textLength = #textLength buffer val textLength = #textLength buffer
val textLengthIsCorrect = textLength = correctIdx val textLengthIsCorrect =
textLength = String.size (String.concat rightStrings)
val lineLength = #lineLength buffer
val correctLineLength = Vector.length (Vector.concat rightLines)
val lineLengthIsCorrect = lineLength = correctLineLength
val _ = val _ =
if idxIsCorrect then if idxIsCorrect then
@@ -3180,7 +3307,7 @@ struct
val _ = val _ =
if textLengthIsCorrect then if textLengthIsCorrect then
print "textLength is correct" print "textLength is correct\n"
else else
let let
val msg = String.concat val msg = String.concat
@@ -3195,9 +3322,12 @@ struct
print msg print msg
end end
val () = print "\n"
in in
if textLengthIsCorrect andalso idxIsCorrect then () if textLengthIsCorrect andalso idxIsCorrect andalso lineLengthIsCorrect then
else raise Fail "either index or text length is incorrect" ()
else
raise Fail "either index or idx metadata or text length is incorrect"
end end
end end
end end