Repository navigation
fix(editor): give the editor one key-down chain so Esc always leaves Vim insert mode (#2914) - #2918
Merged
Merged
Conversation
…Vim insert mode (#2914)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2914.
Root cause
Bare Escape in the SQL editor had two app-wide
NSEventlocal key-down monitors claiming it for thesame text view, with nothing deciding which ran first:
TextViewController.setUpKeyBindingsresolvedEditorKeyCommand.escapeand ranhandleEscape→handleShowCompletions, which returnsnilwhether it opens or dismisses the completion list.VimKeyInterceptor.installMonitorhanded Escape to the Vim engine.NSEvent.addLocalMonitorForEventsgives same-mask monitors no defined order. Measured on macOS 26with a standalone AppKit probe: three monitors registered in the order 1,2,3 fired as
2,3,1,1,3,2and1,2,3across three process runs, and identically for every press within a run. Sowhich 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 becauseEditorKeyCommanddoes not match it, so it fell through to the Vimmonitor 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 byactiveTextViewhaving somewindow, and it can outlive its panel for the rest of the session:
NSWindow.close()on analready-closed window posts no
willCloseNotification(measured), and that notification is the onlything that runs
performCleanup()→removeEventMonitors().FindPanelHostingView's matchedkVK_Escapewith no window or editor check, so a find bar open in one window swallowed Escape inevery other one.
Two of the paths named in the issue are refuted.
KeyboardSettings.menuKeyEquivalent(for:)refuses abare 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.performKeyEquivalentisnever called for key code 53, which reaches
keyDown:andcancelOperation(_:)through theresponder chain, as
StandardKeyBinding.dictmaps0x1Bto. The comments asserting the opposite inEditorEventRouterandSQLEditorCoordinatorare corrected; the menu item still works when it isclicked, 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 callsclaimKeyDown(_: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,SuggestionControllerandFindPanelHostingViewmonitors,VimKeyEventScopeResolverand its tests, and the Vim interceptor's focus latch. The inlinesuggestion 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
until the user clicked away and back:
install(controller:)cleared a focus latch that nothingset again. There is no latch now, so this cannot recur.
Tabaccepted a ghost AI suggestion instead of the selected completion item..clearSelection: .special(.escape)is gone from the shipped defaults. It never produced a keyequivalent, and
ShortcutRecorderViewrefuses to record a bare Escape, so Settings > Keyboarddisplayed
⎋for a binding that did nothing. Clearing the grid selection with Escape isunaffected: it runs through
KeyHandlingTableView.cancelOperation, which is the native path.Verified
verify.sh build: PASS.verify.sh testover 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 --strictover the changed files: clean. The one repo-wide violation it reports(
MainContentCommandActions.swifttype body length) is pre-existing onmainand not in this diff.scratchpad/myprobe/holds the four AppKit probes behind the measured facts above.New tests:
EditorKeyChainOrderTestsasserts the order itself (a coordinator gets Escape before theeditor's commands,
Ctrl+[is never claimed by them, a focused find panel gets its Escape and noediting chord, an idle Escape still reaches code completion).
VimNormalModeChordUITestsgains acase 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.