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:
@@ -1,27 +1,5 @@
|
|||||||
structure Cursor =
|
structure Cursor =
|
||||||
struct
|
struct
|
||||||
(* todo: viL does not handle \r\n pair right now
|
|
||||||
* but this is not a priority so it's okay *)
|
|
||||||
local
|
|
||||||
fun helpVilLf (hd, strIdx, bufferIdx, cursorIdx, tl) =
|
|
||||||
(* if we are in \n,
|
|
||||||
* move cursor to character after newline
|
|
||||||
* if possible *)
|
|
||||||
if strIdx < String.size hd - 3 then
|
|
||||||
(* we know it is safe to move cursorIdx by 2
|
|
||||||
* if strIdx + 2 is before or at the end of the string. *)
|
|
||||||
cursorIdx + 2
|
|
||||||
else
|
|
||||||
(* we have to check the tl to see if it is not empty
|
|
||||||
* so we know it is safe to move. *)
|
|
||||||
case tl of
|
|
||||||
_ :: _ =>
|
|
||||||
(* not empty, so we can increment by 2 *)
|
|
||||||
cursorIdx + 2
|
|
||||||
| [] =>
|
|
||||||
(* empty, so return end of string which is \n *)
|
|
||||||
bufferIdx + String.size hd - 1
|
|
||||||
in
|
|
||||||
(* Prerequisite: lineGap is moved to requested idx first
|
(* Prerequisite: lineGap is moved to requested idx first
|
||||||
* todo: check if we are in a \r\n pair, but this is not a priority *)
|
* todo: check if we are in a \r\n pair, but this is not a priority *)
|
||||||
fun viL (lineGap: LineGap.t, cursorIdx) =
|
fun viL (lineGap: LineGap.t, cursorIdx) =
|
||||||
@@ -34,19 +12,56 @@ struct
|
|||||||
(* convert absolute cursorIdx to idx relative to hd string *)
|
(* convert absolute cursorIdx to idx relative to hd string *)
|
||||||
val strIdx = cursorIdx - bufferIdx
|
val strIdx = cursorIdx - bufferIdx
|
||||||
in
|
in
|
||||||
if strIdx < String.size hd - 2 then
|
if strIdx + 1 < String.size hd then
|
||||||
(case String.sub (hd, strIdx + 1) of
|
(* if there is at least one character after this idx *)
|
||||||
#"\n" => helpVilLf (hd, strIdx, bufferIdx, cursorIdx, tl)
|
let
|
||||||
| _ => cursorIdx + 1)
|
val nextChr = String.sub (hd, strIdx + 1)
|
||||||
|
in
|
||||||
|
(case nextChr of
|
||||||
|
#"\n" =>
|
||||||
|
if strIdx + 2 < String.size hd then
|
||||||
|
(* if there are at least two chars after strIdx *)
|
||||||
|
cursorIdx + 2
|
||||||
else
|
else
|
||||||
|
(* only one char after strIdx which is \n
|
||||||
|
* if there is a string at the tl, can increment by 2 *)
|
||||||
|
(case tl of
|
||||||
|
_ :: _ => cursorIdx + 2
|
||||||
|
| [] => cursorIdx + 1)
|
||||||
|
| _ => cursorIdx + 1)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
(* no chars after this idx; have to check tl *)
|
||||||
(case tl of
|
(case tl of
|
||||||
tlhd :: tltl =>
|
tlhd :: tltl =>
|
||||||
(* if there is another string after current head, we can increment cursorIdx
|
(* if there is another string after current head, we can increment cursorIdx
|
||||||
* however, first we need to check if next char is \n. *)
|
* however, first we need to check if next char is \n. *)
|
||||||
(case String.sub (tlhd, 0) of
|
let
|
||||||
#"\n" => helpVilLf (tlhd, 0, bufferIdx, cursorIdx, tltl)
|
val nextChr = String.sub (tlhd, 0)
|
||||||
| _ => cursorIdx + 1)
|
in
|
||||||
|
(case nextChr of
|
||||||
|
#"\n" =>
|
||||||
|
if String.size tlhd > 2 then
|
||||||
|
(* if there is at least one character after \n
|
||||||
|
* then increment cursorIdx by 2 *)
|
||||||
|
cursorIdx + 2
|
||||||
|
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 *)
|
(* if there is no string after current head, return original cursorIdx *)
|
||||||
cursorIdx)
|
cursorIdx)
|
||||||
end
|
end
|
||||||
@@ -54,5 +69,4 @@ struct
|
|||||||
(* return original cursorIdx if there is nothing to the right *)
|
(* return original cursorIdx if there is nothing to the right *)
|
||||||
cursorIdx
|
cursorIdx
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -326,6 +326,16 @@ struct
|
|||||||
else
|
else
|
||||||
(* in cursor position, so build cursorAcc
|
(* in cursor position, so build cursorAcc
|
||||||
* and call AfterCursor function *)
|
* and call AfterCursor function *)
|
||||||
|
if pos = String.size str - 1 andalso tl = [] then
|
||||||
|
(* if we are at end of lineGap, we want to build cursorAcc
|
||||||
|
* at different coordinates than usual *)
|
||||||
|
let
|
||||||
|
val cursorAcc =
|
||||||
|
buildCursor (startX, posY + ySpace, fWindowWidth, fWindowHeight, r, g, b)
|
||||||
|
in
|
||||||
|
accToDrawMsg (acc, cursorAcc)
|
||||||
|
end
|
||||||
|
else
|
||||||
let
|
let
|
||||||
val cursorAcc = buildCursor (posX, posY, fWindowWidth, fWindowHeight, r, g ,b)
|
val cursorAcc = buildCursor (posX, posY, fWindowWidth, fWindowHeight, r, g ,b)
|
||||||
in
|
in
|
||||||
|
|||||||
Reference in New Issue
Block a user