Skip to content

feat(editor): word and line movement/deletion keys - #34

Open
heeseon87 wants to merge 1 commit into
plannotator:mainfrom
heeseon87:feat/editor-word-line-keys
Open

feat(editor): word and line movement/deletion keys#34
heeseon87 wants to merge 1 commit into
plannotator:mainfrom
heeseon87:feat/editor-word-line-keys

Conversation

@heeseon87

Copy link
Copy Markdown
Contributor

Problem

The comment editor only handles single-character Backspace/Left/Right. Every modifier combination is dropped, because the keypress handler ignores ctrl/meta and falls through to the text-insert branch guarded by !key.ctrl && !key.meta.

Terminals translate the usual macOS/Linux editing keys into readline (Emacs) sequences, which every shell and REPL already understands:

Key Bytes sent (Ghostty defaults, iTerm2 "Natural Text Editing", Linux Alt)
Opt/Alt + ← / → ESC b / ESC f or ESC [1;3D / ESC [1;3C
Cmd + ← / → ^A / ^E or ESC [1;9D / ESC [1;9C
Opt/Alt + Backspace ^W or ESC DEL
Cmd + Backspace ^U

So in the editor these keys either did nothing or moved one character, while they work as expected in the shell right next to it.

Change

  • New src/edit-keys.ts with pure helpers (wordStart, wordEnd, lineStart, lineEnd) and resolveEditKey, which maps a readline Key to an edit action.
  • Node's readline folds both xterm modifier 3 (Alt) and 9 (Super) into key.meta, so resolveEditKey inspects key.sequence to tell word moves from line moves.
  • src/editor.ts applies the resolved action before the existing single-character handling; everything else is unchanged.
  • test/edit-keys.test.ts covers word/line boundaries (including CJK runs and newlines) and each sequence above.

bun test (68 pass) and bunx tsc --noEmit are clean.

Handle the readline-style sequences terminals emit for Option/Command
combinations: ESC b/f and modifier-3 arrows move by word, Ctrl+A/E and
modifier-9 arrows jump to line edges, Ctrl+W / Meta+Backspace delete the
previous word, Ctrl+U deletes to the line start.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant