refactor another cursor.sml function

This commit is contained in:
2024-10-24 13:30:02 +01:00
parent 50a0a3e31b
commit 5534fd3645
2 changed files with 23 additions and 86 deletions

View File

@@ -1030,12 +1030,8 @@ struct
absIdx - 1 absIdx - 1
fun startNextWord (shd, strIdx, absIdx, stl, ltl) = fun startNextWord (shd, strIdx, absIdx, stl, ltl) =
let helpNextWordString
val chr = String.sub (shd, strIdx) (strIdx, shd, absIdx, stl, ltl)
in
helpNextWordString
(strIdx, shd, absIdx, stl, ltl)
end
fun nextWord (lineGap: LineGap.t, cursorIdx) = fun nextWord (lineGap: LineGap.t, cursorIdx) =
let let
@@ -1065,90 +1061,38 @@ struct
| (_, _) => cursorIdx | (_, _) => cursorIdx
end end
fun helpPrevWordString fun helpPrevWordString (strPos, str, absIdx, strTl, lineTl) =
(strPos, str, absIdx, strTl, lineTl, lastWordType) =
if strPos < 0 then if strPos < 0 then
helpPrevWordList helpPrevWordList (strTl, lineTl, absIdx)
(strTl, lineTl, absIdx, lastWordType)
else else
let let
val chr = String.sub (str, strPos) val chr = String.sub (str, strPos)
in in
if Char.isAlphaNum chr orelse chr = #"_" then if Char.isAlphaNum chr orelse chr = #"_" then
case lastWordType of if isPrevChrSpace (strPos, str, strTl)
ALPHA_NUM => orelse isPrevChrNonBlank (strPos, str, strTl) then
(* check if prev chr is space; absIdx
* if it is, return current idx else
* and if not, keep iterating *) helpPrevWordString
if isPrevChrSpace (strPos, str, strTl) then (strPos - 1, str, absIdx - 1, strTl, lineTl)
absIdx
else
helpPrevWordString
( strPos - 1, str, absIdx - 1
, strTl, lineTl, ALPHA_NUM
)
| SPACE =>
(* last chr checked was space
* and this chr is ALPHA_NUM
* so keep iterating *)
helpPrevWordString
( strPos - 1, str, absIdx - 1
, strTl, lineTl, ALPHA_NUM
)
| NON_BLANK =>
(* last chr was NON_BLANK
* and this chr is ALPHA_NUM
* and this change is a word break.
* So return idx of last chr. *)
absIdx + 1
else if Char.isSpace chr then else if Char.isSpace chr then
case lastWordType of helpPrevWordString
SPACE => (strPos - 1, str, absIdx - 1, strTl, lineTl)
(* nothing to do on double space,
* except keep iterating *)
helpPrevWordString
( strPos - 1, str, absIdx - 1
, strTl, lineTl, SPACE
)
| _ =>
(* last chr was either ALPHA_NUM or NON_BLANK
* and current chr is space
* so we have word break.
* thus, return start of last word *)
absIdx + 1
else else
(* is NON_BLANK *) (* is NON_BLANK *)
case lastWordType of if isPrevChrSpace (strPos, str, strTl)
NON_BLANK => orelse isPrevChrAlphaNum (strPos, str, strTl) then
(* last and current wordType is same absIdx
* so keep iterating *) else
if isPrevChrSpace (strPos, str, strTl) then helpPrevWordString
absIdx (strPos - 1, str, absIdx - 1, strTl, lineTl)
else
helpPrevWordString
( strPos - 1, str, absIdx - 1
, strTl, lineTl, NON_BLANK
)
| ALPHA_NUM =>
(* word break since we last chr was ALPHA_NUM
* and this one is NON_BLANK
* so return idx of last chr *)
absIdx + 1
| SPACE =>
(* space means keep iterating *)
helpPrevWordString
( strPos - 1, str, absIdx - 1
, strTl, lineTl, NON_BLANK
)
end end
and helpPrevWordList (strings, lines, absIdx, lastWordType) = and helpPrevWordList (strings, lines, absIdx) =
case (strings, lines) of case (strings, lines) of
(strHd::strTl, lineHd::lineTl) => (strHd::strTl, lineHd::lineTl) =>
helpPrevWordString helpPrevWordString
( String.size strHd - 1, strHd, absIdx (String.size strHd - 1, strHd, absIdx, strTl, lineTl)
, strTl, lineTl, lastWordType
)
| (_, _) => | (_, _) =>
(* reached start of lineGap; (* reached start of lineGap;
* return 0 which is start idx *) * return 0 which is start idx *)
@@ -1159,23 +1103,16 @@ struct
* and ignore the character the cursor is at * and ignore the character the cursor is at
* so check previous character *) * so check previous character *)
if strIdx > 0 then if strIdx > 0 then
let helpPrevWordString
val prevChr = String.sub (shd, strIdx - 1) (strIdx - 1, shd, absIdx - 1, stl, ltl)
val prevWordType = getWordType prevChr
in
helpPrevWordString
(strIdx - 1, shd, absIdx - 1, stl, ltl, prevWordType)
end
else else
case (stl, ltl) of case (stl, ltl) of
(stlhd::stltl, ltlhd::ltltl) => (stlhd::stltl, ltlhd::ltltl) =>
let let
val prevIdx = String.size stlhd - 1 val prevIdx = String.size stlhd - 1
val prevChr = String.sub (stlhd, prevIdx)
val prevWordType = getWordType prevChr
in in
helpPrevWordString helpPrevWordString
(prevIdx, stlhd, absIdx - 1, stltl, ltltl, prevWordType) (prevIdx, stlhd, absIdx - 1, stltl, ltltl)
end end
| (_, _) => | (_, _) =>
(* tl is empty; just return idx 0 *) (* tl is empty; just return idx 0 *)

BIN
shf

Binary file not shown.