texteditor: replaceChar

This commit is contained in:
Dustin Swan 2026-04-06 18:18:05 -06:00
parent fcb8feb16f
commit 88098a9ce0
No known key found for this signature in database
GPG key ID: 30D46587E2100467

View file

@ -1,7 +1,7 @@
@textEditor @textEditor
_ = "TODO _ = "TODO
# r to repalce # R to replace instead of insert
# s to seek # s to seek
# w / b bug, can't select `textEditor` at start of line # w / b bug, can't select `textEditor` at start of line
# smart case searching # smart case searching
@ -161,6 +161,17 @@ textEditor = name \
{ state = state.{ lines = newLines, cursorCol = state.cursorCol + 1 }, emit = [] }; { state = state.{ lines = newLines, cursorCol = state.cursorCol + 1 }, emit = [] };
replaceChar = char state \
newLines = updateAt state.cursorRow (line \
newLine = deleteCharAt line state.cursorCol;
insertCharAt newLine state.cursorCol char
) state.lines;
stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol };
newUndoStack = [stateSnapshot, ...state.undoStack];
{ state = state.{ lines = newLines, mode = Normal, undoStack = newUndoStack }, emit = [] };
insertMode = state \ insertMode = state \
stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol }; stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol };
newUndoStack = [stateSnapshot, ...state.undoStack]; newUndoStack = [stateSnapshot, ...state.undoStack];
@ -424,6 +435,9 @@ textEditor = name \
| Key { key = "Enter" } \ enter state | Key { key = "Enter" } \ enter state
| Key { key = k, printable = True } \ insertChar k state | Key { key = k, printable = True } \ insertChar k state
| _ \ { state = state , emit = [] }) | _ \ { state = state , emit = [] })
| ReplaceChar \ (event
| Key { key = k, printable = True } \ replaceChar k state
| _ \ { state = state.{ mode = Normal }, emit = [] })
| Normal \ (event | Normal \ (event
| Scrolled delta \ ( | Scrolled delta \ (
maxLineLen = fold (acc line \ max acc (len line)) 0 state.lines; maxLineLen = fold (acc line \ max acc (len line)) 0 state.lines;
@ -450,6 +464,7 @@ textEditor = name \
| Key { key = "u", ctrl = True } \ scrollHalfUp state ctx | Key { key = "u", ctrl = True } \ scrollHalfUp state ctx
| Key { key = "u" } \ undo state | Key { key = "u" } \ undo state
| Key { key = "r", ctrl = True } \ redo state | Key { key = "r", ctrl = True } \ redo state
| Key { key = "r" } \ { state = state.{ mode = ReplaceChar }, emit = [] }
| Key { key = "x" } \ applyOperator Delete Cursor state | Key { key = "x" } \ applyOperator Delete Cursor state
| Key { key = "d" } \ (state.pending | Key { key = "d" } \ (state.pending
| Some Delete \ applyOperator Delete WholeLine state | Some Delete \ applyOperator Delete WholeLine state