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:
parent
7a97ec3f90
commit
fcce2ac4c0
1 changed files with 66 additions and 89 deletions
|
|
@ -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,10 +200,18 @@ 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
|
||||||
|
| Insert \ (event
|
||||||
|
| Key { key = "Escape" } \ escape state
|
||||||
|
| Key { key = "Control" } \ escape state
|
||||||
|
| Key { key = "Backspace" } \ backspace state
|
||||||
|
| Key { key = "Enter" } \ enter state
|
||||||
|
| Key { key = k } \ insertChar k state)
|
||||||
|
| 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;
|
||||||
totalW = maxLineLen * charW * scale + maxLineLen * charGap;
|
totalW = maxLineLen * charW * scale + maxLineLen * charGap;
|
||||||
|
|
@ -199,72 +221,27 @@ textEditor = name \
|
||||||
{ state = state.{ scrollY = newY, scrollX = newX }, emit = [] })
|
{ 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
|
|
||||||
| Normal \ withScroll (downArrow state))
|
|
||||||
|
|
||||||
| Key { key = "ArrowUp" } \ withScroll (upArrow state)
|
| Key { key = "ArrowUp" } \ withScroll (upArrow state)
|
||||||
| Key { key = "k" } \ (state.mode
|
| Key { key = "k" } \ withScroll(upArrow state)
|
||||||
| Insert \ insertChar "k" state
|
|
||||||
| Normal \ withScroll(upArrow state))
|
|
||||||
|
|
||||||
| Key { key = "ArrowLeft" } \ withScroll (leftArrow state)
|
| Key { key = "ArrowLeft" } \ withScroll (leftArrow state)
|
||||||
| Key { key = "h" } \ (state.mode
|
| Key { key = "h" } \ withScroll(leftArrow state)
|
||||||
| Insert \ insertChar "h" state
|
|
||||||
| Normal \ withScroll(leftArrow state))
|
|
||||||
|
|
||||||
| Key { key = "ArrowRight" } \ withScroll (rightArrow state)
|
| Key { key = "ArrowRight" } \ withScroll (rightArrow state)
|
||||||
| Key { key = "l" } \ (state.mode
|
| Key { key = "l" } \ withScroll(rightArrow state)
|
||||||
| Insert \ insertChar "l" state
|
| Key { key = "i" } \ insertMode state
|
||||||
| Normal \ withScroll(rightArrow state))
|
| Key { key = "o" } \ openLine state
|
||||||
|
|
||||||
| Key { key = "i" } \ (state.mode
|
|
||||||
| 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 = "d", ctrl = True } \ scrollHalfDown state ctx
|
||||||
| Key { key = "u", ctrl = True } \ scrollHalfUp state ctx
|
| Key { key = "u", ctrl = True } \ scrollHalfUp state ctx
|
||||||
|
| Key { key = "u" } \ undo state
|
||||||
| Key { key = "u" } \ (state.mode
|
| Key { key = "d" } \ (state.pending
|
||||||
| Insert \ insertChar "u" state
|
| Some "d" \ deleteLine state
|
||||||
| Normal \ undo state)
|
| _ \ { state = state.{ pending = Some "d" }, emit = [] })
|
||||||
|
| Key { key = "r", ctrl = True } \ redo state
|
||||||
| Key { key = "r", ctrl = True } \ (state.mode
|
| Key { key = "W", ctrl = True, shift = True } \ write state
|
||||||
| Insert \ { state = state, emit = [] }
|
| Key { key = "A", ctrl = True, shift = True } \ apply state
|
||||||
| Normal \ redo state)
|
# any other key or event
|
||||||
|
| _ \ { state = state, emit = [] }
|
||||||
| 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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue