From 8d540f4df9979bb7ba357b002d4c4ad949f12ea5 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sun, 7 Jul 2024 19:49:37 +0100 Subject: [PATCH] code another deletion helper function for line_gap.sml --- src/line_gap.sml | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/line_gap.sml b/src/line_gap.sml index ea0c925..202f2ba 100644 --- a/src/line_gap.sml +++ b/src/line_gap.sml @@ -1380,6 +1380,75 @@ struct , rightLinesTl ) end + | (_, _) => + { idx = curIdx + , line = curLine + , leftStrings = leftStrings + , leftLines = leftLines + , rightStrings = rightStrings + , rightLines = rightLines + } + + fun deleteLeftFromHere + (start, curIdx, curLines, leftStrings, leftLines, rightStrings, rightLines) = + case (leftStrings, leftLines) of + (leftStringsHd :: leftStringsTl, leftLinesHd :: leftLinesTl) => + let + val prevIdx = curIdx - String.size leftStringsHd + val prevLine = curLine - Vector.length leftLinesHd + in + if start < prevIdx then + (* Continue deleting leftward. *) + deleteLeftFromHere + ( start + , prevIdx + , prevLine + , leftStringsTl + , leftLinesTl + , rightStrings + , rightLines + ) + else if start > prevIdx then + (* Base case: delete end part of this string and return. *) + let + val length = start - prevIdx + val newStr = String.substring (leftStringsHd, 0, length) + val midpoint = binSearch (String.size newStr - 1, leftLinesHd) + val newLines = + let + val slice = VectorSlice.slice + (leftLinesHd, 0, SOME (midpoint + 1)) + in + VectorSlice.vector slice + end + in + { idx = prevIdx + String.size newStr + , line = prevLine + Vector.length newLines + , leftStrings = newStr :: leftStringsTl + , leftLines = newLines :: leftLinesTl + , rightStrings = rightStrings + , rightLines = rightLines + } + end + else + (* start = prevIdx + * Base case: Remove leftStrings/LinesHd without removing any further. *) + { idx = prevIdx + , line = prevLine + , leftStrings = leftStringsTl + , leftLines = leftLinesTl + , rightStrings = rightStrings + , rightLines = rightLines + } + end + | (_, _) => + { idx = curIdx + , line = curLine + , leftStrings = leftStrings + , leftLines = leftLines + , rightStrings = rightStrings + , rightLines = rightLines + } (* TEST CODE *)