improve code to move rightwards in cursor.sml (making it clearer with comments), and improve code to draw cursor at newline in text=builder.sml
This commit is contained in:
122
fcore/cursor.sml
122
fcore/cursor.sml
@@ -1,58 +1,72 @@
|
|||||||
structure Cursor =
|
structure Cursor =
|
||||||
struct
|
struct
|
||||||
(* todo: viL does not handle \r\n pair right now
|
(* Prerequisite: lineGap is moved to requested idx first
|
||||||
* but this is not a priority so it's okay *)
|
* todo: check if we are in a \r\n pair, but this is not a priority *)
|
||||||
local
|
fun viL (lineGap: LineGap.t, cursorIdx) =
|
||||||
fun helpVilLf (hd, strIdx, bufferIdx, cursorIdx, tl) =
|
let
|
||||||
(* if we are in \n,
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
||||||
* move cursor to character after newline
|
in
|
||||||
* if possible *)
|
case rightStrings of
|
||||||
if strIdx < String.size hd - 3 then
|
hd :: tl =>
|
||||||
(* we know it is safe to move cursorIdx by 2
|
let
|
||||||
* if strIdx + 2 is before or at the end of the string. *)
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||||
cursorIdx + 2
|
val strIdx = cursorIdx - bufferIdx
|
||||||
else
|
in
|
||||||
(* we have to check the tl to see if it is not empty
|
if strIdx + 1 < String.size hd then
|
||||||
* so we know it is safe to move. *)
|
(* if there is at least one character after this idx *)
|
||||||
case tl of
|
let
|
||||||
_ :: _ =>
|
val nextChr = String.sub (hd, strIdx + 1)
|
||||||
(* not empty, so we can increment by 2 *)
|
in
|
||||||
cursorIdx + 2
|
(case nextChr of
|
||||||
| [] =>
|
#"\n" =>
|
||||||
(* empty, so return end of string which is \n *)
|
if strIdx + 2 < String.size hd then
|
||||||
bufferIdx + String.size hd - 1
|
(* if there are at least two chars after strIdx *)
|
||||||
in
|
cursorIdx + 2
|
||||||
(* Prerequisite: lineGap is moved to requested idx first
|
else
|
||||||
* todo: check if we are in a \r\n pair, but this is not a priority *)
|
(* only one char after strIdx which is \n
|
||||||
fun viL (lineGap: LineGap.t, cursorIdx) =
|
* if there is a string at the tl, can increment by 2 *)
|
||||||
let
|
(case tl of
|
||||||
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
_ :: _ => cursorIdx + 2
|
||||||
in
|
| [] => cursorIdx + 1)
|
||||||
case rightStrings of
|
|
||||||
hd :: tl =>
|
|
||||||
let
|
|
||||||
(* convert absolute cursorIdx to idx relative to hd string *)
|
|
||||||
val strIdx = cursorIdx - bufferIdx
|
|
||||||
in
|
|
||||||
if strIdx < String.size hd - 2 then
|
|
||||||
(case String.sub (hd, strIdx + 1) of
|
|
||||||
#"\n" => helpVilLf (hd, strIdx, bufferIdx, cursorIdx, tl)
|
|
||||||
| _ => cursorIdx + 1)
|
| _ => cursorIdx + 1)
|
||||||
else
|
end
|
||||||
(case tl of
|
else
|
||||||
tlhd :: tltl =>
|
(* no chars after this idx; have to check tl *)
|
||||||
(* if there is another string after current head, we can increment cursorIdx
|
(case tl of
|
||||||
* however, first we need to check if next char is \n. *)
|
tlhd :: tltl =>
|
||||||
(case String.sub (tlhd, 0) of
|
(* if there is another string after current head, we can increment cursorIdx
|
||||||
#"\n" => helpVilLf (tlhd, 0, bufferIdx, cursorIdx, tltl)
|
* however, first we need to check if next char is \n. *)
|
||||||
| _ => cursorIdx + 1)
|
let
|
||||||
| _ =>
|
val nextChr = String.sub (tlhd, 0)
|
||||||
(* if there is no string after current head, return original cursorIdx *)
|
in
|
||||||
cursorIdx)
|
(case nextChr of
|
||||||
end
|
#"\n" =>
|
||||||
| [] =>
|
if String.size tlhd > 2 then
|
||||||
(* return original cursorIdx if there is nothing to the right *)
|
(* if there is at least one character after \n
|
||||||
cursorIdx
|
* then increment cursorIdx by 2 *)
|
||||||
end
|
cursorIdx + 2
|
||||||
end
|
else
|
||||||
|
(* this string only contains \n
|
||||||
|
* but there is a small possibility tltl
|
||||||
|
* contains another string.
|
||||||
|
* If it does, we can increment cursorIdx by 2,
|
||||||
|
* moving past newline.
|
||||||
|
* If not, increment cursorIdx by 1,
|
||||||
|
* landing on newline. *)
|
||||||
|
(case tltl of
|
||||||
|
tltlhd :: _ => cursorIdx + 2
|
||||||
|
| [] => cursorIdx + 1)
|
||||||
|
| _ =>
|
||||||
|
(* next char is not newline,
|
||||||
|
* so we can just increment by 1 *)
|
||||||
|
cursorIdx + 1)
|
||||||
|
end
|
||||||
|
| [] =>
|
||||||
|
(* if there is no string after current head, return original cursorIdx *)
|
||||||
|
cursorIdx)
|
||||||
|
end
|
||||||
|
| [] =>
|
||||||
|
(* return original cursorIdx if there is nothing to the right *)
|
||||||
|
cursorIdx
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -326,15 +326,25 @@ struct
|
|||||||
else
|
else
|
||||||
(* in cursor position, so build cursorAcc
|
(* in cursor position, so build cursorAcc
|
||||||
* and call AfterCursor function *)
|
* and call AfterCursor function *)
|
||||||
let
|
if pos = String.size str - 1 andalso tl = [] then
|
||||||
val cursorAcc = buildCursor (posX, posY, fWindowWidth, fWindowHeight, r, g ,b)
|
(* if we are at end of lineGap, we want to build cursorAcc
|
||||||
in
|
* at different coordinates than usual *)
|
||||||
buildTextStringAfterCursor
|
let
|
||||||
( pos + 1, str, acc, startX, posY + ySpace, startX
|
val cursorAcc =
|
||||||
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
buildCursor (startX, posY + ySpace, fWindowWidth, fWindowHeight, r, g, b)
|
||||||
, r, g, b, tl, cursorAcc
|
in
|
||||||
)
|
accToDrawMsg (acc, cursorAcc)
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val cursorAcc = buildCursor (posX, posY, fWindowWidth, fWindowHeight, r, g ,b)
|
||||||
|
in
|
||||||
|
buildTextStringAfterCursor
|
||||||
|
( pos + 1, str, acc, startX, posY + ySpace, startX
|
||||||
|
, windowWidth, windowHeight, fWindowWidth, fWindowHeight
|
||||||
|
, r, g, b, tl, cursorAcc
|
||||||
|
)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
accToDrawMsg (acc, cursorAcc)
|
accToDrawMsg (acc, cursorAcc)
|
||||||
| #"\r" =>
|
| #"\r" =>
|
||||||
|
|||||||
Reference in New Issue
Block a user