further refactoring, functorising a function because it will later be used elsewhere as well

This commit is contained in:
2025-07-20 13:39:48 +01:00
parent 554c3b0354
commit f57af737b9
2 changed files with 50 additions and 46 deletions

View File

@@ -61,3 +61,24 @@ struct
| [] => cursorIdx | [] => cursorIdx
end end
end end
functor MakePrevDfaLoopMinus1(M: MAKE_DFA_LOOP) =
struct
fun prev (lineGap: LineGap.t, cursorIdx) =
let
val {idx = bufferIdx, leftStrings, ...} = lineGap
val strIdx = cursorIdx - bufferIdx - 1
val absIdx = cursorIdx - 1
in
if strIdx < 0 then
case leftStrings of
lhd :: ltl =>
M.fStart (String.size lhd - 1, absIdx, lhd, ltl, M.startState, 1)
| [] => 0
else
case #rightStrings lineGap of
rhd :: _ =>
M.fStart (strIdx, absIdx, rhd, leftStrings, M.startState, 1)
| [] => Int.max (0, cursorIdx - 2)
end
end

View File

@@ -108,14 +108,16 @@ struct
end end
end) end)
val startOfNextWORD = StartOfNextWORD.next structure StartOfCurrentWORD =
val endOfPrevWORD = EndOfPrevWORD.prev MakePrevDfaLoopMinus1
(struct
val startState = startState
fun loopStartOfCurrentWORD (idx, absIdx, str, tl, currentState, counter) = fun fStart (idx, absIdx, str, tl, currentState, counter) =
if idx < 0 then if idx < 0 then
case tl of case tl of
str :: tl => str :: tl =>
loopStartOfCurrentWORD fStart
(String.size str - 1, absIdx, str, tl, currentState, counter) (String.size str - 1, absIdx, str, tl, currentState, counter)
| [] => 0 | [] => 0
else else
@@ -127,33 +129,14 @@ struct
if counter - 1 = 0 then if counter - 1 = 0 then
absIdx + 1 absIdx + 1
else else
loopStartOfCurrentWORD fStart
(idx - 1, absIdx - 1, str, tl, startState, counter - 1) (idx - 1, absIdx - 1, str, tl, startState, counter - 1)
else else
loopStartOfCurrentWORD fStart (idx - 1, absIdx - 1, str, tl, newState, counter)
(idx - 1, absIdx - 1, str, tl, newState, counter)
end end
end)
(* we do not rely on MakeDfaLoop functor because we want to val startOfNextWORD = StartOfNextWORD.next
* ignore the character the cursor is currently on, val endOfPrevWORD = EndOfPrevWORD.prev
* and start tracking state from the character before the current one. *) val startOfCurrentWORD = StartOfCurrentWORD.prev
fun startOfCurrentWORD (lineGap: LineGap.t, cursorIdx) =
let
val {idx = bufferIdx, leftStrings, ...} = lineGap
val strIdx = cursorIdx - bufferIdx - 1
val absIdx = cursorIdx - 1
in
if strIdx < 0 then
case leftStrings of
lhd :: ltl =>
loopStartOfCurrentWORD
(String.size lhd - 1, absIdx, lhd, ltl, startState, 1)
| [] => 0
else
case #rightStrings lineGap of
rhd :: _ =>
loopStartOfCurrentWORD
(strIdx, absIdx, rhd, leftStrings, startState, 1)
| [] => Int.max (0, cursorIdx - 2)
end
end end