fix(desktop): send terminal line-edit chords after committed IME text

BR
1d ago
+81-0

What & why

Fixes #10177

In a terminal pane, Shift+Enter pressed while an IME syllable is still in composition sent the newline (ESC+CR) to the PTY before the syllable. With Korean 2-Set, 안녕 + Shift+Enter gave 안 on the first line and 녕 on the next.

The custom key handler in terminal-key-event-handler.ts runs before xterm's CompositionHelper and did not look at composition state. Two things went wrong:

  • It sent the translated chord on the keydown the IME takes to commit (keyCode === 229). Chromium on macOS sends the key again after compositionend, so the chord went out two times.
  • At that second keydown, xterm still holds the committed text in the setTimeout(0) it starts on compositionend. A chord sent synchronously goes before that text.

This applies to every chord translateLineEditChord returns (Shift+Enter, Cmd+Enter, Cmd+Backspace, Cmd/Option+arrow on Mac, Ctrl+arrow on Windows), because they share the one send path.

The fix: before the handler sends a translated chord, it calls xterm's CompositionHelper.keydown(event). That is the hook xterm itself uses for a plain Enter after a composition:

  • For keyCode === 229 it returns false (the IME owns the key). The handler then sends nothing and gives the key to xterm.
  • For a real key while a composition is open or its text is still in the timer, it sends the composed text at once and cancels the timer. The chord goes after it.
  • With no composition it does nothing, so chords without an IME still send at once.

CompositionHelper is private in xterm. The handler reaches it through _core, as it already does for the kitty keyboard flags. If the helper is absent, the handler sends as before.

The select-all and clipboard branches of the handler are not changed.

How I tested it

  • New test terminal-key-event-handler.test.ts replays this event order against real xterm: compositionstart, compositionupdate, keydown Enter keyCode=229 isComposing=true, compositionend, keydown Enter keyCode=13. On main the PTY data is ["\x1b\r", "\x1b\r", "글"]. With the fix it is ["글", "\x1b\r"].
  • bun test src/renderer/lib/terminal (4068 pass) and bun test src/xterm-ime-patch.test.ts in apps/desktop, with bun 1.4.3.
  • tsc --noEmit in apps/desktop and ./scripts/lint.sh apps/desktop pass.

Not tested with a real IME. The event order in the test is the one recorded in #10179 (Electron, macOS, Korean 2-Set); I did not record it again. The other chords were not tested one by one; they use the same code path as Shift+Enter. If an IME does not send the commit key a second time, Shift+Enter only commits the text and a second press adds the newline.

Checklist

  • PR title follows conventional commits (type(scope): subject)
  • bun run lint and bun run typecheck pass (CI fails on lint warnings too)
  • "Allow edits from maintainers" is checked on fork PRs
  • I have signed the CLA (the bot asks on your first PR)

https://claude.ai/code/session_01Scav27nNN1H5PPKjtrqXYY


Summary by cubic

Fixes terminal line-edit chords (Shift+Enter, Cmd+Enter, Cmd+Backspace, Cmd/Option+arrow) being sent before committed IME text, which split Korean syllables across lines. Fixes #10177.

  • The key handler now calls xterm's CompositionHelper.keydown before sending a chord, so IME-owned commit keys are left to the IME and pending composed text is sent first.
  • Adds a test that replays Chromium's IME commit event order and verifies the chord reaches the PTY once, after the composed text.
  • Not verified with a real IME; the test only replays the event order recorded from Korean 2-Set on macOS.

Summary by CodeRabbit

  • Bug Fixes
    • Improved terminal input handling for translated line breaks during text composition on macOS. Entering composed text followed by Shift+Enter now preserves the text and sends the expected line break, avoiding dropped or duplicated input.