fix bug in index metadata in line_gap.sml (make sure we add and subtract by size of string by calling String.size)

This commit is contained in:
2025-08-05 15:58:08 +01:00
parent a84490c86c
commit a2ca88da92

View File

@@ -1347,8 +1347,8 @@ struct
* while origIdx considers index to return * while origIdx considers index to return
* once buffer is done deleting. *) * once buffer is done deleting. *)
deleteRightFromHere deleteRightFromHere
( curIdx + String.size newString ( curIdx + String.size newLeftStringsHd
, curLine + Vector.length newLines , curLine + Vector.length newLeftLinesHd
, nextIdx , nextIdx
, finish , finish
, newLeftStringsHd :: leftStringsTl , newLeftStringsHd :: leftStringsTl
@@ -1372,7 +1372,7 @@ struct
) )
| (_, _) => | (_, _) =>
deleteRightFromHere deleteRightFromHere
( nextIdx ( curIdx + String.size newString
, curLine + Vector.length newLines , curLine + Vector.length newLines
, nextIdx , nextIdx
, finish , finish
@@ -1418,7 +1418,7 @@ struct
else else
Vector.fromList [] Vector.fromList []
in in
{ idx = curIdx + sub1Length { idx = curIdx + String.size sub1
, line = curLine + Vector.length sub1Lines , line = curLine + Vector.length sub1Lines
, leftStrings = sub1 :: leftStrings , leftStrings = sub1 :: leftStrings
, leftLines = sub1Lines :: leftLines , leftLines = sub1Lines :: leftLines
@@ -1444,7 +1444,7 @@ struct
VectorSlice.vector slice VectorSlice.vector slice
end end
in in
{ idx = curIdx + strLength { idx = curIdx + String.size str
, line = curLine + Vector.length newLeftLines , line = curLine + Vector.length newLeftLines
, leftStrings = str :: leftStrings , leftStrings = str :: leftStrings
, leftLines = newLeftLines :: leftLines , leftLines = newLeftLines :: leftLines
@@ -1460,7 +1460,7 @@ struct
* 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. *)
deleteRightFromHere deleteRightFromHere
( nextIdx ( curIdx + String.size rightStringsHd
, curLine + Vector.length rightLinesHd , curLine + Vector.length rightLinesHd
, nextIdx , nextIdx
, finish , finish
@@ -1759,7 +1759,7 @@ struct
Vector.fromList [] Vector.fromList []
end end
in in
{ idx = prevIdx + sub1Length { idx = prevIdx + String.size sub1
, line = , line =
(curLine - Vector.length leftLinesHd) (curLine - Vector.length leftLinesHd)
+ Vector.length sub1Lines + Vector.length sub1Lines
@@ -1954,10 +1954,7 @@ struct
local local
fun consIfNotEmpty (s, acc) = fun consIfNotEmpty (s, acc) =
if String.size s > 0 then if String.size s > 0 then s :: acc else acc
s :: acc
else
acc
(* We build up the string list and, at the end, (* We build up the string list and, at the end,
* we always make sure to reverse the list too * we always make sure to reverse the list too
@@ -1981,19 +1978,14 @@ struct
else else
(* nextIdx = finish (* nextIdx = finish
* so add current hd to vec and then concat *) * so add current hd to vec and then concat *)
let let
val acc = hd :: acc val acc = hd :: acc
val acc = consIfNotEmpty (endWith, acc) val acc = consIfNotEmpty (endWith, acc)
in in
List.rev acc List.rev acc
end end
end
| [] =>
let
val acc = consIfNotEmpty (endWith, acc)
in
List.rev acc
end end
| [] => let val acc = consIfNotEmpty (endWith, acc) in List.rev acc end
fun moveRightAndSub (start, finish, curIdx, right, endWith) = fun moveRightAndSub (start, finish, curIdx, right, endWith) =
case right of case right of
@@ -2008,14 +2000,14 @@ struct
if nextIdx < finish then if nextIdx < finish then
(* get starting acc, (* get starting acc,
* and then call subRightFromHere *) * and then call subRightFromHere *)
let let
val substart = start - curIdx val substart = start - curIdx
val length = String.size hd - substart val length = String.size hd - substart
val acc = [String.substring (hd, substart, length)] val acc = [String.substring (hd, substart, length)]
val acc = subRightFromHere (nextIdx, finish, tl, acc, endWith) val acc = subRightFromHere (nextIdx, finish, tl, acc, endWith)
in in
String.concat acc String.concat acc
end end
else if nextIdx > finish then else if nextIdx > finish then
(* have to get susbstring from middle of this string *) (* have to get susbstring from middle of this string *)
let let
@@ -2024,9 +2016,7 @@ struct
val length = subfinish - substart val length = subfinish - substart
val str = String.substring (hd, substart, length) val str = String.substring (hd, substart, length)
in in
if String.size endWith > 0 if String.size endWith > 0 then str ^ endWith else str
then str ^ endWith
else str
end end
else else
(* have to get substring from middle to end *) (* have to get substring from middle to end *)
@@ -2035,19 +2025,15 @@ struct
val length = String.size hd - substart val length = String.size hd - substart
val str = String.substring (hd, substart, length) val str = String.substring (hd, substart, length)
in in
if String.size endWith > 0 if String.size endWith > 0 then str ^ endWith else str
then str ^ endWith
else str
end end
else else
(* nextIdx = start (* nextIdx = start
* so we have to ignore this string * so we have to ignore this string
* and start building acc from tl *) * and start building acc from tl *)
let let val acc = subRightFromHere (nextIdx, finish, tl, [], endWith)
val acc = subRightFromHere (nextIdx, finish, tl, [], endWith) in String.concat acc
in end
String.concat acc
end
end end
| [] => | [] =>
(* if there are no strings to the right, (* if there are no strings to the right,
@@ -2078,19 +2064,15 @@ struct
else else
(* start = prevIdx (* start = prevIdx
* add hd to acc and return *) * add hd to acc and return *)
let let val acc = hd :: acc
val acc = hd :: acc in String.concat acc
in end
String.concat acc
end
end end
| [] => String.concat acc | [] => String.concat acc
fun subFromLeftAndRight (start, finish, curIdx, left, right, endWith) = fun subFromLeftAndRight (start, finish, curIdx, left, right, endWith) =
let let val acc = subRightFromHere (curIdx, finish, right, [], endWith)
val acc = subRightFromHere (curIdx, finish, right, [], endWith) in subLeftFromHere (start, curIdx, left, acc)
in
subLeftFromHere (start, curIdx, left, acc)
end end
fun moveLeftAndSub (start, finish, curIdx, left, endWith) = fun moveLeftAndSub (start, finish, curIdx, left, endWith) =
@@ -2108,40 +2090,37 @@ struct
* and continue substring leftwards *) * and continue substring leftwards *)
let let
val length = finish - prevIdx val length = finish - prevIdx
val str = String.substring (hd, 0, length) val acc = [str, endWith] val str = String.substring (hd, 0, length)
val acc = [str, endWith]
in in
subLeftFromHere (start, prevIdx, tl, acc) subLeftFromHere (start, prevIdx, tl, acc)
end end
else if prevIdx < start then else if prevIdx < start then
(* we want to return a substring (* we want to return a substring
* extracted from the middle of hd *) * extracted from the middle of hd *)
let let
val substart = start - prevIdx val substart = start - prevIdx
val subfinish = finish - prevIdx val subfinish = finish - prevIdx
val length = subfinish - substart val length = subfinish - substart
val str = String.substring (hd, substart, length) val str = String.substring (hd, substart, length)
in in
if String.size endWith > 0 if String.size endWith > 0 then str ^ endWith else str
then str ^ endWith end
else str
end
else else
(* prevIdx = start (* prevIdx = start
* we want to return a substring starting from 0 *) * we want to return a substring starting from 0 *)
let let
val subfinish = finish - prevIdx val subfinish = finish - prevIdx
val length = String.size hd - subfinish val length = String.size hd - subfinish
val str = String.substring (hd, 0, length) val str = String.substring (hd, 0, length)
in in
if String.size endWith > 0 if String.size endWith > 0 then str ^ endWith else str
then str ^ endWith end
else str
end
else else
(* prevIdx = finish (* prevIdx = finish
* so we want to ignore hd and start * so we want to ignore hd and start
* subLeftFromHere with an empty list *) * subLeftFromHere with an empty list *)
subLeftFromHere (start, prevIdx, tl, [endWith]) subLeftFromHere (start, prevIdx, tl, [endWith])
end end
| [] => endWith | [] => endWith
@@ -2163,7 +2142,7 @@ struct
String.concat acc String.concat acc
end end
in in
fun substringWithEnd (start, length, buffer : t, endWith) = fun substringWithEnd (start, length, buffer: t, endWith) =
let let
val finish = start + length val finish = start + length
val {idx, leftStrings, rightStrings, ...} = buffer val {idx, leftStrings, rightStrings, ...} = buffer
@@ -2171,7 +2150,7 @@ struct
sub (start, finish, idx, leftStrings, rightStrings, endWith) sub (start, finish, idx, leftStrings, rightStrings, endWith)
end end
fun nullSubstring (start, length, buffer : t) = fun nullSubstring (start, length, buffer: t) =
let let
val finish = start + length val finish = start + length
val {idx, leftStrings, rightStrings, ...} = buffer val {idx, leftStrings, rightStrings, ...} = buffer
@@ -2179,7 +2158,7 @@ struct
sub (start, finish, idx, leftStrings, rightStrings, "\u0000") sub (start, finish, idx, leftStrings, rightStrings, "\u0000")
end end
fun substring (start, length, buffer : t) = fun substring (start, length, buffer: t) =
let let
val finish = start + length val finish = start + length
val {idx, leftStrings, rightStrings, ...} = buffer val {idx, leftStrings, rightStrings, ...} = buffer
@@ -2251,8 +2230,7 @@ struct
({idx, line, leftStrings, leftLines, rightStrings, rightLines}: t) = ({idx, line, leftStrings, leftLines, rightStrings, rightLines}: t) =
helpGoToStart (idx, line, leftStrings, leftLines, rightStrings, rightLines) helpGoToStart (idx, line, leftStrings, leftLines, rightStrings, rightLines)
fun helpGoToEnd fun helpGoToEnd (idx, line, leftStrings, leftLines, rightStrings, rightLines) =
(idx, line, leftStrings, leftLines, rightStrings, rightLines) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
(rStrHd :: rStrTl, rLnHd :: rLnTl) => (rStrHd :: rStrTl, rLnHd :: rLnTl) =>
(case (leftStrings, leftLines) of (case (leftStrings, leftLines) of
@@ -2310,8 +2288,7 @@ struct
, rightLines = [] , rightLines = []
} }
fun goToEnd fun goToEnd ({idx, line, leftStrings, leftLines, rightStrings, rightLines}: t) =
({idx, line, leftStrings, leftLines, rightStrings, rightLines}: t) =
helpGoToEnd (idx, line, leftStrings, leftLines, rightStrings, rightLines) helpGoToEnd (idx, line, leftStrings, leftLines, rightStrings, rightLines)
(* function to abstract leftwards movement. (* function to abstract leftwards movement.
@@ -2329,117 +2306,133 @@ struct
* the search number, this parameter is just passed to fGoLeft. * the search number, this parameter is just passed to fGoLeft.
* *) * *)
fun moveLeft fun moveLeft
( idx, line, searchTo ( idx
, leftStrings, leftLines, rightStrings, rightLines , line
, lStrHd, lStrTl, lLnHd, lLnTl , searchTo
, leftStrings
, leftLines
, rightStrings
, rightLines
, lStrHd
, lStrTl
, lLnHd
, lLnTl
, fGoLeft , fGoLeft
) = ) =
case (rightStrings, rightLines) of case (rightStrings, rightLines) of
(rStrHd :: rStrTl, rLnHd :: rLnTl) => (rStrHd :: rStrTl, rLnHd :: rLnTl) =>
if isInLimit (lStrHd, rStrHd, lLnHd, rLnHd) then if isInLimit (lStrHd, rStrHd, lLnHd, rLnHd) then
(* join into a single node before moving *) (* join into a single node before moving *)
let let
val newRstrHd = lStrHd ^ rStrHd val newRstrHd = lStrHd ^ rStrHd
val newRlnHd = val newRlnHd =
Vector.tabulate Vector.tabulate
( Vector.length lLnHd + Vector.length rLnHd ( Vector.length lLnHd + Vector.length rLnHd
, fn lnIdx => , fn lnIdx =>
if lnIdx < Vector.length lLnHd then if lnIdx < Vector.length lLnHd then
Vector.sub (lLnHd, lnIdx) Vector.sub (lLnHd, lnIdx)
else else
Vector.sub (rLnHd, lnIdx - Vector.length lLnHd) Vector.sub (rLnHd, lnIdx - Vector.length lLnHd)
+ String.size lStrHd + String.size lStrHd
)
in
fGoLeft
( idx - String.size lStrHd
, line - Vector.length lLnHd
, searchTo
, lStrTl
, lLnTl
, newRstrHd :: rStrTl
, newRlnHd :: rLnTl
) )
end in
else
(* move without joining *)
fGoLeft fGoLeft
( idx - String.size lStrHd ( idx - String.size lStrHd
, line - Vector.length lLnHd , line - Vector.length lLnHd
, searchTo , searchTo
, lStrTl , lStrTl
, lLnTl , lLnTl
, lStrHd :: rightStrings , newRstrHd :: rStrTl
, lLnHd :: rightLines , newRlnHd :: rLnTl
) )
| (_, _) => end
(* right side is empty, so just move left without joining *) else
(* move without joining *)
fGoLeft fGoLeft
( idx - String.size lStrHd ( idx - String.size lStrHd
, line - Vector.length lLnHd , line - Vector.length lLnHd
, searchTo , searchTo
, lStrTl , lStrTl
, lLnTl , lLnTl
, [lStrHd] , lStrHd :: rightStrings
, [lLnHd] , lLnHd :: rightLines
) )
| (_, _) =>
(* right side is empty, so just move left without joining *)
fGoLeft
( idx - String.size lStrHd
, line - Vector.length lLnHd
, searchTo
, lStrTl
, lLnTl
, [lStrHd]
, [lLnHd]
)
(* same as moveLeft function, except it move rightwards instead *) (* same as moveLeft function, except it move rightwards instead *)
fun moveRight fun moveRight
( idx, line, searchTo ( idx
, leftStrings, leftLines, rightStrings, rightLines , line
, rStrHd, rStrTl, rLnHd, rLnTl , searchTo
, leftStrings
, leftLines
, rightStrings
, rightLines
, rStrHd
, rStrTl
, rLnHd
, rLnTl
, fGoRight , fGoRight
) = ) =
case (leftStrings, leftLines) of case (leftStrings, leftLines) of
(lStrHd :: lStrTl, lLnHd :: lLnTl) => (lStrHd :: lStrTl, lLnHd :: lLnTl) =>
if isInLimit (lStrHd, rStrHd, lLnHd, rLnHd) then if isInLimit (lStrHd, rStrHd, lLnHd, rLnHd) then
(* can join while staying in limit, so join and move right *) (* can join while staying in limit, so join and move right *)
let let
val newLstrHd = lStrHd ^ rStrHd val newLstrHd = lStrHd ^ rStrHd
val newLlnHd = val newLlnHd =
Vector.tabulate Vector.tabulate
( Vector.length lLnHd + Vector.length rLnHd ( Vector.length lLnHd + Vector.length rLnHd
, fn lnIdx => , fn lnIdx =>
if lnIdx < Vector.length lLnHd then if lnIdx < Vector.length lLnHd then
Vector.sub (lLnHd, lnIdx) Vector.sub (lLnHd, lnIdx)
else else
Vector.sub (rLnHd, lnIdx - Vector.length lLnHd) Vector.sub (rLnHd, lnIdx - Vector.length lLnHd)
+ String.size lStrHd + String.size lStrHd
) )
in in
fGoRight fGoRight
( idx + String.size rStrHd ( idx + String.size rStrHd
, line + Vector.length rLnHd , line + Vector.length rLnHd
, searchTo , searchTo
, newLstrHd :: lStrTl , newLstrHd :: lStrTl
, newLlnHd :: lLnTl , newLlnHd :: lLnTl
, rStrTl , rStrTl
, rLnTl , rLnTl
) )
end end
else else
(* cannot join while staying in limit, so just move right *) (* cannot join while staying in limit, so just move right *)
fGoRight fGoRight
( idx + String.size rStrHd ( idx + String.size rStrHd
, line + Vector.length rLnHd , line + Vector.length rLnHd
, searchTo , searchTo
, rStrHd :: leftStrings , rStrHd :: leftStrings
, rLnHd :: leftLines , rLnHd :: leftLines
, rStrTl , rStrTl
, rLnTl , rLnTl
) )
| (_, _) => | (_, _) =>
(* left side is empty, so just move rightwards without joining *) (* left side is empty, so just move rightwards without joining *)
fGoRight fGoRight
( String.size rStrHd ( String.size rStrHd
, Vector.length rLnHd , Vector.length rLnHd
, searchTo , searchTo
, [rStrHd] , [rStrHd]
, [rLnHd] , [rLnHd]
, rStrTl , rStrTl
, rLnTl , rLnTl
) )
fun helpGoToLineLeft fun helpGoToLineLeft
(idx, line, searchLine, leftStrings, leftLines, rightStrings, rightLines) = (idx, line, searchLine, leftStrings, leftLines, rightStrings, rightLines) =
@@ -2448,9 +2441,17 @@ struct
if searchLine < line - Vector.length lLnHd then if searchLine < line - Vector.length lLnHd then
(* move leftwards, joining if possible *) (* move leftwards, joining if possible *)
moveLeft moveLeft
( idx, line, searchLine ( idx
, leftStrings, leftLines, rightStrings, rightLines , line
, lStrHd, lStrTl, lLnHd, lLnTl , searchLine
, leftStrings
, leftLines
, rightStrings
, rightLines
, lStrHd
, lStrTl
, lLnHd
, lLnTl
, helpGoToLineLeft , helpGoToLineLeft
) )
else else
@@ -2479,9 +2480,17 @@ struct
if searchLine > line + Vector.length rLnHd then if searchLine > line + Vector.length rLnHd then
(* have to move rightwards *) (* have to move rightwards *)
moveRight moveRight
( idx, line, searchLine ( idx
, leftStrings, leftLines, rightStrings, rightLines , line
, rStrHd, rStrTl, rLnHd, rLnTl , searchLine
, leftStrings
, leftLines
, rightStrings
, rightLines
, rStrHd
, rStrTl
, rLnHd
, rLnTl
, helpGoToLineRight , helpGoToLineRight
) )
else else
@@ -2538,9 +2547,17 @@ struct
if searchIdx < idx - String.size lStrHd then if searchIdx < idx - String.size lStrHd then
(* move leftwards, joining if possible *) (* move leftwards, joining if possible *)
moveLeft moveLeft
( idx, line, searchIdx ( idx
, leftStrings, leftLines, rightStrings, rightLines , line
, lStrHd, lStrTl, lLnHd, lLnTl , searchIdx
, leftStrings
, leftLines
, rightStrings
, rightLines
, lStrHd
, lStrTl
, lLnHd
, lLnTl
, helpGoToIdxLeft , helpGoToIdxLeft
) )
else else
@@ -2569,9 +2586,17 @@ struct
if searchIdx > idx + String.size rStrHd then if searchIdx > idx + String.size rStrHd then
(* have to move rightwards *) (* have to move rightwards *)
moveRight moveRight
( idx, line, searchIdx ( idx
, leftStrings, leftLines, rightStrings, rightLines , line
, rStrHd, rStrTl, rLnHd, rLnTl , searchIdx
, leftStrings
, leftLines
, rightStrings
, rightLines
, rStrHd
, rStrTl
, rLnHd
, rLnTl
, helpGoToIdxRight , helpGoToIdxRight
) )
else else