fix bug with tillNextChr which would place cursor on a newline

This commit is contained in:
2024-10-26 16:20:24 +01:00
parent eeacde8e7a
commit 01213fb2fb
2 changed files with 83 additions and 23 deletions

View File

@@ -1263,36 +1263,36 @@ struct
cursorIdx cursorIdx
end end
fun helpNextChr (strPos, str, absIdx, stl, ltl, origIdx, findChr, fResult) = fun helpToNextChr (strPos, str, absIdx, stl, ltl, origIdx, findChr) =
if strPos = String.size str then if strPos = String.size str then
case (stl, ltl) of case (stl, ltl) of
(shd :: stl, lhd :: ltl) => (shd :: stl, lhd :: ltl) =>
helpNextChr helpToNextChr
(0, shd, absIdx, stl, ltl, origIdx, findChr, fResult) (0, shd, absIdx, stl, ltl, origIdx, findChr)
| (_, _) => | (_, _) =>
origIdx origIdx
else else
if String.sub (str, strPos) = findChr then if String.sub (str, strPos) = findChr then
fResult absIdx absIdx
else else
helpNextChr helpToNextChr
(strPos + 1, str, absIdx + 1, stl, ltl, origIdx, findChr, fResult) (strPos + 1, str, absIdx + 1, stl, ltl, origIdx, findChr)
fun startNextChr (shd, strIdx, absIdx, stl, ltl, findChr, fResult) = fun startToNextChr (shd, strIdx, absIdx, stl, ltl, findChr) =
(* we want to start iterating from next char after strIdx *) (* we want to start iterating from next char after strIdx *)
if strIdx - 1 < String.size shd then if strIdx - 1 < String.size shd then
helpNextChr helpToNextChr
(strIdx + 1, shd, absIdx + 1, stl, ltl, absIdx, findChr, fResult) (strIdx + 1, shd, absIdx + 1, stl, ltl, absIdx, findChr)
else else
case (stl, ltl) of case (stl, ltl) of
(stlhd :: stltl, ltlhd :: ltltl) => (stlhd :: stltl, ltlhd :: ltltl) =>
helpNextChr helpToNextChr
(0, stlhd, absIdx + 1, stltl, ltltl, absIdx, findChr, fResult) (0, stlhd, absIdx + 1, stltl, ltltl, absIdx, findChr)
| (_, _) => | (_, _) =>
(* tl is empty; just return absIdx *) (* tl is empty; just return absIdx *)
absIdx absIdx
fun nextChr (lineGap: LineGap.t, cursorIdx, chr, fResult) = fun toNextChr (lineGap: LineGap.t, cursorIdx, chr) =
let let
val {rightStrings, rightLines, idx = bufferIdx, ...} = lineGap val {rightStrings, rightLines, idx = bufferIdx, ...} = lineGap
in in
@@ -1304,8 +1304,8 @@ struct
in in
if strIdx < String.size shd then if strIdx < String.size shd then
(* strIdx is in this string *) (* strIdx is in this string *)
startNextChr startToNextChr
(shd, strIdx, cursorIdx, stl, ltl, chr, fResult) (shd, strIdx, cursorIdx, stl, ltl, chr)
else else
(* strIdx is in tl *) (* strIdx is in tl *)
(case (stl, ltl) of (case (stl, ltl) of
@@ -1313,21 +1313,81 @@ struct
let let
val strIdx = strIdx - String.size shd val strIdx = strIdx - String.size shd
in in
startNextChr startToNextChr
(shd, strIdx, cursorIdx, stl, ltl, chr, fResult) (shd, strIdx, cursorIdx, stl, ltl, chr)
end end
| (_, _) => cursorIdx) | (_, _) => cursorIdx)
end end
| (_, _) => cursorIdx | (_, _) => cursorIdx
end end
fun tillNextChrResult absIdx = absIdx - 1 fun helpTillNextChr (strPos, str, absIdx, stl, ltl, origIdx, findChr, lastNonLine) =
if strPos = String.size str then
case (stl, ltl) of
(shd :: stl, lhd :: ltl) =>
helpTillNextChr
(0, shd, absIdx, stl, ltl, origIdx, findChr, lastNonLine)
| (_, _) =>
origIdx
else
let
val chr = String.sub (str, strPos)
in
if chr = findChr then
lastNonLine
else
let
val lastNonLine =
if chr = #"\n" orelse chr = #"\r" then
lastNonLine
else
absIdx
in
helpTillNextChr
(strPos + 1, str, absIdx + 1, stl, ltl, origIdx, findChr, lastNonLine)
end
end
fun tillNextChr (lineGap, cursorIdx, chr) = fun startTillNextChr (shd, strIdx, absIdx, stl, ltl, findChr) =
nextChr (lineGap, cursorIdx, chr, tillNextChrResult) (* we want to start iterating from next char after strIdx *)
if strIdx - 1 < String.size shd then
helpTillNextChr
(strIdx + 1, shd, absIdx + 1, stl, ltl, absIdx, findChr, absIdx)
else
case (stl, ltl) of
(stlhd :: stltl, ltlhd :: ltltl) =>
helpTillNextChr
(0, stlhd, absIdx + 1, stltl, ltltl, absIdx, findChr, absIdx)
| (_, _) =>
(* tl is empty; just return absIdx *)
absIdx
fun toNextChrResult absIdx = absIdx fun tillNextChr (lineGap: LineGap.t, cursorIdx, chr) =
let
fun toNextChr (lineGap, cursorIdx, chr) = val {rightStrings, rightLines, idx = bufferIdx, ...} = lineGap
nextChr (lineGap, cursorIdx, chr, toNextChrResult) in
case (rightStrings, rightLines) of
(shd :: stl, lhd :: ltl) =>
let
(* convert absolute cursorIdx to idx relative to hd string *)
val strIdx = cursorIdx - bufferIdx
in
if strIdx < String.size shd then
(* strIdx is in this string *)
startTillNextChr
(shd, strIdx, cursorIdx, stl, ltl, chr)
else
(* strIdx is in tl *)
(case (stl, ltl) of
(stlhd :: stltl, ltlhd :: ltltl) =>
let
val strIdx = strIdx - String.size shd
in
startTillNextChr
(shd, strIdx, cursorIdx, stl, ltl, chr)
end
| (_, _) => cursorIdx)
end
| (_, _) => cursorIdx
end
end end

BIN
shf

Binary file not shown.