From d36f694d80a71e0d486e617629c85e4aeab9af88 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Mon, 6 Apr 2026 17:12:19 -0600 Subject: [PATCH] Adding findPrev for searching backwards to textEditor. Fixing findNext bug --- src/cg/textEditor.cg | 50 +++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/cg/textEditor.cg b/src/cg/textEditor.cg index 23651b7..8142a4b 100644 --- a/src/cg/textEditor.cg +++ b/src/cg/textEditor.cg @@ -1,5 +1,13 @@ @textEditor +# TODO +# r to repalce +# O to open new line above +# s to seek +# w / b bug, can't select `textEditor` at start of line +# a to append +# smart case searching + textEditor = name \ # defaults = {}; # c = { ...defaults, ...config }; @@ -136,7 +144,6 @@ textEditor = name \ { state = state, emit = [rebindAt [buffersKey, name] content] }; apply = state \ - # content = name & " = " & (join "\n" state.lines) & ";"; content = (join "\n" state.lines); result = eval! content; _ = debug! "apply" [content, result]; @@ -243,7 +250,7 @@ textEditor = name \ { state = newState, emit = [] }; findNext = query lines row col \ - rest = strIndexOf query (nth row lines ~ unwrapOr "") (col + 1); + rest = strIndexOf query (nth row lines ~ unwrapOr "") col; rest | Some c \ { row = row, col = c } | None \ ( @@ -259,7 +266,23 @@ textEditor = name \ | None \ findInLines (r + 1))); findInLines (row + 1)); - # TODO: findPrev + findPrev = query lines row col \ + line = slice (nth row lines ~ unwrapOr "") 0 col; + rest = strLastIndexOf query line; + rest + | Some c \ { row = row, col = c } + | None \ ( + findInLines = r \ + r < 0 + | True \ findInLines (len lines - 1) # wrap to bottom + | False \ (r == row + | True \ (strLastIndexOf query (nth row lines ~ unwrapOr "") + | Some c \ { row = row, col = c } + | None \ { row = row, col = col }) # no match + | False \ (strLastIndexOf query (nth r lines ~ unwrapOr "") + | Some c \ { row = r, col = c } + | None \ findInLines (r - 1))); + findInLines (row - 1)); { view = ctx \ @@ -320,17 +343,10 @@ textEditor = name \ updateSearchQuery (state.searchQuery & char) state; endSearch = state \ - # next = findNext state.searchQuery state.lines state.cursorRow state.cursorCol; - newState = autoScroll state.{ - mode = Normal, - # cursorCol = next.col, - # cursorRow = next.row, - }; - - { state = newState, emit = [] }; + { state = autoScroll state.{ mode = Normal }, emit = [] }; nextSearch = state \ - next = findNext state.searchQuery state.lines state.cursorRow state.cursorCol; + next = findNext state.searchQuery state.lines state.cursorRow (state.cursorCol + 1); newState = autoScroll state.{ mode = Normal, cursorCol = next.col, @@ -339,6 +355,16 @@ textEditor = name \ { state = newState, emit = [] }; + prevSearch = state \ + prev = findPrev state.searchQuery state.lines state.cursorRow state.cursorCol; + newState = autoScroll state.{ + mode = Normal, + cursorCol = prev.col, + cursorRow = prev.row, + }; + + { state = newState, emit = [] }; + scrollHalfUp = state ctx \ ( diff = floor ((ctx.h / 2) / lineH); newRow = state.cursorRow - diff;