add mutually recursive functions to skip to start of next line in text builder

This commit is contained in:
2025-09-12 08:05:19 +01:00
parent 4fb35d075f
commit a2c3df65f7

View File

@@ -94,7 +94,102 @@ struct
, #cursorOnCharB env , #cursorOnCharB env
) )
fun buildTextString structure TextWithCursor =
struct
fun goToFirstLineAfter
(stl, ltl, posY, lineNumber, absIdx, cursorIdx, env, acc) =
case (stl, ltl) of
(shd :: stl, lhd :: ltl) =>
if Vector.length lhd > 0 then
let
val lineOffset = Vector.sub (lhd, 0)
val strPos = lineOffset + 1
val absIdx = absIdx + strPos
val posY = posY + TC.ySpace
val lineNumber = lineNumber + 1
in
build
( strPos
, shd
, stl
, lhd
, ltl
, #startX env
, posY
, 0
, lineNumber
, absIdx
, cursorIdx
, env : env_data
, acc
)
end
else
(* keep looping until we find a linebreak *)
goToFirstLineAfter
( stl
, ltl
, posY
, lineNumber
, absIdx + String.size shd
, cursorIdx
, env
, acc
)
| (_, _) => acc
and skipToColumnStart
(pos, str, stl, line, ltl, posY, lineNumber, absIdx, cursorIdx, env, acc) =
if Vector.length line = 0 then
let
(* get index of buffer after this string *)
val absIdx = absIdx - pos
val absIdx = absIdx + String.size str
in
goToFirstLineAfter
(stl, ltl, posY, lineNumber, absIdx, cursorIdx, env, acc)
end
else
(* bin search lines *)
let
val searchPos = BinSearch.equalOrMore (pos + 1, searchList)
in
if searchPos = Vector.length lines then
(* next line is not in this node *)
let
val absIdx = absIdx - pos
val absIdx = absIdx + String.size str
in
goToFirstLineAfter
(stl, ltl, posY, lineNumber, absIdx, cursorIdx, env, acc)
end
else
let
val lineOffset = Vector.sub (line, searchPos)
val newStrPos = lineOffset + 1
val absIdx = absIdx - pos + newStrPos
val posY = posY + TC.ySpace
val lineNumber = lineNumber + 1
in
build
( newStrPos
, str
, stl
, line
, ltl
, #startX env
, posY
, 0
, lineNumber
, absIdx
, cursorIdx
, env
, acc
)
end
end
and build
( pos ( pos
, str , str
, stl , stl
@@ -112,7 +207,7 @@ struct
if pos = String.size str then if pos = String.size str then
case (stl, ltl) of case (stl, ltl) of
(str :: stl, line :: ltl) => (str :: stl, line :: ltl) =>
buildTextString build
( 0 ( 0
, str , str
, stl , stl
@@ -150,7 +245,7 @@ struct
if absIdx = cursorIdx then makeCursor (posX, posY, env) :: acc if absIdx = cursorIdx then makeCursor (posX, posY, env) :: acc
else acc else acc
in in
buildTextString build
( pos + 1 ( pos + 1
, str , str
, stl , stl
@@ -177,7 +272,7 @@ struct
if nextLineNumber > #lastLineNumber env then if nextLineNumber > #lastLineNumber env then
acc acc
else else
buildTextString build
( pos + 1 ( pos + 1
, str , str
, stl , stl
@@ -203,7 +298,7 @@ struct
else else
makeCursor (chr, posX, posY, env) :: acc makeCursor (chr, posX, posY, env) :: acc
in in
buildTextString build
( pos + 1 ( pos + 1
, str , str
, stl , stl
@@ -219,6 +314,7 @@ struct
, acc , acc
) )
end end
end
fun buildTextString fun buildTextString
( pos ( pos