fix exception when the buffer is empty

This commit is contained in:
2025-09-16 04:35:49 +01:00
parent 0793744e24
commit 169f96f459
2 changed files with 83 additions and 49 deletions

View File

@@ -19,53 +19,70 @@ struct
, acc , acc
) = ) =
let let
val {rightStrings, rightLines, line = curLine, idx = curIdx, ...} = buffer val
{ rightStrings
, rightLines
, line = curLine
, idx = curIdx
, textLength
, ...
} = buffer
val env = Utils.initEnv
( 5
, 5
, windowWidth
, windowHeight
, floatWindowWidth
, floatWindowHeight
, searchList
, String.size searchString
, visualScrollColumn
, startLine
)
val {startX, startY, ...} = env
in in
case (rightStrings, rightLines) of if textLength = 1 then
(shd :: stl, lhd :: ltl) => (* empty string, so there is nothing we can draw
let * except a cursor at the line start.
(* get relative index of line to start building from *) * An empty string is usually thought of to have a length of 0
val strPos = * and this is true, but we always have a \n at the end of the buffer
Utils.getRelativeLineStartFromRightHead (startLine, curLine, lhd) * to respect Unix-style file endings, which we always uphold.
(* get absolute idx of line *) * So, for us, an empty string has a length of 1. *)
val absIdx = curIdx + strPos [Utils.makeCursor (startX, startY, env)]
else
case (rightStrings, rightLines) of
(shd :: stl, lhd :: ltl) =>
let
(* get relative index of line to start building from *)
val strPos =
Utils.getRelativeLineStartFromRightHead
(startLine, curLine, lhd)
(* get absolute idx of line *)
val absIdx = curIdx + strPos
val searchPos = BinSearch.equalOrMore (absIdx, searchList) val searchPos = BinSearch.equalOrMore (absIdx, searchList)
val searchPos = val searchPos =
if searchPos = ~1 then Vector.length searchList else searchPos if searchPos = ~1 then Vector.length searchList else searchPos
in
val env = Utils.initEnv TextBuilderWithHighlight.build
( 5 ( strPos
, 5 , shd
, windowWidth , stl
, windowHeight , lhd
, floatWindowWidth , ltl
, floatWindowHeight , startX
, searchList , startY
, String.size searchString , 0
, visualScrollColumn , startLine
, startLine , absIdx
) , cursorPos
val {startX, startY, ...} = env , env
in , acc
TextBuilderWithHighlight.build , searchPos
( strPos )
, shd end
, stl | (_, _) => acc
, lhd
, ltl
, startX
, startY
, 0
, startLine
, absIdx
, cursorPos
, env
, acc
, searchPos
)
end
| (_, _) => acc
end end
fun buildWithExisting fun buildWithExisting

View File

@@ -49,10 +49,27 @@ struct
() ()
end end
fun ioToLineGap (io, acc) = local
case TextIO.inputLine io of fun loop (io, acc, lastCharWasNewline) =
SOME str => ioToLineGap (io, LineGap.append (str, acc)) case TextIO.inputLine io of
| NONE => LineGap.goToStart acc SOME str =>
let
val endsWithNewline =
String.size str > 0
andalso String.sub (str, String.size str - 1) = #"\n"
in
loop (io, LineGap.append (str, acc), endsWithNewline)
end
| NONE =>
if lastCharWasNewline then
LineGap.goToStart acc
else
let val acc = LineGap.append ("\n", acc)
in LineGap.goToStart acc
end
in
fun ioToLineGap (io, acc) = loop (io, acc, false)
end
fun main () = fun main () =
let let