fix bug revealed by checking for incorrect index metadta in line_gap.sml

This commit is contained in:
2024-07-18 15:38:31 +01:00
parent 117ffeba6e
commit 371bc093ff

View File

@@ -107,38 +107,37 @@ struct
fun calcIndexList (accIdx, lst) = fun calcIndexList (accIdx, lst) =
case lst of case lst of
[] => accIdx [] => accIdx
| hd::tl => | hd :: tl => calcIndexList (String.size hd + accIdx, tl)
calcIndexList (String.size hd + accIdx, tl)
fun calcIndexStart lst = calcIndexList (0, lst) fun calcIndexStart lst = calcIndexList (0, lst)
fun verifyIndex (buffer: t) = fun verifyIndex (buffer: t) =
let let
val bufferIdx = #idx buffer val bufferIdx = #idx buffer
val correctIdx = calcIndexStart (#leftStrings buffer) val correctIdx = calcIndexStart (#leftStrings buffer)
val _ = val _ =
if bufferIdx = correctIdx then if bufferIdx = correctIdx then
print "idx is correct\n" print "idx is correct\n"
else else
let let
val msg = String.concat [ val msg = String.concat
"idx is incorrect;", [ "idx is incorrect;"
"bufferIdx: ", , "bufferIdx: "
Int.toString bufferIdx, , Int.toString bufferIdx
"; correctIdx: ", , "; correctIdx: "
Int.toString correctIdx, , Int.toString correctIdx
"\n" , "\n"
] ]
val _ = print msg val _ = print msg
val _ = raise Size val _ = raise Size
in in
print msg print msg
end end
in in
() ()
end end
local local
@@ -1118,24 +1117,30 @@ struct
end end
else else
(* Delete this node fully, but delete no further. *) (* Delete this node fully, but delete no further. *)
let val _ = println "1085" in let
{ idx = origIdx val _ = println "1085"
, line = origLine in
, leftStrings = leftStrings { idx = origIdx
, leftLines = leftLines , line = origLine
, rightStrings = rightStringsTl , leftStrings = leftStrings
, rightLines = rightLinesTl , leftLines = leftLines
} end , rightStrings = rightStringsTl
, rightLines = rightLinesTl
}
end
end end
| (_, _) => | (_, _) =>
let val _ = println "1095" in let
{ idx = 0 val _ = println "1095"
, line = 0 in
, leftStrings = [] { idx = 0
, leftLines = [] , line = 0
, rightStrings = rightStrings , leftStrings = []
, rightLines = rightLines , leftLines = []
} end , rightStrings = rightStrings
, rightLines = rightLines
}
end
fun moveRightAndDelete fun moveRightAndDelete
( start ( start
@@ -1190,16 +1195,16 @@ struct
val newLeftLines = newLeftLinesHd :: leftLinesTl val newLeftLines = newLeftLinesHd :: leftLinesTl
val _ = println "1093" val _ = println "1093"
in in
moveRightAndDelete moveRightAndDelete
( start ( start
, finish , finish
, nextIdx , nextIdx
, curLine + Vector.length rightLinesHd , curLine + Vector.length rightLinesHd
, newLeftStrings , newLeftStrings
, newLeftLines , newLeftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
) )
end end
else else
(* Can't join heads while staying in limit, so just cons. *) (* Can't join heads while staying in limit, so just cons. *)
@@ -1297,7 +1302,24 @@ struct
else else
(* Can't join new string with left head (* Can't join new string with left head
* while staying in limit, so just cons. *) * while staying in limit, so just cons. *)
let val _ = println "1264" in let
val _ = println "1264"
in
deleteRightFromHere
( nextIdx
, nextLine
, nextIdx
, finish
, newString :: leftStrings
, newLines :: leftLines
, rightStringsTl
, rightLinesTl
)
end
| (_, _) =>
let
val _ = println "1276"
in
deleteRightFromHere deleteRightFromHere
( nextIdx ( nextIdx
, nextLine , nextLine
@@ -1307,19 +1329,8 @@ struct
, newLines :: leftLines , newLines :: leftLines
, rightStringsTl , rightStringsTl
, rightLinesTl , rightLinesTl
) end )
| (_, _) => end)
let val _ = println "1276" in
deleteRightFromHere
( nextIdx
, nextLine
, nextIdx
, finish
, newString :: leftStrings
, newLines :: leftLines
, rightStringsTl
, rightLinesTl
) end)
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. *)
@@ -1363,13 +1374,13 @@ struct
Vector.fromList [] Vector.fromList []
val _ = println "1326" val _ = println "1326"
in in
{ idx = curIdx + sub1Length { idx = curIdx + sub1Length
, line = curLine + Vector.length sub1Lines , line = curLine + Vector.length sub1Lines
, leftStrings = sub1 :: leftStrings , leftStrings = sub1 :: leftStrings
, leftLines = sub1Lines :: leftLines , leftLines = sub1Lines :: leftLines
, rightStrings = sub2 :: rightStringsTl , rightStrings = sub2 :: rightStringsTl
, rightLines = sub2Lines :: rightLinesTl , rightLines = sub2Lines :: rightLinesTl
} }
end end
else else
(* nextIdx = finish (* nextIdx = finish
@@ -1406,27 +1417,33 @@ struct
* and it may extend beyond the current head. * and it may extend beyond the current head.
* So pass the rightStringsTl and rightLinesTl to a function that * So pass the rightStringsTl and rightLinesTl to a function that
* will delete rightwards if it needs to, or else terminates. *) * will delete rightwards if it needs to, or else terminates. *)
let val _ = println "1373" in let
deleteRightFromHere val _ = println "1373"
( curIdx in
, curLine deleteRightFromHere
, curIdx ( curIdx
, finish , curLine
, leftStrings , curIdx
, leftLines , finish
, rightStringsTl , leftStrings
, rightLinesTl , leftLines
) end , rightStringsTl
, rightLinesTl
)
end
end end
| (_, _) => | (_, _) =>
let val _ = println "1386" in let
{ idx = curIdx val _ = println "1386"
, line = curLine in
, leftStrings = leftStrings { idx = curIdx
, leftLines = leftLines , line = curLine
, rightStrings = rightStrings , leftStrings = leftStrings
, rightLines = rightLines , leftLines = leftLines
} end , rightStrings = rightStrings
, rightLines = rightLines
}
end
fun deleteLeftFromHere fun deleteLeftFromHere
(start, curIdx, curLine, leftStrings, leftLines, rightStrings, rightLines) = (start, curIdx, curLine, leftStrings, leftLines, rightStrings, rightLines) =
@@ -1479,24 +1496,30 @@ struct
else else
(* start = prevIdx (* start = prevIdx
* Base case: Remove leftStrings/LinesHd without removing any further. *) * Base case: Remove leftStrings/LinesHd without removing any further. *)
let val _ = println "1446" in let
{ idx = prevIdx val _ = println "1446"
, line = prevLine in
, leftStrings = leftStringsTl { idx = prevIdx
, leftLines = leftLinesTl , line = prevLine
, rightStrings = rightStrings , leftStrings = leftStringsTl
, rightLines = rightLines , leftLines = leftLinesTl
} end , rightStrings = rightStrings
, rightLines = rightLines
}
end
end end
| (_, _) => | (_, _) =>
let val _ = println "1456" in let
{ idx = curIdx val _ = println "1456"
, line = curLine in
, leftStrings = leftStrings { idx = curIdx
, leftLines = leftLines , line = curLine
, rightStrings = rightStrings , leftStrings = leftStrings
, rightLines = rightLines , leftLines = leftLines
} end , rightStrings = rightStrings
, rightLines = rightLines
}
end
fun deleteFromLetAndRight fun deleteFromLetAndRight
( start ( start
@@ -1627,14 +1650,17 @@ struct
| (_, _) => | (_, _) =>
(* Base case: reached empty list while trying to move leftwards. (* Base case: reached empty list while trying to move leftwards.
* Cannot do anything so just return. *) * Cannot do anything so just return. *)
let val _ = println "1594" in let
{ idx = 0 val _ = println "1594"
, line = 0 in
, leftStrings = leftStrings { idx = 0
, leftLines = leftLines , line = 0
, rightStrings = rightStrings , leftStrings = leftStrings
, rightLines = rightLines , leftLines = leftLines
} end) , rightStrings = rightStrings
, rightLines = rightLines
}
end)
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,
@@ -1776,7 +1802,7 @@ struct
end end
val _ = println "1741" val _ = println "1741"
in in
{ idx = prevIdx { idx = prevIdx + String.size str
, line = , line =
(curLine - Vector.length leftLinesHd) + String.size str (curLine - Vector.length leftLinesHd) + String.size str
, leftStrings = str :: leftStringsTl , leftStrings = str :: leftStringsTl
@@ -1847,25 +1873,31 @@ struct
end end
| (_, _) => | (_, _) =>
(* Left strings and lines are empty, so just return. *) (* Left strings and lines are empty, so just return. *)
let val _ = println "1814" in let
{ idx = 0 val _ = println "1814"
, line = 0 in
, leftStrings = [] { idx = 0
, leftLines = [] , line = 0
, rightStrings = rightStrings , leftStrings = []
, rightLines = rightLines , leftLines = []
} end) , rightStrings = rightStrings
, rightLines = rightLines
}
end)
end end
| (_, _) => | (_, _) =>
(* Can't move further leftward so just return. *) (* Can't move further leftward so just return. *)
let val _ = println "1825" in let
{ idx = 0 val _ = println "1825"
, line = 0 in
, leftStrings = [] { idx = 0
, leftLines = [] , line = 0
, rightStrings = rightStrings , leftStrings = []
, rightLines = rightLines , leftLines = []
} end , rightStrings = rightStrings
, rightLines = rightLines
}
end
fun del fun del
( start ( start