remove 'skipToFirstVisibleColumn' function in TextBuilder, as control flow is simpler if we have fewer mutually recursive functions and we can implement its functionality in the 'build' function instead. Also change the 'z' values for cursor and highlight; the cursor should appear above the highlight and now it does, but it previously didn't

This commit is contained in:
2025-09-13 23:22:40 +01:00
parent 7a388f2983
commit 2ab48626cd
3 changed files with 119 additions and 304 deletions

View File

@@ -75,8 +75,8 @@ struct
, cursorOnCharB = 0.83 , cursorOnCharB = 0.83
, charZ = 0.01 , charZ = 0.01
, cursorZ = 0.05 , cursorZ = 0.03
, highlightZ = 0.03 , highlightZ = 0.05
, startX = 5 , startX = 5
, startY = 5 , startY = 5
@@ -126,8 +126,8 @@ struct
, cursorOnCharB = 0.83 , cursorOnCharB = 0.83
, charZ = 0.01 , charZ = 0.01
, cursorZ = 0.05 , cursorZ = 0.03
, highlightZ = 0.03 , highlightZ = 0.05
, startX = startX , startX = startX
, startY = 5 , startY = 5

View File

@@ -15,12 +15,13 @@ struct
val posY = posY + TC.ySpace val posY = posY + TC.ySpace
val lineNumber = lineNumber + 1 val lineNumber = lineNumber + 1
in in
skipToFirstVisibleColumn build
( strPos ( strPos
, shd , shd
, stl , stl
, lhd , lhd
, ltl , ltl
, #startX env
, posY , posY
, 0 , 0
, lineNumber , lineNumber
@@ -77,12 +78,13 @@ struct
val posY = posY + TC.ySpace val posY = posY + TC.ySpace
val lineNumber = lineNumber + 1 val lineNumber = lineNumber + 1
in in
skipToFirstVisibleColumn build
( newStrPos ( newStrPos
, str , str
, stl , stl
, line , line
, ltl , ltl
, #startX env
, posY , posY
, 0 , 0
, lineNumber , lineNumber
@@ -94,100 +96,6 @@ struct
end end
end end
and skipToFirstVisibleColumn
( pos
, str
, stl
, line
, ltl
, posY
, column
, lineNumber
, absIdx
, cursorIdx
, env: Utils.env_data
, acc
) =
if column = #scrollColumnStart env then
(* return to build function *)
build
( pos
, str
, stl
, line
, ltl
, #startX env
, posY
, column
, lineNumber
, absIdx
, cursorIdx
, env
, acc
)
else if pos = String.size str then
(* go to next node *)
case (stl, ltl) of
(shd :: stl, lhd :: ltl) =>
skipToFirstVisibleColumn
( 0
, shd
, stl
, lhd
, ltl
, posY
, column
, lineNumber
, absIdx
, cursorIdx
, env
, acc
)
| (_, _) => acc
else
case String.sub (str, pos) of
#"\n" =>
let
(* increment line lineNumber and posY,
* and then call skipToFirstVisibleColumn recursively.
* The recursive call check this condition:
* Is the new column 0 the same as the column the scroll starts at?
* If it is, then we call build, or else we continue skipping
* until we reach the start column. *)
val posY = posY + TC.ySpace
val lineNumber = lineNumber + 1
in
skipToFirstVisibleColumn
( pos + 1
, str
, stl
, line
, ltl
, posY
, 0
, lineNumber
, absIdx + 1
, cursorIdx
, env
, acc
)
end
| chr =>
skipToFirstVisibleColumn
( pos + 1
, str
, stl
, line
, ltl
, posY
, column + 1
, lineNumber
, absIdx + 1
, cursorIdx
, env
, acc
)
and build and build
( pos ( pos
, str , str
@@ -222,62 +130,9 @@ struct
, acc , acc
) )
| (_, _) => acc | (_, _) => acc
else if column < #scrollColumnStart env then
skipToFirstVisibleColumn
( pos
, str
, stl
, line
, ltl
, posY
, column
, lineNumber
, absIdx
, cursorIdx
, env
, acc
)
else if column > #scrollColumnEnd env then
skipToNextLine
( pos
, str
, stl
, line
, ltl
, posY
, lineNumber
, absIdx
, cursorIdx
, env
, acc
)
else else
case String.sub (str, pos) of case String.sub (str, pos) of
#" " => #"\n" =>
let
val acc =
if absIdx = cursorIdx then
Utils.makeCursor (posX, posY, env) :: acc
else
acc
in
build
( pos + 1
, str
, stl
, line
, ltl
, posX + TC.xSpace
, posY
, column + 1
, lineNumber
, absIdx + 1
, cursorIdx
, env
, acc
)
end
| #"\n" =>
let let
val acc = val acc =
if absIdx = cursorIdx then if absIdx = cursorIdx then
@@ -306,15 +161,13 @@ struct
, acc , acc
) )
end end
| chr => | #" " =>
let let
val acc = val acc =
if absIdx = cursorIdx then if absIdx = cursorIdx then
let val acc = Utils.makeCursor (posX, posY, env) :: acc Utils.makeCursor (posX, posY, env) :: acc
in Utils.makeCursorOnChr (chr, posX, posY, env) :: acc
end
else else
Utils.makeChr (chr, posX, posY, env) :: acc acc
in in
build build
( pos + 1 ( pos + 1
@@ -332,4 +185,61 @@ struct
, acc , acc
) )
end end
| chr =>
if column < #scrollColumnStart env then
build
( pos + 1
, str
, stl
, line
, ltl
, #startX env
, posY
, column + 1
, lineNumber
, absIdx + 1
, cursorIdx
, env
, acc
)
else if column > #scrollColumnEnd env then
skipToNextLine
( pos
, str
, stl
, line
, ltl
, posY
, lineNumber
, absIdx
, cursorIdx
, env
, acc
)
else
let
val acc =
if absIdx = cursorIdx then
let val acc = Utils.makeCursor (posX, posY, env) :: acc
in Utils.makeCursorOnChr (chr, posX, posY, env) :: acc
end
else
Utils.makeChr (chr, posX, posY, env) :: acc
in
build
( pos + 1
, str
, stl
, line
, ltl
, posX + TC.xSpace
, posY
, column + 1
, lineNumber
, absIdx + 1
, cursorIdx
, env
, acc
)
end
end end

View File

@@ -15,12 +15,13 @@ struct
val posY = posY + TC.ySpace val posY = posY + TC.ySpace
val lineNumber = lineNumber + 1 val lineNumber = lineNumber + 1
in in
skipToFirstVisibleColumn build
( strPos ( strPos
, shd , shd
, stl , stl
, lhd , lhd
, ltl , ltl
, #startX env
, posY , posY
, 0 , 0
, lineNumber , lineNumber
@@ -100,12 +101,13 @@ struct
val posY = posY + TC.ySpace val posY = posY + TC.ySpace
val lineNumber = lineNumber + 1 val lineNumber = lineNumber + 1
in in
skipToFirstVisibleColumn build
( newStrPos ( newStrPos
, str , str
, stl , stl
, line , line
, ltl , ltl
, #startX env
, posY , posY
, 0 , 0
, lineNumber , lineNumber
@@ -118,105 +120,6 @@ struct
end end
end end
and skipToFirstVisibleColumn
( pos
, str
, stl
, line
, ltl
, posY
, column
, lineNumber
, absIdx
, cursorIdx
, env: Utils.env_data
, acc
, searchPos
) =
if column = #scrollColumnStart env then
(* return to build function *)
build
( pos
, str
, stl
, line
, ltl
, #startX env
, posY
, column
, lineNumber
, absIdx
, cursorIdx
, env
, acc
, searchPos
)
else if pos = String.size str then
(* go to next node *)
case (stl, ltl) of
(shd :: stl, lhd :: ltl) =>
skipToFirstVisibleColumn
( 0
, shd
, stl
, lhd
, ltl
, posY
, column
, lineNumber
, absIdx
, cursorIdx
, env
, acc
, searchPos
)
| (_, _) => acc
else
case String.sub (str, pos) of
#"\n" =>
let
(* increment line lineNumber and posY,
* and then call skipToFirstVisibleColumn recursively.
* The recursive call check this condition:
* Is the new column 0 the same as the column the scroll starts at?
* If it is, then we call build, or else we continue skipping
* until we reach the start column. *)
val posY = posY + TC.ySpace
val lineNumber = lineNumber + 1
in
skipToFirstVisibleColumn
( pos + 1
, str
, stl
, line
, ltl
, posY
, 0
, lineNumber
, absIdx + 1
, cursorIdx
, env
, acc
, searchPos
)
end
| chr =>
skipToFirstVisibleColumn
( pos + 1
, str
, stl
, line
, ltl
, posY
, column + 1
, lineNumber
, absIdx + 1
, cursorIdx
, env
, acc
, searchPos
)
and build and build
( pos ( pos
, str , str
@@ -270,37 +173,6 @@ struct
, searchPos , searchPos
) )
| (_, _) => acc | (_, _) => acc
else if column < #scrollColumnStart env then
skipToFirstVisibleColumn
( pos
, str
, stl
, line
, ltl
, posY
, column
, lineNumber
, absIdx
, cursorIdx
, env
, acc
, searchPos
)
else if column > #scrollColumnEnd env then
skipToNextLine
( pos
, str
, stl
, line
, ltl
, posY
, lineNumber
, absIdx
, cursorIdx
, env
, acc
, searchPos
)
else else
let let
val searchPos = Utils.advanceSearchPos (absIdx, searchPos, env) val searchPos = Utils.advanceSearchPos (absIdx, searchPos, env)
@@ -380,24 +252,14 @@ struct
) )
end end
| chr => | chr =>
let if column < #scrollColumnStart env then
val acc =
if absIdx = cursorIdx then
Utils.makeCursorOnChr (chr, posX, posY, env)
:: Utils.makeCursor (posX, posY, env) :: acc
else if Utils.isInSearchRange (absIdx, searchPos, env) then
Utils.makeHighlightChr (chr, posX, posY, env)
:: Utils.makeHighlight (posX, posY, env) :: acc
else
Utils.makeChr (chr, posX, posY, env) :: acc
in
build build
( pos + 1 ( pos + 1
, str , str
, stl , stl
, line , line
, ltl , ltl
, posX + TC.xSpace , #startX env
, posY , posY
, column + 1 , column + 1
, lineNumber , lineNumber
@@ -407,6 +269,49 @@ struct
, acc , acc
, searchPos , searchPos
) )
end else if column > #scrollColumnEnd env then
skipToNextLine
( pos
, str
, stl
, line
, ltl
, posY
, lineNumber
, absIdx
, cursorIdx
, env
, acc
, searchPos
)
else
let
val acc =
if absIdx = cursorIdx then
Utils.makeCursorOnChr (chr, posX, posY, env)
:: Utils.makeCursor (posX, posY, env) :: acc
else if Utils.isInSearchRange (absIdx, searchPos, env) then
Utils.makeHighlightChr (chr, posX, posY, env)
:: Utils.makeHighlight (posX, posY, env) :: acc
else
Utils.makeChr (chr, posX, posY, env) :: acc
in
build
( pos + 1
, str
, stl
, line
, ltl
, posX + TC.xSpace
, posY
, column + 1
, lineNumber
, absIdx + 1
, cursorIdx
, env
, acc
, searchPos
)
end
end end
end end