From 16f65c0d9d7ee847f855adad41d124ddf9290d59 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 30 Oct 2024 10:23:55 +0000 Subject: [PATCH] add 'goToEnd' function in line_gap.sml --- src/line_gap.sml | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/line_gap.sml b/src/line_gap.sml index 060bb91..4943987 100644 --- a/src/line_gap.sml +++ b/src/line_gap.sml @@ -20,6 +20,7 @@ sig val append: string * t -> t val goToStart: t -> t + val goToEnd: t -> t val goToIdx: int * t -> t val goToLine: int * t -> t @@ -2010,6 +2011,69 @@ struct ({idx, line, leftStrings, leftLines, rightStrings, rightLines}: t) = helpGoToStart (idx, line, leftStrings, leftLines, rightStrings, rightLines) + fun helpGoToEnd + (idx, line, leftStrings, leftLines, rightStrings, rightLines) = + case (rightStrings, rightLines) of + (rStrHd :: rStrTl, rLnHd :: rLnTl) => + (case (leftStrings, leftLines) of + (lStrHd :: lStrTl, lLnHd :: lLnTl) => + if isInLimit (lStrHd, rStrHd, lLnHd, rLnHd) then + (* join if possible *) + let + val newLstrHd = lStrHd ^ rStrHd + val newLlnHd = + Vector.tabulate + ( Vector.length lLnHd + Vector.length rLnHd + , fn lnIdx => + if lnIdx < Vector.length lLnHd then + Vector.sub (lLnHd, lnIdx) + else + Vector.sub (rLnHd, lnIdx - Vector.length lLnHd) + + String.size lStrHd + ) + in + helpGoToEnd + ( idx + String.size rStrHd + , line + Vector.length rLnHd + , newLstrHd :: lStrTl + , newLlnHd :: lLnTl + , rStrTl + , rLnTl + ) + end + else + helpGoToEnd + ( idx + String.size rStrHd + , line + Vector.length rLnHd + , rStrHd :: leftStrings + , rLnHd :: leftLines + , rStrTl + , rLnTl + ) + | (_, _) => + (* rightStrings and rightLines are both empty *) + helpGoToEnd + ( idx + String.size rStrHd + , line + Vector.length rLnHd + , rStrHd :: leftStrings + , rLnHd :: leftLines + , [] + , [] + )) + | (_, _) => + (* rightStrings strings are empty, meaning we are at end and can return *) + { idx = idx + , line = line + , leftStrings = leftStrings + , leftLines = leftLines + , rightStrings = [] + , rightLines = [] + } + + fun goToEnd + ({idx, line, leftStrings, leftLines, rightStrings, rightLines}: t) = + helpGoToEnd (idx, line, leftStrings, leftLines, rightStrings, rightLines) + (* function to abstract leftwards movement. * if the left hd and the right hd can be joined in one node * during movement, while staying in limit, then join and move.