From 2ab48626cd4fc03b30d935e668881580e804c511 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sat, 13 Sep 2025 23:22:40 +0100 Subject: [PATCH] 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 --- fcore/text-builder/text-builder-utils.sml | 8 +- .../text-builder/text-builder-with-cursor.sml | 220 ++++++------------ .../text-builder-with-highlight.sml | 195 ++++------------ 3 files changed, 119 insertions(+), 304 deletions(-) diff --git a/fcore/text-builder/text-builder-utils.sml b/fcore/text-builder/text-builder-utils.sml index a8e21ad..095ae3e 100644 --- a/fcore/text-builder/text-builder-utils.sml +++ b/fcore/text-builder/text-builder-utils.sml @@ -75,8 +75,8 @@ struct , cursorOnCharB = 0.83 , charZ = 0.01 - , cursorZ = 0.05 - , highlightZ = 0.03 + , cursorZ = 0.03 + , highlightZ = 0.05 , startX = 5 , startY = 5 @@ -126,8 +126,8 @@ struct , cursorOnCharB = 0.83 , charZ = 0.01 - , cursorZ = 0.05 - , highlightZ = 0.03 + , cursorZ = 0.03 + , highlightZ = 0.05 , startX = startX , startY = 5 diff --git a/fcore/text-builder/text-builder-with-cursor.sml b/fcore/text-builder/text-builder-with-cursor.sml index a0443b6..7478026 100644 --- a/fcore/text-builder/text-builder-with-cursor.sml +++ b/fcore/text-builder/text-builder-with-cursor.sml @@ -15,12 +15,13 @@ struct val posY = posY + TC.ySpace val lineNumber = lineNumber + 1 in - skipToFirstVisibleColumn + build ( strPos , shd , stl , lhd , ltl + , #startX env , posY , 0 , lineNumber @@ -77,12 +78,13 @@ struct val posY = posY + TC.ySpace val lineNumber = lineNumber + 1 in - skipToFirstVisibleColumn + build ( newStrPos , str , stl , line , ltl + , #startX env , posY , 0 , lineNumber @@ -94,100 +96,6 @@ struct 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 ( pos , str @@ -222,62 +130,9 @@ struct , 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 case String.sub (str, pos) of - #" " => - 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" => + #"\n" => let val acc = if absIdx = cursorIdx then @@ -306,15 +161,13 @@ struct , acc ) end - | chr => + | #" " => 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 + Utils.makeCursor (posX, posY, env) :: acc else - Utils.makeChr (chr, posX, posY, env) :: acc + acc in build ( pos + 1 @@ -332,4 +185,61 @@ struct , acc ) 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 diff --git a/fcore/text-builder/text-builder-with-highlight.sml b/fcore/text-builder/text-builder-with-highlight.sml index 7d38239..fe5101b 100644 --- a/fcore/text-builder/text-builder-with-highlight.sml +++ b/fcore/text-builder/text-builder-with-highlight.sml @@ -15,12 +15,13 @@ struct val posY = posY + TC.ySpace val lineNumber = lineNumber + 1 in - skipToFirstVisibleColumn + build ( strPos , shd , stl , lhd , ltl + , #startX env , posY , 0 , lineNumber @@ -100,12 +101,13 @@ struct val posY = posY + TC.ySpace val lineNumber = lineNumber + 1 in - skipToFirstVisibleColumn + build ( newStrPos , str , stl , line , ltl + , #startX env , posY , 0 , lineNumber @@ -118,105 +120,6 @@ struct 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 ( pos , str @@ -270,37 +173,6 @@ struct , searchPos ) | (_, _) => 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 let val searchPos = Utils.advanceSearchPos (absIdx, searchPos, env) @@ -380,24 +252,14 @@ struct ) end | chr => - 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 + if column < #scrollColumnStart env then build ( pos + 1 , str , stl , line , ltl - , posX + TC.xSpace + , #startX env , posY , column + 1 , lineNumber @@ -407,6 +269,49 @@ struct , acc , 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