From c28dc429b7ae9179a9b88506a70b73b0e694362c Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Mon, 25 Nov 2024 04:37:47 +0000 Subject: [PATCH] address todo in text-builder.sml by adding prerequisite: always call SearchList.goToNum before calling TextBuilder.build --- fcore/app-update.sml | 29 ++++++++++++++++-- fcore/app-with.sml | 7 +++-- fcore/text-builder.sml | 67 ++++++++++++++++++++++++++++-------------- 3 files changed, 75 insertions(+), 28 deletions(-) diff --git a/fcore/app-update.sml b/fcore/app-update.sml index 97efa4a..65daa71 100644 --- a/fcore/app-update.sml +++ b/fcore/app-update.sml @@ -28,6 +28,9 @@ struct } = app val newBuffer = LineGap.goToLine (startLine, buffer) + val lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer) + val searchList = SearchList.goToNum (lineIdx, searchList) + val drawMsg = TextBuilder.build ( startLine , cursorIdx @@ -38,7 +41,8 @@ struct , searchString ) - val newApp = AppWith.bufferAndSize (app, newBuffer, newWidth, newHeight) + val newApp = + AppWith.bufferAndSize (app, newBuffer, newWidth, newHeight, searchList) in (newApp, drawMsg) end @@ -47,7 +51,8 @@ struct let val {windowWidth, windowHeight, startLine, searchString, ...} = app - (* move LineGap to first line displayed on screen *) + (* move LineGap to first line displayed on screen + * and move searchList to line's start idx as well *) val buffer = LineGap.goToLine (startLine, buffer) (* get new startLine which may move screen depending on cursor movements *) @@ -56,6 +61,8 @@ struct (* move buffer to new startLine as required by TextBuilder.build *) val buffer = LineGap.goToLine (startLine, buffer) + val lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer) + val searchList = SearchList.goToNum (lineIdx, searchList) val drawMsg = TextBuilder.build ( startLine @@ -90,8 +97,11 @@ struct val startLine = TextWindow.getStartLine (buffer, startLine, cursorIdx, windowWidth, windowHeight) - (* move buffer to new startLine as required by TextBuilder.build *) + (* move buffer to new startLine as required by TextBuilder.build + * and move searchList to idx where line starts as well *) val buffer = LineGap.goToLine (startLine, buffer) + val lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer) + val searchList = SearchList.goToNum (lineIdx, searchList) val drawMsg = TextBuilder.build ( startLine @@ -128,6 +138,8 @@ struct (buffer, cursorIdx, origLine, windowWidth, windowHeight div 2) val buffer = LineGap.goToLine (startLine, buffer) + val lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer) + val searchList = SearchList.goToNum (lineIdx, searchList) val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx, NORMAL_MODE "", startLine, searchList) @@ -155,6 +167,7 @@ struct val cursorIdx = 0 val startLine = 0 val buffer = LineGap.goToIdx (cursorIdx, buffer) + val searchList = SearchList.goToNum (0, searchList) val drawMsg = TextBuilder.build ( startLine @@ -195,6 +208,9 @@ struct end val buffer = LineGap.goToLine (bufferLine, buffer) + val lineIdx = TextBuilder.getLineAbsIdx (bufferLine, buffer) + val searchList = SearchList.goToNum (lineIdx, searchList) + val drawMsg = TextBuilder.build ( bufferLine , bufferIdx @@ -236,6 +252,8 @@ struct (buffer, cursorIdx, origLine, windowWidth, windowHeight div 2) val buffer = LineGap.goToLine (startLine, buffer) + val lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer) + val searchList = SearchList.goToNum (lineIdx, searchList) val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx, NORMAL_MODE "", startLine, searchList) @@ -297,6 +315,8 @@ struct val cursorIdx = Cursor.matchPair (buffer, cursorIdx) val buffer = LineGap.goToLine (startLine, buffer) + val lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer) + val searchList = SearchList.goToNum (lineIdx, searchList) in if TextWindow.isCursorVisible @@ -327,6 +347,8 @@ struct (buffer, cursorIdx, startLine, windowWidth, windowHeight div 2) val buffer = LineGap.goToLine (startLine, buffer) + val lineIdx = TextBuilder.getLineAbsIdx (startLine, buffer) + val searchList = SearchList.goToNum (lineIdx, searchList) val newApp = AppWith.bufferAndCursorIdx (app, buffer, cursorIdx, NORMAL_MODE "", startLine, searchList) @@ -700,6 +722,7 @@ struct val cursorIdx = 0 val startLine = 0 val buffer = LineGap.goToIdx (cursorIdx, buffer) + val searchList = SearchList.goToNum (0, searchList) val drawMsg = TextBuilder.build ( startLine diff --git a/fcore/app-with.sml b/fcore/app-with.sml index 4ca0550..a1d23d0 100644 --- a/fcore/app-with.sml +++ b/fcore/app-with.sml @@ -26,14 +26,15 @@ struct } end - fun bufferAndSize (app: app_type, newBuffer, newWidth, newHeight) = + fun bufferAndSize + (app: app_type, newBuffer, newWidth, newHeight, newSearchList) = let val { mode , buffer = _ , windowWidth = _ , windowHeight = _ - , searchList + , searchList = _ , searchString , startLine , cursorIdx @@ -43,7 +44,7 @@ struct , buffer = newBuffer , windowWidth = newWidth , windowHeight = newHeight - , searchList = searchList + , searchList = newSearchList , searchString = searchString , startLine = startLine , cursorIdx = cursorIdx diff --git a/fcore/text-builder.sml b/fcore/text-builder.sml index 41c02ad..9834378 100644 --- a/fcore/text-builder.sml +++ b/fcore/text-builder.sml @@ -1,5 +1,8 @@ signature TEXT_BUILDER = sig + (* Prerequisites: LineGap is moved to requested line first. *) + val getLineAbsIdx: int * LineGap.t -> int + (* Prerequisites: LineGap is moved to requested line first. *) val build: int * int * LineGap.t * int * int * SearchList.t * string -> MailboxType.t list @@ -459,6 +462,45 @@ struct | [] => accToDrawMsg (acc, cursorAcc, bgAcc) + (* gets line start idx, relative to right hd *) + fun helpGetLineStartIdx (startLine, curLine, rLnHd) = + if startLine > curLine then + let + val lnPos = startLine - curLine - 1 + in + Vector.sub (rLnHd, lnPos) + 1 + end + else + 0 + + (* gets line start idx, absolute *) + fun helpGetLineAbsIdx (curIdx, startLine, curLine, rLnHd) = + let + val startIdx = + if startLine > curLine then + let + val lnPos = startLine - curLine - 1 + in + Vector.sub (rLnHd, lnPos) + 1 + end + else + 0 + in + curIdx + startIdx + end + + fun getLineAbsIdx (startLine, lineGap: LineGap.t) = + let + val {rightLines, line = curLine, idx = curIdx, ...} = lineGap + in + case rightLines of + rLnHd :: _ => + helpGetLineAbsIdx (curIdx, startLine, curLine, rLnHd) + | [] => + (* should never happen *) + 0 + end + fun build ( startLine, cursorPos, lineGap: LineGap.t , windowWidth, windowHeight @@ -470,30 +512,11 @@ struct case (rightStrings, rightLines) of (rStrHd :: rStrTl, rLnHd :: _) => let - (* get index of line to start building from *) - val startIdx = - if startLine > curLine then - let - val lnPos = startLine - curLine - 1 - val startIdx = Vector.sub (rLnHd, lnPos) - in - if - String.sub (rStrHd, startIdx) = #"\r" - andalso startIdx < String.size rStrHd - 1 - andalso String.sub (rStrHd, startIdx + 1) = #"\n" - then - (* handle \r\n pair *) - startIdx + 2 - else startIdx + 1 - end - else - 0 + (* get relative index of line to start building from *) + val startIdx = helpGetLineStartIdx (startLine, curLine, rLnHd) + (* get absolute idx of line *) val absIdx = curIdx + startIdx - (* todo: make going to absIdx a prerequisite for using this - * function *) - val searchList = SearchList.goToNum (absIdx, searchList) - val windowData = { w = windowWidth , h = windowHeight