done refactoring 'startOfCurrentWORD' function, with tests still passing

This commit is contained in:
2025-07-20 13:28:40 +01:00
parent 6b5a95ff45
commit 554c3b0354

View File

@@ -108,16 +108,14 @@ struct
end end
end) end)
structure StartOfCurrentWORD = val startOfNextWORD = StartOfNextWORD.next
MakePrevDfaLoop val endOfPrevWORD = EndOfPrevWORD.prev
(struct
val startState = startState
fun fStart (idx, absIdx, str, tl, currentState, counter) = fun loopStartOfCurrentWORD (idx, absIdx, str, tl, currentState, counter) =
if idx < 0 then if idx < 0 then
case tl of case tl of
str :: tl => str :: tl =>
fStart loopStartOfCurrentWORD
(String.size str - 1, absIdx, str, tl, currentState, counter) (String.size str - 1, absIdx, str, tl, currentState, counter)
| [] => 0 | [] => 0
else else
@@ -129,18 +127,33 @@ struct
if counter - 1 = 0 then if counter - 1 = 0 then
absIdx + 1 absIdx + 1
else else
fStart loopStartOfCurrentWORD
(idx - 1, absIdx - 1, str, tl, startState, counter - 1) (idx - 1, absIdx - 1, str, tl, startState, counter - 1)
else else
fStart (idx - 1, absIdx - 1, str, tl, newState, counter) loopStartOfCurrentWORD
(idx - 1, absIdx - 1, str, tl, newState, counter)
end end
end)
val startOfNextWORD = StartOfNextWORD.next
val endOfPrevWORD = EndOfPrevWORD.prev
(* we do not rely on MakeDfaLoop functor because we want to
* ignore the character the cursor is currently on,
* and start tracking state from the character before the current one. *)
fun startOfCurrentWORD (lineGap: LineGap.t, cursorIdx) = fun startOfCurrentWORD (lineGap: LineGap.t, cursorIdx) =
let val lineGap = LineGap.goToIdx (cursorIdx - 1, lineGap) let
in StartOfCurrentWORD.prev (lineGap, cursorIdx - 1) 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 end