Files
pane-editor/docs/architecture/minimal-action-design.md
Greg ShuflinandClaude Opus 4.5 ed6280205e Refactor to minimal EditorAction architecture for scriptability
Move cursor movement logic from EditorCore/Cursor into Configuration layer.
This prepares the codebase for a Turing-complete scripting DSL by keeping
EditorAction minimal (only absolute MoveCursor { x, y }) and having the
scripting layer compute all movement targets via BufferQuery.

- Add BufferQuery trait for read-only buffer access
- Remove CursorMovement enum, replace with MoveCursor { x, y }
- Simplify Cursor to just hold position (remove move_cursor method)
- Configuration now computes movement targets using BufferQuery

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-09 21:24:26 -08:00

5.9 KiB

Minimal EditorAction Architecture

This document describes the architectural approach for making pane-editor highly scriptable by keeping EditorAction minimal and pushing movement logic into the scripting/configuration layer.

Goals

  1. Emacs-level scriptability via a Turing-complete DSL (non-Lisp)
  2. Vim-like UX - modal, light, fast
  3. Stable core - EditorCore changes rarely, all "smarts" live in scripts

Architecture Overview

┌─────────────────────────────────────────────────────┐
│  Scripting Layer (Configuration / future DSL)      │
│  - Keybindings                                      │
│  - Movement definitions (what is a "word"?)         │
│  - Custom commands                                  │
│  - Plugins                                          │
│                                                     │
│  [reads via BufferQuery]      [writes via actions]  │
└──────────┬─────────────────────────────────────────┬┘
           │                                         │
           ▼                                         ▼
┌─────────────────────┐       ┌─────────────────────────┐
│  BufferQuery trait  │       │  EditorAction           │
│                     │       │                         │
│  - cursor()         │       │  - MoveCursor { x, y }  │
│  - line_count()     │       │  - InsertCharacter(ch)  │
│  - line_len(y)      │       │  - DeleteChar           │
│  - char_at(x, y)    │       │  - SetMode(Mode)        │
│                     │       │  - etc.                 │
└──────────┬──────────┘       └────────────┬────────────┘
           │                               │
           ▼                               ▼
┌─────────────────────────────────────────────────────┐
│  EditorCore                                         │
│  - Buffers (Rope storage)                           │
│  - Cursor positions (just x, y - no logic)          │
│  - Mode state                                       │
│  - Undo history (future)                            │
│                                                     │
│  (Dumb state container - no movement logic)         │
└─────────────────────────────────────────────────────┘

Key Design Decisions

1. EditorAction is Minimal

EditorAction::MoveCursor takes absolute { x, y } coordinates only. There is no NextWord, PrevLine, or EndOfLine variant. The scripting layer computes target positions using BufferQuery and emits absolute moves.

pub enum EditorAction {
    MoveCursor { x: usize, y: usize },
    InsertCharacter(char),
    DeleteChar,
    SetMode(Mode),
    // ...
}

2. BufferQuery Exposes Read-Only State

The scripting layer needs to inspect buffer contents to compute movement targets:

pub trait BufferQuery {
    fn cursor(&self) -> (usize, usize);
    fn line_count(&self) -> usize;
    fn line_len(&self, y: usize) -> usize;
    fn char_at(&self, x: usize, y: usize) -> Option<char>;
}

3. Cursor is a Dumb Position Holder

Cursor stores (x, y) and nothing else. No move_cursor() method. All movement computation happens in the scripting layer.

4. Configuration Gets Query Access

Configuration::get_editor_action() receives a &dyn BufferQuery so it can compute movement targets:

impl Configuration {
    pub fn get_editor_action(
        &self,
        input: Input,
        mode: Mode,
        buffer: Option<&dyn BufferQuery>,
    ) -> Option<EditorAction>;
}

Example: How "j" (move down) Works

  1. User presses j in Normal mode
  2. EditorCore::send_input(Input::Character('j')) is called
  3. EditorCore passes the active buffer (as &dyn BufferQuery) to Configuration
  4. Configuration queries buffer.cursor() → (5, 10) (x=5, y=10)
  5. Configuration queries buffer.line_count() → 100
  6. Configuration computes: new_y = min(10 + 1, 100 - 1) = 11
  7. Configuration returns EditorAction::MoveCursor { x: 5, y: 11 }
  8. EditorCore sets cursor to (5, 11)

Example: How "w" (next word) Will Work

  1. User presses w in Normal mode
  2. Configuration queries buffer for current position and text
  3. Configuration's word-finding logic (eventually defined in DSL) scans forward:
    • Skip current word characters
    • Skip non-word characters
    • Return position of next word start
  4. Configuration returns EditorAction::MoveCursor { x: new_x, y: new_y }

The word definition (what characters are "word" vs "punctuation") lives entirely in the scripting layer, not in EditorCore.

Benefits

  • EditorCore is stable: Adding new movement types never requires EditorCore changes
  • Full scriptability: DSL can define arbitrarily complex movements
  • Configurable word definitions: "word" means whatever the user's config says
  • Testable: Movement logic can be unit tested without EditorCore
  • Frontend agnostic: EditorCore remains decoupled from input handling

Migration Path

  1. Replace CursorMovement enum with MoveCursor { x, y }
  2. Add BufferQuery trait, implement on TextBuffer
  3. Move movement logic from Cursor::move_cursor() to Configuration
  4. Simplify Cursor to just hold position
  5. (Future) Replace Configuration's Rust code with DSL interpreter