progress with viJ to move cursor downwards

This commit is contained in:
2024-10-20 21:30:06 +01:00
parent 47f4d452fe
commit 6b3d322c1c
5 changed files with 188 additions and 29 deletions

View File

@@ -291,4 +291,168 @@ struct
end
| (_, _) => helpGetCursorColumn (0, leftStrings, leftLines)
end
fun helpViJString
( strPos, str, absIdx
, lineColumn, preferredColumn, hasPassedLine
, strTl, lineTl
) =
if strPos = String.size str then
helpViJList
(absIdx, lineColumn, preferredColumn, hasPassedLine, strTl, lineTl)
else
case String.sub (str, strPos) of
#"\n" =>
if
hasPassedLine
then
(* reached end of line twice,
* but line has fewer chars than preferredColumn
* note: if in double \n\n linebreak,
* then return absIdx of second linebreak. *)
if strPos = String.size str - 1 then
if String.sub (str, strPos + 1) = #"\n" then
(* we are in double linebreak *)
absIdx + 1
else
(* not in double linebreak *)
absIdx - 1
else
(* this is last chr of string; must check string's tl next *)
(case strTl of
hd :: tl =>
if String.sub (hd, 0) = #"\n" then
(* in double linebreak *)
absIdx + 1
else
(* not in double linebreak *)
absIdx - 1
| [] =>
(* no more strings so return last idx *)
absIdx - 1)
else
(* reached end of line once;
* have to check if this is a double linebreak,
* and return idx of second linebreak if so *)
if strPos < String.size str - 1 then
if String.sub (str, strPos + 1) = #"\n" then
absIdx + 1
else
helpViJString
( strPos + 1, str, absIdx + 1
, 0, preferredColumn, true
, strTl, lineTl
)
else
(* this is last chr of string; must check string's tl next *)
(case strTl of
hd :: tl =>
if String.sub (hd, 0) = #"\n" then
(* in double linebreak *)
absIdx + 1
else
(* not in double linebreak *)
helpViJString
( strPos + 1, str, absIdx + 1
, 0, preferredColumn, true
, strTl, lineTl
)
| [] =>
(* no more strings so return last idx *)
absIdx)
| _ =>
if lineColumn <> preferredColumn orelse not hasPassedLine then
(* we're not in the preferred column, so keep iterating *)
helpViJString
( strPos + 1, str, absIdx + 1
, lineColumn + 1, preferredColumn, hasPassedLine
, strTl, lineTl
)
else
(* we're at the preferredColumn so return absIdx *)
absIdx
and helpViJList
(absIdx, lineColumn, preferredColumn, hasPassedLine, strings, lines) =
case (strings, lines) of
(strHd :: strTl, lineHd :: lineTl) =>
(* todo: possibly check if we have passed line,
* and if so, if there are any line breaks in the lineHd
* which we could use to skip searching part of the string.
* However, this will likely have worse cache locality
* as we switch to searching in string to searcing in line vector
* so perhaps not. *)
helpViJString
( 0, strHd, absIdx
, lineColumn, preferredColumn, hasPassedLine
, strTl, lineTl
)
| (_, _) =>
(* empty, so return end of previous string *)
absIdx - 1
fun viJ (lineGap: LineGap.t, cursorIdx) =
let
val
{rightStrings, idx = bufferIdx, rightLines, leftStrings, leftLines, ...} =
lineGap
in
case (rightStrings, rightLines) of
(strHd :: strTl, lnHd :: lnTl) =>
let
(* convert absolute cursorIdx to idx relative to hd string *)
val strIdx = cursorIdx - bufferIdx
in
if strIdx < String.size strHd then
(* strIdx is in this string *)
if String.sub (strHd, strIdx) = #"\n" then
(* if we are at a newline
* note:
* don't need to check if we are a double linebreak,
* because cursor navigation functions already check
* for that condition at the end.
* So there is no way at the start of a navigation function
* that cursor is in a double linebreak incorrectly. *)
helpViJString
(strIdx + 1, strHd, cursorIdx + 1, 0, 0, true, strTl, lnTl)
else
(* not at newline
* so get column number and start iterating *)
let
val lineColumn = getCursorColumn (lineGap, cursorIdx)
in
helpViJString
( strIdx + 1, strHd, cursorIdx + 1
, lineColumn, lineColumn, false
, strTl, lnTl
)
end
else
(* strIdx must be in the strTl *)
(case (strTl, lnTl) of
(nestStrHd :: nestStrTl, nestLnHd :: nestLnTl) =>
let
val strIdx = strIdx - String.size strHd
in
if String.sub (nestStrHd, strIdx) = #"\n" then
helpViJString
(strIdx + 1, strHd, cursorIdx + 1, 0, 0, true, strTl, lnTl)
else
(* not in linebreak *)
let
val lineColumn = getCursorColumn (lineGap, cursorIdx)
in
helpViJString
( strIdx + 1, strHd, cursorIdx + 1
, lineColumn, lineColumn, false
, strTl, lnTl
)
end
end
| (_, _) => cursorIdx)
end
| (_, _) =>
(* nowhere to go rightward, so return cursorIdx *)
cursorIdx
end
end