Skip to content

fix(editor): give the editor one key-down chain so Esc always leaves Vim insert mode (#2914) - #2918

Merged
datlechin merged 1 commit into
mainfrom
fix/editor-escape-one-key-chain
Sep 16, 2026
Merged

datlechin merged 1 commit into
mainfrom
fix/editor-escape-one-key-chain

Conversation

@datlechin

Copy link
Copy Markdown
Member

Fixes #2914.

Root cause

Bare Escape in the SQL editor had two app-wide NSEvent local key-down monitors claiming it for the
same text view, with nothing deciding which ran first:

  • TextViewController.setUpKeyBindings resolved EditorKeyCommand.escape and ran handleEscape →
    handleShowCompletions, which returns nil whether it opens or dismisses the completion list.
  • VimKeyInterceptor.installMonitor handed Escape to the Vim engine.

NSEvent.addLocalMonitorForEvents gives same-mask monitors no defined order. Measured on macOS 26
with a standalone AppKit probe: three monitors registered in the order 1,2,3 fired as 2,3,1,
1,3,2 and 1,2,3 across three process runs, and identically for every press within a run. So
which monitor won was decided once per registration and then stayed decided. The Vim interceptor
re-registered its monitor on every focus change, which re-rolled it, and when the editor's own
monitor landed first the keystroke was consumed and the engine never saw it: the badge stayed
INSERT and nothing else visibly happened.

Ctrl+[ is immune because EditorKeyCommand does not match it, so it fell through to the Vim
monitor every time. That is the asymmetry in the report.

Two more claimants shared the same race and were not scoped to their own editor at all.
SuggestionController's monitor is a singleton's, guarded only by activeTextView having some
window, and it can outlive its panel for the rest of the session: NSWindow.close() on an
already-closed window posts no willCloseNotification (measured), and that notification is the only
thing that runs performCleanup() → removeEventMonitors(). FindPanelHostingView's matched
kVK_Escape with no window or editor check, so a find bar open in one window swallowed Escape in
every other one.

Two of the paths named in the issue are refuted. KeyboardSettings.menuKeyEquivalent(for:) refuses a
bare key, so Edit > Clear Selection never carried an Escape key equivalent, and AppKit does not
dispatch an unmodified Escape as a key equivalent at all: measured, NSMenu.performKeyEquivalent is
never called for key code 53, which reaches keyDown: and cancelOperation(_:) through the
responder chain, as StandardKeyBinding.dict maps 0x1B to. The comments asserting the opposite in
EditorEventRouter and SQLEditorCoordinator are corrected; the menu item still works when it is
clicked, which is the only way it ever fired.

The fix

One key-down chokepoint for the editor, with an explicit order, through the seam the codebase
already has for installing app behaviour into an editor (TextViewCoordinator).

TextViewController's single monitor now calls claimKeyDown(_:textViewHasFocus:findPanelHasFocus:),
which consults, in order: the app's coordinators (Vim, then the inline AI suggestion), the completion
list, the find panel, then the editor's own commands. Vim first is what Xcode and IdeaVim do, so one
Escape closes the completion popup and leaves Insert rather than asking for a second press.

Removed: the VimKeyInterceptor, SuggestionController and FindPanelHostingView monitors,
VimKeyEventScopeResolver and its tests, and the Vim interceptor's focus latch. The inline
suggestion now defers Tab to an open completion list instead of racing it.

Escape with Vim off, no popup and no find panel still opens code completion. That is Xcode's own
convention and is deliberate; a test pins it.

Net −212 lines.

Collateral fixed with it

  • Turning Vim mode on while the editor already had focus left the new interceptor completely inert
    until the user clicked away and back: install(controller:) cleared a focus latch that nothing
    set again. There is no latch now, so this cannot recur.
  • A find bar open in one window swallowed Escape in every other window.
  • Tab accepted a ghost AI suggestion instead of the selected completion item.
  • .clearSelection: .special(.escape) is gone from the shipped defaults. It never produced a key
    equivalent, and ShortcutRecorderView refuses to record a bare Escape, so Settings > Keyboard
    displayed ⎋ for a binding that did nothing. Clearing the grid selection with Escape is
    unaffected: it runs through KeyHandlingTableView.cancelOperation, which is the native path.

Verified

  • verify.sh build: PASS.
  • verify.sh test over the suites owning the changed types (VimKeyInterceptorFocusTests,
    ShortcutUniquenessTests, ShortcutActionDefaultsTests, DefaultShortcutHygieneTests,
    BareKeyValidationTests, ShortcutConflictTests, KeyboardSettingsSanitizeTests,
    KeyboardSettingsMigrationTests): PASS.
  • swift test --package-path Packages/TableProEditor: 336 + 160 cases across 59 suites, all pass.
    This is the only thing that runs the editor package suites, and the new chain-order tests live
    there.
  • swiftlint --strict over the changed files: clean. The one repo-wide violation it reports
    (MainContentCommandActions.swift type body length) is pre-existing on main and not in this diff.
  • Probes: scratchpad/myprobe/ holds the four AppKit probes behind the measured facts above.

New tests: EditorKeyChainOrderTests asserts the order itself (a coordinator gets Escape before the
editor's commands, Ctrl+[ is never claimed by them, a focused find panel gets its Escape and no
editing chord, an idle Escape still reaches code completion). VimNormalModeChordUITests gains a
case that leaves and re-enters Insert five times and fails if any Escape does not land.

No screenshots: nothing visual changes. No docs change: the keyboard-shortcuts page documents
behaviour this restores rather than alters.

@datlechin
datlechin merged commit ab1a175 into main Sep 16, 2026
8 checks passed
@datlechin
datlechin deleted the fix/editor-escape-one-key-chain branch September 16, 2026 08:17
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.

Vim mode: Esc stops leaving Insert mode after switching modes with two connection windows open

1 participant