pass env argument in if-character-folder functor, to make it more generic (can access some environment data, which enables more reuse)

This commit is contained in:
2025-08-04 04:05:39 +01:00
parent aca8ba44b9
commit 55e5778ff7
2 changed files with 31 additions and 13 deletions

View File

@@ -190,13 +190,16 @@ end
signature MAKE_IF_CHAR_FOLDER = signature MAKE_IF_CHAR_FOLDER =
sig sig
val fStart: int * string * int vector * int * string list * int vector list type env
val fStart:
int * string * int vector * int * string list * int vector list * env
-> int -> int
end end
functor MakeIfCharFolderPrev(Fn: MAKE_IF_CHAR_FOLDER) = functor MakeIfCharFolderPrev(Fn: MAKE_IF_CHAR_FOLDER) =
struct struct
fun foldPrev (lineGap: LineGap.t, cursorIdx) = fun foldPrev (lineGap: LineGap.t, cursorIdx, env: Fn.env) =
let let
val val
{rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} = {rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} =
@@ -210,7 +213,8 @@ struct
in in
if strIdx < String.size strHd then if strIdx < String.size strHd then
(* strIdx is in this string *) (* strIdx is in this string *)
Fn.fStart (strIdx, strHd, lnHd, cursorIdx, leftStrings, leftLines) Fn.fStart
(strIdx, strHd, lnHd, cursorIdx, leftStrings, leftLines, env)
else else
(* strIdx must be in the strTl *) (* strIdx must be in the strTl *)
(case (strTl, lnTl) of (case (strTl, lnTl) of
@@ -225,6 +229,7 @@ struct
, cursorIdx , cursorIdx
, strHd :: leftStrings , strHd :: leftStrings
, lnHd :: leftLines , lnHd :: leftLines
, env
) )
end end
| (_, _) => cursorIdx) | (_, _) => cursorIdx)

View File

@@ -36,9 +36,16 @@ struct
else else
helpVi0 (absIdx - strPos, stl, ltl) helpVi0 (absIdx - strPos, stl, ltl)
structure Vi0 = MakeIfCharFolderPrev (struct val fStart = startVi0 end) structure Vi0 =
MakeIfCharFolderPrev
(struct
type env = unit
val fStart = startVi0
fun fStart (strPos, shd, lhd, absIdx, stl, ltl, _) =
startVi0 (strPos, shd, lhd, absIdx, stl, ltl)
end)
val vi0 = Vi0.foldPrev fun vi0 (lineGap, cursorIdx) = Vi0.foldPrev (lineGap, cursorIdx, ())
fun helpViDlr (strPos, str, absIdx, strTl, lineTl) = fun helpViDlr (strPos, str, absIdx, strTl, lineTl) =
if strPos = String.size str then if strPos = String.size str then
@@ -156,6 +163,8 @@ struct
structure ViH = structure ViH =
MakeIfCharFolderPrev MakeIfCharFolderPrev
(struct (struct
type env = unit
fun helpViH (strIdx, hd, cursorIdx, leftStrings) = fun helpViH (strIdx, hd, cursorIdx, leftStrings) =
if strIdx > 0 then if strIdx > 0 then
(* bounds check: can access prev char in hd *) (* bounds check: can access prev char in hd *)
@@ -216,11 +225,11 @@ struct
cursorIdx - 1 cursorIdx - 1
| [] => 0) | [] => 0)
fun fStart (strIdx, hd, _, cursorIdx, leftStrings, _) = fun fStart (strIdx, hd, _, cursorIdx, leftStrings, _, _) =
helpViH (strIdx, hd, cursorIdx, leftStrings) helpViH (strIdx, hd, cursorIdx, leftStrings)
end) end)
val viH = ViH.foldPrev fun viH (lineGap, cursorIdx) = ViH.foldPrev (lineGap, cursorIdx, ())
fun helpGetCursorColumn (distanceFromLine, strList, lineList) = fun helpGetCursorColumn (distanceFromLine, strList, lineList) =
case (strList, lineList) of case (strList, lineList) of
@@ -438,6 +447,8 @@ struct
structure ViK = structure ViK =
MakeIfCharFolderPrev MakeIfCharFolderPrev
(struct (struct
type env = unit
fun helpViK fun helpViK
( strPos ( strPos
, str , str
@@ -517,7 +528,7 @@ struct
, lineTl , lineTl
) )
fun fStart (strIdx, shd, lhd, cursorIdx, leftStrings, leftLines) = fun fStart (strIdx, shd, lhd, cursorIdx, leftStrings, leftLines, _) =
if String.sub (shd, strIdx) = #"\n" then if String.sub (shd, strIdx) = #"\n" then
(* ? -> ? -> \n *) (* ? -> ? -> \n *)
if strIdx > 0 then if strIdx > 0 then
@@ -640,10 +651,9 @@ struct
, leftLines , leftLines
) )
end end
end) end)
val viK = ViK.foldPrev fun viK (lineGap, cursorIdx) = ViK.foldPrev (lineGap, cursorIdx, ())
(* equivalent of vi's 'w' command *) (* equivalent of vi's 'w' command *)
val nextWord = ViWordDfa.startOfNextWord val nextWord = ViWordDfa.startOfNextWord
@@ -676,6 +686,8 @@ struct
structure FirstNonSpaceChr = structure FirstNonSpaceChr =
MakeIfCharFolderPrev MakeIfCharFolderPrev
(struct (struct
type env = unit
fun helpFirstNonSpaceChr (strPos, str, absIdx, stl) = fun helpFirstNonSpaceChr (strPos, str, absIdx, stl) =
if strPos = String.size str then if strPos = String.size str then
case stl of case stl of
@@ -691,7 +703,7 @@ struct
absIdx absIdx
end end
fun fStart (strIdx, shd, _, absIdx, stl, _) = fun fStart (strIdx, shd, _, absIdx, stl, _, _) =
if strIdx < String.size shd then if strIdx < String.size shd then
helpFirstNonSpaceChr (strIdx, shd, absIdx, stl) helpFirstNonSpaceChr (strIdx, shd, absIdx, stl)
else else
@@ -700,7 +712,8 @@ struct
| [] => (* tl is empty; just return absIdx *) absIdx | [] => (* tl is empty; just return absIdx *) absIdx
end) end)
val firstNonSpaceChr = FirstNonSpaceChr.foldPrev fun firstNonSpaceChr (lineGap, cursorIdx) =
FirstNonSpaceChr.foldPrev (lineGap, cursorIdx, ())
fun helpToNextChr (strPos, str, absIdx, stl, ltl, origIdx, findChr) = fun helpToNextChr (strPos, str, absIdx, stl, ltl, origIdx, findChr) =
if strPos = String.size str then if strPos = String.size str then