refactoring key events so all Insert mode are together... may regret that later but its cleaner now. Getting delete line working

This commit is contained in:
Dustin Swan 2026-03-12 21:04:52 -06:00
parent 7a97ec3f90
commit fcce2ac4c0
No known key found for this signature in database
GPG key ID: 30D46587E2100467

View file

@ -18,6 +18,19 @@ textEditor = name \
lines = split "\n" source; lines = split "\n" source;
clampCursor = state \
line = nth state.cursorRow state.lines ~ unwrapOr "";
newRow = max 0 state.cursorRow;
newRow2 = min (len state.lines - 1) newRow;
maxCol = state.mode
| Insert \ len line
| Normal \ max 0 (len line - 1);
newCol2 = min maxCol (max 0 state.cursorCol);
state.{ cursorRow = newRow2, cursorCol = newCol2 };
write = state \ write = state \
content = join "\n" state.lines; content = join "\n" state.lines;
{ state = state, emit = [rebindAt [buffersKey, name] content] }; { state = state, emit = [rebindAt [buffersKey, name] content] };
@ -62,12 +75,26 @@ textEditor = name \
state = state.{ state = state.{
lines = newLines, lines = newLines,
cursorRow = cursorRow, cursorRow = cursorRow,
cursorCol = 0,
undoStack = newUndoStack, undoStack = newUndoStack,
mode = Insert mode = Insert
}, },
emit = [] emit = []
}; };
deleteLine = state \
stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol };
newUndoStack = [stateSnapshot, ...state.undoStack];
newLines = [...(take (state.cursorRow) state.lines), ...(drop (state.cursorRow + 1) state.lines)];
{
state = clampCursor state.{
lines = newLines,
undoStack = newUndoStack,
pending = None
},
emit = []
};
escape = state \ { state = state.{ mode = Normal }, emit = [] }; escape = state \ { state = state.{ mode = Normal }, emit = [] };
undo = state \ state.undoStack undo = state \ state.undoStack
@ -111,19 +138,6 @@ textEditor = name \
{ state = state.{ lines = newLines2, cursorCol = 0, cursorRow = state.cursorRow + 1 }, emit = [] }; { state = state.{ lines = newLines2, cursorCol = 0, cursorRow = state.cursorRow + 1 }, emit = [] };
clampCursor = state \
line = nth state.cursorRow state.lines ~ unwrapOr "";
newRow = max 0 state.cursorRow;
newRow2 = min (len state.lines - 1) newRow;
maxCol = state.mode
| Insert \ len line
| Normal \ max 0 (len line - 1);
newCol2 = min maxCol (max 0 state.cursorCol);
state.{ cursorRow = newRow2, cursorCol = newCol2 };
upArrow = state \ upArrow = state \
newState = clampCursor state.{ cursorRow = state.cursorRow - 1 }; newState = clampCursor state.{ cursorRow = state.cursorRow - 1 };
{ state = newState, emit = [] }; { state = newState, emit = [] };
@ -186,85 +200,48 @@ textEditor = name \
scrollY = 0, scrollY = 0,
undoStack = [], undoStack = [],
redoStack = [], redoStack = [],
mode = Normal # Normal | Insert | Visual mode = Normal, # Normal | Insert | Visual
pending = None # Some "d" | Some "g" | etc.
}, },
update = state event \ event update = state event \ state.mode
| Scrolled delta \ ( | Insert \ (event
maxLineLen = fold (acc line \ max acc (len line)) 0 state.lines; | Key { key = "Escape" } \ escape state
totalW = maxLineLen * charW * scale + maxLineLen * charGap; | Key { key = "Control" } \ escape state
totalH = len state.lines * lineH; | Key { key = "Backspace" } \ backspace state
newX = max 0 (min (totalW - ctx.w) (state.scrollX + delta.deltaX)); | Key { key = "Enter" } \ enter state
newY = max 0 (min (totalH - ctx.h) (state.scrollY + delta.deltaY)); | Key { key = k } \ insertChar k state)
{ state = state.{ scrollY = newY, scrollX = newX }, emit = [] }) | Normal \ (event
| Scrolled delta \ (
maxLineLen = fold (acc line \ max acc (len line)) 0 state.lines;
totalW = maxLineLen * charW * scale + maxLineLen * charGap;
totalH = len state.lines * lineH;
newX = max 0 (min (totalW - ctx.w) (state.scrollX + delta.deltaX));
newY = max 0 (min (totalH - ctx.h) (state.scrollY + delta.deltaY));
{ state = state.{ scrollY = newY, scrollX = newX }, emit = [] })
| Key { key = "ArrowDown" } \ withScroll (downArrow state) | Key { key = "ArrowDown" } \ withScroll (downArrow state)
| Key { key = "j" } \ (state.mode | Key { key = "j" } \ withScroll (downArrow state)
| Insert \ insertChar "j" state | Key { key = "ArrowUp" } \ withScroll (upArrow state)
| Normal \ withScroll (downArrow state)) | Key { key = "k" } \ withScroll(upArrow state)
| Key { key = "ArrowLeft" } \ withScroll (leftArrow state)
| Key { key = "ArrowUp" } \ withScroll (upArrow state) | Key { key = "h" } \ withScroll(leftArrow state)
| Key { key = "k" } \ (state.mode | Key { key = "ArrowRight" } \ withScroll (rightArrow state)
| Insert \ insertChar "k" state | Key { key = "l" } \ withScroll(rightArrow state)
| Normal \ withScroll(upArrow state)) | Key { key = "i" } \ insertMode state
| Key { key = "o" } \ openLine state
| Key { key = "ArrowLeft" } \ withScroll (leftArrow state) | Key { key = "d", ctrl = True } \ scrollHalfDown state ctx
| Key { key = "h" } \ (state.mode | Key { key = "u", ctrl = True } \ scrollHalfUp state ctx
| Insert \ insertChar "h" state | Key { key = "u" } \ undo state
| Normal \ withScroll(leftArrow state)) | Key { key = "d" } \ (state.pending
| Some "d" \ deleteLine state
| Key { key = "ArrowRight" } \ withScroll (rightArrow state) | _ \ { state = state.{ pending = Some "d" }, emit = [] })
| Key { key = "l" } \ (state.mode | Key { key = "r", ctrl = True } \ redo state
| Insert \ insertChar "l" state | Key { key = "W", ctrl = True, shift = True } \ write state
| Normal \ withScroll(rightArrow state)) | Key { key = "A", ctrl = True, shift = True } \ apply state
# any other key or event
| Key { key = "i" } \ (state.mode | _ \ { state = state, emit = [] }
| Insert \ insertChar "i" state ),
| Normal \ insertMode state)
| Key { key = "o" } \ (state.mode
| Insert \ insertChar "o" state
| Normal \ openLine state)
| Key { key = "d", ctrl = True } \ scrollHalfDown state ctx
| Key { key = "u", ctrl = True } \ scrollHalfUp state ctx
| Key { key = "u" } \ (state.mode
| Insert \ insertChar "u" state
| Normal \ undo state)
| Key { key = "r", ctrl = True } \ (state.mode
| Insert \ { state = state, emit = [] }
| Normal \ redo state)
| Key { key = "W", shift = True } \ (state.mode
| Insert \ insertChar "W" state
| Normal \ write state)
| Key { key = "W", ctrl = True, shift = True } \ (state.mode
| Insert \ insertChar "W" state
| Normal \ write state)
| Key { key = "A", ctrl = True, shift = True } \ (state.mode
| Insert \ insertChar "A" state
| Normal \ apply state)
| Key { key = "Escape" } \ escape state
| Key { key = "Backspace" } \ (state.mode
| Insert \ backspace state
| _ \ { state = state, emit = [] })
| Key { key = "Enter" } \ (state.mode
| Insert \ enter state
| _ \ { state = state, emit = [] })
# any other key
| Key { key = key, printable = True } \ (state.mode
| Insert \ insertChar key state
| _ \ { state = state, emit = [] })
| _ \ { state = state, emit = [] },
view = state emit \ view = state emit \
cursorX = state.cursorCol * charW * scale + charGap * state.cursorCol; cursorX = state.cursorCol * charW * scale + charGap * state.cursorCol;