Extract EditorState from AppLayout (step 3a)#189
Conversation
Moves the three editor fields out of AppLayout into a dedicated class: - #editorLines -> EditorState.#lines (exposed via .lines getter) - #cursorLine -> EditorState.#cursorLine (getter + setter) - #cursorCol -> EditorState.#cursorCol (getter + setter) AppLayout holds this.#editorState and accesses state through it. Two reset sites (completeTurn, waitForInput) replaced by reset(). Key handling and rendering stay in AppLayout for now. The lines array is intentionally mutable from AppLayout — direct index assignment and splice still happen there until key handling moves in 3b. The setter pattern (cursorLine++, cursorLine = n) works correctly through the getter/setter pair. TypeScript caught every missed reference at compile time.
bananabot9000
left a comment
There was a problem hiding this comment.
Clean mechanical extraction 🍌
EditorState is pure state — three fields (#lines, #cursorLine, #cursorCol), zero imports, zero ANSI, zero I/O. Exactly what step 3a should be.
Extraction is complete — all three fields removed from AppLayout, replaced with #editorState instance. Both reset sites use editorState.reset(). No orphaned references.
Mutable lines getter is documented and temporary — returns live internal array because key handling still mutates from AppLayout. Fixed in 3b when key handling methods move into EditorState.
One minor nit: ctrl+enter does this.#editorState.lines.join('\n').trim() — could use the new text getter instead (this.#editorState.text.trim()). Not blocking, just a freebie.
No tests, but that's correct — the test surface appears in 3b when key handling becomes independently testable. TypeScript compile verified the mechanical transformation.
reset() replacing the array reference (not clearing) is the right call — no stale mutation risk across turn boundaries.
Ship it 🚢🍌
What
Moves the three editor fields out of
AppLayoutinto a dedicatedEditorStateclass.Changes
#editorLines→EditorState.#lines, exposed via.linesgetter#cursorLine→EditorState.#cursorLine, getter + setter#cursorCol→EditorState.#cursorCol, getter + settercompleteTurn,waitForInput) replaced byeditorState.reset()AppLayoutfor nowWhy the lines array is mutable
Key handling still lives in
AppLayoutuntil step 3b. Direct index assignment andspliceon the array still happen there, solinesreturns the live internal array rather than a copy. The comment inEditorStatedocuments this explicitly as temporary.Testing
TypeScript found every missed reference at compile time. Manual test: start the CLI, type in the editor, use backspace/enter/ctrl+enter — all input handling should work identically.