From 9fdbdf448f1645b85573b73bf1998f39f49cb315 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Thu, 19 Mar 2026 21:11:19 -0600 Subject: [PATCH] Drying up some code --- src/cg/06-textEditor.cg | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/cg/06-textEditor.cg b/src/cg/06-textEditor.cg index e9e6ed8..42cd9be 100644 --- a/src/cg/06-textEditor.cg +++ b/src/cg/06-textEditor.cg @@ -266,6 +266,11 @@ textEditor = name \ newRow = state.cursorRow + diff; withScroll { state = clampCursor state.{ cursorRow = newRow }, emit = [] }); + handleMotion = motion state \ + state.pending + | None \ withScroll { state = moveCursor (resolveMotion motion state) state, emit = [] } + | Some Delete \ withScroll (applyOperator Delete motion state); + ui.stateful { focusable = True, autoFocus = True, @@ -319,15 +324,9 @@ textEditor = name \ | Key { key = "d" } \ (state.pending | Some Delete \ applyOperator Delete WholeLine state | _ \ { state = state.{ pending = Some Delete }, emit = [] }) - | Key { key = "b" } \ (state.pending - | None \ withScroll { state = moveCursor (resolveMotion BackWord state) state, emit = [] } - | Some Delete \ withScroll (applyOperator Delete BackWord state)) - | Key { key = "w" } \ (state.pending - | None \ withScroll { state = moveCursor (resolveMotion Word state) state, emit = [] } - | Some Delete \ withScroll (applyOperator Delete Word state)) - | Key { key = "$" } \ (state.pending - | None \ withScroll { state = moveCursor (resolveMotion EndOfLine state) state, emit = [] } - | Some Delete \ withScroll (applyOperator Delete EndOfLine state)) + | Key { key = "b" } \ handleMotion BackWord state + | Key { key = "w" } \ handleMotion Word state + | Key { key = "$" } \ handleMotion EndOfLine state | Key { key = "." } \ (state.lastAction | None \ { state = state, emit = [] } | Some action \ applyOperator action.operator action.motion state)