Repository navigation
fix(terminal): stop IME newline defer holding Shift+Enter with no composition (#10203) - #11778
itisvincent wants to merge 1 commit into
Conversation
… composition (stablyai#10203) sendTerminalInputAfterComposition armed a 200ms fallback timer and waited for compositionend even when no IME composition was actually pending. On Windows, Shift+Enter is a modified-Enter chord routed through this defer (keyboard-handlers.ts), while non-Enter chords like Alt+J bypass it, so every Shift+Enter newline was held up to the full 200ms fallback - seen as a brief input/cursor freeze before the newline was accepted. Only hold the newline while a composition is genuinely pending (hasPendingTerminalImeComposition); otherwise send on the next macrotask, matching the no-terminal-element path. Genuine pending compositions still wait for compositionend / the captured xterm transaction to settle, preserving IME correctness.
📝 WalkthroughWalkthrough
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 69d72b9f-8858-46a0-b3cc-be5d509b1a33
📒 Files selected for processing (2)
src/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.test.tssrc/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.ts
| it('sends on the next macrotask when no composition is pending instead of waiting for the fallback (#10203)', () => { | ||
| const el = document.createElement('div') | ||
| const send = vi.fn() | ||
|
|
||
| sendTerminalInputAfterComposition(el, send) | ||
| // Why: with no pending composition the newline must not be held for the 200ms fallback. | ||
| vi.advanceTimersByTime(1) | ||
| expect(send).toHaveBeenCalledTimes(1) | ||
| }) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert that delivery is asynchronous.
The test only checks that send runs after advancing timers. A synchronous send() implementation would also pass. Assert that send has not been called before advancing the timer.
Proposed test fix
sendTerminalInputAfterComposition(el, send)
+ expect(send).not.toHaveBeenCalled()
// Why: with no pending composition the newline must not be held for the 200ms fallback.
vi.advanceTimersByTime(1)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| it('sends on the next macrotask when no composition is pending instead of waiting for the fallback (#10203)', () => { | |
| const el = document.createElement('div') | |
| const send = vi.fn() | |
| sendTerminalInputAfterComposition(el, send) | |
| // Why: with no pending composition the newline must not be held for the 200ms fallback. | |
| vi.advanceTimersByTime(1) | |
| expect(send).toHaveBeenCalledTimes(1) | |
| }) | |
| it('sends on the next macrotask when no composition is pending instead of waiting for the fallback (`#10203`)', () => { | |
| const el = document.createElement('div') | |
| const send = vi.fn() | |
| sendTerminalInputAfterComposition(el, send) | |
| expect(send).not.toHaveBeenCalled() | |
| // Why: with no pending composition the newline must not be held for the 200ms fallback. | |
| vi.advanceTimersByTime(1) | |
| expect(send).toHaveBeenCalledTimes(1) | |
| }) |
Greptile SummaryThis PR fixes a ~200ms input freeze on Windows when pressing Shift+Enter by short-circuiting the IME deferred-newline path when no composition is actually pending. Previously
Confidence Score: 4/5The production fix is safe to merge — it removes a guaranteed 200ms hold on every non-IME modified-Enter (Shift+Enter) on Windows, and genuine pending compositions continue through the original deferred path unchanged. The two-line guard in terminal-ime-deferred-newline.ts is straightforward and correct. The only concern is in the test file: several existing tests that were designed to exercise the compositionend listener and the 200ms fallback now silently take the new fast path (because they use bare div elements with no pending composition primed in the WeakMap). Those code paths remain untested. The production behavior for IME users is preserved, so this is a test-coverage gap rather than a functional regression. Files Needing Attention: src/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.test.ts — tests that dispatch compositionend now exercise the fast path, leaving the compositionend listener and the 200ms fallback for genuine compositions without coverage.
|
| Filename | Overview |
|---|---|
| src/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.ts | Adds an early-return fast path when no IME composition is pending, removing the 200ms delay for modified-Enter keys like Shift+Enter; the deferred path for genuine compositions is unchanged. |
| src/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.test.ts | Adds one new regression test verifying the 0ms fast path; however, existing tests that dispatch compositionend now silently exercise the fast path (no pending composition) rather than the compositionend listener in the deferred path, leaving that listener and the 200ms fallback for genuine compositions untested. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["sendTerminalInputAfterComposition"] --> B{"terminalElement null?"}
B -- yes --> C["setTimeout(send,0) — existing null path"]
B -- no --> D{"hasPendingImeComposition?"}
D -- "false — NEW fast path" --> E["setTimeout(send,0) — skip 200ms wait"]
D -- "true — existing deferred path" --> F["Register compositionend + SESSION_END listeners\nArm 200ms fallback timer"]
F --> G{"compositionend or SESSION_END fires"}
G -- "pending == false" --> H["finish: clear listeners/timer\nsetTimeout(send,0)"]
G -- "still pending" --> G
F --> I["200ms fallback fires — finish()"]
H --> J["send called"]
I --> J
C --> J
E --> J
Comments Outside Diff (1)
-
src/renderer/src/components/terminal-pane/terminal-ime-deferred-newline.test.ts, line 25-38 (link)Test now exercises the fast path, not the
compositionendlistenerAfter the fix,
sendTerminalInputAfterCompositioncheckshasPendingTerminalImeCompositionbefore registering any listeners. Because this test uses a plaindocument.createElement('div')with no priorXTERM_COMPOSITION_SESSION_START_EVENT, the element is absent from theWeakMap, so the function exits early viasetTimeout(send, 0)and never attachesonCompositionEnd. Theel.dispatchEvent(new Event('compositionend'))call on line 32 lands on an element with no listener, andvi.runAllTimers()fires only the 0ms macro-task timeout — not the compositionend-triggered path the test was designed to cover.This means a regression in the
compositionendevent listener (e.g. accidental removal, wrong guard) or in the 200ms fallback for genuinely pending compositions would not be caught by any test in this file. To restore coverage, the test would need to install a composition route onel, dispatch aXTERM_COMPOSITION_SESSION_START_EVENTto prime theWeakMap, and then exercise the compositionend / SESSION_END paths through the deferred branch.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Reviews (1): Last reviewed commit: "fix(terminal): stop the IME newline defe..." | Re-trigger Greptile
Closing — the IME deferred-newline path is NOT the causeRuntime diagnostic on Windows (v1.4.167 + a
The correct fix is in #12299 (LF encoding + launch-agent fallback), which:
This (#11778) targeted the wrong layer. Closing. |
Summary
Fixes the "Pi input freezes briefly on Shift+Enter" symptom from #10203 — and more precisely, the freeze anyone sees on Windows when comparing Shift+Enter (freezes ~200ms before the newline lands) vs Alt+J (smooth).
Root cause
On Windows, Shift+Enter is a modified-Enter chord.
keyboard-handlers.tsroutes modified Enter through the IME deferred-newline path:```ts
if ((e.isComposing || hasPendingImeComposition) && (e.key === 'Enter' || imeProcessEnter)) {
...
deferredNewlineSender.defer(e, pane.terminal.element, sendResolvedInput)
return
}
```
defer()calls `sendTerminalInputAfterComposition`, which always armed a 200ms fallback timer and waited for `compositionend` / a captured xterm transaction end — even when no IME composition was actually pending:```ts
const fallbackTimer = window.setTimeout(finish, fallbackMs) // 200ms
```
When no compositionend arrives (the common case: IME idle, or no IME), the newline is held for the full `TERMINAL_IME_DEFERRED_NEWLINE_FALLBACK_MS = 200` — the visible "brief freeze before the newline is accepted."
Alt+J is not an Enter chord (`e.key !== 'Enter'`), so it never enters this defer and is sent immediately — which is exactly why Alt+J is smooth and Shift+Enter is not.
Fix
Only hold the newline while a composition is genuinely pending (`hasPendingTerminalImeComposition`). If nothing is pending, send on the next macrotask (matching the existing no-terminal-element path). Genuine pending compositions still wait for `compositionend` / the captured xterm transaction to settle, so IME correctness is preserved.
```ts
if (!hasPendingTerminalImeComposition(terminalElement)) {
window.setTimeout(send, 0)
return
}
```
Test
New regression case in `terminal-ime-deferred-newline.test.ts`: with no pending composition, `sendTerminalInputAfterComposition` sends on the next macrotask instead of waiting for the 200ms fallback. This test fails on main (`expected 1, got 0` after `advanceTimersByTime(1)`) and passes with the fix.
All 33 existing `terminal-ime-deferred-newline` tests still pass, including the overlapping-transaction and compositionend-flush cases.
Verification
Cross-platform / compatibility notes
Reviewer checklist
Closes #10203
X handle: @itisvincent